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.


Tauri 中文网 - 粤ICP备13048890号