Use WasmPack instead
Webpack loader for Rust
#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
[lib]
crate-type = ["cdylib"] # important
fn its_magic_number(x: i32) -> bool {
x == 42
}
#[no_mangle]
pub fn plus_one(x: i32) -> i32 {
if its_magic_number(x) { 420 } else { x + 1 }
}
import add from "./add.rs"
import hugeCrate from "./huge_crate/Cargo.toml"
(async () => {
const add = await add.then(buf => WebAssembly.instantiate(buf))
const hugeCrate = await hugeCrate.then(buf => WebAssembly.instantiate(buf))
console.log(add.instance.exports.add(1, 2)) // 3
console.log(hugeCrate.instance.exports.plus_one(6)) // 7
console.log(hugeCrate.instance.exports.plus_one(42)) // 420
})()
- Dont injects WASM to JS bundle, dynamic http fetching
.wasm
files viafile-loader
- Hot module replacement
- Prepare system
- Install nightly Rust:
rustup update nightly
- Install rustc wasm target:
rustup target add wasm32-unknown-unknown --toolchain nightly
- Install wasm-gc:
cargo install --git https://github.com/alexcrichton/wasm-gc --force
- Install nightly Rust:
- Configure Webpack
- Install
rust-loader
andfile-loader
:yarn add rust-loader file-loader --dev
- Use it in Webpack config:
rules: [ { test: /(\.rs$|Cargo.toml)/, loader: "rust-loader" }, { test: /\.wasm$/, loader: "file-loader", type: "javascript/auto" } ]
- Install