Created
February 23, 2026 23:38
-
-
Save arctic-hen7/c1f2ada1c24d491da501fa77ed0d039d to your computer and use it in GitHub Desktop.
Optimised Rust Wasm build script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Build script for building Rust libraries to Wasm SDKs. | |
| set -euo pipefail | |
| WASM_OPT_PATH="${WASM_OPT_PATH:-wasm-opt}" | |
| WASM_BINDGEN_PATH="${WASM_BINDGEN_PATH:-wasm-bindgen}" | |
| # Gets the name of the library we're building | |
| lib_name=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.manifest_path | endswith("Cargo.toml")) | .targets[] | select(.kind[] | contains("cdylib")) | .name') | |
| if [[ "$1" == "--release" ]]; then | |
| # MODE: building for production | |
| cargo build --release --target wasm32-unknown-unknown | |
| "$WASM_BINDGEN_PATH" "target/wasm32-unknown-unknown/release/$lib_name.wasm" --out-dir dist/ --target web | |
| "$WASM_OPT_PATH" \ | |
| -Oz \ | |
| --strip-debug \ | |
| --strip-dwarf \ | |
| --strip-target-features \ | |
| --strip-producers \ | |
| "dist/${lib_name}_bg.wasm" \ | |
| -o "dist/${lib_name}_bg.wasm" | |
| echo "Release build complete, artefacts available in 'dist/'." | |
| bundle_size=$(stat -c %s "dist/${lib_name}_bg.wasm" | awk '{printf "%.2f\n", $1/1024}') | |
| echo "Wasm bundle is ${bundle_size}kB." | |
| elif [[ "$1" == "--install" ]]; then | |
| # MODE: installing toolchain | |
| # Install the toolchain in a temporary directory, or where the user told us | |
| if [[ "$2" != "" ]]; then | |
| toolchain_path="$2" | |
| else | |
| toolchain_path=$(mktemp -d) | |
| fi | |
| toolchain_path=$(realpath "$toolchain_path") | |
| mkdir -p "$toolchain_path" | |
| echo "Installing toolchain at '$toolchain_path'..." | |
| if [ -z "${BINARYEN_VERSION-}" ]; then | |
| echo "Warning: 'BINARYEN_VERSION' not set, using 'version_124' as default." | |
| BINARYEN_VERSION="version_124" | |
| fi | |
| # Get the version of `wasm-bindgen` from `Cargo.toml` (we need to match) | |
| wb_version=$(cargo metadata --format-version 1 | jq -r ' | |
| # Find the ID of the root package (your crate) and save it in a variable. | |
| .resolve.root as $root_id | | |
| # Find the package ID for wasm-bindgen as specified in the root crate''s dependencies. | |
| ( | |
| .resolve.nodes[] | |
| | select(.id == $root_id) | |
| | .deps[] | |
| | select(.name == "wasm_bindgen") | |
| | .pkg | |
| ) as $wb_id | | |
| # Now, find the full package details using the ID and print its version. | |
| .packages[] | |
| | select(.id == $wb_id) | |
| | .version | |
| ') | |
| echo "Installing 'wasm-bindgen' version $wb_version..." | |
| # Install `wasm-bindgen-cli` to `$toolchain_path/bin/wasm-bindgen` | |
| cargo install --root "$toolchain_path" wasm-bindgen-cli --version "$wb_version" | |
| mv "$toolchain_path/bin/wasm-bindgen" "$toolchain_path/wasm-bindgen" | |
| rm -rf "$toolchain_path/bin" | |
| # Install `wasm-opt` (more involved) | |
| echo "Installing 'wasm-opt' version '$BINARYEN_VERSION'..." | |
| curl -fsSL -o "$toolchain_path/binaryen.tar.gz" "https://github.com/WebAssembly/binaryen/releases/download/$BINARYEN_VERSION/binaryen-$BINARYEN_VERSION-x86_64-linux.tar.gz" | |
| tar -xzf "$toolchain_path/binaryen.tar.gz" -C "$toolchain_path" | |
| rm "$toolchain_path/binaryen.tar.gz" | |
| mv "$toolchain_path/binaryen-$BINARYEN_VERSION/bin/wasm-opt" "$toolchain_path/wasm-opt" | |
| rm -rf "$toolchain_path/binaryen-$BINARYEN_VERSION" | |
| echo "Toolchain installed in '$toolchain_path'." | |
| else | |
| # MODE: building for development | |
| cargo build --target wasm32-unknown-unknown | |
| "$WASM_BINDGEN_PATH" "target/wasm32-unknown-unknown/release/$lib_name.wasm" --out-dir dist/ --target web | |
| echo "Debug build complete, artefacts available in 'dist/'." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment