Skip to content

Commit 6ed6883

Browse files
authored
Fix automatic .wasm file detection with no-modules (#3169)
1 parent f0b154e commit 6ed6883

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ impl<'a> Context<'a> {
378378
// function.
379379
OutputMode::NoModules { global } => {
380380
js.push_str("const __exports = {};\n");
381+
js.push_str("let script_src;\n");
382+
js.push_str(
383+
"\
384+
if (typeof document === 'undefined') {
385+
script_src = location.href;
386+
} else {
387+
script_src = document.currentScript.src;
388+
}\n",
389+
);
381390
js.push_str("let wasm;\n");
382391
init = self.gen_init(needs_manual_start, None)?;
383392
footer.push_str(&format!(
@@ -712,13 +721,7 @@ impl<'a> Context<'a> {
712721
),
713722
OutputMode::NoModules { .. } => "\
714723
if (typeof input === 'undefined') {
715-
let src;
716-
if (typeof document === 'undefined') {
717-
src = location.href;
718-
} else {
719-
src = document.currentScript.src;
720-
}
721-
input = src.replace(/\\.js$/, '_bg.wasm');
724+
input = script_src.replace(/\\.js$/, '_bg.wasm');
722725
}"
723726
.to_string(),
724727
_ => "".to_string(),

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,19 @@ fn default_module_path_target_no_modules() {
258258
cmd.assert().success();
259259
let contents =
260260
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
261+
assert!(contents.contains(
262+
"\
263+
if (typeof document === 'undefined') {
264+
script_src = location.href;
265+
} else {
266+
script_src = document.currentScript.src;
267+
}",
268+
));
261269
assert!(contents.contains(
262270
"\
263271
async function init(input) {
264272
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');
273+
input = script_src.replace(/\\.js$/, '_bg.wasm');
272274
}",
273275
));
274276
}

0 commit comments

Comments
 (0)