Skip to content

Commit 299fda8

Browse files
committed
Add option luaModuleSystem
1 parent 74ef4e3 commit 299fda8

8 files changed

Lines changed: 93 additions & 5 deletions

File tree

src/CommandLineParser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as ts from "typescript";
3-
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
3+
import { CompilerOptions, LuaLibImportKind, LuaTarget, LuaModuleSystemKind } from "./CompilerOptions";
44
import * as diagnosticFactories from "./diagnostics";
55

66
export interface ParsedCommandLine extends ts.ParsedCommandLine {
@@ -31,6 +31,12 @@ const optionDeclarations: CommandLineOption[] = [
3131
type: "enum",
3232
choices: Object.values(LuaLibImportKind),
3333
},
34+
{
35+
name: "luaModuleSystem",
36+
description: "Specify the way modules are resolved on the target Lua environment.",
37+
type: "enum",
38+
choices: Object.values(LuaModuleSystemKind),
39+
},
3440
{
3541
name: "luaTarget",
3642
aliases: ["lt"],

src/CompilerOptions.ts

Lines changed: 6 additions & 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+
luaModuleSystem?: LuaModuleSystemKind;
2324
luaTarget?: LuaTarget;
2425
luaLibImport?: LuaLibImportKind;
2526
noHoisting?: boolean;
@@ -28,6 +29,11 @@ export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
2829
[option: string]: ts.CompilerOptions[string] | Array<ts.PluginImport | TransformerImport>;
2930
};
3031

32+
export enum LuaModuleSystemKind {
33+
Require = "require",
34+
None = "none",
35+
}
36+
3137
export enum LuaLibImportKind {
3238
None = "none",
3339
Always = "always",

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export enum LuaLibFeature {
3333
InstanceOf = "InstanceOf",
3434
InstanceOfObject = "InstanceOfObject",
3535
Iterator = "Iterator",
36+
LuaRequire = "LuaRequire",
3637
Map = "Map",
3738
NewIndex = "NewIndex",
3839
Number = "Number",

src/LuaTransformer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as ts from "typescript";
3-
import { CompilerOptions, LuaTarget } from "./CompilerOptions";
3+
import { CompilerOptions, LuaTarget, LuaModuleSystemKind } from "./CompilerOptions";
44
import { DecoratorKind } from "./Decorator";
55
import * as tstl from "./LuaAST";
66
import { LuaLibFeature } from "./LuaLib";
@@ -546,7 +546,12 @@ export class LuaTransformer {
546546
)
547547
: moduleSpecifier.text;
548548
const modulePath = tstl.createStringLiteral(modulePathString);
549-
return tstl.createCallExpression(tstl.createIdentifier("require"), [modulePath], moduleSpecifier);
549+
const requireCallString =
550+
this.options.luaModuleSystem === LuaModuleSystemKind.None ? "__TS__LuaRequire" : "require";
551+
if (this.options.luaModuleSystem === LuaModuleSystemKind.None) {
552+
this.importLuaLibFeature(LuaLibFeature.LuaRequire);
553+
}
554+
return tstl.createCallExpression(tstl.createIdentifier(requireCallString), [modulePath], moduleSpecifier);
550555
}
551556

552557
protected validateClassElement(element: ts.ClassElement): void {

src/lualib/LuaRequire.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
globalThis.package = {};
2+
globalThis.package.preload = {};
3+
globalThis.package.loaded = {};
4+
5+
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];
8+
if (module) {
9+
globalThis.package.loaded[moduleName] = module(moduleName);
10+
} else {
11+
// tslint:disable-next-line: no-string-throw
12+
throw `module '${moduleName}' not found:`;
13+
}
14+
}
15+
return globalThis.package.loaded[moduleName];
16+
}

test/unit/luaModuleSystem.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as util from "../util";
2+
import * as ts from "typescript";
3+
import { LuaModuleSystemKind } from "../../src";
4+
5+
test.each<[string, Record<string, string>]>([
6+
[
7+
"Import module -> main",
8+
{
9+
"main.ts": `
10+
import { value } from "./module";
11+
if (value !== true) {
12+
throw "Failed to import value";
13+
}
14+
`,
15+
"module.ts": `
16+
export const value = true;
17+
`,
18+
},
19+
],
20+
])("luaModuleSystem with outFile (%s)", (_, files) => {
21+
const testBuilder = util.testBundle`
22+
${files["main.ts"]}
23+
`
24+
.setOptions({ outFile: "main.lua", module: ts.ModuleKind.AMD, luaModuleSystem: LuaModuleSystemKind.None })
25+
.setModuleSystem("none");
26+
27+
const extraFiles = Object.keys(files)
28+
.map(file => ({ fileName: file, code: files[file] }))
29+
.filter(file => file.fileName !== "main.ts");
30+
31+
extraFiles.forEach(extraFile => {
32+
testBuilder.addExtraFile(extraFile.fileName, extraFile.code);
33+
});
34+
35+
testBuilder.expectNoExecutionError();
36+
});

test/unit/outFile.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ test.each<[string, Record<string, string>]>([
138138
])("outFile tests (%s)", (_, files) => {
139139
const testBuilder = util.testBundle`
140140
${files["main.ts"]}
141-
`.setOptions({ outFile: "main.lua", module: ts.ModuleKind.AMD });
141+
`
142+
.setOptions({ outFile: "main.lua", module: ts.ModuleKind.AMD })
143+
.setModuleSystem("require");
142144

143145
const extraFiles = Object.keys(files)
144146
.map(file => ({ fileName: file, code: files[file] }))

test/util.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,23 @@ class AccessorTestBuilder extends TestBuilder {
419419
}
420420
}
421421

422-
class BundleTestBuilder extends AccessorTestBuilder {}
422+
class BundleTestBuilder extends AccessorTestBuilder {
423+
protected moduleSystemKind: "none" | "require" = "require";
424+
public setModuleSystem(kind: "none" | "require"): this {
425+
this.moduleSystemKind = kind;
426+
return this;
427+
}
428+
public getLuaCodeWithWrapper(): string {
429+
if (this.moduleSystemKind === "require") {
430+
return super.getLuaCodeWithWrapper();
431+
}
432+
433+
const noRequire = "_G.require = nil";
434+
const noPackage = "_G.package = nil";
435+
const luaCode = super.getLuaCodeWithWrapper();
436+
return `${noRequire}\n${noPackage}\n${luaCode}`;
437+
}
438+
}
423439

424440
class ModuleTestBuilder extends AccessorTestBuilder {
425441
public setReturnExport(name: string): this {

0 commit comments

Comments
 (0)