开发应用

在单一代码库中开发应用可以解锁强大的工作流程,使你能够通过轻松访问代码对源代码控制进行原子提交。

¥Developing applications in a monorepo unlocks powerful workflows, enabling you to make atomic commits to source control with easy access to code.

大多数开发任务都是长期运行的任务,会监视代码的更改。Turborepo 通过强大的终端 UI 和其他功能增强了此体验,例如:

¥Most development tasks are long-running tasks that watch for changes to your code. Turborepo enhances this experience with a powerful terminal UI and other capabilities like:

配置开发任务

¥Configuring development tasks

turbo.json 中定义开发任务会告知 Turborepo 你将运行一个长期任务。这对于运行开发服务器、运行测试或构建应用等操作非常有用。

¥Defining a development task in turbo.json tells Turborepo that you'll be running a long-lived task. This is useful for things like running a development server, running tests, or building your application.

要注册 dev 任务,请将其添加到你的 turbo.json 中,并添加以下两个属性:

¥To register a dev task, add it to your turbo.json with two properties:

Turborepo logo
./turbo.json
{
  "tasks": {
    "dev": {
      "cache": false,
      "persistent": true
    }
  }
}
  • "cache":false:指示 Turborepo 不要尝试缓存任务的结果。由于这是一项开发任务,你可能会频繁更改代码,因此缓存结果毫无意义。

    ¥"cache": false: Tells Turborepo to not attempt to cache the results of the task. Since this is a development task, you're likely to be making frequent changes to your code, so caching the results is not useful.

  • "persistent":true:指示 Turborepo 保持任务运行,直到你停止它。此键会向你的终端 UI 触发信号,指示该任务被视为长时间运行且可交互。此外,它还可以防止你意外依赖不会退出的任务。

    ¥"persistent": true: Tells Turborepo to keep the task running until you stop it. This key serves as a signal for your terminal UI to treat the task as long-running and interactive. Additionally, it prevents you from accidentally depending on a task that will not exit.

你现在可以运行 dev 任务来并行启动开发脚本:

¥You can now run your dev task to start your development scripts in parallel:

Terminal
turbo dev

dev 之前运行设置任务

¥Running setup tasks before dev

你可能还想运行设置开发环境或预构建包的脚本。你可以使用 dependsOn 确保这些任务在 dev 任务之前运行:

¥You may also want to run scripts that set up your development environment or pre-build packages. You can make sure those tasks run before the dev task with dependsOn:

Turborepo logo
./turbo.json
{
  "tasks": {
    "dev": {
      "cache": false,
      "persistent": true,
      "dependsOn": ["//#dev:setup"]
    },
    "//#dev:setup": {
      "outputs": [".codegen/**"]
    }
  }
}

在本例中,我们使用的是 根任务,但你可以对 包中的任意任务 使用相同的方法。

¥In this example, we're using a Root Task but you can use the same idea for arbitrary tasks in packages.

运行特定应用

¥Running a specific application

--filter 标志允许你选择 软件包图表 的子集,以便你可以为特定应用及其依赖运行 dev 任务:

¥The --filter flag allows you to pick a subset of your Package Graph so you can run your dev task for a specific application and its dependencies:

Terminal
turbo dev --filter=web

使用终端 UI

¥Using the terminal UI

Turborepo 的终端 UI 支持多种功能,可为你的任务创建高度交互的体验。

¥Turborepo's terminal UI enables a number of features that create a highly interactive experience around your tasks.

自定义视图

¥Customizing your view

你可以使用快捷键快速调整 UI 以满足你的需求。

¥You can quickly adjust the UI to your needs using keybinds.

按键绑定操作
m切换弹出式按键绑定列表
/在任务列表中选择下一个/上一个任务
j/k在任务列表中选择下一个/上一个任务
p切换选定任务的固定选择
h切换任务列表的可见性
c日志高亮显示时,将选定内容复制到系统剪贴板
u/d滚动日志 up 和 down

与任务交互

¥Interacting with tasks

某些工具可能允许你在其中键入输入。例如 Drizzle ORM 的交互式迁移或 Jest 的测试套件过滤和重新运行。

¥Some of your tools may allow you to type input into them. Examples of this include Drizzle ORM's interactive migrations or Jest's filtering and re-running of test suites.

你可以与 标记为交互式 任务交互,为其提供输入。

¥You can interact with tasks that are marked as interactive to give them input.

按键绑定操作
i开始交互
Ctrl+z停止交互

监视模式

¥Watch Mode

许多工具都内置了监视器,例如 tsc --watch,它可以响应源代码中的更改。但是,有些任务日志不隐藏。

¥Many tools have a built-in watcher, like tsc --watch, that will respond to changes in your source code. However, some don't.

turbo watch 为任何工具添加了一个依赖感知监视器。源代码的更改将遵循你在 turbo.json 中描述的 任务图,就像你的所有其他任务一样。

¥turbo watch adds a dependency-aware watcher to any tool. Changes to source code will follow the Task Graph that you've described in turbo.json, just like all your other tasks.

例如,使用类似 create-turbo 的包结构,其中包含以下任务和脚本:

¥For example, using a package structure like create-turbo with the following tasks and scripts:

Turborepo logo
turbo.json
{
  "tasks": {
    "dev": {
      "persistent": true,
      "cache": false
    },
    "lint": {
      "dependsOn": ["^lint"]
    }
  }
}

运行 turbo watch dev lint 时,尽管 ESLint 没有内置的监视程序,但每当你更改源代码时,你都会看到 lint 脚本重新运行。turbo watch 还能感知内部依赖,因此 @repo/ui 中的代码更改将在 @repo/uiweb 中重新运行该任务。

¥When you run turbo watch dev lint, you'll see the lint scripts are re-run whenever you make source code changes, despite ESLint not having a built-in watcher. turbo watch is also aware of internal dependencies, so a code change in @repo/ui will re-run the task in both @repo/ui and web.

web 中的 Next.js 开发服务器和 @repo/ui 中 TypeScript 编译器的内置监视器将继续正常工作,因为它们被标记为 persistent

¥The Next.js development server in web and the TypeScript Compiler's built-in watcher in @repo/ui will continue to work as usual, since they are marked with persistent.

更多信息,请使用 访问 turbo watch 参考

¥For more information, visit the turbo watch reference.

限制

¥Limitations

拆卸任务

¥Teardown tasks

在某些情况下,你可能希望在 dev 任务停止时运行脚本。Turborepo 在退出时无法运行那些拆卸脚本,因为 turbo 会在你的 dev 任务退出时退出。

¥In some cases, you may want to run a script when the dev task is stopped. Turborepo is unable to run those teardown scripts when exiting because turbo exits when your dev tasks exit.

相反,创建一个 turbo dev:teardown 脚本,在退出主要 turbo dev 任务后单独运行该脚本。

¥Instead, create a turbo dev:teardown script that you run separately after you've exited your primary turbo dev task.

后续步骤

¥Next steps

当你有了想要部署的应用版本后,就该学习如何在 Turborepo 中配置环境变量了。

¥Once you have a version of your application that you'd like to deploy, it's time to learn how to configure environment variables in Turborepo.