Skip to content

Commit e3dbf77

Browse files
fixed passing the dependency path instead of the required path and fixed
the test plugin
1 parent 1e64c54 commit e3dbf77

2 files changed

Lines changed: 26 additions & 28 deletions

File tree

src/transpilation/resolve.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,51 +98,43 @@ class ResolutionContext {
9898
}
9999
}
100100

101-
private resolveDependencyPathsWithPlugins(required: ProcessedFile, dependency: string) {
102-
const requiredFromLuaFile = required.fileName.endsWith(".lua");
103-
const dependencyPath = requiredFromLuaFile ? luaRequireToPath(dependency) : dependency;
101+
private resolveDependencyPathsWithPlugins(requiringFile: ProcessedFile, dependency: string) {
102+
const requiredFromLuaFile = requiringFile.fileName.endsWith(".lua");
103+
const moduleIdentifier = requiredFromLuaFile ? luaRequireToPath(dependency) : dependency;
104104

105105
for (const plugin of this.plugins) {
106106
if (plugin.moduleResolution != null) {
107107
const pluginResolvedPath = plugin.moduleResolution(
108-
dependency,
109-
dependencyPath,
108+
moduleIdentifier,
109+
requiringFile.fileName,
110110
this.options,
111111
this.emitHost
112112
);
113113
if (pluginResolvedPath !== undefined) {
114114
// If lua file is in node_module
115-
if (requiredFromLuaFile && isNodeModulesFile(required.fileName)) {
115+
if (requiredFromLuaFile && isNodeModulesFile(requiringFile.fileName)) {
116116
// If requiring file is in lua module, try to resolve sibling in that file first
117117
const resolvedNodeModulesFile = this.resolveLuaDependencyPathFromNodeModules(
118-
required,
118+
requiringFile,
119119
pluginResolvedPath
120120
);
121121
if (resolvedNodeModulesFile) {
122122
if (this.options.tstlVerbose) {
123123
console.log(
124-
`Resolved file path for module ${dependency} to path ${dependencyPath} using plugin.`
124+
`Resolved file path for module ${moduleIdentifier} to path ${pluginResolvedPath} using plugin.`
125125
);
126126
}
127127
return resolvedNodeModulesFile;
128128
}
129129
}
130130

131-
const isRelative = ["/", "./", "../"].some(p => pluginResolvedPath.startsWith(p));
132-
133-
// // If the import is relative, always resolve it relative to the requiring file
134-
// // If the import is not relative, resolve it relative to options.baseUrl if it is set
135-
const fileDirectory = path.dirname(required.fileName);
136-
const relativeTo = isRelative ? fileDirectory : this.options.baseUrl ?? fileDirectory;
137-
138-
// // Check if file is a file in the project
139-
const resolvedPath = path.join(relativeTo, pluginResolvedPath);
131+
const resolvedPath = this.formatPathToFile(pluginResolvedPath, requiringFile);
140132
const fileFromPath = this.getFileFromPath(resolvedPath);
141133

142134
if (fileFromPath) {
143135
if (this.options.tstlVerbose) {
144136
console.log(
145-
`Resolved file path for module ${dependency} to path ${resolvedPath} using plugin.`
137+
`Resolved file path for module ${moduleIdentifier} to path ${pluginResolvedPath} using plugin.`
146138
);
147139
}
148140
return fileFromPath;
@@ -154,6 +146,19 @@ class ResolutionContext {
154146

155147
public processedDependencies = new Set<string>();
156148

149+
private formatPathToFile(pluginResolvedPath: string, required: ProcessedFile) {
150+
const isRelative = ["/", "./", "../"].some(p => pluginResolvedPath.startsWith(p));
151+
152+
// // If the import is relative, always resolve it relative to the requiring file
153+
// // If the import is not relative, resolve it relative to options.baseUrl if it is set
154+
const fileDirectory = path.dirname(required.fileName);
155+
const relativeTo = isRelative ? fileDirectory : this.options.baseUrl ?? fileDirectory;
156+
157+
// // Check if file is a file in the project
158+
const resolvedPath = path.join(relativeTo, pluginResolvedPath);
159+
return resolvedPath;
160+
}
161+
157162
private processDependency(dependencyPath: string): void {
158163
if (this.processedDependencies.has(dependencyPath)) return;
159164
this.processedDependencies.add(dependencyPath);
@@ -199,15 +204,8 @@ class ResolutionContext {
199204
if (resolvedNodeModulesFile) return resolvedNodeModulesFile;
200205
}
201206

202-
// Check if the import is relative
203-
const isRelative = ["/", "./", "../"].some(p => dependency.startsWith(p));
204-
205-
// If the import is relative, always resolve it relative to the requiring file
206-
// If the import is not relative, resolve it relative to options.baseUrl if it is set
207-
const relativeTo = isRelative ? fileDirectory : this.options.baseUrl ?? fileDirectory;
208-
209207
// Check if file is a file in the project
210-
const resolvedPath = path.join(relativeTo, dependencyPath);
208+
const resolvedPath = this.formatPathToFile(dependencyPath, requiringFile);
211209
const fileFromPath = this.getFileFromPath(resolvedPath);
212210
if (fileFromPath) return fileFromPath;
213211

test/transpile/plugins/moduleResolution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type * as tstl from "../../../src";
22

33
const plugin: tstl.Plugin = {
4-
moduleResolution(moduleIdentifier, requiringFile) {
4+
moduleResolution(moduleIdentifier) {
55
if (moduleIdentifier.includes("foo")) {
6-
return requiringFile.replace("foo", "bar");
6+
return moduleIdentifier.replace("foo", "bar");
77
}
88
},
99
};

0 commit comments

Comments
 (0)