WASM Skill
Rust to WebAssembly scaffolds and interop - an Agent Skill for Rust 1.97.0 and browser or WASI deployments.
What This Skill Does
Creates cdylib crates with wasm-bindgen, wasm-pack build, JS glue, and size-tuned release profiles. Reviews #[wasm_bindgen] exports and panic hooks for browser consoles.
When to Invoke
- Porting compute-heavy Rust to the browser
- Sharing logic between native CLI and web UI
- Building WASI modules for edge runtimes
- Reviewing wasm bundle size regressions
Inputs
| Input | Why |
|---|---|
| Target | wasm32-unknown-unknown vs wasi |
| JS framework | Raw bundler vs npm package |
| Exported API | Functions, classes, strings |
| Size budget | KB limit for CDN delivery |
Outputs
Cargo.tomlwithcrate-type = ["cdylib"]wasm-packbuild scriptswee_allocor default allocator choice documentedindex.htmldemo or npmpkg/publish layout- Size report from
twiggyorwasm-objdump
Guardrails
- Release profile
opt-level = "s"or"z"for size-sensitive WASM. - Avoid panics across boundary - map to
Resultin exports. - No filesystem assumptions - browser sandbox differs from native.
- Feature-gate console_error_panic_hook - dev only when possible.
- Test in headless browser CI - wasm-bindgen-test or playwright smoke.
Recipe
wasm-pack build --target web --release
wasm-opt -Oz -o pkg/my_module_bg.wasm pkg/my_module_bg.wasmWorking Example
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add(a: u32, b: u32) -> u32 {
a + b
}FAQs
wasm-bindgen vs wasi?
wasm-bindgen for browsers; WASI for server-side WASM runtimes.
Can I use tokio in WASM?
Generally no full Tokio; use wasm-bindgen-futures for browser async.
How small can bundles get?
Strip debug, run wasm-opt, avoid pulling full serde where hand-rolled JS types suffice.
Related
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.