Skip to content

Commit

Permalink
fix(core): run plugin init script in a separate context (#9570)
Browse files Browse the repository at this point in the history
* fix(core): run plugin init script in a separate context

* fix ci
  • Loading branch information
lucasfernog committed Apr 28, 2024
1 parent fd2f8ab commit 3e98145
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 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.
4 changes: 2 additions & 2 deletions .github/workflows/covector-version-or-publish-v1.yml
Expand Up @@ -28,7 +28,7 @@ jobs:
}
- {
target: x86_64-apple-darwin,
os: macos-latest,
os: macos-13,
toolchain: '1.60.0'
}
steps:
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
platform: [ubuntu-latest, macos-13, windows-latest]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-core.yml
Expand Up @@ -43,7 +43,7 @@ jobs:
}
- {
target: x86_64-apple-darwin,
os: macos-latest,
os: macos-13,
toolchain: '1.60.0'
}
features:
Expand Down
2 changes: 0 additions & 2 deletions core/tauri/scripts/init.js
Expand Up @@ -29,6 +29,4 @@
window.__TAURI_INVOKE__('__initialized', { url: window.location.href })
})
}

__RAW_plugin_initialization_script__
})()
13 changes: 6 additions & 7 deletions core/tauri/src/manager.rs
Expand Up @@ -427,7 +427,7 @@ impl<R: Runtime> WindowManager<R> {
app_handle: AppHandle<R>,
) -> crate::Result<PendingWindow<EventLoopMessage, R>> {
let is_init_global = self.inner.config.build.with_global_tauri;
let plugin_init = self
let plugin_init_scripts = self
.inner
.plugins
.lock()
Expand Down Expand Up @@ -471,8 +471,11 @@ impl<R: Runtime> WindowManager<R> {
window_labels_array = serde_json::to_string(&window_labels)?,
current_window_label = serde_json::to_string(&label)?,
))
.initialization_script(&self.initialization_script(&ipc_init.into_string(),&pattern_init.into_string(),&plugin_init, is_init_global)?)
;
.initialization_script(&self.initialization_script(&ipc_init.into_string(),&pattern_init.into_string(),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 Pattern::Isolation { schema, .. } = self.pattern() {
Expand Down Expand Up @@ -781,7 +784,6 @@ impl<R: Runtime> WindowManager<R> {
&self,
ipc_script: &str,
pattern_script: &str,
plugin_initialization_script: &str,
with_global_tauri: bool,
) -> crate::Result<String> {
#[derive(Template)]
Expand All @@ -801,8 +803,6 @@ impl<R: Runtime> WindowManager<R> {
#[raw]
event_initialization_script: &'a str,
#[raw]
plugin_initialization_script: &'a str,
#[raw]
freeze_prototype: &'a str,
#[raw]
hotkeys: &'a str,
Expand Down Expand Up @@ -867,7 +867,6 @@ impl<R: Runtime> WindowManager<R> {
.render_default(&Default::default())?
.into_string(),
event_initialization_script: &self.event_initialization_script(),
plugin_initialization_script,
freeze_prototype,
hotkeys: &hotkeys,
}
Expand Down
7 changes: 3 additions & 4 deletions core/tauri/src/plugin.rs
Expand Up @@ -607,14 +607,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
.values()
.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 3e98145

Please sign in to comment.