#cargo #run #wasm #serve #wasm-bindgen

cargo-run-wasm

Trivially run wasm applications and examples in the browser

2 releases

0.1.1 Feb 23, 2022
0.1.0 Jan 5, 2022

#105 in WebAssembly

Download history 181/week @ 2022-01-24 186/week @ 2022-01-31 120/week @ 2022-02-07 146/week @ 2022-02-14 193/week @ 2022-02-21 182/week @ 2022-02-28 172/week @ 2022-03-07 152/week @ 2022-03-14 176/week @ 2022-03-21 220/week @ 2022-03-28 285/week @ 2022-04-04 240/week @ 2022-04-11 469/week @ 2022-04-18 217/week @ 2022-04-25 413/week @ 2022-05-02 325/week @ 2022-05-09

1,453 downloads per month

MIT/Apache

11KB
140 lines

cargo run-wasm

Crates.io Docs dependency status

Allows running wasm applications and examples as simply as:

cargo run-wasm crate_name

or

cargo run-wasm --example example_name

In the background it:

  1. Compiles the rust project to wasm
  2. Runs wasm-bindgen
  3. Generates an index.html that runs the wasm.
  4. Launches a tiny webserver to serve index.html + your wasm

Setup

  1. Setup your wasm runnable project as a crate within a cargo workspace
  2. Create a crate in the workspace named run-wasm with:

Cargo.toml:

[package]
name = "run-wasm"
version = "0.1.0"
edition = "2021"

[dependencies]
cargo-run-wasm = "0.1.0"

main.rs:

fn main() {
    cargo_run_wasm::run_wasm();
}
  1. Create a .cargo/config file containing:
[alias]
run-wasm = "run --release --package run-wasm --"
  1. Thats it, you can now run the commands described earlier. You can also run cargo run-wasm --help to view all the possible flags.

Advantages over an equivalent bash/powershell/bat script

  • cross platform
  • 0 external dependencies
  • better UX + more robust than anything hacked together with bash/powershell/bat
  • wasm-bindgen-cli version is always in sync with wasm-bindgen version because cargo update updates both of them at the same time thanks to being in the same workspace

cargo custom command

cargo-run-wasm is not available as a cargo custom command as that would cause:

  • issues with mismatches between wasm-bindgen versions
  • issues with keeping a stable interface with the wasm app
  • gives the idea that the command is compatible with every project that uses wasm which is not the case.

License

Licensed under either of

at your option.

Dependencies

~5MB
~101K SLoC