Skip to content
Tauri 中文网

配置文件

由于 Tauri 是用于构建应用的工具包,因此可以使用许多文件来配置项目设置。你可能会遇到的一些常见文件是 tauri.conf.jsonpackage.jsonCargo.toml。我们在此页面上简要解释每个内容,以帮助你指出需要修改哪些文件的正确方向。

¥Since Tauri is a toolkit for building applications there can be many files to configure project settings. Some common files that you may run across are tauri.conf.json, package.json and Cargo.toml. We briefly explain each on this page to help point you in the right direction for which files to modify.

Tauri 配置

¥Tauri Config

Tauri 配置用于定义 Web 应用的来源、描述应用的元数据、配置包、设置插件配置、通过配置窗口、托盘图标、菜单等修改运行时行为。

¥The Tauri configuration is used to define the source of your Web app, describe your application’s metadata, configure bundles, set plugin configurations, modify runtime behavior by configuring windows, tray icons, menus and more.

此文件由 Tauri 运行时和 Tauri CLI 使用。你可以定义构建设置(例如 tauri build 之前运行的命令tauri dev 启动),设置 name你的应用版本控制 Tauri 运行时配置插件

¥This file is used by the Tauri runtime and the Tauri CLI. You can define build settings (such as the command run before tauri build or tauri dev kicks in), set the name and version of your app, control the Tauri runtime, and configure plugins.

:::tip 提示

你可以在 配置参考 中找到所有选项。

¥You can find all of the options in the configuration reference.

:::

支持的格式

¥Supported Formats

默认的 Tauri 配置格式为 JSON。可以通过将 config-json5config-toml 功能标志(分别)添加到 Cargo.toml 中的 tauritauri-build 依赖来启用 JSON5 或 TOML 格式。

¥The default Tauri config format is JSON. The JSON5 or TOML format can be enabled by adding the config-json5 or config-toml feature flag (respectively) to the tauri and tauri-build dependencies in Cargo.toml.

Cargo.toml
[build-dependencies]
tauri-build = { version = "2.0.0", features = [ "config-json5" ] }
[dependencies]
tauri = { version = "2.0.0", features = [ "config-json5" ] }

所有格式的结构和值都相同,但是格式应与相应文件的格式一致:

¥The structure and values are the same across all formats, however, the formatting should be consistent with the respective file’s format:

tauri.conf.json
{
build: {
devUrl: 'http://localhost:3000',
// start the dev server
beforeDevCommand: 'npm run dev',
},
bundle: {
active: true,
icon: ['icons/app.png'],
},
app: {
windows: [
{
title: 'MyApp',
},
],
},
plugins: {
updater: {
pubkey: 'updater pub key',
endpoints: ['https://my.app.updater/{{target}}/{{current_version}}'],
},
},
}
Tauri.toml
[build]
dev-url = "http://localhost:3000"
# start the dev server
before-dev-command = "npm run dev"
[bundle]
active = true
icon = ["icons/app.png"]
[[app.windows]]
title = "MyApp"
[plugins.updater]
pubkey = "updater pub key"
endpoints = ["https://my.app.updater/{{target}}/{{current_version}}"]

请注意,JSON5 和 TOML 支持注释,并且 TOML 可以使用更符合地道的 kebab-case 作为配置名称。

¥Note that JSON5 and TOML supports comments, and TOML can use kebab-case for config names which are more idiomatic.

平台特定配置

¥Platform-specific Configuration

除了默认配置文件之外,Tauri 还可以从以下位置读取特定于平台的配置:

¥In addition to the default configuration file, Tauri can read a platform-specific configuration from:

  • Linux 的 tauri.linux.conf.jsonTauri.linux.toml

    ¥tauri.linux.conf.json or Tauri.linux.toml for Linux

  • Windows 的 tauri.windows.conf.jsonTauri.windows.toml

    ¥tauri.windows.conf.json or Tauri.windows.toml for Windows

  • macOS 的 tauri.macos.conf.jsonTauri.macos.toml

    ¥tauri.macos.conf.json or Tauri.macos.toml for macOS

  • Android 的 tauri.android.conf.jsonTauri.android.toml

    ¥tauri.android.conf.json or Tauri.android.toml for Android

  • iOS 的 tauri.ios.conf.jsonTauri.ios.toml

    ¥tauri.ios.conf.json or Tauri.ios.toml for iOS

平台特定的配置文件将按照 JSON 合并补丁 (RFC 7396) 规范与主配置对象合并。

¥The platform-specific configuration file gets merged with the main configuration object following the JSON Merge Patch (RFC 7396) specification.

例如,给定以下基本 tauri.conf.json

¥For example, given the following base tauri.conf.json:

