-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·38 lines (27 loc) · 1.27 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
set -e # Stop on error
# Build the WASM module
echo "Building WASM module..."
cargo build --target wasm32-unknown-unknown --release
# Strip the WASM file to reduce its size
echo "Stripping WASM file..."
wasm-strip target/wasm32-unknown-unknown/release/blockwars.wasm
# # Optimize the WASM file with wasm-opt
echo "Optimizing WASM file..."
wasm-opt -o target/wasm32-unknown-unknown/release/blockwars.wasm -Oz target/wasm32-unknown-unknown/release/blockwars.wasm
# Generate the base64-encoded WASM string
echo "Generating base64-encoded string..."
BASE64_WASM=$(base64 -i target/wasm32-unknown-unknown/release/blockwars.wasm)
# Path to your index.html
INDEX_HTML="docs/index.html"
# Update the index.html with the new base64 string
awk -v base64="$BASE64_WASM" '
BEGIN {printme=1}
/\/\/ Base64WasmStart/ {print;printme=0;print " const base64Wasm = \x27" base64 "\x27;";next}
/\/\/ Base64WasmEnd/ {printme=1}
printme {print}
' "$INDEX_HTML" > "$INDEX_HTML".tmp && mv "$INDEX_HTML".tmp "$INDEX_HTML"
echo "Build complete! 🎉 ${BASE64_WASM: -10}"
# Output the file size
ls -lh target/wasm32-unknown-unknown/release/blockwars.wasm | awk '{print "\033[0;95mwasm size: " $5 "\033[0m"}'
ls -lh docs/index.html | awk '{print "\033[0;91mindex.html size: " $5 "\033[0m"}'