Skip to content

Export as Global

By default, tslua wraps each module’s exports in a ____exports table and returns it, matching standard Lua module conventions. The exportAsGlobal option strips this wrapper and emits exported declarations as bare globals instead.

This is useful for embedded Lua environments where scripts run in a global scope rather than as require()-able modules.

Set exportAsGlobal in tsconfig.json:

{
"tstl": {
"exportAsGlobal": true
}
}

Or use the CLI flag:

Terminal window
tslua -p tsconfig.json --exportAsGlobal

Given this TypeScript:

export const SPEED = 200;
export const GRAVITY = 9.8;
export const PLAYER_NAME = "hero";

Default output (module wrapper):

local ____exports = {}
____exports.SPEED = 200
____exports.GRAVITY = 9.8
____exports.PLAYER_NAME = "hero"
return ____exports

With exportAsGlobal: true:

SPEED = 200
GRAVITY = 9.8
PLAYER_NAME = "hero"

Exports become top-level declarations, accessible to the host environment without a module wrapper.

Instead of a boolean, exportAsGlobal accepts a regex string to selectively apply to specific files:

{
"tstl": {
"exportAsGlobal": "\\.script\\.ts$"
}
}

This applies export-as-global only to files matching the pattern (e.g. game.script.ts), while other files (e.g. util.ts) keep their module wrappers. Useful when some files are entry-point scripts and others are shared modules.