Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepending Lua script to emission prior to source map finalization #1529

Open
BribeFromTheHive opened this issue Feb 3, 2024 · 2 comments
Open
Labels
question scope: bundling Related to bundling of generated lua files.

Comments

@BribeFromTheHive
Copy link

BribeFromTheHive commented Feb 3, 2024

Hi,

In the WarCraft 3 environment, we start with a preexisting war3map.lua file (containing information about preplaced units and other GUI elements), and we also do not have access to the debug library. With the help of the WarCraft 3 community, this exists as a decent enough manual implementation of debug.traceback:

local tracebackHeader = 'stack traceback:'
local infoObject = {short_src = 'war3map.lua'}
local _
debug = {
    traceback = function(message, lineNumber)
        message = message and message .. '\n' .. tracebackHeader
        lineNumber = lineNumber or 1
        local traceback = {message or tracebackHeader}
        local lineError, lastError = '', ''
        repeat
            _, lineError = pcall(error, '', lineNumber)
            if lineError ~= '' and lineError ~= lastError then
                traceback[#traceback + 1] = lineError
                lastError = lineError
            end
            lineNumber = lineNumber + 1
        until lineNumber >= 200
        return table.concat(traceback, '\n\t')
    end,
    getinfo = function(whatInfo)
        return (whatInfo == 1) and infoObject or nil
    end
}

I've tailored this with a custom getinfo that taps into the positions used by TypeScript2Lua to get the name of the current map file.

WarCraft 3 doesn't allow the require syntax either, so we are stuck with just one finalized war3map.lua output file. Therefore, being able to have full control over how the compiled output is framed, prior to source map finalization, would allow extremely powerful source mapping in the WarCraft 3 environment (as we've been restricted to hunting through a tens-of-thousands-of-lines-long Lua file for years).

The problem is, that both war3map.lua AND this custom debug library, need to be placed ABOVE the emitted TypeScript2Lua compiled file. This interferes with the ability to properly use source maps. Therefore, I'd ask if it's possible (or if it can be made possible) to perhaps include a table structure for the compilation, and the output will be merged all into one blob, prior to the source mapping being finalized.

e.g. adding something like this to the TSTL config:

'prepended-files': ['custom-debug.lua', 'war3map.lua']

I am not sure if I can use something like the afterPrint Plugin - does this run before or after the source maps were resolved? Would I be able to prepend my own files to the ProcessedFile array passed as the 4th parameter to afterPrint?

@Perryvw
Copy link
Member

Perryvw commented Feb 3, 2024

You could probably get this to work with plugins somehow, but an easier might be to simply import those pre-existing bundles into your bundle as external lua. I don't know enough of the w3 environment to know if this would work or not but I would use something like this:

tsconfig.json:

{
    "compilerOptions": {
        "target": "esnext",
        "types": ["@typescript-to-lua/language-extensions"]
    },
    "tstl": {
        "luaBundle": "bundle.lua",
        "luaBundleEntry": "main.ts",
        "sourceMapTraceback": true
    }
}

main.ts:

import "custom-debug";
import "war3map.lua";

And then create custom-debug.d.ts and war3map.d.ts files next to the lua files you are importing. They can be empty or you can put some stuff in there you need from those files.

I tested this and it produces the correct result, it imports the two lua files verbatim into the bundle and will execute them before any user code. This should allow the debug traceback on your bundle to function normally.

@Perryvw Perryvw added question scope: bundling Related to bundling of generated lua files. labels Feb 3, 2024
@BribeFromTheHive
Copy link
Author

Thanks so much for clarifying! Maybe it really is a lot less complicated than I thought!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question scope: bundling Related to bundling of generated lua files.
Projects
None yet
Development

No branches or pull requests

2 participants