Add an example of --no-modules in action

This commit is contained in:
Alex Crichton 2018-04-19 07:20:35 -07:00
parent 45e4983e8c
commit 574e54a89d
8 changed files with 73 additions and 0 deletions

View File

@ -33,6 +33,7 @@ members = [
"examples/performance",
"examples/wasm-in-wasm",
"examples/closures",
"examples/no_modules",
]
[profile.release]

View File

@ -27,3 +27,5 @@ The examples here are:
`WebAssembly.Module` and shows off creation of a WebAssembly module from Rust
* `closures` - an example of how to invoke functions like `setInterval` or use
the `onclick` property in conjunction with closures.
* `no_modules` - an example of how to use the `--no-modules` flag to
the `wasm-bindgen` CLI tool

2
examples/no_modules/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
no_modules.js
no_modules_bg.wasm

View File

@ -0,0 +1,10 @@
[package]
name = "no_modules"
version = "0.1.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = { path = "../.." }

View File

@ -0,0 +1,10 @@
# `--no-modules`
This directory is an example of using the `--no-modules` flag and how it
integrates with the rest of the HTML/JS used.
You can build the example locally with:
```
$ ./build.sh
```

12
examples/no_modules/build.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
set -ex
cargo +nightly build --target wasm32-unknown-unknown
cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml \
--bin wasm-bindgen -- \
--no-modules \
../../target/wasm32-unknown-unknown/debug/no_modules.wasm --out-dir .
python -m SimpleHTTPServer

View File

@ -0,0 +1,21 @@
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<script src='./no_modules.js'></script>
<script>
// the `wasm_bindgen` global is set to the exports of the Rust module
const { greet } = wasm_bindgen;
// we'll defer our execution until the wasm is ready to go
function run() {
greet('World');
}
// here we tell bindgen the path to the wasm file so it can run
// initialization and return to us a promise when it's done
wasm_bindgen('./no_modules_bg.wasm').then(run);
</script>
</body>
</html>

View File

@ -0,0 +1,15 @@
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}