Commit 9020125f authored by mara's avatar mara
Browse files

Switch from webpack/npm to direct use of webassembly

parent 09273a3e
......@@ -3,4 +3,5 @@
Cargo.lock
/bin
/pkg
/dist
wasm-pack.log
......@@ -26,20 +26,21 @@ Rust.
## Dependencies
This currently requires the Rust toolchain, `npm` and `wasm-pack`
(see [setup instructions](https://rustwasm.github.io/book/game-of-life/setup.html)).
This currently requires the Rust toolchain for WebAssembly, with the following
one-time setup:
(As the javascript is trivial, going through `npm` seems cruel and
unnecessary. It could be removed, I suppose, at the cost of needing to write
more bootstrap javascript)
```
rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli
```
## Running and buildling
## Running and building
To build everything and start a webserver on `0.0.0.0:8080`, run:
```
./run.sh
```
There's also `./dist.sh` that will build a distribution directory in `www/dist`,
There's also `./build.sh` that will build a distribution directory in `dist`,
for deploying to a separate web server.
#!/bin/bash
set -e
wasm-pack build --no-typescript
#--debug
set -ex
SRC="${PWD}"
OUT="${PWD}/dist"
cd www
if [ ! -d node_modules ]; then
npm install
cd node_modules
ln -s ../../pkg wasm-game-of-life
fi
cd ..
cargo build --target wasm32-unknown-unknown --release
mkdir -p ${OUT}
wasm-bindgen ./target/wasm32-unknown-unknown/release/wasm_game_of_life.wasm --no-typescript --no-modules --out-dir ${OUT}
cp ${SRC}/www/index.html ${OUT}
#!/bin/bash
set -e
./build.sh
cd www
npm run build
#!/bin/bash
set -e
set -ex
./build.sh
cd www
npm run start
cd dist
python3 -m http.server 8080
node_modules
dist
.bin
package-lock.json
language: node_js
node_js: "10"
script:
- ./node_modules/.bin/webpack
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));
......@@ -48,6 +48,23 @@
</div>
<canvas id="game-of-life-canvas" title="Universe-visualization
(click cell to toggle)"></canvas>
<script src="./bootstrap.js"></script>
<script>delete WebAssembly.instantiateStreaming; /* work around issue with file: URLs and mime types */ </script>
<script src="./wasm_game_of_life.js"></script>
<script>
const { main } = wasm_bindgen;
var eventloop;
function run() {
eventloop = main(
document.getElementById("game-of-life-canvas"),
document.getElementById("play-pause"),
document.getElementById("randomize"),
document.getElementById("omnicide"));
}
wasm_bindgen('./wasm_game_of_life_bg.wasm').then(run);
</script>
</body>
</html>
import { main } from "wasm-game-of-life";
const eventloop = main(
document.getElementById("game-of-life-canvas"),
document.getElementById("play-pause"),
document.getElementById("randomize"),
document.getElementById("omnicide"));
{
"name": "create-wasm-app",
"version": "0.1.0",
"description": "create an app to consume rust-generated wasm packages",
"main": "index.js",
"bin": {
"create-wasm-app": ".bin/create-wasm-app.js"
},
"scripts": {
"build": "webpack --config webpack.config.js",
"start": "webpack-dev-server --host 0.0.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rustwasm/create-wasm-app.git"
},
"keywords": [
"webassembly",
"wasm",
"rust",
"webpack"
],
"author": "Ashley Williams <ashley666ashley@gmail.com>",
"license": "(MIT OR Apache-2.0)",
"bugs": {
"url": "https://github.com/rustwasm/create-wasm-app/issues"
},
"homepage": "https://github.com/rustwasm/create-wasm-app#readme",
"devDependencies": {
"webpack": "^4.16.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"copy-webpack-plugin": "^4.5.2"
}
}
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');
module.exports = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
mode: "development",
plugins: [
new CopyWebpackPlugin(['index.html'])
],
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment