Skip to content

Commit 8ae6fe1

Browse files
authored
Merge pull request #153 from FreeMasen/master
include fetch arg for wasm2es6js
2 parents da20576 + 6a6be7e commit 8ae6fe1

File tree

4 files changed

+78
-23
lines changed

4 files changed

+78
-23
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ matrix:
3939
(cd examples/hello_world && sed -i 's/: "webpack-dev-server"/: "webpack"/' package.json && ./build.sh)
4040
- |
4141
(cd examples/hello_world/chrome && ./build.sh)
42+
- |
43+
(cd examples/hello_world/chrome && ./build_fetch.sh)
4244
- |
4345
(cd examples/smorgasboard && sed -i 's/: "webpack-dev-server"/: "webpack"/' package.json && ./build.sh)
4446
- |

crates/cli-support/src/wasm2es6js.rs

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,44 @@ use super::Error;
88

99
pub struct Config {
1010
base64: bool,
11+
fetch_path: Option<String>,
1112
}
1213

1314
pub struct Output {
1415
module: Module,
1516
base64: bool,
17+
fetch_path: Option<String>,
1618
}
1719

1820
impl Config {
1921
pub fn new() -> Config {
2022
Config {
2123
base64: false,
24+
fetch_path: None,
2225
}
2326
}
2427

2528
pub fn base64(&mut self, base64: bool) -> &mut Self {
2629
self.base64 = base64;
2730
self
2831
}
32+
33+
pub fn fetch(&mut self, path: Option<String>) -> &mut Self {
34+
self.fetch_path = path;
35+
self
36+
}
2937

3038
pub fn generate(&mut self, wasm: &[u8]) -> Result<Output, Error> {
31-
assert!(self.base64);
39+
if !self.base64 && !self.fetch_path.is_some() {
40+
panic!("the option --base64 or --fetch is required");
41+
}
3242
let module = deserialize_buffer(wasm).map_err(|e| {
3343
::Error(format!("{:?}", e))
3444
})?;
3545
Ok(Output {
3646
module,
3747
base64: self.base64,
48+
fetch_path: self.fetch_path.clone(),
3849
})
3950
}
4051
}
@@ -180,35 +191,51 @@ impl Output {
180191
));
181192
}
182193
}
183-
184-
let wasm = serialize(self.module)
185-
.expect("failed to serialize");
186-
194+
let inst = format!("WebAssembly.instantiate(bytes,{{ {imports} }})
195+
.then(obj => {{
196+
wasm = obj.instance;
197+
{memory}
198+
}})",
199+
imports = imports,
200+
memory = if export_mem { "memory = wasm.exports.memory;" } else { "" },
201+
);
202+
let (bytes, booted) = if self.base64 {
203+
let wasm = serialize(self.module)
204+
.expect("failed to serialize");
205+
(
206+
format!("
207+
let bytes;
208+
const base64 = \"{base64}\";
209+
if (typeof Buffer === 'undefined') {{
210+
bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
211+
}} else {{
212+
bytes = Buffer.from(base64, 'base64');
213+
}}", base64 = base64::encode(&wasm)),
214+
inst
215+
)
216+
} else if self.fetch_path.is_some() {
217+
(
218+
String::new(),
219+
format!("fetch('{path}')
220+
.then(res => res.arrayBuffer())
221+
.then(bytes => {inst})", path = self.fetch_path.unwrap(), inst = inst)
222+
)
223+
} else {
224+
panic!("the option --base64 or --fetch is required");
225+
};
187226
format!("
188227
{js_imports}
189228
let wasm;
190-
let bytes;
191-
const base64 = \"{base64}\";
192-
if (typeof Buffer === 'undefined') {{
193-
bytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
194-
}} else {{
195-
bytes = Buffer.from(base64, 'base64');
196-
}}
229+
{bytes}
197230
{mem_export}
198-
export const booted = WebAssembly.instantiate(bytes, {{ {imports} }})
199-
.then(obj => {{
200-
wasm = obj.instance;
201-
{memory}
202-
}});
203-
231+
export const booted = {booted};
204232
{exports}
205233
",
206-
base64 = base64::encode(&wasm),
234+
bytes = bytes,
235+
booted = booted,
207236
js_imports = js_imports,
208-
imports = imports,
209237
exports = exports,
210238
mem_export = if export_mem { "export let memory;" } else { "" },
211-
memory = if export_mem { "memory = wasm.exports.memory;" } else { "" },
212239
)
213240
}
214241
}

crates/cli/src/bin/wasm2es6js.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Options:
2222
-o --output FILE File to place output in
2323
--typescript Output a `*.d.ts` file next to the JS output
2424
--base64 Inline the wasm module using base64 encoding
25+
--fetch PATH Load module by passing the PATH argument to `fetch()`
2526
2627
Note that this is not intended to produce a production-ready output module
2728
but rather is intended purely as a temporary \"hack\" until it's standard in
@@ -33,6 +34,7 @@ struct Args {
3334
flag_output: Option<PathBuf>,
3435
flag_typescript: bool,
3536
flag_base64: bool,
37+
flag_fetch: Option<String>,
3638
arg_input: PathBuf,
3739
}
3840

@@ -41,8 +43,8 @@ fn main() {
4143
.and_then(|d| d.deserialize())
4244
.unwrap_or_else(|e| e.exit());
4345

44-
if !args.flag_base64 {
45-
panic!("unfortunately only works right now with base64");
46+
if !args.flag_base64 && !args.flag_fetch.is_some() {
47+
panic!("unfortunately only works right now with base64 or fetch");
4648
}
4749

4850
let mut wasm = Vec::new();
@@ -51,6 +53,7 @@ fn main() {
5153

5254
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
5355
.base64(args.flag_base64)
56+
.fetch(args.flag_fetch)
5457
.generate(&wasm)
5558
.expect("failed to parse wasm");
5659

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
# This is the same build.sh, later we are going to use the
6+
# fetch flag to avoid including the wasm module as a string
7+
# of base64, instead using the js `fetch` function
8+
#to request the module from the server.
9+
cargo +nightly build --target wasm32-unknown-unknown
10+
cargo +nightly run -p wasm-bindgen-cli --bin wasm-bindgen -- \
11+
../../../target/wasm32-unknown-unknown/debug/hello_world.wasm --out-dir .
12+
13+
# To avoid a bug occurring when webpack, wasm, and Chrome are used together, we
14+
# create a .js module that will download the .wasm module via fetch.
15+
cargo +nightly run -p wasm-bindgen-cli --bin wasm2es6js -- \
16+
--fetch ./hello_world_bg.wasm -o hello_world_bg.js hello_world_bg.wasm
17+
18+
# Install the npm items as usual.
19+
npm install
20+
21+
# since we kept the same name for the .js module, we need
22+
# to force webpack to ignore any other file extensions
23+
npm run serve -- --resolve-extensions .js

0 commit comments

Comments
 (0)