Skip to content

Commit 228d772

Browse files
authored
More resolution fixes (#1064)
* Fix files nested deeper in depedencies not correctly being resolved * Do not rewrite NoResolution imports in library build mode * Fix bundle require executing modules too often
1 parent 7b39ce3 commit 228d772

File tree

8 files changed

+86
-10
lines changed

8 files changed

+86
-10
lines changed

src/transpilation/bundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ local ____moduleCache = {}
1818
local ____originalRequire = require
1919
local function require(file)
2020
if ____moduleCache[file] then
21-
return ____moduleCache[file]
21+
return ____moduleCache[file].value
2222
end
2323
if ____modules[file] then
24-
____moduleCache[file] = ____modules[file]()
25-
return ____moduleCache[file]
24+
____moduleCache[file] = { value = ____modules[file]() }
25+
return ____moduleCache[file].value
2626
else
2727
if ____originalRequire then
2828
return ____originalRequire(file)

src/transpilation/resolve.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,14 @@ function resolveFileDependencies(file: ProcessedFile, context: ResolutionContext
134134

135135
// Do not resolve noResolution paths
136136
if (required.startsWith("@NoResolution:")) {
137-
const path = required.replace("@NoResolution:", "");
138-
replaceRequireInCode(file, required, path);
139-
replaceRequireInSourceMap(file, required, path);
137+
// Remove @NoResolution prefix if not building in library mode
138+
if (!isBuildModeLibrary(context.program)) {
139+
const path = required.replace("@NoResolution:", "");
140+
replaceRequireInCode(file, required, path);
141+
replaceRequireInSourceMap(file, required, path);
142+
}
143+
144+
// Skip
140145
continue;
141146
}
142147

@@ -227,7 +232,7 @@ function resolveLuaPath(fromPath: string, dependency: string, emitHost: EmitHost
227232
emitHost.directoryExists(path.join(dir, splitDependency[0]))
228233
);
229234
if (luaRoot) {
230-
return path.join(luaRoot, dependency.replace(".", path.sep)) + ".lua";
235+
return path.join(luaRoot, dependency.replace(/\./g, path.sep)) + ".lua";
231236
}
232237
}
233238
}

test/transpile/module-resolution.spec.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from "path";
22
import * as tstl from "../../src";
33
import * as util from "../util";
44
import * as ts from "typescript";
5-
import { transpileProject } from "../../src";
5+
import { BuildMode, transpileProject } from "../../src";
66

77
describe("basic module resolution", () => {
88
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-node-modules");
@@ -284,6 +284,7 @@ describe("dependency with complicated inner structure", () => {
284284
const expectedResult = {
285285
otherFileResult: "someFunc from otherfile.lua",
286286
otherFileUtil: "util",
287+
subsubresult: "result from subsub dir",
287288
utilResult: "util",
288289
};
289290

@@ -309,3 +310,43 @@ describe("dependency with complicated inner structure", () => {
309310
util.testProject(tsConfigPath).setMainFileName(mainFilePath).expectToEqual(expectedResult);
310311
});
311312
});
313+
314+
test("module resolution should not try to resolve @noResolution annotation", () => {
315+
util.testModule`
316+
import * as json from "json";
317+
const test = json.decode("{}");
318+
`
319+
.addExtraFile(
320+
"json.d.ts",
321+
`
322+
/** @noResolution */
323+
declare module "json" {
324+
function encode(this: void, data: unknown): string;
325+
function decode(this: void, data: string): unknown;
326+
}
327+
`
328+
)
329+
.expectToHaveNoDiagnostics();
330+
});
331+
332+
test("module resolution should not rewrite @NoResolution requires in library mode", () => {
333+
const { transpiledFiles } = util.testModule`
334+
import * as json from "json";
335+
const test = json.decode("{}");
336+
`
337+
.addExtraFile(
338+
"json.d.ts",
339+
`
340+
/** @noResolution */
341+
declare module "json" {
342+
function encode(this: void, data: unknown): string;
343+
function decode(this: void, data: string): unknown;
344+
}
345+
`
346+
)
347+
.setOptions({ buildMode: BuildMode.Library })
348+
.getLuaResult();
349+
350+
expect(transpiledFiles).toHaveLength(1);
351+
expect(transpiledFiles[0].lua).toContain('require("@NoResolution:');
352+
});

test/transpile/module-resolution/project-with-complicated-dependency/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import * as dependency1 from "dependency1";
33
export const otherFileResult = dependency1.otherFileFromDependency1();
44
export const utilResult = dependency1.callUtil();
55
export const otherFileUtil = dependency1.otherFileUtil();
6+
export const subsubresult = dependency1.subsubdirfileResult;

test/transpile/module-resolution/project-with-complicated-dependency/node_modules/dependency1/index.d.ts

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/transpile/module-resolution/project-with-complicated-dependency/node_modules/dependency1/index.lua

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/transpile/module-resolution/project-with-complicated-dependency/node_modules/dependency1/subdir/subsubdir/subsubdirfile.lua

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/unit/bundle.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ test("cyclic imports", () => {
109109
.expectToEqual(new util.ExecutionError("stack overflow"));
110110
});
111111

112+
test("does not evaluate files multiple times", () => {
113+
util.testBundle`
114+
import "./countingfile";
115+
import "./otherfile";
116+
117+
export const count = _count;
118+
`
119+
.addExtraFile(
120+
"otherfile.ts",
121+
`
122+
import "./countingfile";
123+
`
124+
)
125+
.addExtraFile(
126+
"countingfile.ts",
127+
`
128+
declare var _count: number | undefined;
129+
_count = (_count ?? 0) + 1;
130+
`
131+
)
132+
.expectToEqual({ count: 1 });
133+
});
134+
112135
test("no entry point", () => {
113136
util.testBundle``
114137
.setOptions({ luaBundleEntry: undefined })

0 commit comments

Comments
 (0)