Skip to content

Bug: packages with index.ts #976

@saksmt

Description

@saksmt

Either code generator or custom require in lua works incorrectly:

When importing import * from './pkg if pkg is a directory with index.ts tstl generates module for lua named pkg.index and generates import referencing just pkg without .index.

Sample:

main.ts:

import { hello } from './pkg';

print(hello);

pkg/index.ts:

export const hello = 'hello';

results in:

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 = {
["pkg.index"] = function() local ____exports = {}
____exports.hello = "hello"
return ____exports
end,
["main"] = function() local ____exports = {}
local ____pkg = require("pkg")
local hello = ____pkg.hello
print(hello)
return ____exports
end,
}
return require("main")

tstl version: 0.37.1

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