应用体积
虽然 Tauri 默认提供非常小的二进制文件,但稍微突破一下限制也无妨,因此这里有一些技巧和窍门,可帮助您获得最佳结果。
Cargo 配置
您可以对项目进行的最简单的前端无关大小改进之一是向其添加 Cargo profile。
根据您使用的是 stable 还是 nightly Rust 工具链,您可用的选项略有不同。建议您坚持使用 stable 工具链,除非您是高级用户。
[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.
[profile.dev]incremental = true # Compile your binary in smaller steps.rustflags = ["-Zthreads=8"] # Better compile performance.
[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.trim-paths = "all" # Removes potentially privileged information from your binaries.rustflags = ["-Cdebuginfo=0", "-Zthreads=8"] # Better compile performance.
参考
- incremental: 以更小的步骤编译您的二进制文件。
- codegen-units: 以牺牲编译时优化为代价来加快编译时间。
- lto: 启用链接时优化。
- opt-level: 确定编译器的重点。使用
3
优化性能,使用z
优化大小,使用s
优化两者之间。 - panic: 通过移除 panic unwind 来减小大小。
- strip: 从二进制文件中剥离符号或 debuginfo。
- rpath: 通过将信息硬编码到二进制文件中,协助查找二进制文件所需的动态库。
- trim-paths: 从二进制文件中移除潜在的特权信息。
- rustflags: 在 profile 的基础上设置 Rust 编译器标志。
-Cdebuginfo=0
: 是否应在构建中包含 debuginfo 符号。-Zthreads=8
: 增加编译期间使用的线程数。
移除未使用的命令
在 Pull Request feat: add a new option to remove unused commands
中,我们在 tauri 配置文件中添加了一个新选项
{ "build": { "removeUnusedCommands": true }}
以移除在您的能力文件 (ACL) 中永远不允许的命令,这样您就不必为不使用的东西付费
它在底层是如何工作的?
tauri-cli
将通过环境变量与 tauri-build
和 tauri
、tauri-plugin
的构建脚本通信,并让他们从 ACL 生成允许的命令列表,然后 generate_handler
宏将使用该列表来移除未使用的命令
一个内部细节是此环境变量当前为 REMOVE_UNUSED_COMMANDS
,它被设置为项目的目录,通常是 src-tauri
目录,这用于构建脚本查找能力文件,虽然不鼓励这样做,但如果您无法或不想使用 tauri-cli
来使其工作,您仍然可以自己设置此环境变量(请注意,由于这是一个实现细节,我们不保证其稳定性)
© 2025 Tauri 贡献者。CC-BY / MIT