Skip to content
Tauri 中文网

应用大小

虽然 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 配置

¥Cargo Configuration

你可以对项目进行的最简单的前端无关大小改进之一是向其添加 Cargo 配置文件。

¥One of the simplest frontend agnostic size improvements you can do to your project is adding a Cargo profile to it.

根据你使用的是稳定版还是夜间版 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.

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.

参考

¥References

:::note 注意

这不是对所有可用选项的完整引用,仅仅是我们想特别引起注意的选项。

¥This is not a complete reference over all available options, merely the ones that we’d like to draw extra attention to.

:::

  • 增量: 以较小的步骤编译二进制文件。

    ¥incremental: Compile your binary in smaller steps.

  • codegen-units: 以编译时优化为代价加快编译时间。

    ¥codegen-units: Speeds up compile times at the cost of compile time optimizations.

  • lto: 启用链接时间优化。

    ¥lto: Enables link time optimizations.

  • opt-level: 确定编译器的焦点。使用 3 优化性能,使用 z 优化大小,使用 s 进行中间优化。

    ¥opt-level: Determines the focus of the compiler. Use 3 to optimize performance, z to optimize for size, and s for something in-between.

  • 恐慌: 通过消除恐慌展开来减小大小。

    ¥panic: Reduce size by removing panic unwinding.

  • strip: 从二进制文件中剥离符号或调试信息。

    ¥strip: Strip either symbols or debuginfo from a binary.

  • rpath: 通过将信息硬编码到二进制文件中,帮助查找二进制文件所需的动态库。

    ¥rpath: Assists in finding the dynamic libraries the binary requires by hard coding information into the binary.

  • trim-paths: 从二进制文件中删除潜在的特权信息。

    ¥trim-paths: Removes potentially privileged information from binaries.

  • rustflags: 根据配置文件设置 Rust 编译器标志。

    ¥rustflags: Sets Rust compiler flags on a profile by profile basis.

    • -Cdebuginfo=0:是否应在构建中包含调试信息符号。

      ¥-Cdebuginfo=0: Whether debuginfo symbols should be included in the build.

    • -Zthreads=8:增加编译期间使用的线程数。

      ¥-Zthreads=8: Increases the number of threads used during compilation.

删除未使用的命令

¥Remove Unused Commands

在 Pull Request 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

tauri.conf.json
{
"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

:::tip 提示

为了最大限度地发挥这一点的好处,只包含你在 ACL 中使用的命令,而不是使用 defaults

¥To maximize the benefit of this, only include commands that you use in the ACL instead of using defaultss

:::

:::note 注意

此功能需要 tauri@2.4tauri-build@2.1tauri-plugin@2.1tauri-cli@2.4

¥This feature requires tauri@2.4, tauri-build@2.1, tauri-plugin@2.1 and tauri-cli@2.4

:::

:::note 注意

这不会考虑运行时动态添加的 ACL,因此请确保在使用时检查它

¥This won’t be accounting for dynamically added ACLs at runtime so make sure to check it when using this

:::

How does it work under the hood?

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

¥tauri-cli will communicate with tauri-build and the build script of tauri, tauri-plugin through an environment variable and let them generate a list of allowed commands from the ACL, this will then be used by the generate_handler macro to remove unused commands based on that

内部细节是这个环境变量当前是 REMOVE_UNUSED_COMMANDS,它被设置为项目的目录,通常是 src-tauri 目录,这用于构建脚本来查找功能文件,虽然不鼓励这样做,但如果你不能或不想使用 tauri-cli 来使其工作,你仍然可以自己设置这个环境变量(请注意,由于这是一个实现细节,我们不保证它的稳定性)

¥An internal detial 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 v2.4 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站