mcpField
C
configuring-tauri-apps

configuring-tauri-apps

github.com/dchuk开发36173 次安装40 周前更新
代码仓库

关于 configuring-tauri-apps

面向 Tauri v2 开发者的配置参考,介绍主要配置文件、平台与环境覆盖项,以及常见应用设置。

主要功能

  1. 01Tauri 配置文件用途
  2. 02窗口与安全设置
  3. 03平台专属配置覆盖
  4. 04开发与发布环境配置
  5. 05Cargo 与 package.json 设置
  6. 06配置验证

适用场景

  1. 01设置窗口行为和应用元数据
  2. 02区分开发与生产配置
  3. 03按平台覆盖配置
  4. 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.jsonTauri 专属设置JSON、JSON5 或 TOML
Cargo.tomlRust 依赖项和元数据TOML
package.json前端依赖项和脚本JSON

tauri.conf.json

位于 src-tauri/ 中的主配置文件。用于定义应用元数据、窗口行为、打包选项和插件设置。

支持的格式
  • JSON(默认):tauri.conf.json
  • JSON5:tauri.conf.json5(需要 config-json5 Cargo 功能)
  • TOML:Tauri.toml(需要 config-toml Cargo 功能)
完整配置结构
{
  "$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}}"]
    }
  }
}
根级字段
字段类型必填描述
productNamestring否应用显示名称
versionstring否Semver 版本或 package.json 的路径
identifierstring是反向域名标识符(例如:com.tauri.example)
mainBinaryNamestring否覆盖主二进制文件名
构建配置字段
字段类型描述
devUrlstring用于热重载的开发服务器 URL
frontendDiststring已构建前端资源的路径或远程 URL
beforeDevCommandstring在 tauri dev 之前运行的脚本
beforeBuildCommandstring在 tauri build 之前运行的脚本
featuresstring[]构建时启用的 Cargo 功能
removeUnusedCommandsboolean从二进制文件中移除未使用的插件命令
窗口配置选项
字段类型默认值描述
titlestring"Tauri"窗口标题
width / heightnumber800 / 600窗口尺寸(像素)
minWidth / minHeightnumber-最小尺寸
maxWidth / maxHeightnumber-最大尺寸
x / ynumber-窗口位置
resizablebooleantrue允许调整窗口大小
fullscreenbooleanfalse启动时进入全屏
centerbooleanfalse将窗口置于屏幕中央
visiblebooleantrue启动时窗口是否可见
decorationsbooleantrue显示窗口装饰
transparentbooleanfalse启用窗口透明效果
alwaysOnTopbooleanfalse将窗口保持在其他窗口上方
urlstring"index.html"要加载的初始 URL
安全配置
字段类型描述
capabilitiesstring[]应用的权限能力
assetProtocol.enableboolean启用自定义资源协议
assetProtocol.scopestring[]资源协议允许的路径
pattern.usestring安全模式(默认值为 "brownfield")
freezePrototypeboolean防止修改原型
各平台的打包目标
平台目标
Windowsnsis、msi
macOSapp、dmg
Linuxappimage、deb、rpm
Androidapk、aab
iOSapp

平台专属配置

创建平台专属文件,通过 JSON Merge Patch(RFC 7396)覆盖基础配置。

平台文件名
Linuxtauri.linux.conf.json
Windowstauri.windows.conf.json
macOStauri.macos.conf.json
Androidtauri.android.conf.json
iOStauri.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