mahoraga-js
v0.2.0
Published
WebAssembly bindings for the mahoraga expression language
Maintainers
Readme
mahoraga-js
WebAssembly bindings for the mahoraga expression language, built for bundlers (Vite, webpack).
npm install mahoraga-jsimport { evaluate } from "mahoraga-js";
evaluate("1 + 2 * 5"); // 11
evaluate("user.name | to_uppercase()", { user: { name: "ann" } }); // "ANN"
evaluate("price | discount(10)", { price: 50, discount: (p, pct) => p * (1 - pct / 100) }); // 45
try {
evaluate("'test' + 2");
} catch (err) {
// err.name: "LexerError" | "ParserError" | "EvalError"
console.log(err.message, err.span, err.help);
}Vite 8 loads the .wasm without plugins, but the dev server's dependency pre-bundling must skip the package.
Otherwise the wasm and its JS glue end up as separate module instances and loading fails with wasm is undefined:
// vite.config.js
export default defineConfig({
optimizeDeps: { exclude: ["mahoraga-js"] },
});webpack 5 needs experiments: { asyncWebAssembly: true }.
Node 24+ can import it directly, with an experimental-feature warning.
API
evaluate(expr, context?, maxLength = 2000) evaluates expr against the standard library.
Keys of context become top-level identifiers.
| JS | mahoraga |
|--------------------------------------|----------|
| null | null |
| undefined | void |
| boolean | bool |
| number (safe integer) | integer |
| number (otherwise) | float |
| bigint (fits in i64) | integer |
| string | string |
| plain object (or null prototype) | object |
| Array | array |
| function | closure |
Errors
Development
cargo install wasm-pack
wasm-pack build --release --target bundler # pkg/, the npm package
wasm-pack build --release --target nodejs --out-dir target/pkg-node
node --test 'tests/*.test.mjs'Publishing
rm -rf pkg && wasm-pack build --release --target bundler
# On a clean build wasm-pack copies the license files but leaves them out of `files`.
npm pkg set 'files[]=LICENSE-MIT' --prefix pkg
npm publish ./pkg