#!/usr/bin/env bash set -e HELP_STRING=$(cat <<- END usage: build_wasm.sh Build script for combining a Macroquad project with wasm-bindgen, allowing integration with the greater wasm-ecosystem. example: ./build_wasm.sh This'll go through the following steps: 1. Build as target 'wasm32-unknown-unknown'. 2. Create the directory 'dist' if it doesn't already exist. 3. Run wasm-bindgen with output into the 'dist' directory. 4. Apply patches to the output js file (detailed here: https://github.com/not-fl3/macroquad/issues/212#issuecomment-835276147). 5. Generate coresponding 'index.html' file. Author: Tom Solberg Edit: Nik codes Edit: Nobbele Version: 0.2 END ) die () { echo >&2 "Error: $@" echo >&2 echo >&2 "$HELP_STRING" exit 1 } # Parse primary commands while [[ $# -gt 0 ]] do key="$1" case $key in --release) RELEASE=yes shift ;; -h|--help) echo "$HELP_STRING" exit 0 ;; *) POSITIONAL+=("$1") shift ;; esac done # Restore positionals set -- "${POSITIONAL[@]}" PROJECT_NAME="hello-world" HTML=$(cat <<- END ${PROJECT_NAME}

Game can't play audio unless a button has been clicked.

END ) # Build example to web cargo build --example hello-world --target wasm32-unknown-unknown # Generate bindgen outputs mkdir -p dist wasm-bindgen target/wasm32-unknown-unknown/debug/examples/$PROJECT_NAME.wasm --out-dir dist --target web --no-typescript # Shim to tie the thing together sed -i "s/import \* as __wbg_star0 from 'env';//" dist/$PROJECT_NAME.js sed -i "s/let wasm;/let wasm; export const set_wasm = (w) => wasm = w;/" dist/$PROJECT_NAME.js sed -i "s/imports\['env'\] = __wbg_star0;/return imports.wbg\;/" dist/$PROJECT_NAME.js sed -i "s/const imports = __wbg_get_imports();/return __wbg_get_imports();/" dist/$PROJECT_NAME.js # Create index from the HTML variable echo "$HTML" > dist/index.html