tauri.conf.json
{
"productName": "MyApp",
"bundle": {
"resources": ["./resources"]
},
"plugins": {
"deep-link": {}
}
}

给定的 tauri.linux.conf.json

¥And the given tauri.linux.conf.json:

tauri.linux.conf.json
{
"productName": "my-app",
"bundle": {
"resources": ["./linux-assets"]
},
"plugins": {
"cli": {
"description": "My app",
"subcommands": {
"update": {}
}
},
"deep-link": {}
}
}

Linux 的解析配置将是以下对象:

¥The resolved configuration for Linux would be the following object:

{
"productName": "my-app",
"bundle": {
"resources": ["./linux-assets"]
},
"plugins": {
"cli": {
"description": "My app",
"subcommands": {
"update": {}
}
},
"deep-link": {}
}
}

此外,你还可以提供要通过 CLI 合并的配置,有关更多信息,请参阅以下部分。

¥Additionally you can provide a configuration to be merged via the CLI, see the following section for more information.

扩展配置

¥Extending the Configuration

Tauri CLI 允许你在运行 devandroid devios devbuildandroid buildios buildbundle 命令之一时扩展 Tauri 配置。配置扩展可以通过 --config 参数提供,既可以是原始 JSON 字符串,也可以是 JSON 文件的路径。Tauri 使用 JSON 合并补丁 (RFC 7396) 规范将提供的配置值与最初解析的配置对象合并。

¥The Tauri CLI allows you to extend the Tauri configuration when running one of the dev, android dev, ios dev, build, android build, ios build or bundle commands. The configuration extension can be provided by the --config argument either as a raw JSON string or as a path to a JSON file. Tauri uses the JSON Merge Patch (RFC 7396) specification to merge the provided configuration value with the originally resolved configuration object.

此机制可用于定义应用的多种风格,或在配置应用包时具有更大的灵活性。

¥This mechanism can be used to define multiple flavours of your application or have more flexibility when configuring your application bundles.

例如,要分发完全独立的测试版应用,你可以使用此功能配置单独的应用名称和标识符:

¥For instance to distribute a completely isolated beta application you can use this feature to configure a separate application name and identifier:

src-tauri/tauri.beta.conf.json
{
"productName": "My App Beta",
"identifier": "com.myorg.myappbeta"
}

要分发这个单独的测试版应用,你需要在构建它时提供此配置文件:

¥And to distribute this separate beta app you provide this configuration file when building it:

npm run tauri build -- --config src-tauri/tauri.beta.conf.json

Cargo.toml

Cargo 的清单文件用于声明你的应用所依赖的 Rust 板条箱、有关你的应用的元数据以及其他与 Rust 相关的功能。如果你不打算使用 Rust 为你的应用进行后端开发,那么你可能不会对其进行太多修改,但了解它的存在及其作用非常重要。

¥Cargo’s manifest file is used to declare Rust crates your app depends on, metadata about your app, and other Rust-related features. If you do not intend to do backend development using Rust for your app then you may not be modifying it much, but it’s important to know that it exists and what it does.

下面是一个 Tauri 项目的 Cargo.toml 文件基本示例:

¥Below is an example of a barebones Cargo.toml file for a Tauri project:

Cargo.toml
[package]
name = "app"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
default-run = "app"
edition = "2021"
rust-version = "1.57"
[build-dependencies]
tauri-build = { version = "2.0.0" }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "2.0.0", features = [ ] }

需要注意的最重要的部分是 tauri-buildtauri 依赖。通常,它们都必须与 Tauri CLI 一样是最新的次要版本,但这并不是严格要求的。如果你在尝试运行应用时遇到问题,你应该检查任何 Tauri 版本(tauritauri-cli)是否是其各自次要版本的最新版本。

¥The most important parts to take note of are the tauri-build and tauri dependencies. Generally, they must both be on the latest minor versions as the Tauri CLI, but this is not strictly required. If you encounter issues while trying to run your app you should check that any Tauri versions (tauri and tauri-cli) are on the latest versions for their respective minor releases.

Cargo 版本号使用 语义版本控制。在 src-tauri 文件夹中运行 cargo update 将提取所有依赖的最新可用 Semver 兼容版本。例如,如果你将 2.0.0 指定为 tauri-build 的版本,Cargo 将检测并下载版本 2.0.0.0,因为它是可用的最新 Semver 兼容版本。每当引入重大更改时,Tauri 都会更新主版本号,这意味着你应该始终能够安全地升级到最新的次要版本和补丁版本,而不必担心代码被破坏。

