Skip to content

Commit b683a48

Browse files
committed
Allow export equals in bundles
1 parent b2ab7db commit b683a48

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/LuaTransformer.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export class LuaTransformer {
131131
const combinedStatements = bundle.sourceFiles.reduce(
132132
(statements: tstl.Statement[], sourceFile: ts.SourceFile) => {
133133
this.currentSourceFile = sourceFile;
134+
this.visitedExportEquals = false;
134135
this.isModule = tsHelper.isFileModule(sourceFile);
135136
const originalSourceFile = ts.getParseTreeNode(sourceFile, ts.isSourceFile) || sourceFile;
136137
this.resolver = this.checker.getEmitResolver(originalSourceFile);
@@ -169,16 +170,18 @@ export class LuaTransformer {
169170
statements = this.performHoisting(this.transformStatements(sourceFile.statements));
170171
this.popScope();
171172

172-
if (this.isModule && !this.isWithinBundle) {
173-
// If export equals was not used. Create the exports table.
174-
// local exports = {}
175-
if (!this.visitedExportEquals) {
176-
statements.unshift(
177-
tstl.createVariableDeclarationStatement(
178-
this.createExportsIdentifier(),
179-
tstl.createTableExpression()
180-
)
181-
);
173+
if (this.isModule) {
174+
if (!this.isWithinBundle) {
175+
// If export equals was not used. Create the exports table.
176+
// local exports = {}
177+
if (!this.visitedExportEquals) {
178+
statements.unshift(
179+
tstl.createVariableDeclarationStatement(
180+
this.createExportsIdentifier(),
181+
tstl.createTableExpression()
182+
)
183+
);
184+
}
182185
}
183186

184187
// return exports
@@ -192,11 +195,10 @@ export class LuaTransformer {
192195
tstl.createStringLiteral("preload")
193196
);
194197
const exportPath = tsHelper.getExportPath(sourceFile.fileName, this.options);
198+
const moduleParameters = this.visitedExportEquals ? undefined : [this.createExportsIdentifier()];
195199
const packagePreloadDeclaration = tstl.createAssignmentStatement(
196200
tstl.createTableIndexExpression(packagePreload, tstl.createStringLiteral(exportPath)),
197-
tstl.createFunctionExpression(tstl.createBlock(statements, sourceFile), [
198-
this.createExportsIdentifier(),
199-
])
201+
tstl.createFunctionExpression(tstl.createBlock(statements, sourceFile), moduleParameters)
200202
);
201203
return tstl.createBlock([packagePreloadDeclaration], sourceFile);
202204
}

src/lualib/LuaRequire.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const tstlpackage: {
2-
preload: Record<string, (this: void, exports: object) => void>;
2+
preload: Record<string, (this: void, exports: object) => object>;
33
loaded: Record<string, object>;
44
} = { preload: {}, loaded: {} };
55

@@ -14,6 +14,6 @@ function __TS__LuaRequire(this: void, moduleName: string): any {
1414
}
1515
const moduleExports = {};
1616
tstlpackage.loaded[moduleName] = moduleExports;
17-
loadScript(moduleExports);
18-
return moduleExports;
17+
tstlpackage.loaded[moduleName] = loadScript(moduleExports);
18+
return tstlpackage.loaded[moduleName];
1919
}

test/unit/outFile.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const reexportValueSource = 'export { value } from "./export";';
88
const exportResultSource = "export const result = value;";
99

1010
function outFileOptionsWithEntry(luaEntry: string): CompilerOptions {
11-
return { outFile: "main.lua", noHoisting: true, module: ts.ModuleKind.AMD, luaEntry };
11+
return { outFile: "main.lua", module: ts.ModuleKind.AMD, luaEntry };
1212
}
1313

1414
test("import module -> main", () => {
@@ -112,7 +112,7 @@ test("cyclic imports", () => {
112112
export const value = a;
113113
`
114114
)
115-
.setOptions(outFileOptionsWithEntry("main.ts"))
115+
.setOptions({ noHoisting: true, ...outFileOptionsWithEntry("main.ts") })
116116
.expectToEqual({ a: true, result: true });
117117
});
118118

@@ -129,3 +129,7 @@ test("luaEntry resolved from path specified in tsconfig", () => {
129129
.setOptions({ rootDir: "src", ...outFileOptionsWithEntry("src/main.ts") })
130130
.expectToHaveNoDiagnostics();
131131
});
132+
133+
test("export equals", () => {
134+
util.testBundle`export = "result"`.setOptions(outFileOptionsWithEntry("main.ts")).expectToEqual("result");
135+
});

0 commit comments

Comments
 (0)