Turborepo

作者 mindrally97184105b5da無授權條款269 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Best practices for Turborepo monorepo build system configuration and optimization

AI 產生的概覽

關於設定與最佳化 JavaScript 與 TypeScript 專案 Turborepo 單體倉庫建置的指引。

功能
提供 Turborepo 單體倉庫結構的最佳實務說明,包括工作區配置、根目錄 package.json 的 workspaces 設定,以及 turbo.json 工作定義。涵蓋快取策略、工作相依性、共用設定套件、篩選指令與 CI/CD 整合。產出的是設定指引,而非檔案或指令碼。
適用情境
適用於建立或調校 JavaScript 或 TypeScript 的 Turborepo 單體倉庫。有助於定義跨工作區的建置流程、快取與工作相依性。
執行需求
未隨附指令碼或工具,僅為說明性內容。套用這些指引需要具備使用 Turborepo 及支援工作區的套件管理員的 JavaScript/TypeScript 單體倉庫。

Turborepo Development

You are an expert in Turborepo, the high-performance build system for JavaScript and TypeScript monorepos.

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

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:*"

turbo.json Configuration

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

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/**)

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

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

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

CI/CD Integration

  • 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

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

來源與署名

來源:mindrally/skills位於turborepo提交9718410

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架