fix(core): invoke key injection on ES module, improve performance (#2094)

This commit is contained in:
Lucas Fernandes Nogueira 2021-06-27 19:51:47 -03:00 committed by GitHub
parent fe32afcc93
commit 7765c7fa28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 7 deletions

View File

@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-codegen": patch
---
Detect ESM scripts and inject the invoke key directly instead of using an IIFE.

View File

@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-codegen": patch
---
Improve invoke key code injection performance time rewriting code at compile time.

View File

@ -177,6 +177,38 @@ impl EmbeddedAssets {
.to_vec();
}
}
let is_javascript = ["js", "cjs", "mjs"]
.iter()
.any(|e| path.extension() == Some(OsStr::new(e)));
if is_javascript {
let js = String::from_utf8_lossy(&input).into_owned();
input = if [
"import{", "import*", "import ", "export{", "export*", "export ",
]
.iter()
.any(|t| js.contains(t))
{
format!(
r#"
const __TAURI_INVOKE_KEY__ = __TAURI__INVOKE_KEY_TOKEN__;
{}
"#,
js
)
.as_bytes()
.to_vec()
} else {
format!(
r#"(function () {{
const __TAURI_INVOKE_KEY__ = __TAURI__INVOKE_KEY_TOKEN__;
{}
}})()"#,
js
)
.as_bytes()
.to_vec()
};
}
// we must canonicalize the base of our paths to allow long paths on windows
let out_dir = std::env::var("OUT_DIR")

View File

@ -467,13 +467,10 @@ impl<P: Params> WindowManager<P> {
if is_javascript {
let js = String::from_utf8_lossy(&asset).into_owned();
Ok(
format!(
r#"(function () {{
const __TAURI_INVOKE_KEY__ = {};
{}
}})()"#,
manager.generate_invoke_key(),
js
js.replacen(
"__TAURI__INVOKE_KEY_TOKEN__",
&manager.generate_invoke_key().to_string(),
1,
)
.as_bytes()
.to_vec(),