44
55PROJECT_NAME=" base64_simd_wasm_vn"
66PKG_DIR=" ./pkg"
7- WASM_TARGET=" target/wasm32-unknown-unknown/release/${PROJECT_NAME} .wasm"
8- FLAGS=" -C target-feature=+simd128"
7+ TARGET_BUILD=" wasm32-unknown-unknown"
8+ WASM_TARGET=" target/${TARGET_BUILD} /release/${PROJECT_NAME} .wasm"
9+ FLAGS=" -C target-feature=+simd128,+bulk-memory"
910
10- # RUSTFLAGS='-C target-feature=+simd128' ./build.sh
1111echo " 🔨 Building ${PROJECT_NAME} ..."
1212
1313# Check if wasm-bindgen is installed
1919# Clean and create directories
2020echo " 📁 Setting up directories..."
2121rm -rf " ${PKG_DIR} "
22- mkdir -p " ${PKG_DIR} " /{deno,nodejs,bundler,web}
2322
2423# Build Rust to WASM
2524echo " 🦀 Compiling Rust to WASM..."
26- RUSTFLAGS=" $FLAGS " cargo build --target wasm32-unknown-unknown --release
25+ RUSTFLAGS=" $FLAGS " cargo build --target ${TARGET_BUILD} --release
2726
2827# Check if WASM file was created
2928if [ ! -f " ${WASM_TARGET} " ]; then
3534# Generate bindings for different targets
3635echo " 📦 Generating bindings for different targets..."
3736
38- echo " 🌐 Web target..."
39- wasm-bindgen --target web --out-dir " ${PKG_DIR} /web " " ${WASM_TARGET} "
37+ echo " 📦 Bundler target..."
38+ wasm-bindgen --target bundler --out-dir " ${PKG_DIR} /bundler " " ${WASM_TARGET} "
4039
4140echo " 🦕 Deno target..."
4241wasm-bindgen --target deno --out-dir " ${PKG_DIR} /deno" " ${WASM_TARGET} "
4342
4443echo " 📟 Node.js target..."
4544wasm-bindgen --target nodejs --out-dir " ${PKG_DIR} /nodejs" " ${WASM_TARGET} "
4645
47- echo " 📦 Bundler target..."
48- wasm-bindgen --target bundler --out-dir " ${PKG_DIR} /bundler" " ${WASM_TARGET} "
46+ echo " 📟 Node.js (ESM) target..."
47+ wasm-bindgen --target experimental-nodejs-module --out-dir " ${PKG_DIR} /esm" " ${WASM_TARGET} "
48+
49+ echo " 📦 Module target..."
50+ wasm-bindgen --target module --out-dir " ${PKG_DIR} /module" " ${WASM_TARGET} "
51+
52+ echo " 📦 No-modules target..."
53+ wasm-bindgen --target no-modules --out-dir " ${PKG_DIR} /no-modules" " ${WASM_TARGET} "
54+
55+ echo " 🌐 Web target..."
56+ wasm-bindgen --target web --out-dir " ${PKG_DIR} /web" " ${WASM_TARGET} "
4957
5058# Copy package files
5159echo " 📄 Copying package files..."
@@ -59,11 +67,17 @@ echo "*" > "${PKG_DIR}/.gitignore"
5967# Optimize WASM
6068echo " ⚡ Optimizing WASM..."
6169
70+ WASM_SUFFIX=" _bg.wasm"
71+ WASM_OTPS=" -Os --converge --enable-bulk-memory --inline-functions-with-loops"
72+
6273if command -v wasm-opt & > /dev/null; then
63- wasm-opt -Oz --enable-bulk-memory " ${PKG_DIR} /web/${PROJECT_NAME} _bg.wasm" -o " ${PKG_DIR} /web/${PROJECT_NAME} _bg.wasm"
64- wasm-opt -Oz --enable-bulk-memory " ${PKG_DIR} /deno/${PROJECT_NAME} _bg.wasm" -o " ${PKG_DIR} /deno/${PROJECT_NAME} _bg.wasm"
65- wasm-opt -Oz --enable-bulk-memory " ${PKG_DIR} /nodejs/${PROJECT_NAME} _bg.wasm" -o " ${PKG_DIR} /nodejs/${PROJECT_NAME} _bg.wasm"
66- wasm-opt -Oz --enable-bulk-memory " ${PKG_DIR} /bundler/${PROJECT_NAME} _bg.wasm" -o " ${PKG_DIR} /bundler/${PROJECT_NAME} _bg.wasm"
74+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /bundler/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /bundler/${PROJECT_NAME}${WASM_SUFFIX} "
75+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /deno/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /deno/${PROJECT_NAME}${WASM_SUFFIX} "
76+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /esm/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /esm/${PROJECT_NAME}${WASM_SUFFIX} "
77+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /module/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /module/${PROJECT_NAME}${WASM_SUFFIX} "
78+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /no-modules/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /no-modules/${PROJECT_NAME}${WASM_SUFFIX} "
79+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /nodejs/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /nodejs/${PROJECT_NAME}${WASM_SUFFIX} "
80+ wasm-opt ${WASM_OTPS} " ${PKG_DIR} /web/${PROJECT_NAME}${WASM_SUFFIX} " -o " ${PKG_DIR} /web/${PROJECT_NAME}${WASM_SUFFIX} "
6781 echo " ✅ WASM optimized with bulk memory"
6882else
6983 echo " ⚠️ wasm-opt not found, skipping optimization"
7286
7387# Verify outputs
7488echo " 🔍 Verifying outputs..."
75- for target in " /web " " /deno" " /nodejs" " /bundler " ; do
76- if [ -f " ${PKG_DIR}${target} /${PROJECT_NAME} _bg.wasm " ]; then
77- wasm_size=$( stat -f%z " ${PKG_DIR}${target} /${PROJECT_NAME} _bg.wasm " 2> /dev/null || stat -c%s " ${PKG_DIR}${target} /${PROJECT_NAME} _bg.wasm " )
78- echo " ✓${target:- / web } : ${wasm_size} bytes"
89+ for target in " /bundler " " /deno" " /esm " " /module " " /no-modules " " / nodejs" " /web " ; do
90+ if [ -f " ${PKG_DIR}${target} /${PROJECT_NAME}${WASM_SUFFIX} " ]; then
91+ wasm_size=$( stat -f%z " ${PKG_DIR}${target} /${PROJECT_NAME}${WASM_SUFFIX} " 2> /dev/null || stat -c%s " ${PKG_DIR}${target} /${PROJECT_NAME}${WASM_SUFFIX} " )
92+ echo " ✓${target} : ${wasm_size} bytes"
7993 else
8094 echo " ❌${target} : WASM file missing"
8195 fi
0 commit comments