mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 12:31:22 +00:00
Add an example of --no-modules
in action
This commit is contained in:
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