mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-01 03:02:28 +03:00
fix(core): invoke key injection on ES module, improve performance (#2094)
This commit is contained in:
parent
fe32afcc93
commit
7765c7fa28
6
.changes/fix-javascript-iife-esm-rewrite.md
Normal file
6
.changes/fix-javascript-iife-esm-rewrite.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri": patch
|
||||
"tauri-codegen": patch
|
||||
---
|
||||
|
||||
Detect ESM scripts and inject the invoke key directly instead of using an IIFE.
|
6
.changes/invoke-key-performance.md
Normal file
6
.changes/invoke-key-performance.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri": patch
|
||||
"tauri-codegen": patch
|
||||
---
|
||||
|
||||
Improve invoke key code injection performance time rewriting code at compile time.
|
@ -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")
|
||||
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user