This example shows you how to use Rust in the Web with future version of webpack.
-
Install rust
-
Install WebAssembly target
rustup update
rustup component add wasm32-unknown-unknown --toolchain nightly
- Install wasm-gc (works better than
wasm-opt
from binaryen)
cargo install --git https://github.com/alexcrichton/wasm-gc
- Install node modules
npm i
- Install webpack#next
npm un -g webpack
git clone [email protected]:webpack/webpack.git --depth 1 --single-branch --branch next
cd webpack && npm i --production
npm link
- Install web server
npm i -g webpack-dev-server
- Build webassembly
rustc +nightly --target wasm32-unknown-unknown -O imul.rs --crate-type=cdylib
wasm-gc imul.wasm imul.wasm
- Build final bundle
webpack
- Start web server
webpack-dev-server
- Open dev tools / console
$ 2 * 3 = 6
There are a few Chrome-specific issues:
TypeError: Incorrect response MIME type. Expected 'application/wasm'
.
You should explicitly set mime type for .wasm
files. This caused by Webassembly.compileStreaming()
.
RangeError: WebAssembly.Instance is disallowed on the main thread, if the buffer size is larger than 4KB
.
Current implementation in webpack#next used synchronous instantiation.