Skip to content

Commit 1cd8f55

Browse files
fix: don't ship global api bundle if withGlobalTauri is false (#13033)
* fix: don't ship global api bundle if withGlobalTauri is false * Comment and prettier
1 parent 8603e42 commit 1cd8f55

File tree

9 files changed

+34
-21
lines changed

9 files changed

+34
-21
lines changed

.changes/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"feat": "New Features",
55
"enhance": "Enhancements",
66
"bug": "Bug Fixes",
7-
"pref": "Performance Improvements",
7+
"perf": "Performance Improvements",
88
"changes": "What's Changed",
99
"sec": "Security fixes",
1010
"deps": "Dependencies",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
tauri: 'patch:perf'
3+
tauri-build: 'patch:perf'
4+
tauri-plugin: 'patch:perf'
5+
tauri-utils: 'patch:perf'
6+
---
7+
8+
Don't ship global `bundle.global.js` if `app > withGlobalTauri` is set to false

crates/tauri-build/src/acl.rs

-2
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,6 @@ pub fn build(out_dir: &Path, target: Target, attributes: &Attributes) -> super::
432432
let capabilities_path = save_capabilities(&capabilities)?;
433433
fs::copy(capabilities_path, out_dir.join(CAPABILITIES_FILE_NAME))?;
434434

435-
tauri_utils::plugin::save_global_api_scripts_paths(out_dir);
436-
437435
let mut permissions_map = inline_plugins_acl.permission_files;
438436
if has_app_manifest {
439437
permissions_map.insert(APP_ACL_KEY.to_string(), app_acl.permission_files);

crates/tauri-build/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
511511

512512
acl::build(&out_dir, target, &attributes)?;
513513

514+
tauri_utils::plugin::save_global_api_scripts_paths(&out_dir, None);
515+
514516
println!("cargo:rustc-env=TAURI_ENV_TARGET_TRIPLE={target_triple}");
515517
// when running codegen in this build script, we need to access the env var directly
516518
env::set_var("TAURI_ENV_TARGET_TRIPLE", &target_triple);

crates/tauri-plugin/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'a> Builder<'a> {
144144
}
145145

146146
if let Some(path) = self.global_api_script_path {
147-
tauri_utils::plugin::define_global_api_script_path(path);
147+
tauri_utils::plugin::define_global_api_script_path(&path);
148148
}
149149

150150
mobile::setup(self.android_path, self.ios_path)?;

crates/tauri-utils/src/plugin.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod build {
1919
pub const GLOBAL_API_SCRIPT_FILE_LIST_PATH: &str = "__global-api-script.js";
2020

2121
/// Defines the path to the global API script using Cargo instructions.
22-
pub fn define_global_api_script_path(path: PathBuf) {
22+
pub fn define_global_api_script_path(path: &Path) {
2323
println!(
2424
"cargo:{GLOBAL_API_SCRIPT_PATH_KEY}={}",
2525
path
@@ -31,18 +31,27 @@ mod build {
3131

3232
/// Collects the path of all the global API scripts defined with [`define_global_api_script_path`]
3333
/// and saves them to the out dir with filename [`GLOBAL_API_SCRIPT_FILE_LIST_PATH`].
34-
pub fn save_global_api_scripts_paths(out_dir: &Path) {
34+
///
35+
/// `tauri_global_scripts` is only used in Tauri's monorepo for the examples to work
36+
/// since they don't have a build script to run `tauri-build` and pull in the deps env vars
37+
pub fn save_global_api_scripts_paths(out_dir: &Path, mut tauri_global_scripts: Option<PathBuf>) {
3538
let mut scripts = Vec::new();
3639

3740
for (key, value) in vars_os() {
3841
let key = key.to_string_lossy();
3942

40-
if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {
43+
if key == format!("DEP_TAURI_{GLOBAL_API_SCRIPT_PATH_KEY}") {
44+
tauri_global_scripts = Some(PathBuf::from(value));
45+
} else if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {
4146
let script_path = PathBuf::from(value);
4247
scripts.push(script_path);
4348
}
4449
}
4550

51+
if let Some(tauri_global_scripts) = tauri_global_scripts {
52+
scripts.insert(0, tauri_global_scripts);
53+
}
54+
4655
fs::write(
4756
out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH),
4857
serde_json::to_string(&scripts).expect("failed to serialize global API script paths"),

crates/tauri/build.rs

+10
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ fn main() {
337337
}
338338
}
339339

340+
let tauri_global_scripts = PathBuf::from("./scripts/bundle.global.js")
341+
.canonicalize()
342+
.expect("failed to canonicalize tauri global API script path");
343+
tauri_utils::plugin::define_global_api_script_path(&tauri_global_scripts);
344+
// This should usually be done in `tauri-build`,
345+
// but we need to do this here for the examples in this workspace to work as they don't have build scripts
346+
if is_tauri_workspace {
347+
tauri_utils::plugin::save_global_api_scripts_paths(&out_dir, Some(tauri_global_scripts));
348+
}
349+
340350
let permissions = define_permissions(&out_dir);
341351
tauri_utils::acl::build::generate_allowed_commands(&out_dir, permissions).unwrap();
342352
}

crates/tauri/scripts/init.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@
1212
__RAW_core_script__
1313

1414
__RAW_event_initialization_script__
15-
16-
__RAW_bundle_script__
1715
})()

crates/tauri/src/manager/webview.rs

-12
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ impl<R: Runtime> WebviewManager<R> {
122122
) -> crate::Result<PendingWebview<EventLoopMessage, R>> {
123123
let app_manager = manager.manager();
124124

125-
let is_init_global = app_manager.config.app.with_global_tauri;
126125
let plugin_init_scripts = app_manager
127126
.plugins
128127
.lock()
@@ -185,7 +184,6 @@ impl<R: Runtime> WebviewManager<R> {
185184
app_manager,
186185
&ipc_init.into_string(),
187186
&pattern_init.into_string(),
188-
is_init_global,
189187
use_https_scheme,
190188
)?
191189
.to_string(),
@@ -346,7 +344,6 @@ impl<R: Runtime> WebviewManager<R> {
346344
app_manager: &AppManager<R>,
347345
ipc_script: &str,
348346
pattern_script: &str,
349-
with_global_tauri: bool,
350347
use_https_scheme: bool,
351348
) -> crate::Result<String> {
352349
#[derive(Template)]
@@ -357,8 +354,6 @@ impl<R: Runtime> WebviewManager<R> {
357354
#[raw]
358355
ipc_script: &'a str,
359356
#[raw]
360-
bundle_script: &'a str,
361-
#[raw]
362357
core_script: &'a str,
363358
#[raw]
364359
event_initialization_script: &'a str,
@@ -374,12 +369,6 @@ impl<R: Runtime> WebviewManager<R> {
374369
invoke_key: &'a str,
375370
}
376371

377-
let bundle_script = if with_global_tauri {
378-
include_str!("../../scripts/bundle.global.js")
379-
} else {
380-
""
381-
};
382-
383372
let freeze_prototype = if app_manager.config.app.security.freeze_prototype {
384373
include_str!("../../scripts/freeze_prototype.js")
385374
} else {
@@ -389,7 +378,6 @@ impl<R: Runtime> WebviewManager<R> {
389378
InitJavascript {
390379
pattern_script,
391380
ipc_script,
392-
bundle_script,
393381
core_script: &CoreJavascript {
394382
os_name: std::env::consts::OS,
395383
protocol_scheme: if use_https_scheme { "https" } else { "http" },

0 commit comments

Comments
 (0)