单包工作区

Turborepo 在 多软件包工作区(通常称为 monorepos)中非常高效,它也可以用于提高 单包工作区 的速度。

¥While Turborepo is highly effective in multi-package workspaces (commonly referred to as monorepos), it can also be used to make single-package workspaces faster.

Turborepo 最重要的功能可在单包工作区中运行,包括本地和 远程缓存 以及任务并行化。无效的功能是指在单个包的上下文中没有意义的功能,例如包任务 (app#build)。

¥Turborepo's most important features work in single-package workspaces including local and Remote Caching and task parallelization. Features that don't work are ones that don't make sense in the context of a single package, like package tasks (app#build).

Good to know: 

单包工作区的示例是 npx create-next-appnpm create vite 的输出。

¥Examples of single-package workspaces are the output of npx create-next-app or npm create vite.

安装

¥Installation

turbo 安装到你的应用中:

¥Install turbo into your application:

Terminal
pnpm add turbo --save-dev

使用全局 turbo 运行 package.json 脚本(可选)

¥Running a package.json script using global turbo (optional)

为了加快开发工作流程,你也可以使用 全局安装 turbo,并直接从命令行运行命令。

¥For even faster developer workflows, you can install turbo globally, too, and run commands directly from the command line.

安装后,你可以运行 turbo build,而 Turborepo 将从 package.json 运行你的 build 脚本。再次运行 turbo build 将触发缓存。

¥Once installed, you can run turbo build and Turborepo will run your build script from package.json. Running turbo build again will hit the cache.

目前,turbo 并没有提供太多价值,因为你可能只会在代码更改时重建应用,而当代码更改时,turbo 将丢失缓存。只需两个简单的步骤,你就可以从 turbo 中获得更多功能。

¥At this point, turbo isn't providing much value since you're likely to only be rebuilding your application when code changes and, when your code changes, turbo will miss the cache. In two short steps, you can get more out of turbo.

使用一个命令运行多个脚本

¥Running many scripts with one command

在许多代码库中,都有需要运行的设置任务或预构建步骤。这些任务通常一次运行一个。 - 但你可以使用 turbo 轻松地将它们合并为一个脚本。

¥In many repositories, there are setup tasks or pre-build steps to run. These tasks are often run one at a time - but you can easily turn them into one script with turbo.

例如,假设你有一个项目,每次开始工作时我们都必须设置开发环境。你需要:

¥For example, let's say you have a project where we always have to set up a development environment whenever you start working. You need to:

  1. 为数据库启动 Docker 容器。

    ¥Start a Docker container for a database.

  2. 将数据库模式推送到数据库。

    ¥Push a database schema to the database.

  3. 用数据填充数据库。

    ¥Seed the database with data.

  4. 启动开发服务器。

    ¥Start the development server.

你可以使用 Turborepo 将这些任务安排到一个命令中。首先,在你的 package.json 中创建脚本:

¥You can schedule these tasks into one command using Turborepo. First, create scripts in your package.json:

package.json
{
  "name": "@acme/my-app",
  "version": "0.0.0",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "check-types": "tsc --noEmit",
    "db:up": "docker-compose up -d",
    "db:push": "your-orm-tool schema-push",
    "db:seed": "node ./db-seed.js"
  }
}

然后,在 turbo.json 中创建任务,按顺序运行这些脚本:

¥Then, create tasks in turbo.json to run these scripts in order:

Turborepo logo
./turbo.json
{
  "$schema": "https://turbo.nodejs.cn/schema.json",
  "tasks": {
    "dev": {
      "dependsOn": ["db:seed"],
      "cache": false,
      "persistent": true
    },
    "db:seed": {
      "dependsOn": ["db:push"],
      "cache": false
    },
    "db:push": {
      "dependsOn": ["db:up"],
      "cache": false
    },
    "db:up": {
      "cache": false
    }
  }
}

上述任务中的 dependsOn 数组为任务创建了顺序。运行 turbo dev 时,将首先运行 db:updb:pushdb:seed 的脚本。

¥The dependsOn arrays in the tasks above create a sequential order for the tasks. When you run turbo dev, the scripts for db:up, then db:push, then db:seed will be run first.

并行化任务

¥Parallelizing tasks

使用 turbo 并行化任务可以通过在可能的情况下一次性运行所有任务来加快速度。例如,你可以同时运行 ESLint、TypeScript 和 Prettier 检查。给定如下脚本:

¥Using turbo to parallelize tasks results in speeding up tasks by running all at once, when they can be. For instance, you can run your ESLint, TypeScript, and Prettier checks at the same time. Given scripts like:

./package.json
{
  "scripts": {
    "lint": "eslint .",
    "format": "prettier .",
    "check-types": "tsc --noEmit"
  }
}

你可以创建如下配置:

¥You can create a configuration like this one:

Turborepo logo
turbo.json
{
  "$schema": "https://turbo.nodejs.cn/schema.json",
  "tasks": {
    "lint": {},
    "format": {},
    "check-types": {}
  }
}

然后,同时运行所有任务:

¥Then, to run all tasks at the same time:

Terminal
turbo check-types lint format

使用输入优化任务

¥Optimizing task using inputs

由于 Turborepo 会将单包工作区视为一个包,因此它可以帮助优化任务的输入,以确保不相关的更改不会导致缓存未命中。

¥Because Turborepo will treat a single-package workspace as one package, it can help to optimize inputs to tasks to make sure unrelated changes don't create cache misses.

例如,使用 tsc --noEmit 检查类型的脚本可以配置为仅包含 TypeScript 文件的输入:

¥For instance, a script for checking types using tsc --noEmit can be configured with inputs that only include TypeScript files:

Turborepo logo
./turbo.json
{
  "$schema": "https://turbo.nodejs.cn/schema.json",
  "tasks": {
    "check-types": {
      "inputs": ["**/*.{ts,tsx}"]
    }
  }
}