Skip to content

Commit 7d77a1e

Browse files
fixed an issue with the plugin module resolution when importing scripts
from other scripts
1 parent 8ca8373 commit 7d77a1e

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

src/transpilation/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface Plugin {
4646
result: EmitFile[]
4747
) => ts.Diagnostic[] | void;
4848

49-
onImportResolutionFailure?: (fileDirectory : string, dependencyPath : string) => string | undefined
49+
onImportResolutionFailure?: (packageRoot : string, dependency : string) => string | undefined
5050
}
5151

5252
export function getPlugins(program: ts.Program): { diagnostics: ts.Diagnostic[]; plugins: Plugin[] } {

src/transpilation/resolve.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,6 @@ class ResolutionContext {
168168
const resolveResult = resolver.resolveSync({}, fileDirectory, dependencyPath);
169169
if (resolveResult) return resolveResult;
170170
} catch (e: any) {
171-
172-
const plugins = getPlugins(this.program).plugins;
173-
for (let p of plugins) {
174-
if (p.onImportResolutionFailure != null) {
175-
const pluginResolvedPath = p.onImportResolutionFailure(fileDirectory, dependencyPath)
176-
if (pluginResolvedPath !== undefined) {
177-
return pluginResolvedPath;
178-
}
179-
}
180-
}
181-
182171
// resolveSync errors if it fails to resolve
183172
if (this.options.tstlVerbose && e.details) {
184173
// Output resolver log
@@ -196,17 +185,34 @@ class ResolutionContext {
196185
// We don't know for sure where the lua root is, so guess it is at package root
197186
const splitPath = path.normalize(requiringFile.fileName).split(path.sep);
198187
let packageRootIndex = splitPath.lastIndexOf("node_modules") + 2;
199-
let packageRoot = splitPath.slice(0, packageRootIndex).join(path.sep);
188+
const packageRoot = splitPath.slice(0, packageRootIndex).join(path.sep);
189+
let currentPackage = packageRoot;
200190

201191
while (packageRootIndex < splitPath.length) {
202192
// Try to find lua file relative to currently guessed Lua root
203-
const resolvedPath = path.join(packageRoot, dependency);
193+
const resolvedPath = path.join(currentPackage, dependency);
204194
const fileFromPath = this.getFileFromPath(resolvedPath);
205195
if (fileFromPath) {
206196
return fileFromPath;
207197
} else {
208198
// Did not find file at current root, try again one directory deeper
209-
packageRoot = path.join(packageRoot, splitPath[packageRootIndex++]);
199+
currentPackage = path.join(packageRoot, splitPath[packageRootIndex++]);
200+
}
201+
}
202+
203+
const plugins = getPlugins(this.program).plugins;
204+
for (let p of plugins) {
205+
if (p.onImportResolutionFailure != null) {
206+
const pluginResolvedPath = p.onImportResolutionFailure(packageRoot, dependency)
207+
if (pluginResolvedPath !== undefined) {
208+
const fileFromPath = this.getFileFromPath(pluginResolvedPath);
209+
if(fileFromPath){
210+
if(this.options.tstlVerbose){
211+
console.log(`Resolved file path for ${dependency} to path ${fileFromPath} using plugin.`)
212+
}
213+
return fileFromPath
214+
}
215+
}
210216
}
211217
}
212218

@@ -247,6 +253,11 @@ class ResolutionContext {
247253
path.join(resolvedPath, "index.lua"), // lua index file in sources
248254
path.join(resolvedPath, "init.lua"), // lua looks for <require>/init.lua if it cannot find <require>.lua
249255
];
256+
257+
if(resolvedPath.endsWith(".lua")){
258+
possibleLuaProjectFiles.push(resolvedPath)
259+
}
260+
250261
for (const possibleFile of possibleLuaProjectFiles) {
251262
if (this.emitHost.fileExists(possibleFile)) {
252263
return possibleFile;

0 commit comments

Comments
 (0)