mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-15 14:01:25 +00:00
Add an example of --no-modules
in action
This commit is contained in:
@ -33,6 +33,7 @@ members = [
|
|||||||
"examples/performance",
|
"examples/performance",
|
||||||
"examples/wasm-in-wasm",
|
"examples/wasm-in-wasm",
|
||||||
"examples/closures",
|
"examples/closures",
|
||||||
|
"examples/no_modules",
|
||||||
]
|
]
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
@ -27,3 +27,5 @@ The examples here are:
|
|||||||
`WebAssembly.Module` and shows off creation of a WebAssembly module from Rust
|
`WebAssembly.Module` and shows off creation of a WebAssembly module from Rust
|
||||||
* `closures` - an example of how to invoke functions like `setInterval` or use
|
* `closures` - an example of how to invoke functions like `setInterval` or use
|
||||||
the `onclick` property in conjunction with closures.
|
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
2
examples/no_modules/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
no_modules.js
|
||||||
|
no_modules_bg.wasm
|
10
examples/no_modules/Cargo.toml
Normal file
10
examples/no_modules/Cargo.toml
Normal 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 = "../.." }
|
10
examples/no_modules/README.md
Normal file
10
examples/no_modules/README.md
Normal 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
12
examples/no_modules/build.sh
Executable 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
|
21
examples/no_modules/index.html
Normal file
21
examples/no_modules/index.html
Normal 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>
|
15
examples/no_modules/src/lib.rs
Normal file
15
examples/no_modules/src/lib.rs
Normal 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));
|
||||||
|
}
|
Reference in New Issue
Block a user