For a detailed write up see the article below by Josh Finnie
https://www.joshfinnie.com/blog/using-webassembly-created-in-rust-for-fast-react-components/
First time set-up for up your local environment step-by-step:
-
Ensure the Rust application can build independently without errors
$ cargo build -
Add a useful taraget for your build
$ rustup target add wasm32-unknown-unknown -
Build the Rust application to the target that was just added
$ cargo build --target wasm32-unknown-unknown --release -
Install the wasm-bindgen-cli command-line application
$ cargo install -f wasm-bindgen-cli -
Now we can take our WebAssembly code generated by Rust and create a wrapping for the React application
$ wasm-bindgen target/wasm32-unknown-unknown/release/nbody_wasm.wasm --out-dir build
To build the Rust, WebAssembly, and JavaScript:
$ npm run build
Hosting a Local Server
$ npm run dev
Scripts Embedded in Package.json
"dev": "webpack server",
"build:wasm": "cargo build --target wasm32-unknown-unknown",
"build:bindgen": "wasm-bindgen target/wasm32-unknown-unknown/debug/nbody_wasm.wasm --out-dir build",
"build": "npm run build:wasm && npm run build:bindgen && npx webpack"
Note: if there is an error building the stdweb crate when building the wasm binary, make sure wasm-pack is installed
$ npm install -g wasm-pack