Skip to content

Commit 7765c7f

Browse files
authored
fix(core): invoke key injection on ES module, improve performance (#2094)
1 parent fe32afc commit 7765c7f

4 files changed

Lines changed: 48 additions & 7 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
"tauri-codegen": patch
4+
---
5+
6+
Detect ESM scripts and inject the invoke key directly instead of using an IIFE.

.changes/invoke-key-performance.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
"tauri-codegen": patch
4+
---
5+
6+
Improve invoke key code injection performance time rewriting code at compile time.

core/tauri-codegen/src/embedded_assets.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,38 @@ impl EmbeddedAssets {
177177
.to_vec();
178178
}
179179
}
180+
let is_javascript = ["js", "cjs", "mjs"]
181+
.iter()
182+
.any(|e| path.extension() == Some(OsStr::new(e)));
183+
if is_javascript {
184+
let js = String::from_utf8_lossy(&input).into_owned();
185+
input = if [
186+
"import{", "import*", "import ", "export{", "export*", "export ",
187+
]
188+
.iter()
189+
.any(|t| js.contains(t))
190+
{
191+
format!(
192+
r#"
193+
const __TAURI_INVOKE_KEY__ = __TAURI__INVOKE_KEY_TOKEN__;
194+
{}
195+
"#,
196+
js
197+
)
198+
.as_bytes()
199+
.to_vec()
200+
} else {
201+
format!(
202+
r#"(function () {{
203+
const __TAURI_INVOKE_KEY__ = __TAURI__INVOKE_KEY_TOKEN__;
204+
{}
205+
}})()"#,
206+
js
207+
)
208+
.as_bytes()
209+
.to_vec()
210+
};
211+
}
180212

181213
// we must canonicalize the base of our paths to allow long paths on windows
182214
let out_dir = std::env::var("OUT_DIR")

core/tauri/src/manager.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,10 @@ impl<P: Params> WindowManager<P> {
467467
if is_javascript {
468468
let js = String::from_utf8_lossy(&asset).into_owned();
469469
Ok(
470-
format!(
471-
r#"(function () {{
472-
const __TAURI_INVOKE_KEY__ = {};
473-
{}
474-
}})()"#,
475-
manager.generate_invoke_key(),
476-
js
470+
js.replacen(
471+
"__TAURI__INVOKE_KEY_TOKEN__",
472+
&manager.generate_invoke_key().to_string(),
473+
1,
477474
)
478475
.as_bytes()
479476
.to_vec(),

0 commit comments

Comments
 (0)