To not include lualib into bundle · Issue #1630 · TypeScriptToLua/TypeScriptToLua · GitHub
Skip to content

To not include lualib into bundle #1630

Open

Description

@inklesspen1rus

Hi! Thank you for great project!
I'm trying to make bundle from project with a lot of files but with external lualib. But didn't find any possible way. Is it possible?

Activity

  1. Zamiell commented on May 8, 2025

    @Zamiell
    Contributor

    Hello!
    I think you could set luaLibImport equal to none. See: https://typescripttolua.github.io/docs/configuration
    Then, you could make sure that the things like __TS__Class and so on are set as global variables and defined elsewhere before your code runs.
    However, I am not 100% sure if this is a good solution for your particular project though, I would need more context as to what you are trying to do.

  2. inklesspen1rus commented on May 8, 2025

    @inklesspen1rus
    Author

    Sadly but I can't predefine exports from lualib.
    My ides is bundle which imports lualib from environment, i.e. require('lualib_bundle')

    luaLibImport = none makes bundle do not require lualib at all

  3. Zamiell commented on May 8, 2025

    @Zamiell
    Contributor

    if you want to require it then can you set luaLibImport to require?

  4. inklesspen1rus commented on May 8, 2025

    @inklesspen1rus
    Author

    No, then bundle includes lualib that's not that I'm trying to get

  5. Zamiell commented on May 8, 2025

    @Zamiell
    Contributor

    You can just delete the lualib file afterward though with a script, right? (Assuming you are not bundling to a single file.)

  6. inklesspen1rus commented on May 8, 2025

    @inklesspen1rus
    Author

    Probably I'm bad in english so you're misunderstanding me
    I'll show example

    Here is my test project:
    project.zip

    I want to get this bundle (with no lualib included but require('lualib') still exists:
    good_bundle.txt

    But TSTL produces me this (lualib included):
    bad_bundle.txt

  7. inklesspen1rus commented on May 8, 2025

    @inklesspen1rus
    Author

    Well I resolved that for me with a small plugin
    Looks not so good and it additionaly minifies bundle but that works

    import * as ts from "typescript";
    import * as tstl from "typescript-to-lua";
    const luaparse = require('luaparse')
    const luamin = require('luamin')
    
    const plugin: tstl.Plugin = {
        beforeEmit(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost, result: tstl.EmitFile[]) {
            if (!options.luaBundle) return;
            const file = result[0];
            
            const ast = luaparse.parse(file.code, {
                luaVersion: options.luaTarget,
                scope: true
            })
    
            
            const modules = (ast.body as any[])
            .filter(x => x.type == 'AssignmentStatement' && (x.variables as any[]).find(x => x.name == '____modules'))
            const initIdx = (modules[0].variables as any[]).findIndex(x => x.name == '____modules')
            
            const initFields = modules[0].init[initIdx].fields as { key: {raw: string} }[]
            const lualibIdx = initFields.findIndex(x => x.key.raw == `"lualib_bundle"`)
            initFields.splice(lualibIdx, 1)
    
            file.code = luamin.minify(ast)
        },
    };
    
    export default plugin;
  8. Perryvw commented on May 8, 2025

    @Perryvw
    Member

    What is the reason you don't want to include the lualib into your bundle?

  9. inklesspen1rus commented on May 8, 2025

    @inklesspen1rus
    Author

    I'm developing programs for computers in minecraft with OpenComputers mod.
    Hard drives and RAM there have very limited size (up to 4MB for hard drivers, up to 1.5MB for memory sticks)

    Current bundle contains a copy of lualib which sizes ~80kb (and ~80kb in memory).
    Each bundle, loaded in memory, contains it's own copy of luabundle. Loading 4 programs at once will consume ~320kb bcs of lualib included.
    Removing lualib from bundle saved for me ~240kb bcs of sharing lualib between programs.

    Also my plugin saved 50kb of space and 30kb of memory due to smaller variable names.

    I want to run several programs at once so that change will save for me a lot of space and memory.

  10. inklesspen1rus commented on May 8, 2025

    @inklesspen1rus
    Author

    Also I want to distribute my programs easily, ideally just one file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions