Skip to content

Commit c6d1f1d

Browse files
committed
Compiler option noReturnMainInBundle
LuaJ does not support top level returns
1 parent ff310f1 commit c6d1f1d

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface TypeScriptToLuaOptions {
3535
noImplicitSelf?: boolean;
3636
noHeader?: boolean;
3737
noResolvePaths?: string[];
38+
noReturnMainInBundle?: boolean;
3839
plugins?: Array<ts.PluginImport | TransformerImport>;
3940
sourceMapTraceback?: boolean;
4041
tstlVerbose?: boolean;

src/cli/parse.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ export const optionDeclarations: CommandLineOption[] = [
9999
description: "Always allow try/catch in async/await functions for Lua 5.1.",
100100
type: "boolean",
101101
},
102+
{
103+
name: "noReturnMainInBundle",
104+
description: "Specify if bundler should emit top-level return statement.",
105+
type: "boolean",
106+
},
102107
{
103108
name: "measurePerformance",
104109
description: "Measure performance of the tstl compiler.",

src/transpilation/bundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function getBundleResult(program: ts.Program, files: ProcessedFile[]): [t
108108

109109
// return require("<entry module path>")
110110
const args = options.luaTarget === LuaTarget.Lua50 ? "unpack(arg == nil and {} or arg)" : "...";
111-
const entryPoint = `return require(${createModulePath(entryModuleFilePath ?? entryModule, program)}, ${args})\n`;
111+
const entryPoint = `${options.noReturnMainInBundle ? "return " : ""}require(${createModulePath(entryModuleFilePath ?? entryModule, program)}, ${args})\n`;
112112

113113
const footers: string[] = [];
114114
if (options.sourceMapTraceback) {
@@ -139,7 +139,7 @@ export function getBundleResult(program: ts.Program, files: ProcessedFile[]): [t
139139
}
140140

141141
function moduleSourceNode({ code, sourceMapNode }: ProcessedFile, modulePath: string): SourceNode {
142-
const tableEntryHead = `[${modulePath}] = function(...) \n`;
142+
const tableEntryHead = `[${modulePath}] = function(...)\n`;
143143
const tableEntryTail = " end,\n";
144144

145145
return joinSourceChunks([tableEntryHead, sourceMapNode ?? code, tableEntryTail]);

tsconfig-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
"description": "An array of import paths that should not be resolved but copied verbatim to output lua.",
7272
"type": "array"
7373
},
74+
"noReturnMainInBundle": {
75+
"description": "Specify if bundler should emit top-level return statement.",
76+
"type": "boolean",
77+
"default": false
78+
},
7479
"sourceMapTraceback": {
7580
"description": "Applies the source map to show source TS files and lines in error tracebacks.",
7681
"default": false,

0 commit comments

Comments
 (0)