Previous to this lesson, we set up our own Rust/wasm project from scratch.
The Rust/wasm team ships a template for you to get started right away. In this lesson, you'll see how to initialize the template (assumes you have wasm-pack
installed) and see how this template gives you additional custom Rust features in addition to the WasmPackPlugin
that will build your library on every file change.
Instructor: [00:00] While we set up our own webpack project, for the sake of going through it step-by-step, the Rust WebAssembly team also ships a template. We start by creating a new directory, myApp. Inside it, we run npm init rust -webpack.
[00:18] This will copy the template into the current directory. Once that's done, we can run npm install to install our JavaScript dependencies. Be aware that this template relies on you having wasm-pack installed. In case you haven't done this, just run cargo install wasm-pack.
[00:37] Now, we've got everything set up, and start using npm start. If you visit localhost:8080, we can see hello from Rust, WebAssembly, and webpack.
[00:49] How is this different from our webpack setup? For once, in our Cargo.toml, this setup defines custom Rust features, console.error, panic hook, and we add log. In our entry file, these features also need to be configured.
[01:09] In addition, this setup ships with the wasm-pack webpack plugin that builds our library with wasm-pack on every file change. Let's change the greeting to verify that this works. As expected, you can see the updated output.
[01:28] In general, I recommend you do start off with this template, since it will always contain the recommended structure and features by the Rust WASM team.
same here, Attilla
Alright, issue solved.
I suppose this is a risk of nightly Rust. In the Github issue they said:
rustup update nightly
cargo update
# cd into the `crate/` directory within the project
wasm-pack build
Then cd'ing back into the project directory and doing npm start
now works :)
Fixed it! Thank you!!
npm init rust-webpack
does nothing for me but initiate an "empty" project. I have wasm-pack
installed, not really sure what I'm doing wrong.
Of course, a silly mistake by me, the installed npm version did not support init templates. Had to update npm.
Glad you figured it out @cbrevik
For anyone else getting an error on npm init rust-webpack
, it may be this issue: https://github.com/rustwasm/rust-webpack-template/issues/148. I had to upgrade git
, then it worked just fine.
The new version "@wasm-tool/wasm-pack-plugin": "^0.4.2"
generate a different project structure with different code. This course needs either an update or a pointer to the fixed version of the template generator. Otherwise it's not possible to follow.
When running
npm start
, I unfortunately get this:I've created a Github issue for the rust webpack template in case the problem is on their end: https://github.com/rustwasm/rust-webpack-template/issues/57