#!/usr/bin/env bash # Build the wasm, bind it for the web, optimize it, and report the sizes. # Output: web/pkg/emu_galaga_web.js + emu_galaga_web_bg.wasm (optimized in place; the # unoptimized module is kept beside it as emu_galaga_web_bg.unopt.wasm). # Usage: scripts/build-wasm.sh (needs wasm-bindgen 0.2.128 and wasm-opt on PATH) set -euo pipefail ROOT=$(cd "$(dirname "$0")/.." && pwd) cd "$ROOT" export PATH="$HOME/.cargo/bin:$HOME/.local/binaryen/bin:$PATH" # Use the rust-toolchain.toml toolchain's own cargo and rustc directly: a # snap-installed rustup's `rustup run` leaves the system rustc first on PATH, # and that rustc has no wasm32 target. TOOLCHAIN=${TOOLCHAIN:-"$HOME/.rustup/toolchains/1.93.1-x86_64-unknown-linux-gnu"} if [ -z "${CARGO:-}" ] && [ -x "$TOOLCHAIN/bin/cargo" ]; then CARGO="$TOOLCHAIN/bin/cargo" export RUSTC="$TOOLCHAIN/bin/rustc" fi CARGO=${CARGO:-cargo} $CARGO build --release -p emu-galaga-wasm --target wasm32-unknown-unknown -q wasm-bindgen --version | grep -q 0.2.128 || { echo "wasm-bindgen CLI must be 0.2.128 (matches Cargo.toml)"; exit 1; } wasm-bindgen --target web --out-dir web/pkg --no-typescript \ target/wasm32-unknown-unknown/release/emu_galaga_web.wasm cp web/pkg/emu_galaga_web_bg.wasm web/pkg/emu_galaga_web_bg.unopt.wasm wasm-opt -Oz --enable-bulk-memory --enable-nontrapping-float-to-int \ -o web/pkg/emu_galaga_web_bg.wasm web/pkg/emu_galaga_web_bg.unopt.wasm printf 'wasm size: %d bytes unoptimized, %d bytes after wasm-opt -Oz\n' \ "$(stat -c %s web/pkg/emu_galaga_web_bg.unopt.wasm)" "$(stat -c %s web/pkg/emu_galaga_web_bg.wasm)"