#!/usr/bin/env bash # Builds crates/didbot-site-anim for the browser and drops the result where # Astro's public/ passthrough picks it up untouched: site/public/wasm/. Astro # copies public/ verbatim into dist/, so nothing else has to know this ran. # # This is its own script, called from scripts/build-site.sh, because it is # the one step in that pipeline needing a toolchain the other two # (`cargo doc`, `npm run build`) do not: the wasm32-unknown-unknown target # and a wasm-bindgen-cli whose version matches the wasm-bindgen crate # dependency exactly (the two talk a private ABI to each other; a mismatch # fails loudly, which is the whole reason the version is pinned rather than # "whatever is installed"). # # `cargo build --workspace` and `cargo test --workspace` never need this: the # wasm32 target is only touched here, on purpose (see # crates/didbot-site-anim/Cargo.toml's own comment on why the crate builds # fine natively without it). This script is the opt-in. set -euo pipefail cd "$(dirname "$0")/.." crate_dir="crates/didbot-site-anim" out_dir="site/public/wasm" locked_version="$( awk ' /^name = "wasm-bindgen"$/ { want = 1; next } want && /^version = / { gsub(/"/, "", $3); print $3; exit } ' Cargo.lock )" if [ -z "$locked_version" ]; then echo "build-wasm: could not find wasm-bindgen's locked version in Cargo.lock" >&2 exit 1 fi if ! rustup target list --installed 2>/dev/null | grep -qx wasm32-unknown-unknown; then echo "build-wasm: the wasm32-unknown-unknown target is not installed." >&2 echo " Install it with: rustup target add wasm32-unknown-unknown" >&2 exit 1 fi if ! command -v wasm-bindgen >/dev/null 2>&1; then echo "build-wasm: wasm-bindgen-cli is not installed." >&2 echo " Install the version matching Cargo.lock ($locked_version) with:" >&2 echo " cargo install wasm-bindgen-cli --version $locked_version --locked" >&2 exit 1 fi installed_version="$(wasm-bindgen --version | awk '{print $2}')" if [ "$installed_version" != "$locked_version" ]; then echo "build-wasm: wasm-bindgen-cli $installed_version does not match Cargo.lock's wasm-bindgen $locked_version." >&2 echo " wasm-bindgen and wasm-bindgen-cli speak a private ABI and must match exactly. Reinstall with:" >&2 echo " cargo install wasm-bindgen-cli --version $locked_version --locked" >&2 exit 1 fi echo "=== cargo build: $crate_dir for wasm32-unknown-unknown ===" >&2 cargo build -p didbot-site-anim --target wasm32-unknown-unknown --release wasm_in="target/wasm32-unknown-unknown/release/didbot_site_anim.wasm" if [ ! -f "$wasm_in" ]; then echo "build-wasm: expected build output at $wasm_in, found nothing" >&2 exit 1 fi echo "=== wasm-bindgen: generating the JS glue in $out_dir ===" >&2 rm -rf "$out_dir" mkdir -p "$out_dir" wasm-bindgen --target web --out-dir "$out_dir" --out-name site-anim "$wasm_in" if command -v wasm-opt >/dev/null 2>&1; then echo "=== wasm-opt: shrinking the release binary ===" >&2 wasm-opt -Oz -o "$out_dir/site-anim_bg.wasm.opt" "$out_dir/site-anim_bg.wasm" mv "$out_dir/site-anim_bg.wasm.opt" "$out_dir/site-anim_bg.wasm" else echo "build-wasm: wasm-opt not found on PATH; shipping the unoptimized binary." >&2 echo " Install binaryen for a smaller artifact: https://github.com/WebAssembly/binaryen" >&2 fi size_bytes="$(wc -c <"$out_dir/site-anim_bg.wasm")" echo "build-wasm: done — $out_dir/site-anim_bg.wasm is ${size_bytes} bytes" >&2