There are several options to build Rust into WebAssembly. cargo-web
was the preferred way for yew
, but nowadays yew also supports wasm-bindgen and this opens up some possibilities for javascript interop.
First, you will need to install some tools:
Write some of the dependencies into Cargo.toml, use versions at least as specified here to enable wasm-bindgen support.
[]
= "0.7.0"
= "0.4.17"
= "0.1.6"
= "0.4.7"
= "^0.2"
= "0.2.0"
[]
= ["cdylib", "rlib"] # IMPORTANT!
Add some helper lines to yarn config:
"scripts": ,
yarn start
will perform a hot-reloadable debug build of your code. It takes some time on first run to build rust code, so be patient. yarn build
could be used to bundle a package.
Your index.html should load a JS wrapper that runs webassembly script:
And the said wrapper is in js/index.js:
;
;
I have my cdylib library for the web assembly code in the workspace under lib
that's why the path looks like this. Adjust to your configuration. Remember to build a library crate, not a binary!
NOTE: To build something useful with yew you can start from this excellent tutorial in 3 parts: Hunt the Wumpus, Part 2, Part 3.
Now just start it:
Let's go!