Skip to content

Commit

Permalink
Remove faulty location.href fallback and disable `--split-linked-mo…
Browse files Browse the repository at this point in the history
…dules` by default (#3279)

* Remove faulty `location.href` fallback

* Disable `--split-linked-modules` in cli-support
  • Loading branch information
daxpedda authored Feb 2, 2023
1 parent c5b073a commit 3a939c4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/cli/src/bin/wasm-bindgen-test-runner/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions crates/cli/tests/wasm-bindgen/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}",
));
Expand Down
4 changes: 4 additions & 0 deletions guide/src/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit 3a939c4

Please sign in to comment.