Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/transpilation/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class ResolutionContext {
this.emitHost
);
if (pluginResolvedPath !== undefined) {
// If resolved path is absolute no need to further resolve it
if (path.isAbsolute(pluginResolvedPath)) {
return pluginResolvedPath;
}

// If lua file is in node_module
if (requiredFromLuaFile && isNodeModulesFile(requiringFile.fileName)) {
// If requiring file is in lua module, try to resolve sibling in that file first
Expand Down
20 changes: 7 additions & 13 deletions test/transpile/module-resolution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ test("paths without baseUrl is error", () => {
test("module resolution using plugin", () => {
const baseProjectPath = path.resolve(__dirname, "module-resolution", "project-with-module-resolution-plugin");
const projectTsConfig = path.join(baseProjectPath, "tsconfig.json");
const mainFile = path.join(baseProjectPath, "src", "main.ts");
const mainFile = path.join(baseProjectPath, "main.ts");

const luaResult = util
const testBuilder = util
.testProject(projectTsConfig)
.setMainFileName(mainFile)
.setOptions({
Expand All @@ -726,19 +726,13 @@ test("module resolution using plugin", () => {
},
],
})
.expectToHaveNoDiagnostics()
.getLuaResult();
.expectToHaveNoDiagnostics();

expect(luaResult.transpiledFiles).toHaveLength(2);
let hasResolvedFile = false;
for (const f of luaResult.transpiledFiles) {
hasResolvedFile = f.outPath.endsWith("bar.lua");
if (hasResolvedFile) {
break;
}
}
const luaResult = testBuilder.getLuaResult();

expect(luaResult.transpiledFiles).toHaveLength(3);

expect(hasResolvedFile).toBe(true);
testBuilder.expectToEqual({ result: ["foo", "absolutefoo"] });
});

function snapshotPaths(files: tstl.TranspiledFile[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local ____exports = {}
function ____exports.absolutefoo(self)
return "absolutefoo"
end
return ____exports
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function absolutefoo(): string;
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { foo } from "./lua_sources/foo";
export const result = foo();
import { absolutefoo } from "./lua_sources/absolutefoo";
export const result = [foo(), absolutefoo()];
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"strict": true,
"target": "esnext",
"lib": ["esnext"],
"types": [],
"outDir": "./dist"
"types": []
}
}
13 changes: 11 additions & 2 deletions test/transpile/plugins/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import type * as tstl from "../../../src";

const plugin: tstl.Plugin = {
moduleResolution(moduleIdentifier) {
const modulePath = moduleIdentifier.replace(/\./g, path.sep);
if (moduleIdentifier.includes("absolutefoo")) {
return path.join(
path.dirname(__dirname),
"module-resolution",
"project-with-module-resolution-plugin",
"lua_sources",
"absolutebar.lua"
);
}

if (moduleIdentifier.includes("foo")) {
return modulePath.replace("foo", "bar");
return moduleIdentifier.replace("foo", "bar");
}
},
};
Expand Down
13 changes: 10 additions & 3 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,18 @@ export abstract class TestBuilder {
@memoize
public getMainLuaFileResult(): ExecutableTranspiledFile {
const { transpiledFiles } = this.getLuaResult();
const mainFileName = normalizeSlashes(this.mainFileName);
const mainFile = this.options.luaBundle
? transpiledFiles[0]
: transpiledFiles.find(({ sourceFiles }) =>
sourceFiles.some(f => f.fileName === normalizeSlashes(this.mainFileName))
);
: transpiledFiles.find(({ sourceFiles }) => sourceFiles.some(f => f.fileName === mainFileName));

if (mainFile === undefined) {
throw new Error(
`No source file could be found matching main file: ${mainFileName}.\nSource files in test:\n${transpiledFiles
.flatMap(f => f.sourceFiles.map(sf => sf.fileName))
.join("\n")}`
);
}

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