posted on 2021-11-20

rust & wasm, without wasm-pack

i like to keep my software stacks simple.

to write wasm modules in rust, i use a relatively simple template, which only requires the wasm-bindgen utility (cargo install -f wasm-bindgen-cli) and the wasm32-unknown-unknown toolchain (rustup target add wasm32-unknown-unknown).

the Cargo.toml of the project looks like this; in it, the crate-type = ["cdylib"] part is the essential thing needed to build a wasm module.

there is a make.sh script, which compiles the project, calls wasm-bindgen, and cobbles together two html files: a light.html which reloads fast (good for development and debugging), but depends on js/wasm.js and js/wasm_bg.wasm, as well as a standalone page.html which doesn't depend on any external files, because it embeds wasm.js verbatim and wasm_bg.wasm encoded in base64 (good for distribution).

after that, it starts a python2 -m SimpleHTTPServer serving the light.html file at http://localhost:8000/light.html (unlike page.html, it unfortunately can't be used with the file:// scheme because of CORS security restrictions).

i also use a script called cw which requires cargo watch (cargo install cargo-watch) and calls make.sh each time the project's source code is modified.

finally, to cobble together the html documents, make.sh uses a head.html and tail.html, which are meant to remain static.

the files from the template can be browsed here or downloaded as a tarball.

posted on 2021-11-20

CC_ -1 License unless otherwise specified on individual pages, all posts on this website are licensed under the CC_-1 license.
unless explicitely mentioned, all content on this site was created by me; not by others nor AI.