@gaiiaa/vua v0.0.2
Lua Vite Plugin
Seamlessly interop between Lua and Javascript via Vite.
Introduction
Ever wish Lua became the language of the browser instead of Javascript? Ever wanted to server render HTML with the same language Roblox uses for plugins? Well now you can. Vua is a Vite plugin for loading and running Lua scripts from Javasript using Wasmoon.
Usage
1. Install as a Vite Plugin
import { defineConfig } from "vite";
import vua from "@gaiiaa/vua";
export default defineConfig({
plugins: [vua()]
});
Typescript (optional)
Add /// <reference types="@gaiiaa/vua" />
to your project.
2. Create a Lua script
print("Hello world!")
function main(args)
local div = window.document.body:appendChild(
window.document:createElement("div")
)
div.textContent = args
end
3. Import into JS
import { init } from "./script.lua";
const script = await init({ window }).then(m => m.run())
script.entry("WOOO!")
Module Mode
Lua scripts postfixed with .module.lua
work like ES6 modules. They run on import and provide a proxy to global variables.
import script from "./script.lua";
script.doWork();
JS global variables can be injected into lua modules in the Vite plugin config.
export default defineConfig({
plugins: [vua({
globals: ["document", "alert" ] // globalThis.document, globalThis.alert
})]
});
Raw
You can import the raw script and set up your own runtime if you want full control of the execution context. Refer to the Wasmoon docs for details.
import { createRuntime } from "@gaiiaa/vua"
import { raw } from "./script.lua"
import $ from "jquery"
const runtime = await createRuntime();
runtime.global.set("j", $);
runtime.global.setMemoryMax(1024);
await runtime.doString(raw);
runtime.global.call("entry", 1, 2, 3)
Plugin Options
include?: Array<RegExp>
exclude?: Array<RegExp>
globals?: Array<string>
ssr?: boolean;
: Should transform SSR imports?
License
Made with 💛
Published under MIT License.
12 months ago