添加到现有仓库

Turborepo 可以逐步采用到任何存储库(单个或多个软件包),以加快存储库的开发者和 CI 工作流程。

¥Turborepo can be incrementally adopted in any repository, single or multi-package, to speed up the developer and CI workflows of the repository.

安装 turbo 并在 turbo.json 中配置任务后,你会注意到 caching 如何帮助你更快地运行任务。

¥After installing turbo and configuring your tasks in turbo.json, you'll notice how caching helps you run tasks much faster.

准备单包工作区

¥Preparing a single-package workspace

例如,单包工作区 是运行 npx create-next-appnpm create vite 后得到的。你无需为 Turborepo 处理你的代码库做任何额外的工作,因此你可以直接跳到下面的第一步。

¥A single-package workspace is, for example, what you get after running npx create-next-app or npm create vite. You don't need to do any extra work for Turborepo to handle your repo so you can jump to the first step below.

要了解更多关于单包工作区中 Turborepo 的信息,请访问 专用指南

¥To learn more about Turborepo in single-package workspaces, visit the dedicated guide.

准备多软件包工作区(monorepo)

¥Preparing a multi-package workspace (monorepo)

turbo 构建于 Workspaces 之上,这是 JavaScript 生态系统中主流包管理器的一项功能。这使得它易于在你现有的代码库中采用。

¥turbo is built on top of Workspaces, a feature of the major package managers in the JavaScript ecosystem. This makes it easy to adopt in your existing codebase.

Good to know: 

如果你发现 turbo 存在问题,例如无法在你的工作区中发现软件包或未遵循依赖图,请访问我们的 构建仓库 页面获取提示。

¥If you're finding that turbo is having issues like not being able to discover packages in your workspace or not following your dependency graph, visit our Structuring a repository page for tips.

请注意,你不必立即使用 turbo 为所有包运行所有任务。你可以从几个包中的单个任务开始,随着你对 Turborepo 的熟悉程度不断提高,逐步添加更多任务和包。

¥Note that you don't have to start running all your tasks for all your packages using turbo right away. You can start with a single task in just a few packages and incrementally add more tasks and packages as you get more familiar with Turborepo.

将 Turborepo 添加到你的仓库

¥Adding Turborepo to your repository

安装 turbo

¥Install turbo

为了获得最佳的开发者体验,我们建议你全局安装 turbo 并将其安装到你的代码库根目录中。

¥We recommend you install turbo both globally and into your repository's root for the best developer experience.

在开始安装之前,请确保你已创建 pnpm-workspace.yaml 文件。如果没有此文件,将导致以下错误: --workspace-root may only be used inside a workspace

¥Ensure you have created a pnpm-workspace.yaml file before you begin the installation. Failure to have this file will result in an error that says: --workspace-root may only be used inside a workspace.

Terminal
# Global install
pnpm add turbo --global
# Install in repository
pnpm add turbo --save-dev --workspace-root

要了解更多关于我们推荐这两种安装方式的原因,请访问 安装页面

¥To learn more about why we recommend both installations, visit the Installation page.

添加 turbo.json 文件

¥Add a turbo.json file

在你的代码库根目录中,创建一个 turbo.json 文件。

¥In the root of your repository, create a turbo.json file.

本指南中我们将使用 buildcheck-types 任务,但你可以将其替换为你感兴趣的其他任务,例如 linttest

¥We'll be using build and check-types tasks in this guide but you can replace these with other tasks that interest you, like lint or test.

Turborepo logo
./turbo.json
{
  "$schema": "https://turbo.nodejs.cn/schema.json",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": [".next/**", "!.next/cache/**"]
    },
    "check-types": {
      "dependsOn": ["^check-types"]
    },
    "dev": {
      "persistent": true,
      "cache": false
    }
  }
}

更多有关配置 turbo.json 的信息,请参阅 配置选项 文档。

¥For more information on configuring your turbo.json, see the Configuration Options documentation.

在你的 Next.js 应用中,确保你有一个可供 turbo 运行的 check-types 脚本。

¥In your Next.js application, make sure you have a check-types script for turbo to run.

apps/web/package.json
{
  "scripts": {
+   "check-types": "tsc --noEmit"
  }
}

在多软件包工作区中,你可能还想将 check-types 脚本添加到一个或多个库软件包中,以查看不同软件包中的多个脚本如何使用一个 turbo 命令运行。

¥In a multi-package workspace, you may also want to add a check-types script to one or more of your library packages to see how multiple scripts across different packages run with one turbo command.

Edit .gitignore

.turbo 添加到你的 .gitignore 文件中。turbo CLI 使用这些文件夹来保存日志、输出和其他功能。

¥Add .turbo to your .gitignore file. The turbo CLI uses these folders for persisting logs, outputs, and other functionality.

.gitignore
+ .turbo

packageManager 字段添加到根 package.json

¥Add a packageManager field to root package.json

Turborepo 使用来自包管理器的信息来优化你的代码库。要声明你正在使用的包管理器,请将 packageManager 字段添加到你的根 package.json(如果你还没有)。

¥Turborepo optimizes your repository using information from your package manager. To declare which package manager you're using, add a packageManager field to your root package.json if you don't have one already.

package.json
{
+  "packageManager": "pnpm@10.0.0"
}

Good to know: 

根据你的代码库,你可能需要在迁移期间或尚无法使用 packageManager 密钥的情况下使用 dangerouslyDisablePackageManagerCheck

¥Depending on your repository, you may need to use the dangerouslyDisablePackageManagerCheck while migrating or in situations where you can't use the packageManager key yet.

设置包管理器工作区

¥Set up package manager workspaces

对于 多软件包工作区,你需要配置包管理器以识别你的工作区结构。

¥For multi-package workspaces, you'll need to configure your package manager to recognize your workspace structure.

workspaces 字段告诉你的包管理器哪些目录包含你的包。常见的模式包括用于应用的 apps/* 和用于共享库的 packages/*

¥The workspaces field tells your package manager which directories contain your packages. Common patterns include apps/* for applications and packages/* for shared libraries.

Good to know: 

如果你使用的是单包存储库,则可以跳过此步骤,因为不需要工作区。

¥If you're working with a single-package repository, you can skip this step as workspaces aren't needed.

pnpm-workspace.yaml
packages:
  - "apps/*"
  - "packages/*"
pnpm workspace documentation

有关如何构建存储库的更多详细信息,请参阅 构建仓库

¥For more details on how to structure your repository, see Structuring a Repository.

使用 turbo 运行任务

¥Run tasks with turbo

你现在可以使用 Turborepo 运行之前添加到 turbo.json 的任务。使用上面的示例任务:

¥You can now run the tasks you added to turbo.json earlier using Turborepo. Using the example tasks from above:

Terminal
turbo build check-types

这会同时运行 buildcheck-types 任务。你的 工作区 的依赖图将用于以正确的顺序运行任务。

¥This runs the build and check-types tasks at the same time. The dependency graph of your Workspace will be used to run tasks in the right order.

在不对代码进行任何更改的情况下,尝试再次运行 buildcheck-types

¥Without making any changes to the code, try running build and check-types again:

Terminal
turbo check-types build

你应该看到如下终端输出:

¥You should see terminal output like this:

Terminal
Tasks:    2 successful, 2 total
Cached:    2 cached, 2 total
Time:    185ms >>> FULL TURBO

恭喜!你只需在几毫秒内构建并检查代码即可。

¥Congratulations! You just built and type checked your code in milliseconds.

要了解更多关于 turbo 如何实现这一点的信息,请查看 缓存文档

¥To learn more about how turbo makes this possible, check out the caching documentation.

通过运行 devturbo 开始开发

¥Begin developing by running dev with turbo

在多包工作区中,你可以运行 turbo dev 来同时启动所有包的开发任务。

¥In a multi-package workspace, you can run turbo dev to start the development tasks for all your packages at once.

Terminal
turbo dev

你还可以使用 使用过滤器 来专注于特定的软件包及其依赖。

¥You can also use a filter to focus on a specific package and its dependencies.

请注意,此步骤在单包工作区中没有太大价值,因为:

¥Note that this step doesn't provide much value in a single-package workspace since:

  • 你无需缓存开发任务的输出。

    ¥You don't cache the outputs for a development task.

  • 只有一个开发脚本,所以没有可以并行运行的脚本。

    ¥There's only one development script so there's nothing to run in parallel.

后续步骤

¥Next steps

你现在已经启动并运行 Turborepo 了!要了解更多改进工作流程和充分利用 turbo 的方法,我们建议你查看以下页面:

¥You're now up and running with Turborepo! To learn about more ways you can improve your workflow and get the most out of turbo, we recommend checking out the following pages: