Skip to content

Commit a5597b6

Browse files
committed
Use enhanced-resolve for module resolution
1 parent 3dd2c7e commit a5597b6

4 files changed

Lines changed: 38 additions & 35 deletions

File tree

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"node": ">=12.13.0"
3737
},
3838
"dependencies": {
39+
"enhanced-resolve": "^5.2.0",
3940
"resolve": "^1.15.1",
4041
"source-map": "^0.7.3",
4142
"typescript": "^3.9.2"

src/transpilation/transpiler.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { ResolverFactory } from "enhanced-resolve";
2+
import * as fs from "fs";
13
import * as path from "path";
2-
import * as resolve from "resolve";
34
import * as ts from "typescript";
45
import { CompilerOptions, isBundleEnabled, LuaTarget } from "../CompilerOptions";
56
import { getLuaLibBundle } from "../LuaLib";
@@ -53,9 +54,6 @@ export class Transpiler {
5354
}
5455
}
5556

56-
// TODO: JIT -> jit?
57-
type PackageLuaField = string | Record<LuaTarget, string>;
58-
5957
class Transpilation {
6058
public readonly diagnostics: ts.Diagnostic[] = [];
6159
private seenFiles = new Set<string>();
@@ -130,37 +128,17 @@ class Transpilation {
130128
this.files.push(file);
131129
}
132130

133-
protected resolveRequest(request: string, issuer: string) {
134-
const resolved = resolve.sync(request, {
135-
basedir: path.dirname(issuer),
136-
extensions: [".lua", ".ts", ".tsx", ".js", ".jsx"],
137-
packageFilter: (pkg, pkgFile) => {
138-
delete pkg.main;
139-
140-
const lua: PackageLuaField = pkg.lua;
141-
if (lua !== undefined) {
142-
if (typeof lua === "string") {
143-
pkg.main = lua;
144-
} else if (typeof lua === "object") {
145-
const luaTarget = this.options.luaTarget ?? LuaTarget.Universal;
146-
pkg.main = lua[luaTarget];
147-
if (pkg.main === undefined) {
148-
throw new Error(`${pkgFile} not supports Lua ${luaTarget} target.`);
149-
}
150-
} else {
151-
throw new Error(`${pkgFile} has invalid "lua" field value.`);
152-
}
153-
}
154-
155-
return pkg;
156-
},
157-
});
158-
159-
if (resolve.isCore(resolved)) {
160-
throw new Error(`"${resolved}" is builtin Node.js module.`);
161-
}
131+
protected resolver = ResolverFactory.createResolver({
132+
extensions: [".lua", ".ts", ".tsx", ".js", ".jsx"],
133+
conditionNames: ["lua", `lua:${this.options.luaTarget ?? LuaTarget.Universal}`],
134+
fileSystem: this.emitHost.fileSystem ?? fs,
135+
useSyncFileSystemCalls: true,
136+
});
162137

163-
return resolved;
138+
protected resolveRequest(request: string, issuer: string) {
139+
const result = this.resolver.resolveSync({}, path.dirname(issuer), request);
140+
assert(typeof result === "string");
141+
return result;
164142
}
165143

166144
protected toGeneratedFileName(fileName: string) {

src/transpilation/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from "fs";
12
import * as path from "path";
23
import * as resolve from "resolve";
34
import { SourceNode } from "source-map";
@@ -7,7 +8,9 @@ import * as cliDiagnostics from "../cli/diagnostics";
78
import * as lua from "../LuaAST";
89
import * as diagnosticFactories from "./diagnostics";
910

10-
export type EmitHost = Pick<ts.System, "getCurrentDirectory" | "readFile" | "writeFile">;
11+
export interface EmitHost extends Pick<ts.System, "getCurrentDirectory" | "readFile" | "writeFile"> {
12+
fileSystem?: typeof fs;
13+
}
1114

1215
interface BaseFile {
1316
code: string;

0 commit comments

Comments
 (0)