Skip to main content

Modules (Import / Export)

Nebra uses an ES-style module system that compiles to Lua's require().

Importing​

Named Import​

import { Vector2, Rect } from "engine/math"

local v = Vector2.new(1, 2)

Compiles to:

local __mod = require("engine/math")
local Vector2 = __mod.Vector2
local Rect = __mod.Rect

Aliased Import​

import { Vector2 as Vec2, Rect as Box } from "engine/math"

Default Import​

import Player from "entities/player"

Namespace Import​

import * as utils from "lib/utils"

utils.debug()
utils.format("hello")

Side-Effect Import​

Runs the module without binding any names:

import "polyfill"

Exporting​

Export Function​

export function calculate(x: number): number
return x * 2
end

Export Local Function​

export local function helper(): string
return "help"
end

Export Variable​

export local PI: number = 3.14159
export local mut counter: number = 0

Export Enum​

export enum LogLevel
Debug
Info
Warn
Error
end

Export Class​

export class Connection
host: string
port: number

constructor(host: string, port: number)
self.host = host
self.port = port
end
end

Export Interface​

export interface Handler
function handle(data: string): void
end

Module Return​

Exports compile to a return table at the end of the file:

-- Generated Lua
local function calculate(x)
return x * 2
end

return {
calculate = calculate
}

Import Path Resolution​

The import path is passed to the import_statement config option (default: require(%s)). Paths use forward slashes and no file extension:

import { Foo } from "lib/foo" -- require("lib/foo")
import Bar from "src/components/bar" -- require("src/components/bar")