1- FROM rustlang/ rust:nightly-slim as base
1+ FROM rust:1.60.0 as base
22
33# Install the wasm32 rust build target
44RUN rustup target add wasm32-unknown-unknown
@@ -7,7 +7,7 @@ WORKDIR /build-deps
77
88# Install curl
99RUN apt-get update
10- RUN apt-get -y install curl
10+ RUN apt-get -y install curl clang llvm build-essential
1111
1212# Install wasm-opt
1313RUN curl -L https://github.com/WebAssembly/binaryen/releases/download/version_101/binaryen-version_101-x86_64-linux.tar.gz | tar -xz \
@@ -16,10 +16,13 @@ RUN curl -L https://github.com/WebAssembly/binaryen/releases/download/version_10
1616 && rm -rf binary-version_101
1717
1818# Install the toml-cli
19- RUN cargo install toml-cli
19+ RUN cargo install -f toml-cli
2020
2121# Install wasm-snip
22- RUN cargo install wasm-snip
22+ RUN cargo install -f wasm-snip
23+
24+ # Install wasm-bindgen
25+ RUN cargo install -f wasm-bindgen-cli
2326
2427{ {#polywrap_linked_packages.length} }
2528WORKDIR /linked-packages
@@ -55,38 +58,50 @@ RUN PACKAGE_NAME={{name}}; \
5558 { {/polywrap_module} }
5659 true
5760{ {/polywrap_linked_packages} }
61+
5862{ {/polywrap_linked_packages.length} }
5963
60- # Remove any Cargo.lock files
6164{ {#polywrap_module} }
65+ # Remove any Cargo.lock files
6266RUN rm -rf { {dir} }/Cargo.lock
6367
6468# Ensure the Wasm module is configured to use imported memory
6569ENV RUSTFLAGS="-C link-arg=-z -C link-arg=stack-size=65536 -C link-arg=--import-memory"
6670
67- # Cleanup an artifact left by the toml CLI program ("[]" -> [])
68- RUN sed -i 's/"\[\]"/\[\]/g' ./{ {dir} }/Cargo.toml
69-
7071# Ensure the module at { {dir} } has the crate-type = ["cdylib"]
71- RUN toml set ./{ {dir} }/Cargo.toml lib.crate-type ["cdylib","rlib" ] > ./{ {dir} }/Cargo-local.toml && \
72+ RUN toml set ./{ {dir} }/Cargo.toml lib.crate-type ["cdylib"] > ./{ {dir} }/Cargo-local.toml && \
7273 rm -rf ./{ {dir} }/Cargo.toml && \
7374 mv ./{ {dir} }/Cargo-local.toml ./{ {dir} }/Cargo.toml && \
7475 true
7576
76- # Clean up artifacts left by the toml CLI program ("["cdylib", "rlib" ]" -> ["cdylib", "rlib "])
77- RUN sed -i 's/"\[cdylib,rlib \]"/\["cdylib","rlib "\]/g' ./{ {dir} }/Cargo.toml
77+ # Clean up artifacts left by the toml CLI program ("["cdylib"]" -> ["cdylib"])
78+ RUN sed -i 's/"\[cdylib\]"/\["cdylib"\]/g' ./{ {dir} }/Cargo.toml
7879
79- # Build the module at { {dir} }
80- RUN cargo +nightly build --manifest-path ./{ {dir} }/Cargo.toml \
81- --target wasm32-unknown-unknown --release
80+ # Ensure the package name = "module"
81+ RUN toml set ./{ {dir} }/Cargo.toml package.name "module" > ./{ {dir} }/Cargo-local.toml && \
82+ rm -rf ./{ {dir} }/Cargo.toml && \
83+ mv ./{ {dir} }/Cargo-local.toml ./{ {dir} }/Cargo.toml && \
84+ true
8285
8386# Make the build directory
8487RUN rm -rf ./build
8588RUN mkdir ./build
8689
90+ # Build the module at { {dir} }
91+ RUN cargo build --manifest-path ./{ {dir} }/Cargo.toml \
92+ --target wasm32-unknown-unknown --release
93+
94+ # Enable the "WASM_INTERFACE_TYPES" feature, which will remove the __wbindgen_throw import.
95+ # See: https://github.com/rustwasm/wasm-bindgen/blob/7f4663b70bd492278bf0e7bba4eeddb3d840c868/crates/cli-support/src/lib.rs#L397-L403
96+ ENV WASM_INTERFACE_TYPES=1
97+
98+ # Run wasm-bindgen over the module, replacing all placeholder __wbindgen_... imports
99+ RUN wasm-bindgen ./{ {dir} }/target/wasm32-unknown-unknown/release/module.wasm --out-dir ./build --out-name bg_module.wasm
100+
101+ RUN wasm-snip ./build/bg_module.wasm -o ./build/snipped_module.wasm && \
102+ rm -rf ./build/bg_module.wasm
103+
87104# Use wasm-opt to perform the "asyncify" post-processing step over all modules
88- RUN WASM_MODULE=$(ls ./{ {dir} }/target/wasm32-unknown-unknown/release/*.wasm); \
89- wasm-snip $WASM_MODULE -o ./build/snipped_{ {name} }.wasm && \
90- wasm-opt --asyncify -Os ./build/snipped_{ {name} }.wasm -o ./build/{ {name} }.wasm && \
91- rm -rf ./build/snipped_{ {name} }.wasm
105+ RUN wasm-opt --asyncify -Os ./build/snipped_module.wasm -o ./build/module.wasm && \
106+ rm -rf ./build/snipped_module.wasm
92107{ {/polywrap_module} }
0 commit comments