From 574e54a89deebb4ae1344e83548951758942e15a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 19 Apr 2018 07:20:35 -0700 Subject: [PATCH] Add an example of `--no-modules` in action --- Cargo.toml | 1 + examples/README.md | 2 ++ examples/no_modules/.gitignore | 2 ++ examples/no_modules/Cargo.toml | 10 ++++++++++ examples/no_modules/README.md | 10 ++++++++++ examples/no_modules/build.sh | 12 ++++++++++++ examples/no_modules/index.html | 21 +++++++++++++++++++++ examples/no_modules/src/lib.rs | 15 +++++++++++++++ 8 files changed, 73 insertions(+) create mode 100644 examples/no_modules/.gitignore create mode 100644 examples/no_modules/Cargo.toml create mode 100644 examples/no_modules/README.md create mode 100755 examples/no_modules/build.sh create mode 100644 examples/no_modules/index.html create mode 100644 examples/no_modules/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index ab4397b62..e25940df1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ members = [ "examples/performance", "examples/wasm-in-wasm", "examples/closures", + "examples/no_modules", ] [profile.release] diff --git a/examples/README.md b/examples/README.md index 38067e8fa..4b799d62d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/examples/no_modules/.gitignore b/examples/no_modules/.gitignore new file mode 100644 index 000000000..fe50a5485 --- /dev/null +++ b/examples/no_modules/.gitignore @@ -0,0 +1,2 @@ +no_modules.js +no_modules_bg.wasm diff --git a/examples/no_modules/Cargo.toml b/examples/no_modules/Cargo.toml new file mode 100644 index 000000000..f482b44e6 --- /dev/null +++ b/examples/no_modules/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "no_modules" +version = "0.1.0" +authors = ["Alex Crichton "] + +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = { path = "../.." } diff --git a/examples/no_modules/README.md b/examples/no_modules/README.md new file mode 100644 index 000000000..abc697952 --- /dev/null +++ b/examples/no_modules/README.md @@ -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 +``` diff --git a/examples/no_modules/build.sh b/examples/no_modules/build.sh new file mode 100755 index 000000000..daaf55461 --- /dev/null +++ b/examples/no_modules/build.sh @@ -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 diff --git a/examples/no_modules/index.html b/examples/no_modules/index.html new file mode 100644 index 000000000..1949a7583 --- /dev/null +++ b/examples/no_modules/index.html @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/examples/no_modules/src/lib.rs b/examples/no_modules/src/lib.rs new file mode 100644 index 000000000..6a83b6bf5 --- /dev/null +++ b/examples/no_modules/src/lib.rs @@ -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)); +}