@@ -27,23 +27,32 @@ mv ./{{dir}}/Cargo-local.toml ./{{dir}}/Cargo.toml
2727cargo build --manifest-path ./{ {dir} }/Cargo.toml \
2828 --target wasm32-unknown-unknown --release
2929
30- # Make the build directory
30+ # Make the build & build-staging directory
3131rm -rf ./build
3232mkdir ./build
33+ rm -rf ./build-staging
34+ mkdir ./build-staging
3335
34- # Run wasm-bindgen over the module, replacing all placeholder __wbindgen_... imports
35- wasm-bindgen ./{ {dir} }/target/wasm32-unknown-unknown/release/module.wasm --out-dir ./build --out-name bg_module .wasm
36+ # Move the rust module into the staging directory
37+ mv ./{ {dir} }/target/wasm32-unknown-unknown/release/module.wasm ./build-staging/module .wasm
3638
37- # Run wasm-tools strip to remove the wasm-interface-types custom section
38- wasm-tools strip ./build/bg_module.wasm -d wasm-interface-types -o ./build/strip_module.wasm
39- rm -rf ./build/bg_module.wasm
39+ # Wasm Post-processing
4040
41- # Run wasm-snip to trip down the size of the binary, removing any dead code
42- wasm-snip ./build/strip_module.wasm -o ./build/snipped_module.wasm
43- rm -rf ./build/strip_module.wasm
41+ # 1. Run `wasm-bindgen` over the module, replacing all placeholder __wbindgen_... imports
42+ wasm-bindgen ./build-staging/module.wasm --out-dir ./build-staging --out-name module_bg.wasm
4443
45- # Use wasm-opt to perform the "asyncify" post-processing step over all modules
44+ # 2. If wasm-bindgen isn't being used, the only import that will be remaining will be __wbindgen_throw.
45+ # So, let's remove this, and if more extraneous imports exist an error will be raised post-compilation
46+ wasm-snip ./build-staging/module_bg.wasm -o ./build-staging/module_bg_snip.wasm -p .*__wbindgen_throw
47+
48+ # 3. Run `wasm-tools strip` to remove the wasm-interface-types custom section (sometimes get injected)
49+ wasm-tools strip ./build-staging/module_bg_snip.wasm -d wasm-interface-types -o ./build-staging/module_bg_snip_strip.wasm
50+
51+ # 4. Use wasm-opt to perform the "asyncify" post-processing step over all modules
4652export ASYNCIFY_STACK_SIZE=24576
47- wasm-opt --asyncify -Os ./build/snipped_module.wasm -o ./build/wrap.wasm
48- rm -rf ./build/snipped_module.wasm
53+ wasm-opt --asyncify -Os ./build-staging/module_bg_snip_strip.wasm -o ./build-staging/module_bg_snip_strip_opt.wasm
54+
55+ # Finish - Move the result
56+ mv ./build-staging/module_bg_snip_strip_opt.wasm ./build/wrap.wasm
57+
4958{ {/polywrap_module} }
0 commit comments