#!/usr/bin/env bash # Build the wasm, bind it for the web, optimize it for speed, and report the sizes. # Output: web/pkg/play_nes.js + play_nes_bg.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} # Panic messages carry source paths: name the crates' sources by the cargo home's # place, not the builder's home directory, and this checkout as ".". CARGO_HOME_DIR=${CARGO_HOME:-$HOME/.cargo} export RUSTFLAGS="${RUSTFLAGS:-} --remap-path-prefix=$CARGO_HOME_DIR=/cargo --remap-path-prefix=$ROOT=." $CARGO build --release --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/play_nes.wasm cp web/pkg/play_nes_bg.wasm target/play_nes_bg.unopt.wasm wasm-opt -O3 --enable-bulk-memory --enable-nontrapping-float-to-int --enable-sign-ext \ -o web/pkg/play_nes_bg.wasm target/play_nes_bg.unopt.wasm printf 'wasm size: %d bytes unoptimized, %d bytes after wasm-opt -O3\n' \ "$(stat -c %s target/play_nes_bg.unopt.wasm)" "$(stat -c %s web/pkg/play_nes_bg.wasm)"