#!/usr/bin/env bash # Build the clawhdf5-wasm package the viewer loads, into examples/wasm-viewer/pkg/. # # Needs the wasm32-unknown-unknown target and the wasm-bindgen CLI at the # exact version cargo resolves for the wasm-bindgen crate: # rustup target add wasm32-unknown-unknown # cargo install wasm-bindgen-cli --version # # Then serve this directory over HTTP (browsers do not load wasm modules from # file://) and open it: # python3 -m http.server -d examples/wasm-viewer 8000 set -euo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" ROOT="$(cd "$HERE/../.." && pwd)" cd "$ROOT" # The resolved crate version (Cargo.lock is not committed, so ask cargo). want=$(cargo pkgid wasm-bindgen | sed 's/.*[@#]//') if ! command -v wasm-bindgen >/dev/null; then echo "wasm-bindgen CLI not found: cargo install wasm-bindgen-cli --version $want" >&2 exit 1 fi have=$(wasm-bindgen --version | awk '{print $2}') if [ "$want" != "$have" ]; then echo "wasm-bindgen CLI is $have but the crate is $want:" >&2 echo " cargo install wasm-bindgen-cli --version $want" >&2 exit 1 fi cargo build -p clawhdf5-wasm --target wasm32-unknown-unknown --profile wasm-release target_dir=$(cargo metadata --format-version 1 --no-deps \ | sed -n 's/.*"target_directory":"\([^"]*\)".*/\1/p') wasm="$target_dir/wasm32-unknown-unknown/wasm-release/clawhdf5_wasm.wasm" rm -rf "$HERE/pkg" wasm-bindgen --target web --out-dir "$HERE/pkg" "$wasm" echo "built $HERE/pkg ($(wc -c < "$HERE/pkg/clawhdf5_wasm_bg.wasm") bytes of wasm)"