{ cmd }: rec { # Build commands (dev mode) build = { wasm-pack, path ? "." }: { "wasm:build:web" = cmd "Build Wasm for web target" '' ${wasm-pack} build ${path} --dev --target=web "$@" ''; "wasm:build:nodejs" = cmd "Build Wasm for Node.js target" '' ${wasm-pack} build ${path} --dev --target=nodejs "$@" ''; "wasm:build:bundler" = cmd "Build Wasm for bundler target" '' ${wasm-pack} build ${path} --dev --target=bundler "$@" ''; }; # Release commands release = { wasm-pack, path ? ".", gzip ? null }: let compress = if gzip != null then " && ${gzip} -f ${path}/pkg/*_bg.wasm" else ""; in { "wasm:release:web" = cmd "Build Wasm release for web target" '' ${wasm-pack} build ${path} --release --target=web "$@"${compress} ''; "wasm:release:nodejs" = cmd "Build Wasm release for Node.js target" '' ${wasm-pack} build ${path} --release --target=nodejs "$@"${compress} ''; "wasm:release:bundler" = cmd "Build Wasm release for bundler target" '' ${wasm-pack} build ${path} --release --target=bundler "$@"${compress} ''; }; # Test commands test = { wasm-pack, path ? ".", features ? null }: let featuresFlag = if features != null then " --features='${features}'" else ""; in { "wasm:test:node" = cmd "Run wasm-pack tests in Node.js" '' ${wasm-pack} test --node ${path}${featuresFlag} "$@" ''; "wasm:test:chrome" = cmd "Run wasm-pack tests in headless Chrome" '' ${wasm-pack} test --headless --chrome ${path}${featuresFlag} "$@" ''; "wasm:test:firefox" = cmd "Run wasm-pack tests in headless Firefox" '' ${wasm-pack} test --headless --firefox ${path}${featuresFlag} "$@" ''; "wasm:test:safari" = cmd "Run wasm-pack tests in headless Safari" '' ${wasm-pack} test --headless --safari ${path}${featuresFlag} "$@" ''; }; # Documentation commands doc = { cargo, xdg-open ? null }: { "wasm:doc" = cmd "Build docs for wasm32-unknown-unknown" '' ${cargo} doc --target=wasm32-unknown-unknown "$@" ''; } // (if xdg-open != null then { "wasm:doc:open" = cmd "Build and open Wasm docs" '' ${cargo} doc --target=wasm32-unknown-unknown --open "$@" ''; } else {}); # Watch commands watch = { cargo-watch }: { "wasm:watch:build" = cmd "Rebuild Wasm on changes" '' ${cargo-watch} --clear -- cargo build --target=wasm32-unknown-unknown ''; "wasm:watch:test" = cmd "Run Wasm tests on changes" '' ${cargo-watch} --clear -x "test --target=wasm32-unknown-unknown" ''; }; # CI workflow ci = { wasm-pack, path ? ".", features ? null }: let featuresFlag = if features != null then " --features='${features}'" else ""; in { "wasm:ci" = cmd "Run Wasm CI suite (test node + chrome)" '' set -e echo "Running wasm-pack tests in Node.js..." ${wasm-pack} test --node ${path}${featuresFlag} echo "Running wasm-pack tests in Chrome..." ${wasm-pack} test --headless --chrome ${path}${featuresFlag} echo "Wasm CI passed!" ''; }; # Convenience: all wasm commands all = { wasm-pack, cargo, path ? ".", gzip ? null, features ? null, cargo-watch ? null, xdg-open ? null }: build { inherit wasm-pack path; } // release { inherit wasm-pack path gzip; } // test { inherit wasm-pack path features; } // doc { inherit cargo xdg-open; } // (if cargo-watch != null then watch { inherit cargo-watch; } else {}) // ci { inherit wasm-pack path features; }; }