跳到內容
Tauri

設定檔

由於 Tauri 是一個用於建構應用程式的工具包,因此可能有很多檔案需要設定專案設定。您可能會遇到的常見檔案有 tauri.conf.jsonpackage.jsonCargo.toml。我們在此頁面簡要說明每個檔案,以協助您找到要修改的正確方向。

Tauri Config

Tauri 設定用於定義您的 Web 應用程式的來源、描述應用程式的中繼資料、設定 bundle、設定外掛程式設定、透過設定視窗、系統 tray 圖示、選單等修改執行時行為。

此檔案由 Tauri 執行時和 Tauri CLI 使用。您可以定義建置設定(例如在 tauri buildtauri dev 啟動之前執行的命令),設定應用程式的名稱版本控制 Tauri 執行時,以及設定外掛程式

支援的格式

預設的 Tauri 設定格式為 JSON。JSON5 或 TOML 格式可以透過將 config-json5config-toml 功能標誌(分別)新增到 Cargo.toml 中的 tauritauri-build 依賴項來啟用。

Cargo.toml
[build-dependencies]
tauri-build = { version = "2.0.0", features = [ "config-json5" ] }
[dependencies]
tauri = { version = "2.0.0", features = [ "config-json5" ] }

所有格式的結構和值都相同,但是,格式應與各自檔案的格式一致

tauri.conf.json
{
build: {
devUrl: 'http://localhost:3000',
// start the dev server
beforeDevCommand: 'npm run dev',
},
bundle: {
active: true,
icon: ['icons/app.png'],
},
app: {
windows: [
{
title: 'MyApp',
},
],
},
plugins: {
updater: {
pubkey: 'updater pub key',
endpoints: ['https://my.app.updater/{{target}}/{{current_version}}'],
},
},
}
Tauri.toml
[build]
dev-url = "http://localhost:3000"
# start the dev server
before-dev-command = "npm run dev"
[bundle]
active = true
icon = ["icons/app.png"]
[[app.windows]]
title = "MyApp"
[plugins.updater]
pubkey = "updater pub key"
endpoints = ["https://my.app.updater/{{target}}/{{current_version}}"]

請注意,JSON5 和 TOML 支援註解,並且 TOML 可以對更符合語言習慣的設定名稱使用 kebab-case。

平台特定設定

除了預設設定檔之外,Tauri 還可以從以下位置讀取平台特定的設定:

  • tauri.linux.conf.jsonTauri.linux.toml 用於 Linux
  • tauri.windows.conf.jsonTauri.windows.toml 用於 Windows
  • tauri.macos.conf.jsonTauri.macos.toml 用於 macOS
  • tauri.android.conf.jsonTauri.android.toml 用於 Android
  • tauri.ios.conf.jsonTauri.ios.toml 用於 iOS

平台特定的設定檔會依照 JSON Merge Patch (RFC 7396) 規範與主要設定物件合併。

例如,假設以下基本 tauri.conf.json

tauri.conf.json
{
"productName": "MyApp",
"bundle": {
"resources": ["./resources"]
},
"plugins": {
"deep-link": {}
}
}

以及給定的 tauri.linux.conf.json

tauri.linux.conf.json
{
"productName": "my-app",
"bundle": {
"resources": ["./linux-assets"]
},
"plugins": {
"cli": {
"description": "My app",
"subcommands": {
"update": {}
}
},
"deep-link": {}
}
}

Linux 的已解析設定將是以下物件

{
"productName": "my-app",
"bundle": {
"resources": ["./linux-assets"]
},
"plugins": {
"cli": {
"description": "My app",
"subcommands": {
"update": {}
}
},
"deep-link": {}
}
}

此外,您可以透過 CLI 提供要合併的設定,請參閱以下章節以取得更多資訊。

擴充設定

當執行 devandroid devios devbuildandroid buildios buildbundle 命令之一時,Tauri CLI 允許您擴充 Tauri 設定。設定擴充可以由 --config 引數以原始 JSON 字串或 JSON 檔案路徑的形式提供。Tauri 使用 JSON Merge Patch (RFC 7396) 規範將提供的設定值與原始解析的設定物件合併。

此機制可用於定義應用程式的多種風味,或在設定應用程式 bundle 時具有更大的彈性。

例如,若要發佈完全隔離的beta應用程式,您可以使用此功能來設定個別的應用程式名稱和識別碼

