Skip to content

Commit 635ba3b

Browse files
committed
Add string array luaEntry option
1 parent c50ad14 commit 635ba3b

5 files changed

Lines changed: 70 additions & 21 deletions

File tree

src/CommandLineParser.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ interface CommandLineOptionOfBoolean extends CommandLineOptionBase {
2222
type: "boolean";
2323
}
2424

25-
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean;
25+
interface CommandLineOptionOfStringArray extends CommandLineOptionBase {
26+
type: "string[]";
27+
}
28+
29+
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean | CommandLineOptionOfStringArray;
2630

2731
const optionDeclarations: CommandLineOption[] = [
32+
{
33+
name: "luaEntry",
34+
description: "Specifies a series of entry points to execute in the resulting output file.",
35+
type: "string[]",
36+
},
2837
{
2938
name: "luaLibImport",
3039
description: "Specifies how js standard features missing in lua are imported.",
@@ -185,6 +194,14 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
185194
};
186195
}
187196

197+
if (option.type === 'string[]') {
198+
return {
199+
error: diagnosticFactories.optionCanOnlyBeSpecifiedInTsconfigJsonFile(option.name),
200+
value: undefined,
201+
increment: 0
202+
};
203+
}
204+
188205
return { ...readValue(option, value), increment: 1 };
189206
}
190207

@@ -227,6 +244,24 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
227244

228245
return { value: enumValue };
229246
}
247+
248+
case "string[]": {
249+
if (Array.isArray(value)) {
250+
if (value.some(str => typeof str !== "string")) {
251+
return {
252+
value: undefined,
253+
error: diagnosticFactories.invalidStringArrayForOption(option.name),
254+
};
255+
}
256+
257+
return { value };
258+
}
259+
260+
return {
261+
value: undefined,
262+
error: diagnosticFactories.invalidStringArrayForOption(option.name),
263+
};
264+
}
230265
}
231266
}
232267

src/CompilerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface TransformerImport {
2020
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2121
noImplicitSelf?: boolean;
2222
noHeader?: boolean;
23+
luaEntry?: string[];
2324
luaTarget?: LuaTarget;
2425
luaLibImport?: LuaLibImportKind;
2526
noHoisting?: boolean;

src/LuaTransformer.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ export class LuaTransformer {
140140
[]
141141
);
142142

143+
if (this.options.luaEntry) {
144+
const requireCalls = this.options.luaEntry.map(entry => {
145+
const formattedEntryName = tsHelper.formatPathToLuaPath(entry.replace(/.ts$/, ""));
146+
return tstl.createExpressionStatement(
147+
this.createModuleRequire(ts.createStringLiteral(formattedEntryName), false)
148+
);
149+
});
150+
combinedStatements.push(...requireCalls);
151+
}
152+
143153
return tstl.createBlock(combinedStatements, bundle);
144154
}
145155

@@ -171,22 +181,22 @@ export class LuaTransformer {
171181

172182
// return exports
173183
statements.push(tstl.createReturnStatement([this.createExportsIdentifier()]));
174-
175-
if (this.isWithinBundle) {
176-
const packagePreload = tstl.createTableIndexExpression(
177-
tstl.createIdentifier("package"),
178-
tstl.createStringLiteral("preload")
179-
);
180-
const exportPath = tsHelper.getExportPath(sourceFile.fileName, this.options);
181-
const packagePreloadDeclaration = tstl.createAssignmentStatement(
182-
tstl.createTableIndexExpression(packagePreload, tstl.createStringLiteral(exportPath)),
183-
tstl.createFunctionExpression(tstl.createBlock(statements, sourceFile))
184-
);
185-
return tstl.createBlock([packagePreloadDeclaration], sourceFile);
186-
}
187184
}
188185
}
189186

187+
if (this.isWithinBundle) {
188+
const packagePreload = tstl.createTableIndexExpression(
189+
tstl.createIdentifier("tstlpackage"),
190+
tstl.createStringLiteral("preload")
191+
);
192+
const exportPath = tsHelper.getExportPath(sourceFile.fileName, this.options);
193+
const packagePreloadDeclaration = tstl.createAssignmentStatement(
194+
tstl.createTableIndexExpression(packagePreload, tstl.createStringLiteral(exportPath)),
195+
tstl.createFunctionExpression(tstl.createBlock(statements, sourceFile))
196+
);
197+
return tstl.createBlock([packagePreloadDeclaration], sourceFile);
198+
}
199+
190200
return tstl.createBlock(statements, sourceFile);
191201
}
192202

src/diagnostics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,8 @@ export const optionBuildMustBeFirstCommandLineArgument = createCommandLineError(
130130
6369,
131131
() => "Option '--build' must be the first command line argument."
132132
);
133+
134+
export const invalidStringArrayForOption = createCommandLineError(
135+
6370,
136+
(name: string) => `Option '${name}' received an invalid array of strings.`
137+
);

src/lualib/LuaRequire.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
globalThis.package = {};
2-
globalThis.package.preload = {};
3-
globalThis.package.loaded = {};
1+
const tstlpackage = { preload: {}, loaded: {} };
42

53
function __TS__LuaRequire(this: void, moduleName: string): any {
6-
if (!globalThis.package.loaded[moduleName]) {
7-
const module: (this: void, module: string) => any = globalThis.package.preload[moduleName];
4+
if (!tstlpackage.loaded[moduleName]) {
5+
const module: (this: void, module: string) => any = tstlpackage.preload[moduleName];
86
if (module) {
9-
globalThis.package.loaded[moduleName] = module(moduleName);
7+
tstlpackage.loaded[moduleName] = module(moduleName);
108
} else {
119
// tslint:disable-next-line: no-string-throw
1210
throw `module '${moduleName}' not found:`;
1311
}
1412
}
15-
return globalThis.package.loaded[moduleName];
13+
return tstlpackage.loaded[moduleName];
1614
}

0 commit comments

Comments
 (0)