Skip to content

Commit

Permalink
feat: Add Rust, web, Node.js example code (#33)
Browse files Browse the repository at this point in the history
Checking in the finished examples to accompany
https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_Wasm#rust_environment_setup

- build for the web
- target Bundlers (webpack in our case)

---------

Co-authored-by: Dipika Bhattacharya <[email protected]>
  • Loading branch information
bsmth and dipikabh authored Jan 12, 2024
1 parent 5a2dd7c commit 8804d07
Show file tree
Hide file tree
Showing 11 changed files with 4,024 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
target
dist
.DS_Store
123 changes: 123 additions & 0 deletions rust-js/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions rust-js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "hello-wasm"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
description = "A sample project with wasm-pack"
license = "MIT/Apache-2.0"
repository = "https://github.com/yourgithubusername/hello-wasm"
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
39 changes: 39 additions & 0 deletions rust-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Rust to WebAssembly in web and Node.js

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](https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_Wasm) article on MDN.

## Running the examples

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.

### Running a compiled example on the web

To run the example in your browser, you can open the `index.html` file:

```bash
# 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!".

### Building and running an npm package

To run the Node.js example, you will need to build the package targeting [Bundlers](https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#bundlers), install the Node.js dev dependencies, and then serve the project:

```bash
# 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!".
15 changes: 15 additions & 0 deletions rust-js/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>hello-wasm example</title>
</head>
<body>
<script type="module">
import init, { greet } from "./pkg/hello_wasm.js";
init().then(() => {
greet("WebAssembly");
});
</script>
</body>
</html>
10 changes: 10 additions & 0 deletions rust-js/site/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>hello-wasm example</title>
</head>
<body>
<script src="./index.js"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions rust-js/site/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as wasm from "hello-wasm";

wasm.greet("WebAssembly with npm");
Loading

0 comments on commit 8804d07

Please sign in to comment.