关于 configuring-tauri-apps
面向 Tauri v2 开发者的配置参考,介绍主要配置文件、平台与环境覆盖项,以及常见应用设置。
主要功能
- 01Tauri 配置文件用途
- 02窗口与安全设置
- 03平台专属配置覆盖
- 04开发与发布环境配置
- 05Cargo 与 package.json 设置
- 06配置验证
适用场景
- 01设置窗口行为和应用元数据
- 02区分开发与生产配置
- 03按平台覆盖配置
- 04检查配置与构建环境
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ dchuk/ claude-code-tauri-skills --skill configuring-tauri-apps安装后在 Claude Code 中输入/configuring-tauri-apps确认已加载。
详细介绍
Tauri 配置文件
Tauri v2 应用使用三个主要配置文件来管理应用行为、依赖项和构建流程。
配置文件概览
| 文件 | 用途 | 格式 |
|---|---|---|
tauri.conf.json | Tauri 专属设置 | JSON、JSON5 或 TOML |
Cargo.toml | Rust 依赖项和元数据 | TOML |
package.json | 前端依赖项和脚本 | JSON |
tauri.conf.json
位于 src-tauri/ 中的主配置文件。用于定义应用元数据、窗口行为、打包选项和插件设置。
支持的格式
- JSON(默认):
tauri.conf.json - JSON5:
tauri.conf.json5(需要config-json5Cargo 功能) - TOML:
Tauri.toml(需要config-tomlCargo 功能)
完整配置结构
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "MyApp",
"version": "1.0.0",
"identifier": "com.company.myapp",
"mainBinaryName": "my-app",
"build": {
"devUrl": "http://localhost:3000",
"frontendDist": "../dist",
"beforeDevCommand": "npm run dev",
"beforeBuildCommand": "npm run build",
"features": ["custom-feature"],
"removeUnusedCommands": true
},
"app": {
"withGlobalTauri": false,
"macOSPrivateApi": false,
"windows": [
{
"title": "My Application",
"width": 1200,
"height": 800,
"minWidth": 800,
"minHeight": 600,
"resizable": true,
"fullscreen": false,
"center": true,
"visible": true,
"decorations": true,
"transparent": false,
"alwaysOnTop": false,
"focus": true,
"url": "index.html"
}
],
"security": {
"capabilities": [],
"assetProtocol": {
"enable": true,
"scope": ["$APPDATA/**"]
},
"pattern": { "use": "brownfield" },
"freezePrototype": false
},
"trayIcon": {
"id": "main-tray",
"iconPath": "icons/tray.png",
"iconAsTemplate": true
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": ["icons/32x32.png", "icons/128x128.png", "icons/icon.icns", "icons/icon.ico"],
"resources": ["assets/**/*"],
"copyright": "Copyright 2024",
"category": "Utility",
"shortDescription": "A short app description",
"longDescription": "A longer description",
"licenseFile": "../LICENSE",
"windows": {
"certificateThumbprint": null,
"timestampUrl": "http://timestamp.digicert.com",
"nsis": { "license": "../LICENSE", "installerIcon": "icons/icon.ico", "installMode": "currentUser" }
},
"macOS": {
"minimumSystemVersion": "10.13",
"signingIdentity": null,
"dmg": { "appPosition": { "x": 180, "y": 170 }, "applicationFolderPosition": { "x": 480, "y": 170 } }
},
"linux": {
"appimage": { "bundleMediaFramework": false },
"deb": { "depends": ["libwebkit2gtk-4.1-0"] },
"rpm": { "depends": ["webkit2gtk4.1"] }
},
"android": { "minSdkVersion": 24 },
"iOS": { "minimumSystemVersion": "13.0" }
},
"plugins": {
"updater": {
"pubkey": "YOUR_PUBLIC_KEY",
"endpoints": ["https://releases.example.com/{{target}}/{{arch}}/{{current_version}}"]
}
}
}
根级字段
| 字段 | 类型 | 必填 | 描述 |
|---|---|---|---|
productName | string | 否 | 应用显示名称 |
version | string | 否 | Semver 版本或 package.json 的路径 |
identifier | string | 是 | 反向域名标识符(例如:com.tauri.example) |
mainBinaryName | string | 否 | 覆盖主二进制文件名 |
构建配置字段
| 字段 | 类型 | 描述 |
|---|---|---|
devUrl | string | 用于热重载的开发服务器 URL |
frontendDist | string | 已构建前端资源的路径或远程 URL |
beforeDevCommand | string | 在 tauri dev 之前运行的脚本 |
beforeBuildCommand | string | 在 tauri build 之前运行的脚本 |
features | string[] | 构建时启用的 Cargo 功能 |
removeUnusedCommands | boolean | 从二进制文件中移除未使用的插件命令 |
窗口配置选项
| 字段 | 类型 | 默认值 | 描述 |
|---|---|---|---|
title | string | "Tauri" | 窗口标题 |
width / height | number | 800 / 600 | 窗口尺寸(像素) |
minWidth / minHeight | number | - | 最小尺寸 |
maxWidth / maxHeight | number | - | 最大尺寸 |
x / y | number | - | 窗口位置 |
resizable | boolean | true | 允许调整窗口大小 |
fullscreen | boolean | false | 启动时进入全屏 |
center | boolean | false | 将窗口置于屏幕中央 |
visible | boolean | true | 启动时窗口是否可见 |
decorations | boolean | true | 显示窗口装饰 |
transparent | boolean | false | 启用窗口透明效果 |
alwaysOnTop | boolean | false | 将窗口保持在其他窗口上方 |
url | string | "index.html" | 要加载的初始 URL |
安全配置
| 字段 | 类型 | 描述 |
|---|---|---|
capabilities | string[] | 应用的权限能力 |
assetProtocol.enable | boolean | 启用自定义资源协议 |
assetProtocol.scope | string[] | 资源协议允许的路径 |
pattern.use | string | 安全模式(默认值为 "brownfield") |
freezePrototype | boolean | 防止修改原型 |
各平台的打包目标
| 平台 | 目标 |
|---|---|
| Windows | nsis、msi |
| macOS | app、dmg |
| Linux | appimage、deb、rpm |
| Android | apk、aab |
| iOS | app |
平台专属配置
创建平台专属文件,通过 JSON Merge Patch(RFC 7396)覆盖基础配置。
| 平台 | 文件名 |
|---|---|
| Linux | tauri.linux.conf.json |
| Windows | tauri.windows.conf.json |
| macOS | tauri.macos.conf.json |
| Android | tauri.android.conf.json |
| iOS | tauri.ios.conf.json |
src-tauri/tauri.windows.conf.json 示例:
{
"app": {
"windows": [{ "title": "My App - Windows Edition" }]
},
"bundle": {
"windows": { "nsis": { "installMode": "perMachine" } }
}
}
src-tauri/tauri.macos.conf.json 示例:
{
"app": { "macOSPrivateApi": true },
"bundle": {
"macOS": { "minimumSystemVersion": "11.0", "entitlements": "entitlements.plist" }
}
}
CLI 配置覆盖
# Development with custom config
tauri dev --config src-tauri/tauri.dev.conf.json
# Build with beta configuration
tauri build --config src-tauri/tauri.beta.conf.json
# Inline configuration override
tauri build --config '{"bundle":{"identifier":"com.company.myapp.beta"}}'
Cargo.toml 配置
位于 src-tauri/Cargo.toml,用于管理 Rust 依赖项。
[package]
name = "my-app"
version = "1.0.0"
edition = "2021"
[build-dependencies]
tauri-build = { version = "2.0", features = [] }
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri = { version = "2.0", features = [] }
tauri-plugin-shell = "2.0"
tauri-plugin-opener = "2.0"
[features]
default = ["custom-protocol"]
custom-protocol = ["tauri/custom-protocol"]
常见 Tauri 功能
[dependencies]
tauri = { version = "2.0", features = [
"config-json5", # Enable JSON5 config format
"config-toml", # Enable TOML config format
"devtools", # Enable WebView devtools
"macos-private-api", # Enable macOS private APIs
"tray-icon", # Enable system tray support
"image-png", # PNG image support
"image-ico", # ICO image support
"protocol-asset" # Custom asset protocol
] }
版本管理
tauri = { version = "2.0" } # Semver-compatible (recommended)
tauri = { version = "=2.0.0" } # Exact version
tauri = { version = "~2.0.0" } # Patch updates only
package.json 集成
{
"name": "my-tauri-app",
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"tauri": "tauri"
},
"dependencies": { "@tauri-apps/api": "^2.0.0" },
"devDependencies": { "@tauri-apps/cli": "^2.0.0" }
}
特定环境的配置
开发环境(src-tauri/tauri.dev.conf.json)
{
"build": { "devUrl": "http://localhost:5173" },
"app": {
"withGlobalTauri": true,
"windows": [{ "title": "My App [DEV]" }]
}
}
生产环境(src-tauri/tauri.prod.conf.json)
{
"build": { "frontendDist": "../dist", "removeUnusedCommands": true },
"bundle": { "active": true, "targets": "all" }
}
Beta 版(src-tauri/tauri.beta.conf.json)
{
"identifier": "com.company.myapp.beta",
"productName": "MyApp Beta",
"plugins": {
"updater": {
"endpoints": ["https://beta-releases.example.com/{{target}}/{{arch}}/{{current_version}}"]
}
}
}
TOML 配置格式
使用 Tauri.toml 时,配置采用 kebab-case:
[build]
dev-url = "http://localhost:3000"
before-dev-command = "npm run dev"
before-build-command = "npm run build"
[app]
with-global-tauri = false
[[app.windows]]
title = "My Application"
width = 1200
height = 800
resizable = true
center = true
[app.security]
freeze-prototype = false
[app.security.asset-protocol]
enable = true
scope = ["$APPDATA/**"]
[bundle]
active = true
targets = "all"
icon = ["icons/32x32.png", "icons/128x128.png", "icons/icon.icns", "icons/icon.ico"]
[plugins.updater]
pubkey = "YOUR_PUBLIC_KEY"
endpoints = ["https://releases.example.com/{{target}}/{{arch}}/{{current_version}}"]
常见配置模式
多窗口应用
{
"app": {
"windows": [
{ "label": "main", "title": "Main Window", "width": 1200, "height": 800, "url": "index.html" },
{ "label": "settings", "title": "Settings", "width": 600, "height": 400, "url": "settings.html", "visible": false }
]
}
}
系统托盘应用
{
"app": {
"trayIcon": { "id": "main-tray", "iconPath": "icons/tray.png", "iconAsTemplate": true },
"windows": [{ "visible": false }]
}
}
插件配置
{
"plugins": {
"updater": {
"pubkey": "YOUR_PUBLIC_KEY",
"endpoints": ["https://releases.example.com/{{target}}/{{arch}}/{{current_version}}"],
"windows": { "installMode": "passive" }
},
"shell": {
"open": true,
"scope": [{ "name": "open-url", "cmd": "open", "args": [{ "validator": "\\S+" }] }]
},
"deep-link": {
"mobile": ["myapp"],
"desktop": { "schemes": ["myapp"] }
}
}
}
锁定文件
提交锁定文件,以确保构建可复现:
| 文件 | 用途 |
|---|---|
Cargo.lock | 锁定 Rust 依赖版本 |
package-lock.json | 锁定 npm 依赖版本 |
yarn.lock | 锁定 Yarn 依赖版本 |
pnpm-lock.yaml | 锁定 pnpm 依赖版本 |
配置验证
使用 JSON schema 实现编辑器自动补全:
{ "$schema": "https://schema.tauri.app/config/2" }
运行 tauri info,验证配置和环境设置。
评论
支持 Markdown