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
This commit is contained in:
Tony
2025-03-20 23:37:31 +08:00
committed by GitHub
parent 8603e42a6b
commit 1cd8f55eed
9 changed files with 34 additions and 21 deletions

View File

@@ -4,7 +4,7 @@
"feat": "New Features",
"enhance": "Enhancements",
"bug": "Bug Fixes",
"pref": "Performance Improvements",
"perf": "Performance Improvements",
"changes": "What's Changed",
"sec": "Security fixes",
"deps": "Dependencies",

View File

@@ -0,0 +1,8 @@
---
tauri: 'patch:perf'
tauri-build: 'patch:perf'
tauri-plugin: 'patch:perf'
tauri-utils: 'patch:perf'
---
Don't ship global `bundle.global.js` if `app > withGlobalTauri` is set to false

View File

@@ -432,8 +432,6 @@ pub fn build(out_dir: &Path, target: Target, attributes: &Attributes) -> super::
let capabilities_path = save_capabilities(&capabilities)?;
fs::copy(capabilities_path, out_dir.join(CAPABILITIES_FILE_NAME))?;
tauri_utils::plugin::save_global_api_scripts_paths(out_dir);
let mut permissions_map = inline_plugins_acl.permission_files;
if has_app_manifest {
permissions_map.insert(APP_ACL_KEY.to_string(), app_acl.permission_files);

View File

@@ -511,6 +511,8 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
acl::build(&out_dir, target, &attributes)?;
tauri_utils::plugin::save_global_api_scripts_paths(&out_dir, None);
println!("cargo:rustc-env=TAURI_ENV_TARGET_TRIPLE={target_triple}");
// when running codegen in this build script, we need to access the env var directly
env::set_var("TAURI_ENV_TARGET_TRIPLE", &target_triple);

View File

@@ -144,7 +144,7 @@ impl<'a> Builder<'a> {
}
if let Some(path) = self.global_api_script_path {
tauri_utils::plugin::define_global_api_script_path(path);
tauri_utils::plugin::define_global_api_script_path(&path);
}
mobile::setup(self.android_path, self.ios_path)?;

View File

@@ -19,7 +19,7 @@ mod build {
pub const GLOBAL_API_SCRIPT_FILE_LIST_PATH: &str = "__global-api-script.js";
/// Defines the path to the global API script using Cargo instructions.
pub fn define_global_api_script_path(path: PathBuf) {
pub fn define_global_api_script_path(path: &Path) {
println!(
"cargo:{GLOBAL_API_SCRIPT_PATH_KEY}={}",
path
@@ -31,18 +31,27 @@ mod build {
/// Collects the path of all the global API scripts defined with [`define_global_api_script_path`]
/// and saves them to the out dir with filename [`GLOBAL_API_SCRIPT_FILE_LIST_PATH`].
pub fn save_global_api_scripts_paths(out_dir: &Path) {
///
/// `tauri_global_scripts` is only used in Tauri's monorepo for the examples to work
/// since they don't have a build script to run `tauri-build` and pull in the deps env vars
pub fn save_global_api_scripts_paths(out_dir: &Path, mut tauri_global_scripts: Option<PathBuf>) {
let mut scripts = Vec::new();
for (key, value) in vars_os() {
let key = key.to_string_lossy();
if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {
if key == format!("DEP_TAURI_{GLOBAL_API_SCRIPT_PATH_KEY}") {
tauri_global_scripts = Some(PathBuf::from(value));
} else if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {
let script_path = PathBuf::from(value);
scripts.push(script_path);
}
}
if let Some(tauri_global_scripts) = tauri_global_scripts {
scripts.insert(0, tauri_global_scripts);
}
fs::write(
out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH),
serde_json::to_string(&scripts).expect("failed to serialize global API script paths"),

View File

@@ -337,6 +337,16 @@ fn main() {
}
}
let tauri_global_scripts = PathBuf::from("./scripts/bundle.global.js")
.canonicalize()
.expect("failed to canonicalize tauri global API script path");
tauri_utils::plugin::define_global_api_script_path(&tauri_global_scripts);
// This should usually be done in `tauri-build`,
// but we need to do this here for the examples in this workspace to work as they don't have build scripts
if is_tauri_workspace {
tauri_utils::plugin::save_global_api_scripts_paths(&out_dir, Some(tauri_global_scripts));
}
let permissions = define_permissions(&out_dir);
tauri_utils::acl::build::generate_allowed_commands(&out_dir, permissions).unwrap();
}

View File

@@ -12,6 +12,4 @@
__RAW_core_script__
__RAW_event_initialization_script__
__RAW_bundle_script__
})()

View File

@@ -122,7 +122,6 @@ impl<R: Runtime> WebviewManager<R> {
) -> crate::Result<PendingWebview<EventLoopMessage, R>> {
let app_manager = manager.manager();
let is_init_global = app_manager.config.app.with_global_tauri;
let plugin_init_scripts = app_manager
.plugins
.lock()
@@ -185,7 +184,6 @@ impl<R: Runtime> WebviewManager<R> {
app_manager,
&ipc_init.into_string(),
&pattern_init.into_string(),
is_init_global,
use_https_scheme,
)?
.to_string(),
@@ -346,7 +344,6 @@ impl<R: Runtime> WebviewManager<R> {
app_manager: &AppManager<R>,
ipc_script: &str,
pattern_script: &str,
with_global_tauri: bool,
use_https_scheme: bool,
) -> crate::Result<String> {
#[derive(Template)]
@@ -357,8 +354,6 @@ impl<R: Runtime> WebviewManager<R> {
#[raw]
ipc_script: &'a str,
#[raw]
bundle_script: &'a str,
#[raw]
core_script: &'a str,
#[raw]
event_initialization_script: &'a str,
@@ -374,12 +369,6 @@ impl<R: Runtime> WebviewManager<R> {
invoke_key: &'a str,
}
let bundle_script = if with_global_tauri {
include_str!("../../scripts/bundle.global.js")
} else {
""
};
let freeze_prototype = if app_manager.config.app.security.freeze_prototype {
include_str!("../../scripts/freeze_prototype.js")
} else {
@@ -389,7 +378,6 @@ impl<R: Runtime> WebviewManager<R> {
InitJavascript {
pattern_script,
ipc_script,
bundle_script,
core_script: &CoreJavascript {
os_name: std::env::consts::OS,
protocol_scheme: if use_https_scheme { "https" } else { "http" },