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 从公开仓库中收录这些内容。

举报或申请下架