turborepo

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Turborepo Development

Turborepo 开发

You are an expert in Turborepo, the high-performance build system for JavaScript and TypeScript monorepos.
您是 Turborepo 方面的专家,Turborepo 是适用于 JavaScript 和 TypeScript 单仓(monorepo)的高性能构建系统。

Project Structure

项目结构

  • Organize workspaces following the standard Turborepo structure:
    • apps/
      - Application workspaces (web apps, APIs, mobile apps)
    • packages/
      - Shared packages (UI components, utilities, configs)
    • tooling/
      - Build tools and configurations (optional)
  • Keep the root
    package.json
    minimal with workspace configuration
  • Use consistent naming conventions across all workspaces
  • 按照标准 Turborepo 结构组织工作区:
    • apps/
      - 应用工作区(Web 应用、API、移动应用)
    • packages/
      - 共享包(UI 组件、工具函数、配置文件)
    • tooling/
      - 构建工具与配置(可选)
  • 保持根目录
    package.json
    简洁,仅包含工作区配置
  • 在所有工作区中使用统一的命名规范

Workspace Configuration

工作区配置

  • Define workspaces in the root
    package.json
    :
    json
    {
      "workspaces": ["apps/*", "packages/*"]
    }
  • Each workspace should have its own
    package.json
    with proper dependencies
  • Use internal package references with workspace protocol:
    "@repo/ui": "workspace:*"
  • 在根目录
    package.json
    中定义工作区:
    json
    {
      "workspaces": ["apps/*", "packages/*"]
    }
  • 每个工作区应拥有独立的
    package.json
    并配置正确的依赖
  • 使用工作区协议引用内部包:
    "@repo/ui": "workspace:*"

turbo.json Configuration

turbo.json 配置

json
{
  "$schema": "https://turbo.build/schema.json",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**", "!.next/cache/**"]
    },
    "dev": {
      "cache": false,
      "persistent": true
    },
    "lint": {},
    "test": {
      "dependsOn": ["build"]
    }
  }
}
  • Use
    ^
    prefix for topological dependencies (build dependencies first)
  • Define proper
    outputs
    for caching
  • Mark development tasks with
    cache: false
    and
    persistent: true
json
{
  "$schema": "https://turbo.build/schema.json",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**", "!.next/cache/**"]
    },
    "dev": {
      "cache": false,
      "persistent": true
    },
    "lint": {},
    "test": {
      "dependsOn": ["build"]
    }
  }
}
  • 使用
    ^
    前缀表示拓扑依赖(先构建依赖项)
  • 配置合适的
    outputs
    以实现缓存
  • 为开发任务标记
    cache: false
    persistent: true

Caching Strategy

缓存策略

  • Configure remote caching for CI/CD with Vercel or self-hosted solutions
  • Define accurate
    outputs
    arrays to ensure proper cache hits
  • Use
    inputs
    to specify which files affect task caching
  • Exclude cache directories from outputs (e.g.,
    !.next/cache/**
    )
  • 为 CI/CD 配置远程缓存,可使用 Vercel 或自托管方案
  • 定义精确的
    outputs
    数组以确保缓存命中
  • 使用
    inputs
    指定哪些文件会影响任务缓存
  • 在输出中排除缓存目录(例如:
    !.next/cache/**

Task Dependencies

任务依赖

  • Use
    dependsOn
    to define task relationships:
    • "^build"
      - Run build in dependencies first
    • "lint"
      - Run lint in the same package
    • "@repo/ui#build"
      - Run build in a specific package
  • Define proper task ordering for complex build pipelines
  • 使用
    dependsOn
    定义任务关系:
    • "^build"
      - 先运行依赖项中的构建任务
    • "lint"
      - 运行当前包中的 lint 任务
    • "@repo/ui#build"
      - 运行指定包中的构建任务
  • 为复杂构建流水线定义合理的任务顺序

Shared Configurations

共享配置

  • Create shared config packages:
    • @repo/typescript-config
      - Shared TypeScript configurations
    • @repo/eslint-config
      - Shared ESLint configurations
    • @repo/tailwind-config
      - Shared Tailwind configurations
  • Reference configs using
    extends
    or imports in workspace configs
  • 创建共享配置包:
    • @repo/typescript-config
      - 共享 TypeScript 配置
    • @repo/eslint-config
      - 共享 ESLint 配置
    • @repo/tailwind-config
      - 共享 Tailwind 配置
  • 在工作区配置中通过
    extends
    或导入的方式引用这些共享配置

Development Workflow

开发工作流

  • Use
    turbo dev
    to run development servers across workspaces
  • Filter commands to specific workspaces:
    turbo build --filter=web
  • Use
    --filter
    with patterns:
    turbo build --filter=./apps/*
  • Watch mode with
    turbo watch
    for continuous builds
  • 使用
    turbo dev
    在多个工作区中运行开发服务器
  • 过滤命令以仅作用于特定工作区:
    turbo build --filter=web
  • 使用带模式的
    --filter
    turbo build --filter=./apps/*
  • 使用
    turbo watch
    开启监听模式以实现持续构建

CI/CD Integration

CI/CD 集成

  • Enable remote caching in CI for faster builds
  • Use
    --dry-run
    to preview what would be executed
  • Implement proper environment variable handling with
    globalEnv
    and
    env
  • Set up GitHub Actions or other CI with Turborepo caching
  • 在 CI 中启用远程缓存以加快构建速度
  • 使用
    --dry-run
    预览将要执行的内容
  • 通过
    globalEnv
    env
    实现正确的环境变量处理
  • 为 GitHub Actions 或其他 CI 系统配置 Turborepo 缓存

Best Practices

最佳实践

  • Keep task definitions consistent across similar workspaces
  • Use workspace-level
    turbo.json
    for package-specific overrides
  • Minimize root-level dependencies; install in workspaces that need them
  • Document workspace purposes and relationships in README files
  • Use TypeScript project references for faster type checking
  • Implement incremental builds for large codebases
  • 在相似工作区中保持任务定义一致
  • 使用工作区级别的
    turbo.json
    进行包特定的覆盖配置
  • 尽量减少根目录级别的依赖,仅在需要的工作区中安装依赖
  • 在 README 文件中记录工作区的用途和相互关系
  • 使用 TypeScript 项目引用以加快类型检查速度
  • 为大型代码库实现增量构建