Skip to content

Commit a45d888

Browse files
moved module resolution logic to resolveImport and made the requested
changes to the interface
1 parent 0bc4fc7 commit a45d888

2 files changed

Lines changed: 40 additions & 23 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?: (packageRoot : string, dependency : string) => string | undefined
49+
moduleResolution?: (moduleIdentifier : string, requiringFile : string) => string | undefined
5050
}
5151

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

src/transpilation/resolve.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ResolutionContext {
7979
return;
8080
}
8181

82-
const dependencyPath = this.resolveDependencyPath(file, required.requirePath);
82+
const dependencyPath = this.resolveDependencyPathsWithPlugins(file, required.requirePath) ?? this.resolveDependencyPath(file, required.requirePath);
8383
if (!dependencyPath) return this.couldNotResolveImport(required, file);
8484

8585
if (this.options.tstlVerbose) {
@@ -95,6 +95,43 @@ class ResolutionContext {
9595
}
9696
}
9797

98+
private resolveDependencyPathsWithPlugins(required: ProcessedFile, dependency: string) {
99+
if (this.plugins != null) {
100+
const requiredFromLuaFile = required.fileName.endsWith(".lua");
101+
const dependencyPath = requiredFromLuaFile ? luaRequireToPath(dependency) : dependency;
102+
103+
for (const p of this.plugins) {
104+
try {
105+
if (p.moduleResolution != null) {
106+
const pluginResolvedPath = p.moduleResolution(dependency, dependencyPath)
107+
if (pluginResolvedPath !== undefined) {
108+
// If node module
109+
if (requiredFromLuaFile && isNodeModulesFile(required.fileName)) {
110+
// If requiring file is in lua module, try to resolve sibling in that file first
111+
const resolvedNodeModulesFile = this.resolveLuaDependencyPathFromNodeModules(required, pluginResolvedPath);
112+
if (resolvedNodeModulesFile) {
113+
if (this.options.tstlVerbose) {
114+
console.log(`Resolved file path for module ${dependency} to path ${dependencyPath} using plugin.`)
115+
}
116+
return resolvedNodeModulesFile
117+
}
118+
} else if (this.getFileFromPath(pluginResolvedPath)) {
119+
console.log(`Resolved file path for module ${dependency} to path ${dependencyPath} using plugin.`)
120+
return pluginResolvedPath;
121+
}
122+
}
123+
}
124+
} catch (e: any) {
125+
// resolveSync errors if it fails to resolve
126+
if (this.options.tstlVerbose) {
127+
// Output resolver log
128+
console.log(e.details ?? e);
129+
}
130+
}
131+
}
132+
}
133+
}
134+
98135
public processedDependencies = new Set<string>();
99136

100137
private processDependency(dependencyPath: string): void {
@@ -183,6 +220,7 @@ class ResolutionContext {
183220
requiringFile: ProcessedFile,
184221
dependency: string
185222
): string | undefined {
223+
186224
// We don't know for sure where the lua root is, so guess it is at package root
187225
const splitPath = path.normalize(requiringFile.fileName).split(path.sep);
188226
let packageRootIndex = splitPath.lastIndexOf("node_modules") + 2;
@@ -201,23 +239,6 @@ class ResolutionContext {
201239
}
202240
}
203241

204-
if (this.plugins != null) {
205-
for (let p of this.plugins) {
206-
if (p.onImportResolutionFailure != null) {
207-
const pluginResolvedPath = p.onImportResolutionFailure(packageRoot, dependency)
208-
if (pluginResolvedPath !== undefined) {
209-
const fileFromPath = this.getFileFromPath(pluginResolvedPath);
210-
if (fileFromPath) {
211-
if (this.options.tstlVerbose) {
212-
console.log(`Resolved file path for ${dependency} to path ${fileFromPath} using plugin.`)
213-
}
214-
return fileFromPath
215-
}
216-
}
217-
}
218-
}
219-
}
220-
221242
return undefined;
222243
}
223244

@@ -256,10 +277,6 @@ class ResolutionContext {
256277
path.join(resolvedPath, "init.lua"), // lua looks for <require>/init.lua if it cannot find <require>.lua
257278
];
258279

259-
if (resolvedPath.endsWith(".lua")) {
260-
possibleLuaProjectFiles.push(resolvedPath)
261-
}
262-
263280
for (const possibleFile of possibleLuaProjectFiles) {
264281
if (this.emitHost.fileExists(possibleFile)) {
265282
return possibleFile;

0 commit comments

Comments
 (0)