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 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.
:::note
This is not a complete reference over all available options, merely the ones that we'd like to draw extra attention to.
:::
- [incremental:](https://doc.rust-lang.org/cargo/reference/profiles.html#incremental) Compile your binary in smaller steps.
- [codegen-units:](https://doc.rust-lang.org/cargo/reference/profiles.html#codegen-units) Speeds up compile times at the cost of compile time optimizations.
- [lto:](https://doc.rust-lang.org/cargo/reference/profiles.html#lto) Enables link time optimizations.
- [opt-level:](https://doc.rust-lang.org/cargo/reference/profiles.html#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:](https://doc.rust-lang.org/cargo/reference/profiles.html#panic) Reduce size by removing panic unwinding.
- [strip:](https://doc.rust-lang.org/cargo/reference/profiles.html#strip) Strip either symbols or debuginfo from a binary.
- [rpath:](https://doc.rust-lang.org/cargo/reference/profiles.html#rpath) Assists in finding the dynamic libraries the binary requires by hard coding information into the binary.
- [trim-paths:](https://rust-lang.github.io/rfcs/3127-trim-paths.html) Removes potentially privileged information from binaries.
- [rustflags:](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-rustflags-option) Sets Rust compiler flags on a profile by profile basis.
- `-Cdebuginfo=0`: Whether debuginfo symbols should be included in the build.
- `-Zthreads=8`: Increases the number of threads used during compilation.
## Remove Unused Commands
In Pull Request [`feat: add a new option to remove unused commands`](https://github.com/tauri-apps/tauri/pull/12890), we added in a new option in the tauri config file
```json title=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 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 v2.8 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站