Skip to content

Commit 7b17745

Browse files
committed
Support cyclic imports in output bundles
1 parent 1606517 commit 7b17745

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/LuaTransformer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class LuaTransformer {
167167
statements = this.performHoisting(this.transformStatements(sourceFile.statements));
168168
this.popScope();
169169

170-
if (this.isModule) {
170+
if (this.isModule && !this.isWithinBundle) {
171171
// If export equals was not used. Create the exports table.
172172
// local exports = {}
173173
if (!this.visitedExportEquals) {
@@ -192,7 +192,9 @@ export class LuaTransformer {
192192
const exportPath = tsHelper.getExportPath(sourceFile.fileName, this.options);
193193
const packagePreloadDeclaration = tstl.createAssignmentStatement(
194194
tstl.createTableIndexExpression(packagePreload, tstl.createStringLiteral(exportPath)),
195-
tstl.createFunctionExpression(tstl.createBlock(statements, sourceFile))
195+
tstl.createFunctionExpression(tstl.createBlock(statements, sourceFile), [
196+
this.createExportsIdentifier(),
197+
])
196198
);
197199
return tstl.createBlock([packagePreloadDeclaration], sourceFile);
198200
}

src/lualib/LuaRequire.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
const tstlpackage = { preload: {}, loaded: {} };
1+
const tstlpackage: {
2+
preload: Record<string, (this: void, exports: object) => void>;
3+
loaded: Record<string, object>;
4+
} = { preload: {}, loaded: {} };
25

36
function __TS__LuaRequire(this: void, moduleName: string): any {
4-
if (!tstlpackage.loaded[moduleName]) {
5-
const module: (this: void, module: string) => any = tstlpackage.preload[moduleName];
6-
if (module) {
7-
tstlpackage.loaded[moduleName] = module(moduleName);
8-
} else {
9-
// tslint:disable-next-line: no-string-throw
10-
throw `module '${moduleName}' not found:`;
11-
}
7+
if (tstlpackage.loaded[moduleName]) {
8+
return tstlpackage.loaded[moduleName];
129
}
13-
return tstlpackage.loaded[moduleName];
10+
const loadScript = tstlpackage.preload[moduleName];
11+
if (!loadScript) {
12+
// tslint:disable-next-line: no-string-throw
13+
throw `module '${moduleName}' not found`;
14+
}
15+
const moduleExports = {};
16+
tstlpackage.loaded[moduleName] = moduleExports;
17+
loadScript(moduleExports);
18+
return moduleExports;
1419
}

test/unit/outFile.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,22 @@ test.each<[string, Record<string, string>]>([
150150

151151
testBuilder.expectNoExecutionError();
152152
});
153+
154+
test("outFile cyclic imports", () => {
155+
util.testBundle`
156+
export const a = true;
157+
import { b } from "./b";
158+
if (b !== true) {
159+
throw "Did not receive true from module b";
160+
}
161+
`
162+
.addExtraFile(
163+
"b.ts",
164+
`
165+
import { a } from "./main";
166+
export const b = a;
167+
`
168+
)
169+
.setOptions({ outFile: "main.lua", noHoisting: true, module: ts.ModuleKind.AMD, luaEntry: ["main.ts"] })
170+
.expectNoExecutionError();
171+
});

0 commit comments

Comments
 (0)