- build with debugging features:
wasm-pack build --dev --features dev
- run the web application in `:
- Include debug symbols (function names, variable names) in the compiled WASM so that stack traces and wasm are more readable
- Disable on release builds to reduce code size
# usage: wasm-pack build --dev
[profile.dev]
debug = true
- causes panics to log with
console.error
- Add the hook to
cargo.toml
but disable it for release builds because it increases code size
# usage: wasm-pack build --features dev
[features]
dev = ["console_error_panic_hook"]
console_error_panic_hook = { version = "0.1.7", optional = true }
- enable it within a function at the start of the rust execution, any subsequent panics will be logged
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
# usage: wasm-pack build --dev
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
dwarf-debug-info = true
- Converts
.wasm
binary into a human-readable WebAssembly Text Format (WAT)
- Edit the
.wat
file and then recompile to .wasm
wasm2wat pkg/project.wasm -o project.wat
wat2wasm project.wat -o new_project.wasm
- exposes bindings to all browser web APIs
- include
web-sys
in cargo.toml
web-sys = { version = "0.3.77", features = ["console"] }
pub fn log(message: &str) {
web_sys::console::log_1(&message.into());
}
- Use
wasm_bindgen
to generate bindings to external functions yourself
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
// binds external JS console.log function to rust function
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
}