Skip to content

Commit

Permalink
fix(core): invoke key injection on ES module, improve performance (#2094
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lucasfernog committed Jun 27, 2021
1 parent fe32afc commit 7765c7f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-javascript-iife-esm-rewrite.md
@@ -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: 6 additions & 0 deletions .changes/invoke-key-performance.md
@@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-codegen": patch
---

Improve invoke key code injection performance time rewriting code at compile time.
32 changes: 32 additions & 0 deletions core/tauri-codegen/src/embedded_assets.rs
Expand Up @@ -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")
Expand Down
11 changes: 4 additions & 7 deletions core/tauri/src/manager.rs
Expand Up @@ -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(),
Expand Down

0 comments on commit 7765c7f

Please sign in to comment.