Skip to content

Commit 24a72ed

Browse files
committed
Make wasm-demo work with and without npm
1 parent 577708e commit 24a72ed

File tree

6 files changed

+78
-49
lines changed

6 files changed

+78
-49
lines changed

examples/wasm-demo/README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
# The Minimal Demo for Plotters + WASM
22

3-
You can run the demo locally using `npm`:
4-
```bash
5-
cd www
6-
npm install
7-
npm start
8-
```
3+
To build the demo you need [wasm-pack](https://rustwasm.github.io/docs/book/game-of-life/setup.html).
94

10-
or with the following command under this directory (Python required).
5+
Then you can run it locally either using `npm` and `webpack-dev-server` or
6+
just with static web server.
117

8+
The following script will install needed software and run the server via `npm`.
129
```
1310
./start-server.sh
1411
```
1512

16-
For Windows users without Bash, `start-server.bat` can be used to launch the server.
13+
For Windows users without Bash, `start-server.bat` can be used to
14+
launch the server.
1715

1816
```
1917
start-server.bat
2018
```
19+
20+
## Developing with NPM
21+
Please use [rust-wasm guide](https://rustwasm.github.io/docs/book/game-of-life/setup.html) for initial setup .
22+
Then you can run the demo locally using `npm`:
23+
```bash
24+
wasm-pack build
25+
cd www
26+
npm install
27+
npm start
28+
```
29+
30+
This will start a dev server which will automatically reload your page
31+
whenever you change anything in `www` directory. To update `rust` code
32+
call `wasm-pack build` manually.
33+
34+
## Developing without dependenices
35+
If you don't want to use `npm` here's how you can run the example
36+
using any web server. We are using rust [basic-http-server](https://github.com/brson/basic-http-server), but
37+
any web server will do.
38+
39+
```bash
40+
# Install web server (instead you can use your local nginx for example)
41+
cargo install basic-http-server
42+
wasm-pack build --target web # Note `--target web`
43+
basic-http-server
44+
```
45+
46+
Then open http://127.0.0.1:4000/www

examples/wasm-demo/server.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/wasm-demo/start-server.bat

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
if not exist "www\pkg" mkdir www\pkg
22
rustup target add wasm32-unknown-unknown
3-
cargo build --target=wasm32-unknown-unknown --release
4-
wasm-bindgen --version
5-
if errorlevel 1 cargo install wasm-bindgen-cli
6-
wasm-bindgen --out-dir www\pkg --target web target\wasm32-unknown-unknown\release\wasm_demo.wasm
3+
wasm-pack build --release
4+
if errorlevel 1 cargo install wasm-pack
5+
wasm-pack build --release
76
cd www
8-
echo "Goto http://localhost:8000/ to see the demo!"
9-
python ..\server.py
7+
npm install
8+
npm start

examples/wasm-demo/start-server.sh

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ mkdir -p www/pkg
66

77
rustup target add wasm32-unknown-unknown
88

9-
if [ -z "$(cargo install --list | grep wasm-bindgen-cli)" ]
9+
if [ -z "$(cargo install --list | grep wasm-pack)" ]
1010
then
11-
cargo install wasm-bindgen-cli
11+
cargo install wasm-pack
1212
fi
1313

1414
if [ "${CONFIG}" = "release" ]
1515
then
16-
cargo build --target=wasm32-unknown-unknown --release
16+
wasm-pack build
1717
else
18-
cargo build --target=wasm32-unknown-unknown
18+
wasm-pack build --release
1919
fi
2020

21-
wasm-bindgen --out-dir www/pkg --target web target/wasm32-unknown-unknown/${CONFIG}/wasm_demo.wasm
2221
cd www
23-
24-
echo "Goto http://localhost:8000/ to see the result"
25-
python ../server.py
22+
npm install
23+
npm start

examples/wasm-demo/www/bootstrap.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
// A dependency graph that contains any wasm must all be imported
2-
// asynchronously. This `bootstrap.js` file does the single async import, so
3-
// that no one else needs to worry about it again.
4-
import("./index.js")
5-
.catch(e => console.error("Error importing `index.js`:", e));
1+
init();
2+
3+
async function init() {
4+
if (typeof process == "object") {
5+
// We run in the npm/webpack environment.
6+
const [{Chart}, {main, setup}] = await Promise.all([
7+
import("wasm-demo"),
8+
import("./index.js"),
9+
]);
10+
setup(Chart);
11+
main();
12+
} else {
13+
const [{Chart, default: init}, {main, setup}] = await Promise.all([
14+
import("../pkg/wasm_demo.js"),
15+
import("./index.js"),
16+
]);
17+
await init();
18+
setup(Chart);
19+
main();
20+
}
21+
}

examples/wasm-demo/www/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
import { Chart, drawPower } from "wasm-demo";
1+
// If you only use `npm` you can simply
2+
// import { Chart } from "wasm-demo" and remove `setup` call from `bootstrap.js`.
3+
class Chart {}
24

35
const canvas = document.getElementById("canvas");
46
const coord = document.getElementById("coord");
57
const plotType = document.getElementById("plot-type");
68
const status = document.getElementById("status");
7-
let chart = undefined;
89

9-
main();
10+
let chart = null;
1011

11-
function main() {
12+
/** Main entry point */
13+
export function main() {
1214
setupUI();
1315
setupCanvas();
1416
}
1517

18+
/** This function is used in `bootstrap.js` to setup imports. */
19+
export function setup(WasmChart) {
20+
Chart = WasmChart;
21+
}
22+
1623
/** Add event listeners. */
1724
function setupUI() {
1825
status.innerText = "WebAssembly loaded!";
@@ -48,7 +55,7 @@ function onMouseMove(event) {
4855
function updatePlot() {
4956
const selected = plotType.selectedOptions[0];
5057
status.innerText = `Rendering ${selected.innerText}...`;
51-
chart = undefined;
58+
chart = null;
5259
const start = performance.now();
5360
chart = (selected.value == "mandelbrot")
5461
? Chart.mandelbrot(canvas)

0 commit comments

Comments
 (0)