Skip to content

Commit 2d42025

Browse files
authored
Allow moduleresolution plugins to return an absolute path (#1496)
1 parent c85001e commit 2d42025

File tree

8 files changed

+42
-21
lines changed

8 files changed

+42
-21
lines changed

src/transpilation/resolve.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ class ResolutionContext {
112112
this.emitHost
113113
);
114114
if (pluginResolvedPath !== undefined) {
115+
// If resolved path is absolute no need to further resolve it
116+
if (path.isAbsolute(pluginResolvedPath)) {
117+
return pluginResolvedPath;
118+
}
119+
115120
// If lua file is in node_module
116121
if (requiredFromLuaFile && isNodeModulesFile(requiringFile.fileName)) {
117122
// If requiring file is in lua module, try to resolve sibling in that file first

test/transpile/module-resolution.spec.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,9 @@ test("paths without baseUrl is error", () => {
714714
test("module resolution using plugin", () => {
715715
const baseProjectPath = path.resolve(__dirname, "module-resolution", "project-with-module-resolution-plugin");
716716
const projectTsConfig = path.join(baseProjectPath, "tsconfig.json");
717-
const mainFile = path.join(baseProjectPath, "src", "main.ts");
717+
const mainFile = path.join(baseProjectPath, "main.ts");
718718

719-
const luaResult = util
719+
const testBuilder = util
720720
.testProject(projectTsConfig)
721721
.setMainFileName(mainFile)
722722
.setOptions({
@@ -726,19 +726,13 @@ test("module resolution using plugin", () => {
726726
},
727727
],
728728
})
729-
.expectToHaveNoDiagnostics()
730-
.getLuaResult();
729+
.expectToHaveNoDiagnostics();
731730

732-
expect(luaResult.transpiledFiles).toHaveLength(2);
733-
let hasResolvedFile = false;
734-
for (const f of luaResult.transpiledFiles) {
735-
hasResolvedFile = f.outPath.endsWith("bar.lua");
736-
if (hasResolvedFile) {
737-
break;
738-
}
739-
}
731+
const luaResult = testBuilder.getLuaResult();
732+
733+
expect(luaResult.transpiledFiles).toHaveLength(3);
740734

741-
expect(hasResolvedFile).toBe(true);
735+
testBuilder.expectToEqual({ result: ["foo", "absolutefoo"] });
742736
});
743737

744738
function snapshotPaths(files: tstl.TranspiledFile[]) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local ____exports = {}
2+
function ____exports.absolutefoo(self)
3+
return "absolutefoo"
4+
end
5+
return ____exports
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function absolutefoo(): string;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import { foo } from "./lua_sources/foo";
2-
export const result = foo();
2+
import { absolutefoo } from "./lua_sources/absolutefoo";
3+
export const result = [foo(), absolutefoo()];

test/transpile/module-resolution/project-with-module-resolution-plugin/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"strict": true,
44
"target": "esnext",
55
"lib": ["esnext"],
6-
"types": [],
7-
"outDir": "./dist"
6+
"types": []
87
}
98
}

test/transpile/plugins/moduleResolution.ts

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

44
const plugin: tstl.Plugin = {
55
moduleResolution(moduleIdentifier) {
6-
const modulePath = moduleIdentifier.replace(/\./g, path.sep);
6+
if (moduleIdentifier.includes("absolutefoo")) {
7+
return path.join(
8+
path.dirname(__dirname),
9+
"module-resolution",
10+
"project-with-module-resolution-plugin",
11+
"lua_sources",
12+
"absolutebar.lua"
13+
);
14+
}
15+
716
if (moduleIdentifier.includes("foo")) {
8-
return modulePath.replace("foo", "bar");
17+
return moduleIdentifier.replace("foo", "bar");
918
}
1019
},
1120
};

test/util.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,18 @@ export abstract class TestBuilder {
257257
@memoize
258258
public getMainLuaFileResult(): ExecutableTranspiledFile {
259259
const { transpiledFiles } = this.getLuaResult();
260+
const mainFileName = normalizeSlashes(this.mainFileName);
260261
const mainFile = this.options.luaBundle
261262
? transpiledFiles[0]
262-
: transpiledFiles.find(({ sourceFiles }) =>
263-
sourceFiles.some(f => f.fileName === normalizeSlashes(this.mainFileName))
264-
);
263+
: transpiledFiles.find(({ sourceFiles }) => sourceFiles.some(f => f.fileName === mainFileName));
264+
265+
if (mainFile === undefined) {
266+
throw new Error(
267+
`No source file could be found matching main file: ${mainFileName}.\nSource files in test:\n${transpiledFiles
268+
.flatMap(f => f.sourceFiles.map(sf => sf.fileName))
269+
.join("\n")}`
270+
);
271+
}
265272

266273
expect(mainFile).toMatchObject({ lua: expect.any(String), luaSourceMap: expect.any(String) });
267274
return mainFile as ExecutableTranspiledFile;

0 commit comments

Comments
 (0)