This repository contains a minimal workflow that lets you use Rust-compiled WebAssembly inside a TypeScript project almost like WGSL — by writing Rust code as a string, compiling it on demand via a local Node.js server, and loading it dynamically on the client.
The goal is to keep WASM usage simple, stable and static-function-like, without exposing JS classes, descriptors or complex bindings.
- You write Rust code as a string inside a TypeScript file.
- The client requests a WASM module using
WASM.get(). - A small Node.js server receives the request and:
- checks if a WASM binary already exists
- or instantly recompiles the Rust source (
wasm32-unknown-unknown)
- The server writes the compiled
.wasmfile into the client’spublic/folder. - The client then loads and uses the module’s exported functions.
This gives a workflow where Rust feels like a collection of static functions with raw pointers and numbers — no accidental JS allocations or hidden complexity.
const source = `
#[no_mangle]
pub extern "C" fn incr_buffer(ptr: *mut f32, len: usize) {
let slice = unsafe { std::slice::from_raw_parts_mut(ptr, len) };
for v in slice.iter_mut() { *v += 1.0; }
}
`;
const wasm = await WASM.get("test", source);
const arr = new Float32Array(wasm.buffer, 0, 4);
console.log(arr); // 0, 0, 0, 0
wasm.incr_buffer(0, 4);
console.log(arr); // 1, 1, 1, 1/
├── server.js # Node.js on-demand compiler
├── wasm/ # Temporary Rust source & build files
└── client/
├── public/ # Compiled .wasm files appear here
├── src/
└── ...Clone the repo and install root dependencies:
npm installInstall client dependencies:
cd client
npm installFrom the root folder:
npm run startThis starts the on-demand Rust → WASM compilation server.
In another terminal:
cd client
npm run devThis starts the client app and automatically reloads whenever a .wasm file is (re)generated.
- Compilation is practically instant for small Rust snippets.
- Ideal for experiments or prototyping low-level logic.
- The WASM API is intentionally minimal: no objects, no classes, only numeric parameters and raw memory.
MIT — feel free to explore, fork, and adapt.