rcl/wasm
Ruud van Asseldonk 59be133128 Bump version to 0.8.0
I'm leaving the Zed extension pointing to the older commit of the
Tree-sitter grammar, I'll update that after this version bump. It's
a bit awkward to do it this way around, but there are circular
dependencies that can't be avoided. Maybe with an attack on SHA1 it
can be done in theory, but let's not go there.
2025-03-02 21:15:33 +01:00
..
src Ensure that we always have a span to insert into 2024-06-23 11:05:08 +02:00
Cargo.rcl Generate Cargo.toml files using RCL 2024-12-08 15:42:28 +01:00
Cargo.toml Bump version to 0.8.0 2025-03-02 21:15:33 +01:00
index.html Implement wasm highlight using built-in lexer 2024-06-23 11:05:08 +02:00
README.md Use the new wasm output in the demo page 2024-03-17 22:04:40 +01:00

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