Skip to content

Commit

Permalink
fix(core): run plugin init script in a separate context (#9571)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 28, 2024
1 parent 6d5a396 commit eff778b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/plugin-init-script-context.md
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Run each plugin initialization script on its own context so they do not interfere with each other or the Tauri init script.
2 changes: 0 additions & 2 deletions core/tauri/scripts/init.js
Expand Up @@ -14,6 +14,4 @@
__RAW_event_initialization_script__

__RAW_bundle_script__

__RAW_plugin_initialization_script__
})()
11 changes: 5 additions & 6 deletions core/tauri/src/manager/webview.rs
Expand Up @@ -133,7 +133,7 @@ impl<R: Runtime> WebviewManager<R> {
let app_manager = manager.manager();

let is_init_global = app_manager.config.app.with_global_tauri;
let plugin_init = app_manager
let plugin_init_scripts = app_manager
.plugins
.lock()
.expect("poisoned plugin store")
Expand Down Expand Up @@ -199,10 +199,13 @@ impl<R: Runtime> WebviewManager<R> {
app_manager,
&ipc_init.into_string(),
&pattern_init.into_string(),
&plugin_init,
is_init_global,
)?);

for plugin_init_script in plugin_init_scripts {
webview_attributes = webview_attributes.initialization_script(&plugin_init_script);
}

#[cfg(feature = "isolation")]
if let crate::Pattern::Isolation { schema, .. } = &*app_manager.pattern {
webview_attributes = webview_attributes.initialization_script(
Expand Down Expand Up @@ -345,7 +348,6 @@ impl<R: Runtime> WebviewManager<R> {
app_manager: &AppManager<R>,
ipc_script: &str,
pattern_script: &str,
plugin_initialization_script: &str,
with_global_tauri: bool,
) -> crate::Result<String> {
#[derive(Template)]
Expand All @@ -362,8 +364,6 @@ impl<R: Runtime> WebviewManager<R> {
#[raw]
event_initialization_script: &'a str,
#[raw]
plugin_initialization_script: &'a str,
#[raw]
freeze_prototype: &'a str,
}

Expand Down Expand Up @@ -398,7 +398,6 @@ impl<R: Runtime> WebviewManager<R> {
app_manager.listeners().function_name(),
app_manager.listeners().listeners_object_name(),
),
plugin_initialization_script,
freeze_prototype,
}
.render_default(&Default::default())
Expand Down
7 changes: 3 additions & 4 deletions core/tauri/src/plugin.rs
Expand Up @@ -783,14 +783,13 @@ impl<R: Runtime> PluginStore<R> {
}

/// Generates an initialization script from all plugins in the store.
pub(crate) fn initialization_script(&self) -> String {
pub(crate) fn initialization_script(&self) -> Vec<String> {
self
.store
.iter()
.filter_map(|p| p.initialization_script())
.fold(String::new(), |acc, script| {
format!("{acc}\n(function () {{ {script} }})();")
})
.map(|script| format!("(function () {{ {script} }})();"))
.collect()
}

/// Runs the created hook for all plugins in the store.
Expand Down

0 comments on commit eff778b

Please sign in to comment.