Skip to content

Latest commit

 

History

History
74 lines (50 loc) · 2.11 KB

README.md

File metadata and controls

74 lines (50 loc) · 2.11 KB

RCL WebAssembly Module

This directory defines a Rust crate that can be compiled to WebAssembly. It is intended to power an interactive demo on the webpage.

Building with Nix

To build the module and bindings with Nix:

$ nix build .#wasm --out-link result
$ ls -l $(realpath result)
total 292
-r--r--r-- 1 root root 289692 Jan  1  1970 rcl_bg.wasm
-r--r--r-- 1 root root   6526 Jan  1  1970 rcl.js

If you don't want to build with Nix, follow the steps below instead.

Dependencies

The devshell in the Nix flake includes the required wasm-bindgen and wasm-opt from Binaryen. You will also need the wasm32-unknown-unknown target. It is not by default part of rust-toolchain.toml because the non-nightly toolchain is of no use for producing small binaries. Add it manually:

rustup target add wasm32-unknown-unknown

Building

Build a large-ish binary with the regular toolchain:

cargo build \
  --manifest-path wasm/Cargo.toml
  --profile=release-wasm \
  --target=wasm32-unknown-unknown \

Build a much smaller binary, but requires nightly:

cargo +nightly build --lib \
  --manifest-path wasm/Cargo.toml \
  --profile=release-wasm \
  --target=wasm32-unknown-unknown \
  -Z build-std=std,panic_abort \
  -Z build-std-features=panic_immediate_abort

Shrink the binary further with wasm-opt from Binaryen:

wasm-opt -Oz \
  target/wasm32-unknown-unknown/release-wasm/rcl_wasm.wasm \
  --output target/rcl.wasm

Inspect the wasm file to confirm it doesn't contain needless fluff:

wasm-dis target/rcl.wasm

Generate the Javascript bindings:

wasm-bindgen \
  --out-dir target/web \
  --target no-modules \
  --no-typescript \
  target/rcl.wasm

Concatenate the required source files (“bundling”) and minify them if desired:

cat wasm/src/rcl_dom.js target/web/rcl.js | esbuild --minify > target/web/bundle.js
mv target/web/{bundle,rcl}.js

Put everything together:

cp wasm/index.html target/web
python -m http.server --directory target/web