From 4e19ead71bb7a035eb54ca33ba887110b5f8858b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 3 Sep 2019 16:05:23 -0400 Subject: [PATCH] Fix the wasm2js example (#1750) This needed and update now that we're explicitly importing `*.wasm` to import `*.js` instead. Additionally this was moved over to the `web` target to avoid needing Webpack Closes #1743 --- examples/wasm2js/README.md | 2 +- examples/wasm2js/build.sh | 9 ++++----- examples/wasm2js/index.html | 1 + examples/wasm2js/index.js | 8 ++++---- examples/wasm2js/package.json | 13 ------------- examples/wasm2js/src/lib.rs | 2 +- examples/wasm2js/webpack.config.js | 23 ----------------------- 7 files changed, 11 insertions(+), 47 deletions(-) delete mode 100644 examples/wasm2js/package.json delete mode 100644 examples/wasm2js/webpack.config.js diff --git a/examples/wasm2js/README.md b/examples/wasm2js/README.md index 8db5c7a14..8a343b13a 100644 --- a/examples/wasm2js/README.md +++ b/examples/wasm2js/README.md @@ -14,4 +14,4 @@ $ ./build.sh (or running the commands on Windows manually) -and then visiting http://localhost:8080 in a browser should run the example! +and then visiting http://localhost:8000 in a browser should run the example! diff --git a/examples/wasm2js/build.sh b/examples/wasm2js/build.sh index 9100b26cd..deb7d1d1a 100755 --- a/examples/wasm2js/build.sh +++ b/examples/wasm2js/build.sh @@ -3,13 +3,12 @@ set -ex # Compile our wasm module and run `wasm-bindgen` -wasm-pack build +wasm-pack build --target web # Run the `wasm2js` tool from `binaryen` wasm2js pkg/wasm2js_bg.wasm -o pkg/wasm2js_bg.js -# Move our original wasm out of the way to avoid cofusing Webpack. -mv pkg/wasm2js_bg.wasm pkg/wasm2js_bg.bak.wasm +# Update our JS shim to require the JS file instead +sed -i 's/wasm2js_bg.wasm/wasm2js_bg.js/' pkg/wasm2js.js -npm install -npm run serve +http diff --git a/examples/wasm2js/index.html b/examples/wasm2js/index.html index 675fa94dc..cee977295 100644 --- a/examples/wasm2js/index.html +++ b/examples/wasm2js/index.html @@ -5,4 +5,5 @@

Open up the developer console to see "Hello, World!"

+ diff --git a/examples/wasm2js/index.js b/examples/wasm2js/index.js index 27ed1ebea..e13ec37f5 100644 --- a/examples/wasm2js/index.js +++ b/examples/wasm2js/index.js @@ -1,4 +1,4 @@ -// Note that since we're not using `WebAssembly` we can do a synchronous import -// here! -import { run } from './pkg/wasm2js'; -run(); +// Import our JS shim and initialize it, executing the start function when it's +// ready. +import init from './pkg/wasm2js.js'; +init(); diff --git a/examples/wasm2js/package.json b/examples/wasm2js/package.json deleted file mode 100644 index 31a944245..000000000 --- a/examples/wasm2js/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "scripts": { - "build": "webpack", - "serve": "webpack-dev-server" - }, - "devDependencies": { - "text-encoding": "^0.7.0", - "html-webpack-plugin": "^3.2.0", - "webpack": "^4.29.4", - "webpack-cli": "^3.1.1", - "webpack-dev-server": "^3.1.0" - } -} diff --git a/examples/wasm2js/src/lib.rs b/examples/wasm2js/src/lib.rs index af514ce5e..0e156c499 100644 --- a/examples/wasm2js/src/lib.rs +++ b/examples/wasm2js/src/lib.rs @@ -7,7 +7,7 @@ extern "C" { fn log(s: &str); } -#[wasm_bindgen] +#[wasm_bindgen(start)] pub fn run() { log("Hello, World!"); } diff --git a/examples/wasm2js/webpack.config.js b/examples/wasm2js/webpack.config.js deleted file mode 100644 index f15dc6b56..000000000 --- a/examples/wasm2js/webpack.config.js +++ /dev/null @@ -1,23 +0,0 @@ -const path = require('path'); -const HtmlWebpackPlugin = require('html-webpack-plugin'); -const webpack = require('webpack'); - -module.exports = { - entry: './index.js', - output: { - path: path.resolve(__dirname, 'dist'), - filename: 'index.js', - }, - plugins: [ - new HtmlWebpackPlugin({ - template: "index.html" - }), - // Have this example work in Edge which doesn't ship `TextEncoder` or - // `TextDecoder` at this time. - new webpack.ProvidePlugin({ - TextDecoder: ['text-encoding', 'TextDecoder'], - TextEncoder: ['text-encoding', 'TextEncoder'] - }) - ], - mode: 'development' -};