Skip to content

Commit

Permalink
Update WASM to use MVP for transpilation
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Dec 9, 2024
1 parent 05c7966 commit d1fc5ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-wasm-internal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 24 additions & 9 deletions crates/bitwarden-wasm-internal/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions crates/bitwarden-wasm-internal/npm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d1fc5ff

Please sign in to comment.