跳到内容
Tauri

应用大小

虽然 Tauri 默认提供非常小的二进制文件,但进一步优化也无妨。以下是一些实现最佳效果的技巧和窍门。

您可以对项目进行最简单的、与前端无关的大小改进,就是为其添加 Cargo 配置。

取决于您使用的是稳定版还是每夜版 Rust 工具链,可用的选项略有不同。除非您是高级用户,否则建议您坚持使用稳定版工具链。

src-tauri/Cargo.toml
[profile.dev]
incremental = true # Compile your binary in smaller steps.
[profile.release]
codegen-units = 1 # Allows LLVM to perform better optimization.
lto = true # Enables link-time-optimizations.
opt-level = "s" # Prioritizes small binary size. Use `3` if you prefer speed.
panic = "abort" # Higher performance by disabling panic handlers.
strip = true # Ensures debug symbols are removed.
  • incremental: 分为小步编译二进制文件。
  • codegen-units: 以编译时优化为代价,加快编译速度。
  • lto: 启用链接时优化。
  • opt-level: 决定编译器的优化重点。使用 3 优化性能,z 优化大小,s 则介于两者之间。
  • panic: 通过移除恐慌展开来减小大小。
  • strip: 从二进制文件中剥离符号或调试信息。
  • rpath: 通过将信息硬编码到二进制文件中来帮助查找二进制文件所需的动态库。
  • trim-paths: 从二进制文件中移除潜在的特权信息。
  • rustflags: 基于配置文件设置 Rust 编译器标志。
    • -Cdebuginfo=0: 调试信息符号是否应包含在构建中。
    • -Zthreads=8: 增加编译过程中使用的线程数。

在拉取请求 feat: add a new option to remove unused commands 中,我们在 Tauri 配置文件中添加了一个新选项

tauri.conf.json
{
"build": {
"removeUnusedCommands": true
}
}

以移除在您的功能文件(ACL)中从未允许的命令,这样您就不必为不使用的功能付出代价。

它在幕后是如何工作的?

tauri-cli 将通过环境变量与 tauri-build 以及 tauritauri-plugin 的构建脚本进行通信,并让它们从 ACL 生成一个允许的命令列表。然后 generate_handler 宏将根据此列表移除未使用的命令。

一个内部细节是,此环境变量目前是 REMOVE_UNUSED_COMMANDS,并且设置为项目的目录,通常是 src-tauri 目录。这用于构建脚本查找功能文件。尽管不鼓励,但如果您无法或不想使用 tauri-cli 来实现此功能,您仍然可以自行设置此环境变量(**请注意,由于这是实现细节,我们不保证其稳定性**)。


© 2025 Tauri 贡献者。CC-BY / MIT