Skip to content

Commit 4424e02

Browse files
committed
fix LuaLib module resolution.
1 parent dd428ba commit 4424e02

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dist/lualib/**/*.json",
2222
"src/lualib/**/*.ts",
2323
"src/lualib/**/*.json",
24+
"src/lualib-build/**/*.ts",
2425
"language-extensions/index.d.ts"
2526
],
2627
"main": "dist/index.js",

src/LuaLib.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ function recompileLuaLibFiles(sourceOptions: CompilerOptions, emitHost: EmitHost
285285
if (!transpiledFiles) {
286286
const tsconfigPath =
287287
sourceOptions.luaTarget === LuaTarget.Lua50
288-
? path.join(__dirname, "./lualib/tsconfig.lua50.json")
289-
: path.join(__dirname, "./lualib/tsconfig.json");
288+
? path.join(__dirname, "../src/lualib/tsconfig.lua50.json")
289+
: path.join(__dirname, "../src/lualib/tsconfig.json");
290290
const config = parseConfigFileWithSystem(tsconfigPath);
291291
const options = config.options;
292292
options.luaPlugins = [...(options.luaPlugins ?? []), ...(sourceOptions.luaPlugins ?? [])];

src/lualib-build/plugin.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
isImport,
1919
isRequire,
2020
} from "./util";
21-
import { createDiagnosticFactoryWithCode } from "../utils";
21+
import { createDiagnosticFactoryWithCode, normalizeSlashes } from "../utils";
22+
import type { CompilerOptions } from "..";
2223

2324
export const lualibDiagnostic = createDiagnosticFactoryWithCode(200000, (message: string, file?: ts.SourceFile) => ({
2425
messageText: message,
@@ -168,6 +169,27 @@ class LuaLibPlugin implements tstl.Plugin {
168169
}
169170
return { result: result as LuaLibModulesInfo, diagnostics };
170171
}
172+
173+
public moduleResolution(
174+
moduleIdentifier: string,
175+
requiringFile: string,
176+
_options: CompilerOptions,
177+
emitHost: EmitHost
178+
): string | undefined {
179+
const tstlRoot = path.resolve(__dirname, "../..");
180+
const relativeRequiringFile = normalizeSlashes(path.relative(tstlRoot, requiringFile));
181+
if (!relativeRequiringFile.startsWith("src/lualib/")) {
182+
return;
183+
}
184+
185+
const tryingPaths = ["./", "./universal/"];
186+
for (const tryingPath of tryingPaths) {
187+
const possiblePath = path.join(tstlRoot, "src", "lualib", tryingPath, `${moduleIdentifier}.ts`);
188+
if (emitHost.fileExists(possiblePath)) {
189+
return possiblePath;
190+
}
191+
}
192+
}
171193
}
172194

173195
class LuaLibPrinter extends tstl.LuaPrinter {

0 commit comments

Comments
 (0)