GitHub - adamisntdead/wasm-starter: WebAssembly Module Starter Code, Powered by Rust, wasm-pack, wasm-bindgen and Webpack 🔥😍

1 min read Original article ↗

WebAssembly Starter

WebAssembly Module Starter Code, Powered by Rust, wasm-pack, wasm-bindgen and Webpack 🔥 😍

If you want an explanation of all of the code in this (rather minimal) boilerplate, check out the Explanation

Setup

First you are gonna have to get Rust at rust-lang.org. Then, you will need to have nodejs installed.

For the WebAssembly side of things, you are going to have to get to Rust nightly, and install the WebAssembly compiler.

rustup default nightly
rustup target add wasm32-unknown-unknown --toolchain nightly

Then, you are going to need wasm-pack

Finally, you can compile the WebAssembly module.

# Run wasm-pack
wasm-pack init .

# Install the example files
cd example
npm install
npm run serve

Note: It can take about 3 minutes just to do that initial wasm-pack so be ready! You can then open the page at localhost:8080

Usage

The file wasm_starter.js exposes a single method hello_world(), which just displays a hello world input

If you setup Webpack 4, it supports webassembly out of the box, and the module can be used like so:

const js = import('../pkg/wasm_starter.js')

js.then(js => {
  js.hello_world()
})

Which displays:

Hello World!

License

MIT