From d4679a03369fb1bc52f83108c8d30b77b4494055 Mon Sep 17 00:00:00 2001 From: diceride Date: Mon, 19 Apr 2021 16:21:35 +0200 Subject: [PATCH] Add --omit-default-module-path CLI flag (#2519) Adds a new CLI flag `--omit-default-module-path`, to control the `default_module_path` generated code. The `default_module_path` generated code is enabled by default, users need to explicitly disable it by setting the `--omit-default-module-path` CLI flag. --- crates/cli-support/src/js/mod.rs | 25 +++++--- crates/cli-support/src/lib.rs | 7 +++ crates/cli/src/bin/wasm-bindgen.rs | 5 +- crates/cli/tests/wasm-bindgen/main.rs | 87 +++++++++++++++++++++++++++ guide/src/reference/cli.md | 4 ++ 5 files changed, 118 insertions(+), 10 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 5c634b508e4..5a39dd420cb 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -640,15 +640,16 @@ impl<'a> Context<'a> { } } - let default_module_path = match self.config.mode { - OutputMode::Web => format!( - "\ + let default_module_path = if !self.config.omit_default_module_path { + match self.config.mode { + OutputMode::Web => format!( + "\ if (typeof input === 'undefined') {{ input = new URL('{stem}_bg.wasm', import.meta.url); }}", - stem = self.config.stem()? - ), - OutputMode::NoModules { .. } => "\ + stem = self.config.stem()? + ), + OutputMode::NoModules { .. } => "\ if (typeof input === 'undefined') { let src; if (typeof document === 'undefined') { @@ -658,11 +659,17 @@ impl<'a> Context<'a> { } input = src.replace(/\\.js$/, '_bg.wasm'); }" - .to_string(), - _ => "".to_string(), + .to_string(), + _ => "".to_string(), + } + } else { + String::from("") }; - let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?; + let ts = self.ts_for_init_fn( + has_memory, + !self.config.omit_default_module_path && !default_module_path.is_empty(), + )?; // Initialize the `imports` object for all import definitions that we're // directed to wire up. diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 26e537a763e..d63b3259d0a 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -33,6 +33,7 @@ pub struct Bindgen { keep_debug: bool, remove_name_section: bool, remove_producers_section: bool, + omit_default_module_path: bool, emit_start: bool, // Experimental support for weakrefs, an upcoming ECMAScript feature. // Currently only enable-able through an env var. @@ -115,6 +116,7 @@ impl Bindgen { multi_value: multi_value || wasm_interface_types, wasm_interface_types, encode_into: EncodeInto::Test, + omit_default_module_path: true, } } @@ -282,6 +284,11 @@ impl Bindgen { self } + pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen { + self.omit_default_module_path = omit_default_module_path; + self + } + pub fn generate>(&mut self, path: P) -> Result<(), Error> { self.generate_output()?.emit(path.as_ref()) } diff --git a/crates/cli/src/bin/wasm-bindgen.rs b/crates/cli/src/bin/wasm-bindgen.rs index 66ce566f86f..877b0c41a12 100644 --- a/crates/cli/src/bin/wasm-bindgen.rs +++ b/crates/cli/src/bin/wasm-bindgen.rs @@ -34,6 +34,7 @@ Options: --keep-debug Keep debug sections in wasm files --remove-name-section Remove the debugging `name` section of the file --remove-producers-section Remove the telemetry `producers` section + --omit-default-module-path Don't add WebAssembly fallback imports in generated JavaScript --encode-into MODE Whether or not to use TextEncoder#encodeInto, valid values are [test, always, never] --nodejs Deprecated, use `--target nodejs` @@ -66,6 +67,7 @@ struct Args { flag_keep_debug: bool, flag_encode_into: Option, flag_target: Option, + flag_omit_default_module_path: bool, arg_input: Option, } @@ -117,7 +119,8 @@ fn rmain(args: &Args) -> Result<(), Error> { .remove_name_section(args.flag_remove_name_section) .remove_producers_section(args.flag_remove_producers_section) .typescript(typescript) - .omit_imports(args.flag_omit_imports); + .omit_imports(args.flag_omit_imports) + .omit_default_module_path(args.flag_omit_default_module_path); if let Some(true) = args.flag_weak_refs { b.weak_refs(true); } diff --git a/crates/cli/tests/wasm-bindgen/main.rs b/crates/cli/tests/wasm-bindgen/main.rs index a2846a064cd..d534b5a9724 100644 --- a/crates/cli/tests/wasm-bindgen/main.rs +++ b/crates/cli/tests/wasm-bindgen/main.rs @@ -226,6 +226,93 @@ fn bin_crate_works() { .stdout("hello, world\n"); } +#[test] +fn default_module_path_target_web() { + let (mut cmd, out_dir) = Project::new("default_module_path_target_web") + .file( + "src/lib.rs", + r#" + "#, + ) + .wasm_bindgen("--target web"); + cmd.assert().success(); + let contents = fs::read_to_string(out_dir.join("default_module_path_target_web.js")).unwrap(); + assert!(contents.contains( + "\ +async function init(input) { + if (typeof input === 'undefined') { + input = new URL('default_module_path_target_web_bg.wasm', import.meta.url); + }", + )); +} + +#[test] +fn default_module_path_target_no_modules() { + let (mut cmd, out_dir) = Project::new("default_module_path_target_no_modules") + .file( + "src/lib.rs", + r#" + "#, + ) + .wasm_bindgen("--target no-modules"); + cmd.assert().success(); + let contents = + fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap(); + assert!(contents.contains( + "\ + async function init(input) { + if (typeof input === 'undefined') { + let src; + if (typeof document === 'undefined') { + src = location.href; + } else { + src = document.currentScript.src; + } + input = src.replace(/\\.js$/, '_bg.wasm'); + }", + )); +} + +#[test] +fn omit_default_module_path_target_web() { + let (mut cmd, out_dir) = Project::new("omit_default_module_path_target_web") + .file( + "src/lib.rs", + r#" + "#, + ) + .wasm_bindgen("--target web --omit-default-module-path"); + cmd.assert().success(); + let contents = + fs::read_to_string(out_dir.join("omit_default_module_path_target_web.js")).unwrap(); + assert!(contents.contains( + "\ +async function init(input) { + + const imports = {};", + )); +} + +#[test] +fn omit_default_module_path_target_no_modules() { + let (mut cmd, out_dir) = Project::new("omit_default_module_path_target_no_modules") + .file( + "src/lib.rs", + r#" + "#, + ) + .wasm_bindgen("--target no-modules --omit-default-module-path"); + cmd.assert().success(); + let contents = + fs::read_to_string(out_dir.join("omit_default_module_path_target_no_modules.js")).unwrap(); + assert!(contents.contains( + "\ + async function init(input) { + + const imports = {};", + )); +} + #[test] fn empty_interface_types() { let (mut cmd, _out_dir) = Project::new("empty_interface_types") diff --git a/guide/src/reference/cli.md b/guide/src/reference/cli.md index 41729d8ee5e..e99cde71983 100644 --- a/guide/src/reference/cli.md +++ b/guide/src/reference/cli.md @@ -118,3 +118,7 @@ proposal](https://github.com/webassembly/reference-types) proposal, meaning that the WebAssembly binary will use `externref` when importing and exporting functions that work with `JsValue`. For more information see the [documentation about reference types](./reference-types.md). + +### `--omit-default-module-path` + +Don't add WebAssembly fallback imports in generated JavaScript. \ No newline at end of file