| 13 | |
| 14 | // Override `require` to read from ____modules table. |
| 15 | function requireOverride(options: CompilerOptions) { |
| 16 | const runModule = |
| 17 | options.luaTarget === LuaTarget.Lua50 |
| 18 | ? "if (table.getn(arg) > 0) then value = module(unpack(arg)) else value = module(file) end" |
| 19 | : 'if (select("#", ...) > 0) then value = module(...) else value = module(file) end'; |
| 20 | return ` |
| 21 | local ____modules = {} |
| 22 | local ____moduleCache = {} |
| 23 | local ____originalRequire = require |
| 24 | local function require(file, ...) |
| 25 | if ____moduleCache[file] then |
| 26 | return ____moduleCache[file].value |
| 27 | end |
| 28 | if ____modules[file] then |
| 29 | local module = ____modules[file] |
| 30 | local value = nil |
| 31 | ${runModule} |
| 32 | ____moduleCache[file] = { value = value } |
| 33 | return value |
| 34 | else |
| 35 | if ____originalRequire then |
| 36 | return ____originalRequire(file) |
| 37 | else |
| 38 | error("module '" .. file .. "' not found") |
| 39 | end |
| 40 | end |
| 41 | end |
| 42 | `; |
| 43 | } |
| 44 | |
| 45 | export const sourceMapTracebackBundlePlaceholder = "{#SourceMapTracebackBundle}"; |
| 46 | |