DEV Community

Cover image for 【06】Tauri 应用篇 - tauri.conf.json 配置
lencx
lencx

Posted on

【06】Tauri 应用篇 - tauri.conf.json 配置

仓库:lencx/OhMyBox
如果文章对你有所帮助,可以 star 此项目,或转发给更多有需要的人。大家的支持也会给我更大的写作动力,感恩。


tauri.conf.json 是由 tauri init 命令生成的文件,该文件位于 Tauri 应用程序源目录 src-tauri 中。可以通过修改它来自定义 Tauri 应用程序。此文仅列举部分常用字段,更多请查看 Tauri Configuration

特定平台

除了 tauri.conf.json 外,Tauri 还可以从 tauri.linux.conf.jsontauri.windows.conf.jsontauri.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
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

tauri.conf.json 由以下部分组成:

  1. package:Package 设置
  2. tauri:Tauri 配置
  3. build:构建配置
  4. plugins:插件配置

package

名称 类型 默认值 描述
productName string? null APP 名称
version string? null 版本号,它是 semver 版本号或包含 version 字段的 package.json 文件的路径

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

PatternKind

tauri -> pattern

应用程序模式,可以是以下任何一种类型:

  • brownfield - 棕地模式,这是使用 Tauri 最简单直接的模式,因为它试图尽可能与现有的前端项目兼容。简而言之,它试图除了现有Web前端可能在浏览器中使用的内容之外,不需要任何其他内容。
  • isolation - 隔离模式是一种在前端发送到 Tauri Core 之前拦截和修改 Tauri API 消息的方法,所有这些都是使用 JavaScript 进行的。由隔离模式注入的安全 JavaScript 代码称为隔离应用程序。出于安全目的,建议使用此模式。
{
  "tauri": {
    "pattern": {
      "use": "brownfield"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
{
  "tauri": {
    "pattern": {
      "use": "isolation",
      "options": {
        "dir": "../dist-isolation"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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

Latest comments (0)