Skip to content

Commit f2563cc

Browse files
committed
style: format 5.0 and non-5.0 require shims
1 parent d1bf66a commit f2563cc

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/transpilation/bundle.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const createModulePath = (pathToResolve: string, program: ts.Program) =>
1313

1414
// Override `require` to read from ____modules table.
1515
function requireOverride(options: CompilerOptions) {
16-
const isLua50 = options.luaTarget === LuaTarget.Lua50;
17-
return `
16+
if (options.luaTarget === LuaTarget.Lua50) {
17+
return `
1818
local ____modules = {}
1919
local ____moduleCache = {}
2020
local ____originalRequire = require
@@ -24,9 +24,7 @@ local function require(file, ...)
2424
end
2525
if ____modules[file] then
2626
local module = ____modules[file]
27-
____moduleCache[file] = { value = (${isLua50 ? "table.getn(arg)" : 'select("#", ...)'} > 0) and module(${
28-
isLua50 ? "unpack(arg)" : "..."
29-
}) or module(file) }
27+
____moduleCache[file] = { value = (table.getn(arg) > 0) and module(unpack(arg)) or module(file) }
3028
return ____moduleCache[file].value
3129
else
3230
if ____originalRequire then
@@ -37,6 +35,29 @@ local function require(file, ...)
3735
end
3836
end
3937
`;
38+
} else {
39+
return `
40+
local ____modules = {}
41+
local ____moduleCache = {}
42+
local ____originalRequire = require
43+
local function require(file, ...)
44+
if ____moduleCache[file] then
45+
return ____moduleCache[file].value
46+
end
47+
if ____modules[file] then
48+
local module = ____modules[file]
49+
____moduleCache[file] = { value = (select("#", ...) > 0) and module(...) or module(file) }
50+
return ____moduleCache[file].value
51+
else
52+
if ____originalRequire then
53+
return ____originalRequire(file)
54+
else
55+
error("module '" .. file .. "' not found")
56+
end
57+
end
58+
end
59+
`;
60+
}
4061
}
4162

4263
export const sourceMapTracebackBundlePlaceholder = "{#SourceMapTracebackBundle}";

0 commit comments

Comments
 (0)