Skip to content

Commit 3bae760

Browse files
authored
Allow configuring output file extension in config (#1229)
1 parent 26ac507 commit 3bae760

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface LuaPluginImport {
2323

2424
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2525
buildMode?: BuildMode;
26+
extension?: string;
2627
luaBundle?: string;
2728
luaBundleEntry?: string;
2829
luaTarget?: LuaTarget;

src/cli/parse.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export const optionDeclarations: CommandLineOption[] = [
3030
type: "enum",
3131
choices: Object.values(BuildMode),
3232
},
33+
{
34+
name: "extension",
35+
description: 'File extension for the resulting Lua files. Defaults to ".lua"',
36+
type: "string",
37+
},
3338
{
3439
name: "luaBundle",
3540
description: "The name of the lua file to bundle output lua to. Requires luaBundleEntry.",

src/transpilation/transpiler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ export function getEmitPathRelativeToOutDir(fileName: string, program: ts.Progra
127127
emitPathSplits[0] = "lua_modules";
128128
}
129129

130-
// Make extension lua
131-
emitPathSplits[emitPathSplits.length - 1] = trimExtension(emitPathSplits[emitPathSplits.length - 1]) + ".lua";
130+
// Set extension
131+
const extension = ((program.getCompilerOptions() as CompilerOptions).extension ?? "lua").trim();
132+
const trimmedExtension = extension.startsWith(".") ? extension.substring(1) : extension;
133+
emitPathSplits[emitPathSplits.length - 1] =
134+
trimExtension(emitPathSplits[emitPathSplits.length - 1]) + "." + trimmedExtension;
132135

133136
return path.join(...emitPathSplits);
134137
}

test/cli/parse.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ describe("command line", () => {
123123

124124
["luaBundle", "foo", { luaBundle: "foo" }],
125125
["luaBundleEntry", "bar", { luaBundleEntry: "bar" }],
126+
127+
["extension", ".lua", { extension: ".lua" }],
128+
["extension", "scar", { extension: "scar" }],
126129
])("--%s %s", (optionName, value, expected) => {
127130
const result = tstl.parseCommandLine([`--${optionName}`, value]);
128131

test/transpile/paths.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,38 @@ describe("getEmitPath", () => {
113113
expect(fileNames).toHaveLength(1);
114114
expect(fileNames).toContain(path.join(cwd, "out1", "out2", "bundle.lua"));
115115
});
116+
117+
test.each([".scar", "scar"])("uses config extension (%p)", extension => {
118+
const { transpiledFiles } = util.testModule``
119+
.setMainFileName("main.ts")
120+
.addExtraFile("dir/extra.ts", "")
121+
.setOptions({ extension })
122+
.expectToHaveNoDiagnostics()
123+
.getLuaResult();
124+
125+
const fileNames = transpiledFiles.map(f => f.outPath);
126+
expect(fileNames).toContain("main.scar");
127+
expect(fileNames).toContain(path.join("dir", "extra.scar"));
128+
});
129+
130+
test("bundle with different extension", () => {
131+
const { transpiledFiles } = util.testModule``
132+
.setMainFileName("src/main.ts")
133+
.addExtraFile("src/extra.ts", "")
134+
.setOptions({
135+
configFilePath,
136+
rootDir: "src",
137+
outDir: "out1",
138+
luaBundle: "out2/bundle.scar",
139+
luaBundleEntry: "src/main.ts",
140+
})
141+
.expectToHaveNoDiagnostics()
142+
.getLuaResult();
143+
144+
const fileNames = transpiledFiles.map(f => f.outPath);
145+
expect(fileNames).toHaveLength(1);
146+
expect(fileNames).toContain(path.join(cwd, "out1", "out2", "bundle.scar"));
147+
});
116148
});
117149

118150
function normalize(path: string) {

0 commit comments

Comments
 (0)