Skip to content

Cache document.currentScript for non-ESM output #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
64 changes: 37 additions & 27 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,35 +656,43 @@ impl<'a> Context<'a> {
}
}

let default_module_path = if !self.config.omit_default_module_path {
match self.config.mode {
OutputMode::Web => format!(
"\
if (typeof input === 'undefined') {{
input = new URL('{stem}_bg.wasm', import.meta.url);
}}",
stem = self.config.stem()?
),
OutputMode::NoModules { .. } => "\
if (typeof input === 'undefined') {
let src;
if (typeof document === 'undefined') {
src = location.href;
} else {
src = document.currentScript.src;
}
input = src.replace(/\\.js$/, '_bg.wasm');
}"
.to_string(),
_ => "".to_string(),
let (default_module_path_creator, default_input_initializer): (String, &'static str) = {
const ASSIGNMENT_STRING: &'static str = " = default_wasm_source_url";
const EMPTY: (String, &'static str) = (String::new(), "");
let config = &self.config;

if !config.omit_default_module_path {
match config.mode {
OutputMode::Web => (
format!(
"const default_wasm_source_url = new URL('{stem}_bg.wasm', import.meta.url);",
stem = config.stem()?
),
ASSIGNMENT_STRING,
),
OutputMode::NoModules { .. } => (
"\
// Document#currentScript is a getter,
// it's value changes upon evaluating each script,
// therefore it must be cached
const default_wasm_source_url = (typeof document !== 'undefined'
? document.currentScript.src
: location.href
).replace(/\\.js$/, '_bg.wasm');
"
.to_string(),
ASSIGNMENT_STRING,
),
_ => EMPTY,
}
} else {
EMPTY
}
} else {
String::from("")
};

let ts = self.ts_for_init_fn(
has_memory,
!self.config.omit_default_module_path && !default_module_path.is_empty(),
!self.config.omit_default_module_path && !default_module_path_creator.is_empty(),
Copy link
Author

@ghost ghost Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm following this logic correctly, the entire expression could be shortened to the following:

!default_module_path_creator.is_empty()

cc @diceride

If someone omits the default path, and chooses, say, Node.js, there still won't be a default path, therefore we can just check if there isn't a default path via .is_empty().

)?;

// Initialize the `imports` object for all import definitions that we're
Expand Down Expand Up @@ -771,8 +779,9 @@ impl<'a> Context<'a> {
}}
}}

async function init(input{init_memory_arg}) {{
{default_module_path}
{default_module_path_creator}

async function init(input{default_input_initializer}{init_memory_arg}) {{
const imports = {{}};
{imports_init}

Expand All @@ -791,7 +800,8 @@ impl<'a> Context<'a> {
}}
",
init_memory_arg = init_memory_arg,
default_module_path = default_module_path,
default_module_path_creator = default_module_path_creator,
default_input_initializer = default_input_initializer,
init_memory = init_memory,
start = if needs_manual_start {
"wasm.__wbindgen_start();"
Expand Down