¥Cargo version numbers use Semantic Versioning. Running cargo update in the src-tauri folder will pull the latest available Semver-compatible versions of all dependencies. For example, if you specify 2.0.0 as the version for tauri-build, Cargo will detect and download version 2.0.0.0 because it is the latest Semver-compatible version available. Tauri will update the major version number whenever a breaking change is introduced, meaning you should always be capable of safely upgrading to the latest minor and patch versions without fear of your code breaking.

如果你想使用特定的 crate 版本,则可以通过在依赖的版本号前面添加 = 来使用确切的版本:

¥If you want to use a specific crate version you can use exact versions instead by prepending = to the version number of the dependency:

tauri-build = { version = "=2.0.0.0" }

需要注意的另一件事是 tauri 依赖的 features=[] 部分。运行 tauri devtauri build 将根据你的 Tauri 配置自动管理需要在你的项目中启用哪些功能。有关 tauri 功能标志的更多信息,请参阅 documentation

¥An additional thing to take note of is the features=[] portion of the tauri dependency. Running tauri dev and tauri build will automatically manage which features need to be enabled in your project based on the your Tauri configuration. For more information about tauri feature flags see the documentation.

构建应用时会生成一个 Cargo.lock 文件。此文件主要用于确保在开发期间跨机器使用相同的依赖(类似于 Node.js 中的 yarn.lockpnpm-lock.yamlpackage-lock.json)。建议将此文件提交到你的源存储库,以便获得一致的构建。

¥When you build your application a Cargo.lock file is produced. This file is used primarily for ensuring that the same dependencies are used across machines during development (similar to yarn.lock, pnpm-lock.yaml or package-lock.json in Node.js). It is recommended to commit this file to your source repository so you get consistent builds.

要了解有关 Cargo 清单文件的更多信息,请参阅 官方文档

¥To learn more about the Cargo manifest file please refer to the official documentation.

package.json

这是 Node.js 使用的包文件。如果你的 Tauri 应用的前端是使用基于 Node.js 的技术(例如 npmyarnpnpm)开发的,则此文件用于配置前端依赖和脚本。

¥This is the package file used by Node.js. If the frontend of your Tauri app is developed using Node.js-based technologies (such as npm, yarn, or pnpm) this file is used to configure the frontend dependencies and scripts.

Tauri 项目的准系统 package.json 文件示例可能看起来有点像这样:

¥An example of a barebones package.json file for a Tauri project might look a little something like this:

package.json
{
"scripts": {
"dev": "command to start your app development mode",
"build": "command to build your app frontend",
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "^2.0.0.0",
"@tauri-apps/cli": "^2.0.0.0"
}
}

通常使用 "scripts" 部分来存储用于启动和构建 Tauri 应用使用的前端的命令。上面的 package.json 文件指定了 dev 命令,你可以使用 yarn devnpm run dev 运行该命令来启动前端框架,以及 build 命令,你可以使用 yarn buildnpm run build 运行该命令来构建前端的 Web 资源,以供 Tauri 在生产中添加。使用这些脚本最方便的方式是通过 Tauri 配置的 beforeDevCommandbeforeBuildCommand 钩子将它们与 Tauri CLI 钩子:

¥It’s common to use the "scripts" section to store the commands used to launch and build the frontend used by your Tauri application. The above package.json file specifies the dev command that you can run using yarn dev or npm run dev to start the frontend framework and the build command that you can run using yarn build or npm run build to build your frontend’s Web assets to be added by Tauri in production. The most convenient way to use these scripts is to hook them with the Tauri CLI via the Tauri configuration’s beforeDevCommand and beforeBuildCommand hooks:

tauri.conf.json
{
"build": {
"beforeDevCommand": "yarn dev",
"beforeBuildCommand": "yarn build"
}
}

:::note 注意

仅在使用 npm 时才需要 "tauri" 脚本

¥The "tauri" script is only needed when using npm

:::

依赖对象指定在运行 yarnpnpm installnpm install(在本例中为 Tauri CLI 和 API)时 Node.js 应下载哪些依赖。

¥The dependencies object specifies which dependencies Node.js should download when you run either yarn, pnpm install or npm install (in this case the Tauri CLI and API).

除了 package.json 文件之外,你还可以看到 yarn.lockpnpm-lock.yamlpackage-lock.json 文件。这些文件有助于确保当你稍后下载依赖时,你将获得与开发期间使用的完全相同的版本(类似于 Rust 中的 Cargo.lock)。

¥In addition to the package.json file you may see either a yarn.lock, pnpm-lock.yaml or package-lock.json file. These files assist in ensuring that when you download the dependencies later you’ll get the exact same versions that you have used during development (similar to Cargo.lock in Rust).

要了解有关 package.json 文件格式的更多信息,请参阅 官方文档

¥To learn more about the package.json file format please refer to the official documentation.


Tauri 中文网 - 粤ICP备13048890号