ParisJS meetup - june 2019
add
.wasm
files
Facilitates high-level interactions between wasm modules and JavaScript.
cargo add wasm-bindgen
Import JavaScript things into Rust and export Rust things to JavaScript.
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
Use exported Rust things from JavaScript with ECMAScript modules!
import { greet } from "./hello_world";
greet("World!");
Promises
to Rust Futures
A wasm workflow tool
cargo install wasm-pack
# or use installer https://rustwasm.github.io/wasm-pack/installer/
Helps you build rust-generated WebAssembly packages that
wasm-pack build --target web
wasm-pack build
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js',
},
plugins: [
new HtmlWebpackPlugin(),
new WasmPackPlugin({
crateDirectory: path.resolve(__dirname, ".")
})
],
mode: 'development'
};
Under the hood
wasm-pack build --target bundler