diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4fff2677b47..e46a64a709f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -41,6 +41,8 @@ jobs: test_wasm_bindgen: name: "Run wasm-bindgen crate tests (unix)" runs-on: ubuntu-latest + env: + WASM_BINDGEN_SPLIT_LINKED_MODULES: 1 steps: - uses: actions/checkout@v2 - run: rustup update --no-self-update stable && rustup default stable diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 5f0cb08abe3..eb2583a9e7c 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -381,9 +381,7 @@ impl<'a> Context<'a> { js.push_str("let script_src;\n"); js.push_str( "\ - if (typeof document === 'undefined') { - script_src = location.href; - } else { + if (typeof document !== 'undefined') { script_src = new URL(document.currentScript.src, location.href).toString(); }\n", ); @@ -720,7 +718,7 @@ impl<'a> Context<'a> { stem = self.config.stem()? ), OutputMode::NoModules { .. } => "\ - if (typeof input === 'undefined') { + if (typeof input === 'undefined' && script_src !== 'undefined') { input = script_src.replace(/\\.js$/, '_bg.wasm'); }" .to_string(), diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index 1f8b446c2dd..00800ca08a3 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -121,7 +121,7 @@ impl Bindgen { wasm_interface_types, encode_into: EncodeInto::Test, omit_default_module_path: true, - split_linked_modules: true, + split_linked_modules: false, } } diff --git a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs index 067367b9fc5..7f6606a3e33 100644 --- a/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs +++ b/crates/cli/src/bin/wasm-bindgen-test-runner/main.rs @@ -167,6 +167,10 @@ integration test.\ TestMode::NoModule => b.no_modules(true)?, }; + if std::env::var("WASM_BINDGEN_SPLIT_LINKED_MODULES").is_ok() { + b.split_linked_modules(true); + } + b.debug(debug) .input_module(module, wasm) .keep_debug(false) diff --git a/crates/cli/tests/wasm-bindgen/main.rs b/crates/cli/tests/wasm-bindgen/main.rs index e854de23e51..bed6b2de9ad 100644 --- a/crates/cli/tests/wasm-bindgen/main.rs +++ b/crates/cli/tests/wasm-bindgen/main.rs @@ -260,16 +260,14 @@ fn default_module_path_target_no_modules() { fs::read_to_string(out_dir.join("default_module_path_target_no_modules.js")).unwrap(); assert!(contents.contains( "\ - if (typeof document === 'undefined') { - script_src = location.href; - } else { + if (typeof document !== 'undefined') { script_src = new URL(document.currentScript.src, location.href).toString(); }", )); assert!(contents.contains( "\ async function init(input) { - if (typeof input === 'undefined') { + if (typeof input === 'undefined' && script_src !== 'undefined') { input = script_src.replace(/\\.js$/, '_bg.wasm'); }", )); diff --git a/guide/src/reference/cli.md b/guide/src/reference/cli.md index d08e066361c..0d66d5cd7f1 100644 --- a/guide/src/reference/cli.md +++ b/guide/src/reference/cli.md @@ -122,3 +122,7 @@ using a plugin. Alternatively, you can leave the syntax as is and instead manually configure the bundler to copy all files in `snippets/` to the output directory, preserving their paths relative to whichever bundled file ends up containing the JS shim. + +On the no-modules target, `link_to!` won't work if used outside of a document, +e.g. inside a worker. This is because it's impossible to figure out what the +URL of the linked module is without a reference point like `import.meta.url`.