From 0ea5cd777c35838b6b4998cc3e841c1bdb0e15d6 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:01:24 -0700 Subject: [PATCH 01/13] Refractor JS init input argument default initialization --- crates/cli-support/src/js/mod.rs | 40 ++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index d3c0cf9dad2..9b2e1ac4711 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -632,29 +632,33 @@ impl<'a> Context<'a> { } } - let default_module_path = match self.config.mode { + let default_module_path_creator = match self.config.mode { OutputMode::Web => format!( "\ - if (typeof input === 'undefined') {{ - input = new URL('{stem}_bg.wasm', import.meta.url); + const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url); + + function create_default_module_path() {{ + return cached_default_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'); + const cached_current_script_src = 'document' in window + ? window.document.currentScript + : null; + + function create_default_module_path() { + const src = typeof document === 'undefined' + ? location.href + : cached_current_script.src; + + return src.replace(/\\.js$/, '_bg.wasm'); }" .to_string(), _ => "".to_string(), }; - let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?; + let ts = self.ts_for_init_fn(has_memory, !default_module_path_creator.is_empty())?; // Initialize the `imports` object for all import definitions that we're // directed to wire up. @@ -740,8 +744,9 @@ impl<'a> Context<'a> { }} }} - async function init(input{init_memory_arg}) {{ - {default_module_path} + {default_module_path_creator} + + async function init(input{init_input_default_argument}{init_memory_arg}) {{ const imports = {{}}; {imports_init} @@ -759,8 +764,13 @@ impl<'a> Context<'a> { return wasm; }} ", + default_module_path_creator = default_module_path_creator, init_memory_arg = init_memory_arg, - default_module_path = default_module_path, + init_input_default_argument = if default_module_path_creator.is_empty() { + "" // the function isn't available to call, thus there is no default value + } else { + " = create_default_module_path()" + } init_memory = init_memory, start = if needs_manual_start { "wasm.__wbindgen_start();" From 0bf818b1728cb61903cb11933a63720052983d38 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:16:45 -0700 Subject: [PATCH 02/13] Update mod.rs --- crates/cli-support/src/js/mod.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 9b2e1ac4711..2c722865c79 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -632,8 +632,10 @@ impl<'a> Context<'a> { } } - let default_module_path_creator = match self.config.mode { - OutputMode::Web => format!( + let default_input_initializer_fn_call = " = create_default_module_path()".to_string(); + + let (default_module_path_creator, default_input_initializer) = match self.config.mode { + OutputMode::Web => (format!( "\ const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url); @@ -641,8 +643,8 @@ impl<'a> Context<'a> { return cached_default_url; }}", stem = self.config.stem()? - ), - OutputMode::NoModules { .. } => "\ + ), default_input_initializer_fn_call), + OutputMode::NoModules { .. } => ("\ const cached_current_script_src = 'document' in window ? window.document.currentScript : null; @@ -653,9 +655,8 @@ impl<'a> Context<'a> { : cached_current_script.src; return src.replace(/\\.js$/, '_bg.wasm'); - }" - .to_string(), - _ => "".to_string(), + }".to_string(), default_input_initializer_fn_call), + _ => (String::new(), String::new()), }; let ts = self.ts_for_init_fn(has_memory, !default_module_path_creator.is_empty())?; @@ -746,7 +747,7 @@ impl<'a> Context<'a> { {default_module_path_creator} - async function init(input{init_input_default_argument}{init_memory_arg}) {{ + async function init(input{input_default_initializer}{init_memory_arg}) {{ const imports = {{}}; {imports_init} @@ -765,12 +766,8 @@ impl<'a> Context<'a> { }} ", default_module_path_creator = default_module_path_creator, - init_memory_arg = init_memory_arg, - init_input_default_argument = if default_module_path_creator.is_empty() { - "" // the function isn't available to call, thus there is no default value - } else { - " = create_default_module_path()" - } + init_memory_arg = init_memory_arg + input_default_initializer = input_default_initializer, init_memory = init_memory, start = if needs_manual_start { "wasm.__wbindgen_start();" From eb58e3b6ffe516610d3f61ceee8e66ad3c4e0840 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:23:11 -0700 Subject: [PATCH 03/13] Optimize ESM module output Remove unecessary function call, in favor of direct variable lookup. --- crates/cli-support/src/js/mod.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 2c722865c79..63cf1e8e06a 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -632,18 +632,11 @@ impl<'a> Context<'a> { } } - let default_input_initializer_fn_call = " = create_default_module_path()".to_string(); - let (default_module_path_creator, default_input_initializer) = match self.config.mode { OutputMode::Web => (format!( - "\ - const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url); - - function create_default_module_path() {{ - return cached_default_url; - }}", + "const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url);", stem = self.config.stem()? - ), default_input_initializer_fn_call), + ), " = cached_default_url".to_string()), OutputMode::NoModules { .. } => ("\ const cached_current_script_src = 'document' in window ? window.document.currentScript @@ -655,7 +648,7 @@ impl<'a> Context<'a> { : cached_current_script.src; return src.replace(/\\.js$/, '_bg.wasm'); - }".to_string(), default_input_initializer_fn_call), + }".to_string(), " = create_default_module_path()".to_string()), _ => (String::new(), String::new()), }; From 6e0f172e4542a96457de40717417d836c615b7ff Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:26:51 -0700 Subject: [PATCH 04/13] Fill in missing comma --- crates/cli-support/src/js/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 63cf1e8e06a..72db061b9f7 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -758,8 +758,8 @@ impl<'a> Context<'a> { return wasm; }} ", + init_memory_arg = init_memory_arg, default_module_path_creator = default_module_path_creator, - init_memory_arg = init_memory_arg input_default_initializer = input_default_initializer, init_memory = init_memory, start = if needs_manual_start { From 88202dd1e5a2b90d46eb5d355cba453c652eeb08 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:59:48 -0700 Subject: [PATCH 05/13] Reformat --- crates/cli-support/src/js/mod.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 72db061b9f7..377d549f31e 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -633,11 +633,15 @@ impl<'a> Context<'a> { } let (default_module_path_creator, default_input_initializer) = match self.config.mode { - OutputMode::Web => (format!( - "const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url);", - stem = self.config.stem()? - ), " = cached_default_url".to_string()), - OutputMode::NoModules { .. } => ("\ + OutputMode::Web => ( + format!( + "const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url);", + stem = self.config.stem()? + ), + " = cached_default_url".to_string() + ), + OutputMode::NoModules { .. } => ( + "\ const cached_current_script_src = 'document' in window ? window.document.currentScript : null; @@ -648,7 +652,10 @@ impl<'a> Context<'a> { : cached_current_script.src; return src.replace(/\\.js$/, '_bg.wasm'); - }".to_string(), " = create_default_module_path()".to_string()), + }" + .to_string(), + " = create_default_module_path()".to_string(), + ), _ => (String::new(), String::new()), }; From 1e2e0fcfeadc09a1d3773b3791154a7ebf21ed1a Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Sat, 10 Apr 2021 00:24:13 -0700 Subject: [PATCH 06/13] Fix formatting --- crates/cli-support/src/js/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 377d549f31e..91645910165 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -638,7 +638,7 @@ impl<'a> Context<'a> { "const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url);", stem = self.config.stem()? ), - " = cached_default_url".to_string() + " = cached_default_url".to_string(), ), OutputMode::NoModules { .. } => ( "\ From d8dadc16bb6a43f306c92b2b0d3c458d5aeab1ec Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Sun, 11 Apr 2021 21:16:37 -0700 Subject: [PATCH 07/13] Use constant variable --- crates/cli-support/src/js/mod.rs | 50 +++++++++++++++----------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 91645910165..67c674517f4 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -632,31 +632,29 @@ impl<'a> Context<'a> { } } - let (default_module_path_creator, default_input_initializer) = match self.config.mode { - OutputMode::Web => ( - format!( - "const cached_default_url = new URL('{stem}_bg.wasm', import.meta.url);", - stem = self.config.stem()? + let (default_module_path_creator, default_input_initializer) = { + const ASSIGNMENT_STRING: &'static str = " = default_wasm_source_url"; + + match self.config.mode { + OutputMode::Web => ( + format!( + "const default_wasm_source_url = new URL('{stem}_bg.wasm', import.meta.url);", + stem = self.config.stem()? + ), + ASSIGNMENT_STRING, ), - " = cached_default_url".to_string(), - ), - OutputMode::NoModules { .. } => ( - "\ - const cached_current_script_src = 'document' in window - ? window.document.currentScript - : null; - - function create_default_module_path() { - const src = typeof document === 'undefined' - ? location.href - : cached_current_script.src; - - return src.replace(/\\.js$/, '_bg.wasm'); - }" - .to_string(), - " = create_default_module_path()".to_string(), - ), - _ => (String::new(), String::new()), + OutputMode::NoModules { .. } => ( + "\ + const default_wasm_source_url = (typeof document !== 'undefined' + ? document.currentScript.src + : location.href + ).replace(/\.js$/, '_bg.wasm'); + " + .to_string(), + ASSIGNMENT_STRING, + ), + _ => (String::new(), ""), + } }; let ts = self.ts_for_init_fn(has_memory, !default_module_path_creator.is_empty())?; @@ -747,7 +745,7 @@ impl<'a> Context<'a> { {default_module_path_creator} - async function init(input{input_default_initializer}{init_memory_arg}) {{ + async function init(input{default_input_initializer}{init_memory_arg}) {{ const imports = {{}}; {imports_init} @@ -767,7 +765,7 @@ impl<'a> Context<'a> { ", init_memory_arg = init_memory_arg, default_module_path_creator = default_module_path_creator, - input_default_initializer = input_default_initializer, + default_input_initializer = default_input_initializer, init_memory = init_memory, start = if needs_manual_start { "wasm.__wbindgen_start();" From 7aae7a6abbd8ae57cc1f14b4d875877a00bb182f Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Sun, 11 Apr 2021 21:20:45 -0700 Subject: [PATCH 08/13] Update mod.rs --- crates/cli-support/src/js/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 67c674517f4..dca7d54a7c1 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -644,7 +644,7 @@ impl<'a> Context<'a> { ASSIGNMENT_STRING, ), OutputMode::NoModules { .. } => ( - "\ + r"\ const default_wasm_source_url = (typeof document !== 'undefined' ? document.currentScript.src : location.href From 2abfbf6f45cf0358e658715b19df18c31086eefc Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Sun, 11 Apr 2021 21:23:38 -0700 Subject: [PATCH 09/13] Update mod.rs --- crates/cli-support/src/js/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index dca7d54a7c1..44d660d2a80 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -632,7 +632,7 @@ impl<'a> Context<'a> { } } - let (default_module_path_creator, default_input_initializer) = { + let (default_module_path_creator, default_input_initializer) = { const ASSIGNMENT_STRING: &'static str = " = default_wasm_source_url"; match self.config.mode { From 673d9cd1627d3d2c67ffa68d6bbd0887ec983d69 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Thu, 15 Apr 2021 17:50:19 -0700 Subject: [PATCH 10/13] Leave comment --- crates/cli-support/src/js/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 44d660d2a80..e85e718b0dd 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -645,6 +645,9 @@ impl<'a> Context<'a> { ), OutputMode::NoModules { .. } => ( r"\ + // 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 From 38bcafccb6319237946222a16171d4cb9a743825 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Mon, 19 Apr 2021 12:30:46 -0700 Subject: [PATCH 11/13] Update mod.rs --- crates/cli-support/src/js/mod.rs | 160 ++++++++++++++++--------------- 1 file changed, 82 insertions(+), 78 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index e85e718b0dd..5a39dd420cb 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -419,11 +419,19 @@ impl<'a> Context<'a> { for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) { let import = self.module.imports.get_mut(*id); import.module = format!("./{}_bg.js", module_name); - footer.push_str("\nexport const "); - footer.push_str(&import.name); - footer.push_str(" = "); - footer.push_str(js.trim()); - footer.push_str(";\n"); + if js.starts_with("function") { + let body = &js[8..]; + footer.push_str("\nexport function "); + footer.push_str(&import.name); + footer.push_str(body.trim()); + footer.push_str(";\n"); + } else { + footer.push_str("\nexport const "); + footer.push_str(&import.name); + footer.push_str(" = "); + footer.push_str(js.trim()); + footer.push_str(";\n"); + } } if needs_manual_start { start = Some("\nwasm.__wbindgen_start();\n".to_string()); @@ -632,35 +640,36 @@ impl<'a> Context<'a> { } } - let (default_module_path_creator, default_input_initializer) = { - const ASSIGNMENT_STRING: &'static str = " = default_wasm_source_url"; - + let default_module_path = if !self.config.omit_default_module_path { match self.config.mode { - OutputMode::Web => ( - format!( - "const default_wasm_source_url = new URL('{stem}_bg.wasm', import.meta.url);", - stem = self.config.stem()? - ), - ASSIGNMENT_STRING, + OutputMode::Web => format!( + "\ + if (typeof input === 'undefined') {{ + input = new URL('{stem}_bg.wasm', import.meta.url); + }}", + stem = self.config.stem()? ), - OutputMode::NoModules { .. } => ( - r"\ - // 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, - ), - _ => (String::new(), ""), + 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(), } + } else { + String::from("") }; - let ts = self.ts_for_init_fn(has_memory, !default_module_path_creator.is_empty())?; + let ts = self.ts_for_init_fn( + has_memory, + !self.config.omit_default_module_path && !default_module_path.is_empty(), + )?; // Initialize the `imports` object for all import definitions that we're // directed to wire up. @@ -746,9 +755,8 @@ impl<'a> Context<'a> { }} }} - {default_module_path_creator} - - async function init(input{default_input_initializer}{init_memory_arg}) {{ + async function init(input{init_memory_arg}) {{ + {default_module_path} const imports = {{}}; {imports_init} @@ -767,8 +775,7 @@ impl<'a> Context<'a> { }} ", init_memory_arg = init_memory_arg, - default_module_path_creator = default_module_path_creator, - default_input_initializer = default_input_initializer, + default_module_path = default_module_path, init_memory = init_memory, start = if needs_manual_start { "wasm.__wbindgen_start();" @@ -1746,17 +1753,14 @@ impl<'a> Context<'a> { (Some(table), Some(alloc)) => { let add = self.expose_add_to_externref_table(table, alloc)?; self.global(&format!( - " - function handleError(f) {{ - return function () {{ - try {{ - return f.apply(this, arguments); - - }} catch (e) {{ - const idx = {}(e); - wasm.{}(idx); - }} - }}; + "\ + function handleError(f, args) {{ + try {{ + return f.apply(this, args); + }} catch (e) {{ + const idx = {}(e); + wasm.{}(idx); + }} }} ", add, store, @@ -1765,16 +1769,13 @@ impl<'a> Context<'a> { _ => { self.expose_add_heap_object(); self.global(&format!( - " - function handleError(f) {{ - return function () {{ - try {{ - return f.apply(this, arguments); - - }} catch (e) {{ - wasm.{}(addHeapObject(e)); - }} - }}; + "\ + function handleError(f, args) {{ + try {{ + return f.apply(this, args); + }} catch (e) {{ + wasm.{}(addHeapObject(e)); + }} }} ", store, @@ -1790,27 +1791,24 @@ impl<'a> Context<'a> { } self.global( "\ - function logError(f) { - return function () { - try { - return f.apply(this, arguments); - - } catch (e) { - let error = (function () { - try { - return e instanceof Error \ - ? `${e.message}\\n\\nStack:\\n${e.stack}` \ - : e.toString(); - } catch(_) { - return \"\"; - } - }()); - console.error(\"wasm-bindgen: imported JS function that \ - was not marked as `catch` threw an error:\", \ - error); - throw e; - } - }; + function logError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + let error = (function () { + try { + return e instanceof Error \ + ? `${e.message}\\n\\nStack:\\n${e.stack}` \ + : e.toString(); + } catch(_) { + return \"\"; + } + }()); + console.error(\"wasm-bindgen: imported JS function that \ + was not marked as `catch` threw an error:\", \ + error); + throw e; + } } ", ); @@ -2412,9 +2410,15 @@ impl<'a> Context<'a> { } Kind::Import(core) => { let code = if catch { - format!("handleError(function{})", code) + format!( + "function() {{ return handleError(function {}, arguments) }}", + code + ) } else if log_error { - format!("logError(function{})", code) + format!( + "function() {{ return logError(function {}, arguments) }}", + code + ) } else { format!("function{}", code) }; From 962be7cee24b472926b051b9433be7f03c07fcf2 Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Mon, 19 Apr 2021 13:14:54 -0700 Subject: [PATCH 12/13] Update with rebase --- crates/cli-support/src/js/mod.rs | 64 ++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 5a39dd420cb..1baecbb2285 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -640,35 +640,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 { .. } => ( + r"\ + // 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(), )?; // Initialize the `imports` object for all import definitions that we're @@ -755,8 +763,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} @@ -775,7 +784,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();" From 8129ebe8339b4bab5314f90f76419882e754922a Mon Sep 17 00:00:00 2001 From: Anonymous <65428781+CrimsonCodes0@users.noreply.github.com> Date: Mon, 12 Jul 2021 15:01:46 -0700 Subject: [PATCH 13/13] remove '\' from output --- crates/cli-support/src/js/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 1baecbb2285..4e289aed43b 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -655,14 +655,14 @@ impl<'a> Context<'a> { ASSIGNMENT_STRING, ), OutputMode::NoModules { .. } => ( - r"\ + "\ // 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'); + ).replace(/\\.js$/, '_bg.wasm'); " .to_string(), ASSIGNMENT_STRING,