4.4 配置文件详解
原文链接: Configuration Files
4.4 配置文件详解
Tauri 工程里最常见的三个配置文件是:
tauri.conf.json:Tauri 自己的配置;Cargo.toml:Rust 依赖与 crate 配置;package.json:前端依赖与脚本。
tauri.conf.json 是“总配置”
一个典型的 v2 配置长这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| {
"$schema": "../gen/schemas/desktop-schema.json",
"productName": "my-tauri-app",
"version": "0.1.0",
"identifier": "com.example.mytauriapp",
"build": {
"beforeDevCommand": "npm run dev",
"devUrl": "http://localhost:5173",
"beforeBuildCommand": "npm run build",
"frontendDist": "../dist"
},
"app": {
"windows": [
{
"title": "我的 Tauri 应用",
"width": 1000,
"height": 700
}
],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
},
"plugins": {}
}
|
各顶层字段的职责:
| 字段 | 作用 |
|---|
productName | 安装包/应用显示的产品名 |
identifier | 应用唯一标识,建议 com.你的域名.应用名 |
version | 应用版本,会同步到各平台 |
build | 前端命令、devUrl、前端构建目录 |
app | 窗口、安全、托盘等运行时配置 |
bundle | 打包图标、目标格式、资源 |
plugins | 各插件的配置 |
$schema 指向 Tauri 生成的 JSON Schema,能让编辑器提供自动补全。该文件位于 src-tauri/gen/schemas/,随构建生成。
JSON5 / TOML 也可以
默认使用 JSON。如果想要注释,可以启用 JSON5 或 TOML。方法是在 Cargo.toml 中给 tauri 与 tauri-build 加 feature:
1
2
3
4
5
| [build-dependencies]
tauri-build = { version = "2", features = ["config-json5"] }
[dependencies]
tauri = { version = "2", features = ["config-json5"] }
|
此时可把 tauri.conf.json 改名/换成 tauri.conf.json5,并写注释:
{
productName: "my-tauri-app",
build: {
devUrl: "http://localhost:5173",
// 启动前端开发服务器
beforeDevCommand: "npm run dev",
},
}
平台专属配置
Tauri 支持按平台覆盖配置:
tauri.linux.conf.jsontauri.windows.conf.jsontauri.macos.conf.jsontauri.android.conf.jsontauri.ios.conf.json
平台文件与主文件按 JSON Merge Patch 合并。注意:对象是逐键合并,数组是整体替换。
例如主配置:
1
2
3
4
5
6
| {
"productName": "MyApp",
"bundle": {
"resources": ["./resources", "./shared-assets"]
}
}
|
tauri.windows.conf.json:
1
2
3
4
5
6
| {
"productName": "MyApp-Windows",
"bundle": {
"resources": ["./windows-assets"]
}
}
|
Windows 构建时,bundle.resources 只会包含 ./windows-assets,而不会自动保留主配置里的两个数组项。
Cargo.toml 的职责
Cargo.toml 声明 Rust crate 与依赖。第一次编译后会出现 Cargo.lock,它锁定精确版本,应提交到 Git。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| [package]
name = "my-tauri-app"
version = "0.1.0"
edition = "2021"
rust-version = "1.90"
[lib]
name = "app_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = [] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
|
注意几点:
[lib] crate-type 包含 staticlib 和 cdylib,是为了让 Rust 代码能被 Android/iOS 原生壳链接;tauri 和 tauri-build 的大版本应尽量与 CLI 保持一致;tauri dev/build 会根据配置自动管理部分 Cargo features。
package.json 的职责
前端部分与普通 Web 项目没有区别:
1
2
3
4
5
6
7
8
9
10
11
| {
"scripts": {
"dev": "vite",
"build": "vite build",
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "^2",
"@tauri-apps/cli": "^2"
}
}
|
"tauri": "tauri" 这一行让 npm 项目能执行 npm run tauri dev。
下一步
配置文件决定了“有什么”,权限系统决定“谁能用什么”。请阅读 4.5 权限系统与安全边界。