This directory contains examples of compiling Rust code to WebAssembly and using it on the web and in a Node.js application. The accompanying documentation can be found in the Compiling from Rust to WebAssembly article on MDN.
This document assumes you're following the instructions in the above linked MDN article, which covers the prerequisites and the installation of the tools required to build the example.
If you've already installed Rust, wasm-pack
, and Node.js and wish to skip the setup steps, the next two sections provide instructions on how to run pre-compiled examples on the web and via Node.js, focusing on the differences between the two approaches.
To run the example in your browser, you can open the index.html
file:
# Target the web
wasm-pack build --target web
# Serve the page
python3 -m http.server
# or if http-server is installed via npm
http-server ./
You should see an alert box that says "Hello, WebAssembly!".
To run the Node.js example, you will need to build the package targeting Bundlers, install the Node.js dev dependencies, and then serve the project:
# Target bundlers (webpack in our case)
wasm-pack build --target bundler
cd site
# Install the dev dependencies and run the server
npm i && npm run serve
You should see an alert box that says "Hello, WebAssembly with npm!".