Skip to content

Commit d4679a0

Browse files
author
diceride
authored
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.
1 parent fda6bb9 commit d4679a0

File tree

5 files changed

+118
-10
lines changed

5 files changed

+118
-10
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,16 @@ impl<'a> Context<'a> {
640640
}
641641
}
642642

643-
let default_module_path = match self.config.mode {
644-
OutputMode::Web => format!(
645-
"\
643+
let default_module_path = if !self.config.omit_default_module_path {
644+
match self.config.mode {
645+
OutputMode::Web => format!(
646+
"\
646647
if (typeof input === 'undefined') {{
647648
input = new URL('{stem}_bg.wasm', import.meta.url);
648649
}}",
649-
stem = self.config.stem()?
650-
),
651-
OutputMode::NoModules { .. } => "\
650+
stem = self.config.stem()?
651+
),
652+
OutputMode::NoModules { .. } => "\
652653
if (typeof input === 'undefined') {
653654
let src;
654655
if (typeof document === 'undefined') {
@@ -658,11 +659,17 @@ impl<'a> Context<'a> {
658659
}
659660
input = src.replace(/\\.js$/, '_bg.wasm');
660661
}"
661-
.to_string(),
662-
_ => "".to_string(),
662+
.to_string(),
663+
_ => "".to_string(),
664+
}
665+
} else {
666+
String::from("")
663667
};
664668

665-
let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?;
669+
let ts = self.ts_for_init_fn(
670+
has_memory,
671+
!self.config.omit_default_module_path && !default_module_path.is_empty(),
672+
)?;
666673

667674
// Initialize the `imports` object for all import definitions that we're
668675
// directed to wire up.

crates/cli-support/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Bindgen {
3333
keep_debug: bool,
3434
remove_name_section: bool,
3535
remove_producers_section: bool,
36+
omit_default_module_path: bool,
3637
emit_start: bool,
3738
// Experimental support for weakrefs, an upcoming ECMAScript feature.
3839
// Currently only enable-able through an env var.
@@ -115,6 +116,7 @@ impl Bindgen {
115116
multi_value: multi_value || wasm_interface_types,
116117
wasm_interface_types,
117118
encode_into: EncodeInto::Test,
119+
omit_default_module_path: true,
118120
}
119121
}
120122

@@ -282,6 +284,11 @@ impl Bindgen {
282284
self
283285
}
284286

287+
pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen {
288+
self.omit_default_module_path = omit_default_module_path;
289+
self
290+
}
291+
285292
pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
286293
self.generate_output()?.emit(path.as_ref())
287294
}

crates/cli/src/bin/wasm-bindgen.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Options:
3434
--keep-debug Keep debug sections in wasm files
3535
--remove-name-section Remove the debugging `name` section of the file
3636
--remove-producers-section Remove the telemetry `producers` section
37+
--omit-default-module-path Don't add WebAssembly fallback imports in generated JavaScript
3738
--encode-into MODE Whether or not to use TextEncoder#encodeInto,
3839
valid values are [test, always, never]
3940
--nodejs Deprecated, use `--target nodejs`
@@ -66,6 +67,7 @@ struct Args {
6667
flag_keep_debug: bool,
6768
flag_encode_into: Option<String>,
6869
flag_target: Option<String>,
70+
flag_omit_default_module_path: bool,
6971
arg_input: Option<PathBuf>,
7072
}
7173

@@ -117,7 +119,8 @@ fn rmain(args: &Args) -> Result<(), Error> {
117119
.remove_name_section(args.flag_remove_name_section)
118120
.remove_producers_section(args.flag_remove_producers_section)
119121
.typescript(typescript)
120-
.omit_imports(args.flag_omit_imports);
122+
.omit_imports(args.flag_omit_imports)
123+
.omit_default_module_path(args.flag_omit_default_module_path);
121124
if let Some(true) = args.flag_weak_refs {
122125
b.weak_refs(true);
123126
}

crates/cli/tests/wasm-bindgen/main.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,93 @@ fn bin_crate_works() {
226226
.stdout("hello, world\n");
227227
}
228228

229+
#[test]
230+
fn default_module_path_target_web() {
231+
let (mut cmd, out_dir) = Project::new("default_module_path_target_web")
232+
.file(
233+
"src/lib.rs",
234+
r#"
235+
"#,
236+
)
237+
.wasm_bindgen("--target web");
238+
cmd.assert().success();
239+
let contents = fs::read_to_string(out_dir.join("default_module_path_target_web.js")).unwrap();
240+
assert!(contents.contains(
241+
"\
242+
async function init(input) {
243+
if (typeof input === 'undefined') {
244+
input = new URL('default_module_path_target_web_bg.wasm', import.meta.url);
245+
}",
246+
));
247+
}
248+
249+
#[test]
250+
fn default_module_path_target_no_modules() {
251+
let (mut cmd, out_dir) = Project::new("default_module_path_target_no_modules")
252+
.file(
253+
"src/lib.rs",
254+
r#"
255+
"#,
256+
)
257+
.wasm_bindgen("--target no-modules");
258+
cmd.assert().success();
259+
let contents =
260+
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
261+
assert!(contents.contains(
262+
"\
263+
async function init(input) {
264+
if (typeof input === 'undefined') {
265+
let src;
266+
if (typeof document === 'undefined') {
267+
src = location.href;
268+
} else {
269+
src = document.currentScript.src;
270+
}
271+
input = src.replace(/\\.js$/, '_bg.wasm');
272+
}",
273+
));
274+
}
275+
276+
#[test]
277+
fn omit_default_module_path_target_web() {
278+
let (mut cmd, out_dir) = Project::new("omit_default_module_path_target_web")
279+
.file(
280+
"src/lib.rs",
281+
r#"
282+
"#,
283+
)
284+
.wasm_bindgen("--target web --omit-default-module-path");
285+
cmd.assert().success();
286+
let contents =
287+
fs::read_to_string(out_dir.join("omit_default_module_path_target_web.js")).unwrap();
288+
assert!(contents.contains(
289+
"\
290+
async function init(input) {
291+
292+
const imports = {};",
293+
));
294+
}
295+
296+
#[test]
297+
fn omit_default_module_path_target_no_modules() {
298+
let (mut cmd, out_dir) = Project::new("omit_default_module_path_target_no_modules")
299+
.file(
300+
"src/lib.rs",
301+
r#"
302+
"#,
303+
)
304+
.wasm_bindgen("--target no-modules --omit-default-module-path");
305+
cmd.assert().success();
306+
let contents =
307+
fs::read_to_string(out_dir.join("omit_default_module_path_target_no_modules.js")).unwrap();
308+
assert!(contents.contains(
309+
"\
310+
async function init(input) {
311+
312+
const imports = {};",
313+
));
314+
}
315+
229316
#[test]
230317
fn empty_interface_types() {
231318
let (mut cmd, _out_dir) = Project::new("empty_interface_types")

guide/src/reference/cli.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ proposal](https://github.com/webassembly/reference-types) proposal, meaning that
118118
the WebAssembly binary will use `externref` when importing and exporting
119119
functions that work with `JsValue`. For more information see the [documentation
120120
about reference types](./reference-types.md).
121+
122+
### `--omit-default-module-path`
123+
124+
Don't add WebAssembly fallback imports in generated JavaScript.

0 commit comments

Comments
 (0)