Skip to content

Compile to JavaScript (--target js)

Inspired by Gleam, Orbita includes a compiler from Lua / TypeLua (.tyl) / .luax to modern JavaScript (ES Modules) — letting you share components and logic between the Luau backend and the browser.

Usage

ob build src/card.luax --target js -o dist/card.js

What Gets Compiled

  • TypeLua annotations: strips type annotations (local x: number = 1, typed parameters/returns, type Foo = { ... } declarations).
  • Control flow: if / elseif / else / end, while ... do ... end, numeric for i = 1, 10 do ... end, and for k, v in pairs/ipairs(t) do ... end.
  • Tables: automatically distinguishes object literals ({ a = 1 } → { a: 1 }) from arrays ({ 1, 2, 3 } → [1, 2, 3]).
  • Operators: .. → +, ~= → !==, == → ===, and → &&, or → ||, not → !, nil → null, #x → x.length.
  • Top-level exports: global functions (function Card(...)) are emitted as export function Card(...).
  • Minimal runtime preamble: generated JS includes ob_jsx, renderToString, escapeHtml, and mount(node, container) for zero-dependency browser mounting.

Example

Input (src/card.luax):

type CardProps = {
    title: string,
}

function Card(props: CardProps)
    local msg: string = "Card: " .. props.title
    return <section class="card"><h2>{msg}</h2></section>
end

Output (dist/card.js):

export function Card(props) {
  let msg = "Card: " + props.title;
  return ob_jsx("section", { "class": "card" }, [ob_jsx("h2", {}, [msg])]);
}

Mount directly in the browser:

import { Card, mount } from "./dist/card.js";

mount(Card({ title: "Orbita in the Browser" }), document.getElementById("app"));