Skip to content

Commit 3a939c4

Browse files
authored
Remove faulty location.href fallback and disable --split-linked-modules by default (#3279)
* Remove faulty `location.href` fallback * Disable `--split-linked-modules` in cli-support
1 parent c5b073a commit 3a939c4

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141
test_wasm_bindgen:
4242
name: "Run wasm-bindgen crate tests (unix)"
4343
runs-on: ubuntu-latest
44+
env:
45+
WASM_BINDGEN_SPLIT_LINKED_MODULES: 1
4446
steps:
4547
- uses: actions/checkout@v2
4648
- run: rustup update --no-self-update stable && rustup default stable

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,7 @@ impl<'a> Context<'a> {
381381
js.push_str("let script_src;\n");
382382
js.push_str(
383383
"\
384-
if (typeof document === 'undefined') {
385-
script_src = location.href;
386-
} else {
384+
if (typeof document !== 'undefined') {
387385
script_src = new URL(document.currentScript.src, location.href).toString();
388386
}\n",
389387
);
@@ -720,7 +718,7 @@ impl<'a> Context<'a> {
720718
stem = self.config.stem()?
721719
),
722720
OutputMode::NoModules { .. } => "\
723-
if (typeof input === 'undefined') {
721+
if (typeof input === 'undefined' && script_src !== 'undefined') {
724722
input = script_src.replace(/\\.js$/, '_bg.wasm');
725723
}"
726724
.to_string(),

crates/cli-support/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Bindgen {
121121
wasm_interface_types,
122122
encode_into: EncodeInto::Test,
123123
omit_default_module_path: true,
124-
split_linked_modules: true,
124+
split_linked_modules: false,
125125
}
126126
}
127127

crates/cli/src/bin/wasm-bindgen-test-runner/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ integration test.\
167167
TestMode::NoModule => b.no_modules(true)?,
168168
};
169169

170+
if std::env::var("WASM_BINDGEN_SPLIT_LINKED_MODULES").is_ok() {
171+
b.split_linked_modules(true);
172+
}
173+
170174
b.debug(debug)
171175
.input_module(module, wasm)
172176
.keep_debug(false)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,14 @@ fn default_module_path_target_no_modules() {
260260
fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap();
261261
assert!(contents.contains(
262262
"\
263-
if (typeof document === 'undefined') {
264-
script_src = location.href;
265-
} else {
263+
if (typeof document !== 'undefined') {
266264
script_src = new URL(document.currentScript.src, location.href).toString();
267265
}",
268266
));
269267
assert!(contents.contains(
270268
"\
271269
async function init(input) {
272-
if (typeof input === 'undefined') {
270+
if (typeof input === 'undefined' && script_src !== 'undefined') {
273271
input = script_src.replace(/\\.js$/, '_bg.wasm');
274272
}",
275273
));

guide/src/reference/cli.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,7 @@ using a plugin. Alternatively, you can leave the syntax as is and instead
122122
manually configure the bundler to copy all files in `snippets/` to the output
123123
directory, preserving their paths relative to whichever bundled file ends up
124124
containing the JS shim.
125+
126+
On the no-modules target, `link_to!` won't work if used outside of a document,
127+
e.g. inside a worker. This is because it's impossible to figure out what the
128+
URL of the linked module is without a reference point like `import.meta.url`.

0 commit comments

Comments
 (0)