Skip to content

Broken require paths in bundles #823

@tomblind

Description

@tomblind

If a TSTL bundled project is built from outside the project directory, the require paths will be broken.

Example:

tstltest/
  tsconfig.json
  foo.ts
  bar.ts
// tsconfig.json
{
    "include": [
        "./*.ts"
    ],
    "tstl":
    {
        "noHeader": true,
        "luaBundle": "bundle.lua",
        "luaBundleEntry": "foo.ts"
    }
}
// foo.ts
import {Bar} from "./bar";
console.log(Bar.bar);
// bar.ts
export namespace Bar {
    export const bar = "bar";
}
-- bundle.lua, if built from parent directory ('tstl -p tstltest')
local ____modules = {}
local ____moduleCache = {}
local ____originalRequire = require
local function require(file)
    if ____moduleCache[file] then
        return ____moduleCache[file]
    end
    if ____modules[file] then
        ____moduleCache[file] = ____modules[file]()
        return ____moduleCache[file]
    else
        if ____originalRequire then
            return ____originalRequire(file)
        else
            error("module '" .. file .. "' not found")
        end
    end
end
____modules = {
["bar"] = function() local ____exports = {}
____exports.Bar = {}
local Bar = ____exports.Bar
do
    Bar.bar = "bar"
end
return ____exports
end,
["foo"] = function() local ____exports = {}
local ____bar = require("tstltest.bar") -- <----- this will fail
local Bar = ____bar.Bar
print(Bar.bar)
return ____exports
end,
}
return require("foo")

This can be worked around right now with a "rootDir": "." in the tsconfig.json, but requiring files that are part of the bundle should always work, regardless of the build environment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugscope: bundlingRelated to bundling of generated lua files.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions