Skip to content

Commit 625d069

Browse files
authored
Make module resolution with custom extension work for extensions starting with and without period (#1402)
1 parent 16cd2d5 commit 625d069

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/transpilation/resolve.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ function replaceRequireInSourceMap(
341341
}
342342

343343
function requirePathForFile(filePath: string, extension = ".lua"): string {
344+
if (!extension.startsWith(".")) {
345+
extension = `.${extension}`;
346+
}
344347
if (filePath.endsWith(extension)) {
345348
return formatPathToLuaPath(filePath.substring(0, filePath.length - extension.length));
346349
} else {

test/transpile/module-resolution.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ describe("module resolution with sourceDir", () => {
182182
.setOptions({ outDir: "tstl-out", extension: ".script" })
183183
.expectToEqual(expectedResult);
184184
});
185+
186+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1394
187+
test("can resolve files with non-standard extension without separator (#1394)", () => {
188+
util.testProject(path.join(projectPath, "tsconfig.json"))
189+
.setMainFileName(path.join(projectPath, "src", "main.ts"))
190+
.setOptions({ outDir: "tstl-out", extension: "script" })
191+
.expectToEqual(expectedResult);
192+
});
185193
});
186194

187195
describe("module resolution project with lua sources", () => {

test/util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,10 @@ end)());`;
468468
}
469469

470470
private injectLuaFile(state: LuaState, lua: Lua, lauxlib: LauxLib, fileName: string, fileContent: string) {
471-
const extension = this.options.extension ?? ".lua";
471+
let extension = this.options.extension ?? ".lua";
472+
if (!extension.startsWith(".")) {
473+
extension = `.${extension}`;
474+
}
472475
const modName = fileName.endsWith(extension)
473476
? formatPathToLuaPath(fileName.substring(0, fileName.length - extension.length))
474477
: fileName;

0 commit comments

Comments
 (0)