應用程式大小
雖然 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 展開來縮減尺寸。
- strip: 從二進位檔案中剝離符號或 debuginfo。
- rpath: 透過將資訊硬編碼到二進位檔案中,協助尋找二進位檔案所需的動態函式庫。
- trim-paths: 從二進位檔案中移除潛在的敏感資訊。
- rustflags: 根據不同的 profile 設定 Rust 編譯器旗標。
-Cdebuginfo=0
: 是否應在建置中包含除錯資訊符號。-Zthreads=8
: 增加編譯期間使用的執行緒數量。
© 2025 Tauri 貢獻者。CC-BY / MIT