From d1fc5ffa4d33e555d35ab3e6bb8e221a65a99c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garci=CC=81a?= Date: Mon, 9 Dec 2024 14:50:49 +0100 Subject: [PATCH] Update WASM to use MVP for transpilation --- .github/workflows/build-wasm-internal.yml | 2 +- crates/bitwarden-wasm-internal/build.sh | 33 ++++++++++++++----- crates/bitwarden-wasm-internal/npm/.gitignore | 1 + 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build-wasm-internal.yml b/.github/workflows/build-wasm-internal.yml index 1ea78bfe..3e5c4195 100644 --- a/.github/workflows/build-wasm-internal.yml +++ b/.github/workflows/build-wasm-internal.yml @@ -54,7 +54,7 @@ jobs: - name: Install rust uses: dtolnay/rust-toolchain@7b1c307e0dcbda6122208f10795a713336a9b35a # stable with: - toolchain: 1.81.0 + toolchain: stable targets: wasm32-unknown-unknown - name: Cache cargo registry diff --git a/crates/bitwarden-wasm-internal/build.sh b/crates/bitwarden-wasm-internal/build.sh index ffd5a70b..30ffba91 100755 --- a/crates/bitwarden-wasm-internal/build.sh +++ b/crates/bitwarden-wasm-internal/build.sh @@ -3,23 +3,38 @@ cd "$(dirname "$0")" cd ../../ if [ "$1" != "-r" ]; then - # Dev - cargo build -p bitwarden-wasm-internal --target wasm32-unknown-unknown - wasm-bindgen --target bundler --out-dir crates/bitwarden-wasm-internal/npm ./target/wasm32-unknown-unknown/debug/bitwarden_wasm_internal.wasm - wasm-bindgen --target nodejs --out-dir crates/bitwarden-wasm-internal/npm/node ./target/wasm32-unknown-unknown/debug/bitwarden_wasm_internal.wasm + echo "Building in debug mode" + RELEASE_FLAG="" + BUILD_FOLDER="debug" else - # Release - cargo build -p bitwarden-wasm-internal --target wasm32-unknown-unknown --release - wasm-bindgen --target bundler --out-dir crates/bitwarden-wasm-internal/npm ./target/wasm32-unknown-unknown/release/bitwarden_wasm_internal.wasm - wasm-bindgen --target nodejs --out-dir crates/bitwarden-wasm-internal/npm/node ./target/wasm32-unknown-unknown/release/bitwarden_wasm_internal.wasm + echo "Building in release mode" + RELEASE_FLAG="--release" + BUILD_FOLDER="release" fi +# Build normally +cargo build -p bitwarden-wasm-internal --target wasm32-unknown-unknown +wasm-bindgen --target bundler --out-dir crates/bitwarden-wasm-internal/npm ./target/wasm32-unknown-unknown/${BUILD_FOLDER}/bitwarden_wasm_internal.wasm +wasm-bindgen --target nodejs --out-dir crates/bitwarden-wasm-internal/npm/node ./target/wasm32-unknown-unknown/${BUILD_FOLDER}/bitwarden_wasm_internal.wasm + +# Build with MVP CPU target, for wasm2js support +# Note that this requirest build-std which is an unstable feature, +# this normally requires a nightly build, but we can also use the +# RUSTC_BOOTSTRAP hack to use the same stable version as the normal build +RUSTFLAGS=-Ctarget-cpu=mvp RUSTC_BOOTSTRAP=1 cargo build -p bitwarden-wasm-internal -Zbuild-std=panic_abort,std --target wasm32-unknown-unknown +cp ./target/wasm32-unknown-unknown/${BUILD_FOLDER}/bitwarden_wasm_internal.wasm ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.mvp.wasm + # Format npx prettier --write ./crates/bitwarden-wasm-internal/npm # Optimize size wasm-opt -Os ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm -o ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm +wasm-opt -Os ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.wasm -o ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.wasm +wasm-opt -Os ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.mvp.wasm -o ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.mvp.wasm # Transpile to JS -wasm2js ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm -o ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm.js +wasm2js ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.mvp.wasm -o ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm.js npx terser ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm.js -o ./crates/bitwarden-wasm-internal/npm/bitwarden_wasm_internal_bg.wasm.js + +# Remove unneeded files +rm ./crates/bitwarden-wasm-internal/npm/node/bitwarden_wasm_internal_bg.mvp.wasm diff --git a/crates/bitwarden-wasm-internal/npm/.gitignore b/crates/bitwarden-wasm-internal/npm/.gitignore index 99cddcd2..eb1c6ca8 100644 --- a/crates/bitwarden-wasm-internal/npm/.gitignore +++ b/crates/bitwarden-wasm-internal/npm/.gitignore @@ -3,5 +3,6 @@ bitwarden_wasm_internal_bg.js bitwarden_wasm_internal_bg.wasm bitwarden_wasm_internal_bg.wasm.d.ts bitwarden_wasm_internal_bg.wasm.js +bitwarden_wasm_internal_bg.mvp.wasm bitwarden_wasm_internal.d.ts bitwarden_wasm_internal.js