adamisntdead / wasm-starter

WebAssembly Module Starter Code, Powered by Rust, wasm-pack, wasm-bindgen and Webpack πŸ”₯😍

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

cargo install 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

About

WebAssembly Module Starter Code, Powered by Rust, wasm-pack, wasm-bindgen and Webpack πŸ”₯😍


Languages

Language:HTML 39.4%Language:JavaScript 36.1%Language:Rust 24.5%