src-tauri/tauri.beta.conf.json
{
"productName": "My App Beta",
"identifier": "com.myorg.myappbeta"
}

若要發佈此個別的 beta 應用程式,您可以在建置時提供此設定檔

npm run tauri build -- --config src-tauri/tauri.beta.conf.json

Cargo.toml

Cargo 的 manifest 檔案用於宣告您的應用程式依賴的 Rust crate、關於您的應用程式的中繼資料,以及其他與 Rust 相關的功能。如果您不打算為您的應用程式使用 Rust 進行後端開發,那麼您可能不會經常修改它,但重要的是要知道它的存在以及它的作用。

以下是 Tauri 專案的基本 Cargo.toml 檔案範例

Cargo.toml
[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
edition = "2021"
rust-version = "1.57"
[build-dependencies]
tauri-build = { version = "2.0.0" }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "2.0.0", features = [ ] }

最需要注意的部分是 tauri-buildtauri 依賴項。通常,它們都必須與 Tauri CLI 位於最新的次要版本上,但這並非嚴格要求。如果您在嘗試執行應用程式時遇到問題,您應該檢查任何 Tauri 版本(tauritauri-cli)是否位於其各自次要版本的最新版本。

Cargo 版本號碼使用語意化版本控制。在 src-tauri 資料夾中執行 cargo update 將會提取所有依賴項的最新可用 Semver 相容版本。例如,如果您將 2.0.0 指定為 tauri-build 的版本,Cargo 將會偵測並下載版本 2.0.0.0,因為它是最新可用的 Semver 相容版本。每當引入重大變更時,Tauri 都會更新主要版本號碼,這表示您應該始終能夠安全地升級到最新的次要版本和修補程式版本,而不用擔心程式碼會中斷。

如果您想要使用特定的 crate 版本,您可以透過在依賴項的版本號碼前面加上 = 來改用精確的版本

tauri-build = { version = "=2.0.0" }

另一個需要注意的事情是 tauri 依賴項的 features=[] 部分。執行 tauri devtauri build 將會根據您的 Tauri 設定自動管理需要在您的專案中啟用的功能。有關 tauri 功能標誌的更多資訊,請參閱文件

當您建置應用程式時,會產生 Cargo.lock 檔案。此檔案主要用於確保在開發期間跨機器使用相同的依賴項(類似於 Node.js 中的 yarn.lockpnpm-lock.yamlpackage-lock.json)。建議將此檔案提交到您的原始碼儲存庫,以便您獲得一致的建置。

若要瞭解有關 Cargo manifest 檔案的更多資訊,請參閱官方文件

package.json

這是 Node.js 使用的套件檔案。如果您的 Tauri 應用程式的前端是使用基於 Node.js 的技術(例如 npmyarnpnpm)開發的,則此檔案用於設定前端依賴項和腳本。

Tauri 專案的基本 package.json 檔案範例可能如下所示

package.json
{
"scripts": {
"dev": "command to start your app development mode",
"build": "command to build your app frontend",
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "^2.0.0.0",
"@tauri-apps/cli": "^2.0.0.0"
}
}

通常使用 "scripts" 區段來儲存用於啟動和建置 Tauri 應用程式使用的前端的命令。上面的 package.json 檔案指定了 dev 命令,您可以使用 yarn devnpm run dev 執行該命令來啟動前端框架,以及 build 命令,您可以使用 yarn buildnpm run build 執行該命令來建置前端的 Web 資產,以便 Tauri 在生產環境中新增。使用這些腳本最方便的方法是透過 Tauri 設定的 beforeDevCommandbeforeBuildCommand hook 將它們與 Tauri CLI 掛鉤

tauri.conf.json
{
"build": {
"beforeDevCommand": "yarn dev",
"beforeBuildCommand": "yarn build"
}
}

dependencies 物件指定當您執行 yarnpnpm installnpm install 時,Node.js 應下載哪些依賴項(在本例中為 Tauri CLI 和 API)。

除了 package.json 檔案之外,您可能會看到 yarn.lockpnpm-lock.yamlpackage-lock.json 檔案。這些檔案有助於確保當您稍後下載依賴項時,您將獲得與開發期間使用的完全相同的版本(類似於 Rust 中的 Cargo.lock)。

若要瞭解有關 package.json 檔案格式的更多資訊,請參閱官方文件


© 2025 Tauri Contributors。CC-BY / MIT