Skip to content

⚠️ "Remove imports => remove Lua 'requires'" breaks bundle because imports never called #24

@websharik

Description

@websharik

I try make bundle with low level TSTL API:

const tstl = require("typescript-to-lua")
function makeBundleFile(filePath, conf) {
  return new Promise(async (resolve, reject) => {
    try {
      let result = await tstl.transpileFiles([filePath], {
        //original from tsconfig.json (wtf mix ts and tstl)
        compilerOptions: {
          ...conf.compilerOptions
        },
        tstl: {
          ...conf.tstl
        },
        plugins: [
          ...conf.compilerOptions.plugins
        ],
        types: ["lua-types/5.1"],
        ...conf.tstl,
        //if not . then put out to src
        rootDir: ".",
        //out path with same name from src
        luaBundle: `${conf.compilerOptions.outDir}/${path.basename(filePath).replace('.ts', '.lua')}`,
        //entry point
        luaBundleEntry: filePath
      })
      console.log(result)
      resolve(result)
    } catch (e) {
      reject(e)
    }
  })
}

test_ts.ts

import { msg } from "./separatedFile"
print(`msg: ${msg}`)

separatedFile.ts

export const msg = 'Hello world'

result:

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 = {
["src.example.separatedFile"] = function()
local ____exports = {}
____exports.msg = "Hello world"
_G.msg = ____exports.msg
return ____exports
 end,
["src.example.test_ts"] = function()
local ____exports = {}
print("msg: " .. msg)
return ____exports
 end,
}
return require("src.example.test_ts")

src.example.separatedFile never called and msg is stay undefined

Mb add plugins option to disable remove imports ? Or else solution...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions