update fetch to take a string parameter

This commit is contained in:
robert masen
2018-04-23 09:04:30 -05:00
parent cbccd2028d
commit 79a49b2a56
3 changed files with 20 additions and 30 deletions

View File

@ -1,7 +1,6 @@
extern crate base64; extern crate base64;
use std::collections::HashSet; use std::collections::HashSet;
use std::path::PathBuf;
use parity_wasm::elements::*; use parity_wasm::elements::*;
@ -9,23 +8,20 @@ use super::Error;
pub struct Config { pub struct Config {
base64: bool, base64: bool,
fetch: bool, fetch_path: Option<String>,
file_name: String,
} }
pub struct Output { pub struct Output {
module: Module, module: Module,
base64: bool, base64: bool,
fetch: bool, fetch_path: Option<String>,
file_name: String,
} }
impl Config { impl Config {
pub fn new() -> Config { pub fn new() -> Config {
Config { Config {
base64: false, base64: false,
fetch: false, fetch_path: None,
file_name: String::new(),
} }
} }
@ -34,18 +30,14 @@ impl Config {
self self
} }
pub fn fetch(&mut self, fetch: bool, in_path: &PathBuf) -> &mut Self { pub fn fetch(&mut self, path: Option<String>) -> &mut Self {
self.fetch = fetch; self.fetch_path = path;
self.file_name = match in_path.file_name() {
Some(os_str) => os_str.to_str().unwrap_or("").to_string(),
None => String::new()
};
self self
} }
pub fn generate(&mut self, wasm: &[u8]) -> Result<Output, Error> { pub fn generate(&mut self, wasm: &[u8]) -> Result<Output, Error> {
if !self.base64 && !self.fetch { if !self.base64 && !self.fetch_path.is_some() {
panic!() panic!("the option --base64 or --fetch is required");
} }
let module = deserialize_buffer(wasm).map_err(|e| { let module = deserialize_buffer(wasm).map_err(|e| {
::Error(format!("{:?}", e)) ::Error(format!("{:?}", e))
@ -53,8 +45,7 @@ impl Config {
Ok(Output { Ok(Output {
module, module,
base64: self.base64, base64: self.base64,
fetch: self.fetch, fetch_path: self.fetch_path.clone(),
file_name: self.file_name.clone(),
}) })
} }
} }
@ -222,12 +213,12 @@ impl Output {
}}", base64 = base64::encode(&wasm)), }}", base64 = base64::encode(&wasm)),
inst inst
) )
} else if self.fetch { } else if self.fetch_path.is_some() {
( (
String::new(), String::new(),
format!("fetch('/{name}') format!("fetch('{path}')
.then(res => res.arrayBuffer()) .then(res => res.arrayBuffer())
.then(bytes => {inst})", name = self.file_name, inst = inst) .then(bytes => {inst})", path = self.fetch_path.unwrap(), inst = inst)
) )
} else { } else {
panic!("the option --base64 or --fetch is required"); panic!("the option --base64 or --fetch is required");

View File

@ -22,7 +22,7 @@ Options:
-o --output FILE File to place output in -o --output FILE File to place output in
--typescript Output a `*.d.ts` file next to the JS output --typescript Output a `*.d.ts` file next to the JS output
--base64 Inline the wasm module using base64 encoding --base64 Inline the wasm module using base64 encoding
--fetch Load module via `fetch()` instead of default webpack implementation --fetch PATH Load module by passing the PATH argument to `fetch()`
Note that this is not intended to produce a production-ready output module Note that this is not intended to produce a production-ready output module
but rather is intended purely as a temporary \"hack\" until it's standard in but rather is intended purely as a temporary \"hack\" until it's standard in
@ -34,7 +34,7 @@ struct Args {
flag_output: Option<PathBuf>, flag_output: Option<PathBuf>,
flag_typescript: bool, flag_typescript: bool,
flag_base64: bool, flag_base64: bool,
flag_fetch: bool, flag_fetch: Option<String>,
arg_input: PathBuf, arg_input: PathBuf,
} }
@ -43,7 +43,7 @@ fn main() {
.and_then(|d| d.deserialize()) .and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit()); .unwrap_or_else(|e| e.exit());
if !args.flag_base64 && !args.flag_fetch { if !args.flag_base64 && !args.flag_fetch.is_some() {
panic!("unfortunately only works right now with base64 or fetch"); panic!("unfortunately only works right now with base64 or fetch");
} }
@ -53,7 +53,7 @@ fn main() {
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new() let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
.base64(args.flag_base64) .base64(args.flag_base64)
.fetch(args.flag_fetch, &args.arg_input) .fetch(args.flag_fetch)
.generate(&wasm) .generate(&wasm)
.expect("failed to parse wasm"); .expect("failed to parse wasm");

View File

@ -8,14 +8,13 @@ cargo +nightly run -p wasm-bindgen-cli --bin wasm-bindgen -- \
../../../target/wasm32-unknown-unknown/debug/hello_world.wasm --out-dir . ../../../target/wasm32-unknown-unknown/debug/hello_world.wasm --out-dir .
# To avoid a bug occurring when webpack, wasm, and Chrome are used together, we # To avoid a bug occurring when webpack, wasm, and Chrome are used together, we
# convert the .wasm module to a .js module that embeds the wasm bytecode. To # create a .js module that will download the .wasm module via fetch.
# enable this, use the "--resolve-enxensions .js" flag when starting
# webpack-dev-server
cargo +nightly run -p wasm-bindgen-cli --bin wasm2es6js -- \ cargo +nightly run -p wasm-bindgen-cli --bin wasm2es6js -- \
--fetch -o hello_world_bg.js hello_world_bg.wasm --fetch ./hello_world_bg.wasm -o hello_world_bg.js hello_world_bg.wasm
# wasm2es6js --base64 -o hello_world_bg.js hello_world_bg.wasm
# And like the directory above this, from here it's the same. # And like the directory above this, from here it's the same.
npm install npm install
#force webpack-dev-server to ignore the .wasm file
# since we kept the same name for the .js module, we need
# to force webpack to ignore any other file extensions
npm run serve -- --resolve-extensions .js npm run serve -- --resolve-extensions .js