应用大小
虽然 Tauri 默认提供非常小的二进制文件,但稍微突破一下限制也无妨,因此这里有一些技巧和窍门可以帮助你达到最佳效果。
🌐 While Tauri by default provides very small binaries it doesn’t hurt to push the limits a bit, so here are some tips and tricks for reaching optimal results.
🌐 Cargo Configuration
你可以对项目进行的最简单的前端无关大小改进之一是向其添加 Cargo 配置文件。
🌐 One of the simplest frontend agnostic size improvements you can do to your project is adding a Cargo profile to it.
根据你使用的是稳定版还是 nightly 版 Rust 工具链,可用的选项会有一些不同。除非你是高级用户,否则建议你坚持使用稳定版工具链。
🌐 Dependent on whether you use the stable or nightly Rust toolchain the options available to you differ a bit. It’s recommended you stick to the stable toolchain unless you’re an advanced user.
[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.🌐 References
- 增量: 将你的二进制文件分步骤编译。
- codegen-units: 以编译时间优化为代价加快编译速度。
- lto: 启用链接时优化。
- opt-level: 决定编译器的关注重点。使用
3优化性能,使用z优化大小,使用s优化介于两者之间的内容。 - panic: 通过移除 panic 展开减小大小。
- strip: 从二进制文件中剥离符号或调试信息。
- rpath: 通过将信息硬编码到二进制文件中,帮助查找二进制文件所需的动态库。
- trim-paths: 从二进制文件中移除可能的敏感信息。
- rustflags: 按配置文件设置 Rust 编译器标志。
-Cdebuginfo=0:是否应在构建中包含调试信息符号。-Zthreads=8:增加编译过程中使用的线程数。
🌐 Remove Unused Commands
在拉取请求 feat: add a new option to remove unused commands 中,我们在 tauri 配置文件中添加了一个新选项
🌐 In Pull Request feat: add a new option to remove unused commands, we added in a new option in the tauri config file
{ "build": { "removeUnusedCommands": true }}删除功能文件 (ACL) 中从未允许的命令,这样你就不必为不使用的内容付费
🌐 to remove commands that’re never allowed in your capability files (ACL), so you don’t have to pay for what you don’t use
它在底层是如何工作的?
tauri-cli 将通过一个环境变量与 tauri-build 以及 tauri、tauri-plugin 的构建脚本进行通信,并让它们从 ACL 中生成允许的命令列表,然后 generate_handler 宏将根据该列表移除未使用的命令
一个内部细节是这个环境变量当前是 REMOVE_UNUSED_COMMANDS,它被设置为项目的目录,通常是 src-tauri 目录,它用于构建脚本查找功能文件,虽然不推荐,但如果你无法或不想使用 tauri-cli 来使其工作,你仍然可以自己设置这个环境变量(请注意,由于这是实现细节,我们不保证它的稳定性)
🌐 An internal detail is this environment variable is currently REMOVE_UNUSED_COMMANDS,
and it’s set to project’s directory, usually the src-tauri directory, this is used for the build scripts to find the capability files,
and although it’s not encouraged, you can still set this environment variable yourself if you can’t or don’t want to use tauri-cli to get this to work
(do note that as this is an implementation detail, we don’t guarantee the stability of it)
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站