wasm-bindings
wasm-bindgen conventions for compiling a Rust core to WebAssembly for JavaScript: wasm-pack packaging, JsValue mapping, async futures, bundle-size tuning, web-sys/js-sys, and TypeScript defs. Load whe
Files1
View on GitHubSKILL.mdinternal/builtins/bindings/wasm/skills/wasm-bindings/SKILL.md
GitHub- Use
wasm-bindgenfor JavaScript interop. Usewasm-packfor building and packaging. - Map Rust types to JS with
#[wasm_bindgen]. UseJsValuefor dynamic types, typed wrappers for known types. - Handle errors with
Result<T, JsValue>. Convert Rust errors to descriptive JS error messages. - Support both browser (ES modules) and Node.js (CommonJS/ESM) targets.
- Async: use
wasm-bindgen-futuresfor Promise-returning functions. Never block the main thread. - Bundle size: use
opt-level = "z",lto = true,codegen-units = 1in release profile. - Use
web-sysandjs-sysfor Web API access. Feature-gate platform-specific APIs. - Test with
wasm-pack test --headless --firefoxand a JS test runner (e.g., Vitest or Jest) for JS integration tests. - Generate TypeScript definitions automatically. Keep
.d.tsfiles in sync with Rust exports. - Anti-patterns: no
std::thread(single-threaded), no synchronous I/O, no panics (becomes JS exception).