07
下面是一个 基于 Tauri 2.0 的 tauri.conf.json 示例模板,包含核心配置项,并参考了你提供的文档内容进行了更新、精简和注释说明。Tauri 2.0 仍然使用 tauri.conf.json 或 tauri.config.json(两者等效),整体结构和 1.x 相似,但引入了更多模块化和插件机制。
✅ src-tauri/tauri.conf.json 示例(Tauri 2.0)
{
"build": {
"beforeDevCommand": "yarn dev",
"beforeBuildCommand": "yarn build",
"devPath": "http://localhost:5173", // 也可以用相对路径如 "../dist"
"distDir": "../dist"
},
"package": {
"productName": "MyTauriApp",
"version": "0.2.0"
},
"tauri": {
"pattern": {
"use": "brownfield"
},
"windows": [
{
"label": "main",
"title": "My Tauri App",
"width": 1024,
"height": 768,
"resizable": true,
"fullscreen": false,
"center": true,
"decorations": true,
"visible": true,
"transparent": false,
"theme": "Dark", // 可选值: "Light" | "Dark"
"url": "index.html"
}
],
"bundle": {
"identifier": "com.example.tauriapp",
"icon": ["icons/icon.icns", "icons/icon.ico", "icons/icon.png"],
"targets": ["deb", "app", "msi"],
"resources": ["../extra/myfile.txt"]
},
"security": {
"csp": null // 默认即可,强需求可写 CSP 规则
},
"allowlist": {
"all": false,
"fs": {
"readFile": true,
"writeFile": true,
"readDir": true
},
"shell": {
"all": false,
"open": true
},
"clipboard": {
"readText": true,
"writeText": true
}
},
"updater": {
"active": false
},
"systemTray": {
"iconPath": "icons/tray_icon.png",
"iconAsTemplate": true
},
"macOSPrivateApi": false
},
"plugins": {
"plugin-log": {
"level": "info"
},
"plugin-store": {
"dir": ".local"
}
}
}
🧩 Tauri 2.0 支持的新变化(需注意)
| 特性 | Tauri 2.0 支持 | 说明 |
|---|---|---|
pattern.isolation | ✅ | 安全隔离模式,适用于加载不信任的前端资源。 |
plugins 字段 | ✅ | 插件统一配置,比如 plugin-log, plugin-store, plugin-autostart 等。 |
| 多窗口支持 | ✅ | 在 tauri.windows[] 中配置多个窗口对象 |
systemTray | ✅ | 系统托盘功能,可配置图标、菜单等 |
security.csp | ✅ | 可设为字符串或 null(放开策略) |
| 平台特定配置 | ✅ | 支持如 tauri.windows.conf.json、tauri.macos.conf.json 等配置文件按平台覆盖主配置 |
📦 打包命令与 dev 命令参考
# 开发调试
pnpm tauri dev
# 构建发布(会使用 tauri.conf.json)
pnpm tauri build
🔐 权限控制建议
建议将 "allowlist.all": false,然后显式开启你需要的 API,避免安全风险。
如果你需要支持:
- 多窗口配置
- 透明窗口 + macOSPrivateApi
- 热更新(updater)
- 自动启动、持久化数据插件(plugin-store)
- 动态修改配置(通过 CLI 或构建脚本)
我也可以为你继续补充具体写法,是否需要进一步示例?
lencx
{折腾 ⇌ 迷茫 ⇌ 思考]ing,在路上...
关注他
6 人赞同了该文章
仓库:lencx/OhMyBox
阅读原文(首发于 GitHub)
关注《浮之静》公众号,私信作者,进 Tauri 技术交流群
tauri.conf.json 是由 tauri init 命令生成的文件,该文件位于 Tauri 应用程序源目录 src-tauri 中。可以通过修改它来自定义 Tauri 应用程序。此文仅列举部分常用字段,更多请查看 Tauri Configuration。
特定平台
除了 tauri.conf.json 外,Tauri 还可以从 tauri.linux.conf.json、tauri.windows.conf.json、tauri.macos.conf.json 中读取特定于平台的配置,并将其与 tauri.conf.json 主配置合并。
配置结构
示例配置:
{
"build": {
"beforeBuildCommand": "",
"beforeDevCommand": "",
"devPath": "../dist",
"distDir": "../dist"
},
"package": {
"productName": "tauri-app",
"version": "0.1.0"
},
"tauri": {
"allowlist": {
"all": true
},
"bundle": {},
"security": {
"csp": null
},
"updater": {
"active": false
},
"windows": [
{
"fullscreen": false,
"height": 600,
"resizable": true,
"title": "Tauri App",
"width": 800
}
]
}
}
tauri.conf.json 由以下部分组成:
package:Package 设置
tauri:Tauri 配置
build:构建配置
plugins:插件配置
package
名称 类型 默认值 描述
productName string? null APP 名称
version string? null 版本号,它是 semver 版本号或包含 version 字段的 package.json 文件的路径
semver 版本号
tauri
名称 类型 默认值 描述
pattern PatternKind 要使用的模式
windows [WindowConfig] [] 窗口配置
cli CliConfig? 命令行界面配置
bundle BundleConfig 捆绑器配置
allowlist AllowlistConfig 白名单配置,如系统文件,路径,剪贴板,脚本,进程等
security SecurityConfig 安全配置
updater UpdaterConfig 更新应用配置,如签名公钥,更新应用的窗口配置等
systemTray SystemTrayConfig? 应用系统托盘的配置,如图标配置
macOSPrivateApi boolean false MacOS 私有 API 配置。启用透明后台 API 并将首选项 fullScreenEnabled 设置为 true
CliConfig
BundleConfig
AllowlistConfig
SecurityConfig
UpdaterConfig
SystemTrayConfig
PatternKind
tauri -> pattern
应用程序模式,可以是以下任何一种类型:
brownfield - 棕地模式,这是使用 Tauri 最简单直接的模式,因为它试图尽可能与现有的前端项目兼容。简而言之,它试图除了现有Web前端可能在浏览器中使用的内容之外,不需要任何其他内容。
isolation - 隔离模式是一种在前端发送到 Tauri Core 之前拦截和修改 Tauri API 消息的方法,所有这些都是使用 JavaScript 进行的。由隔离模式注入的安全 JavaScript 代码称为隔离应用程序。出于安全目的,建议使用此模式。
{
"tauri": {
"pattern": {
"use": "brownfield"
}
}
}
{
"tauri": {
"pattern": {
"use": "isolation",
"options": {
"dir": "../dist-isolation"
}
}
}
}
WindowConfig
tauri -> windows[]
名称 类型 默认值 描述
label string null 窗口标识符,它必须是字母数字
url WindowUrl 窗口 Web 视图 URL
fileDropEnabled boolean true 是否在 Web 视图上启用文件拖拽
center boolean false 窗口是否以居中方式启动
x number?(format: double) null 窗口左上角的水平位置
y number?(format: double) null 窗口左上角的垂直位置
width number?(format: double) null 窗口宽度
height number?(format: double) null 窗口高度
minWidth number?(format: double) null 窗口最小宽度
minHeight number?(format: double) null 窗口最小高度
maxWidth number?(format: double) null 窗口最大宽度
maxHeight number?(format: double) null 窗口最大高度
resizable boolean true 窗口是否可调整大小
title string null 窗口标题
fullscreen boolean false 窗口是否以全屏方式启动
focus boolean true 窗口是否聚焦
transparent boolean false 窗口是否透明,注意:mac 需要启用 macOSPrivateApi(警告:使用私有 API 会阻止应用程序被接受)
maximized boolean false 窗口是否最大化
visible boolean true 窗口是否可见
decorations boolean true 窗口是否具有边框和标题栏
alwaysOnTop boolean false 窗口是否应始终位于其他窗口上层
skipTaskbar boolean false 是否将窗口图标添加到任务栏
theme Theme? 初始窗口主题,默认为系统主题。仅在 Windows 和 macOS 10.14+ 上实现
WindowUrl
tauri -> windows[] -> url
要在Tauri Web 视图窗口中打开的 URL,可以是以下任何一种类型:
格式 uri - 外部 URL
应用程序 URL 的路径部分 - 例如:tauri://localhost/users/lencx 的部分路径 users/lencx
Theme
tauri -> windows[] -> theme
系统主题,可以是以下任一值:
Light
Dark