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
This commit is contained in:
Alex Crichton 2019-09-03 16:05:23 -04:00 committed by GitHub
parent 1f39a3045f
commit 4e19ead71b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 47 deletions

View File

@ -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!

View File

@ -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

View File

@ -5,4 +5,5 @@
<body>
<p>Open up the developer console to see "Hello, World!"</p>
</body>
<script src=index.js type=module></script>
</html>

View File

@ -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();

View File

@ -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"
}
}

View File

@ -7,7 +7,7 @@ extern "C" {
fn log(s: &str);
}
#[wasm_bindgen]
#[wasm_bindgen(start)]
pub fn run() {
log("Hello, World!");
}

View File

@ -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'
};