← 返回
未分类 中文

Turborepo Monorepo Patterns

Use when setting up or managing a Turborepo-based monorepo. Covers workspace configuration, task pipelines, caching strategies, shared packages, and CI/CD in...
用于设置或管理基于 Turborepo 的Monorepo,涵盖工作区配置、任务管道、缓存策略、共享包以及 CI/CD 等。
goldath goldath 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 288
下载
💾 0
安装
1
版本
#latest

概述

Monorepo with Turborepo

A practical guide to building and managing scalable monorepos using Turborepo.

When to Use

  • Setting up a new monorepo with multiple apps/packages
  • Optimizing build/test pipelines with caching
  • Sharing UI components, utilities, or configs across apps
  • Configuring CI for monorepo with selective builds

Core Workflow

1. Initialize Monorepo

npx create-turbo@latest my-monorepo
cd my-monorepo

Workspace layout:

my-monorepo/
├── apps/
│   ├── web/          # Next.js app
│   └── docs/         # Docusaurus
├── packages/
│   ├── ui/           # Shared components
│   ├── config/       # Shared ESLint/TS configs
│   └── utils/        # Shared utilities
├── turbo.json
└── package.json

2. Configure turbo.json Pipeline

{
  "$schema": "https://turbo.build/schema.json",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": [".next/**", "!.next/cache/**", "dist/**"]
    },
    "test": {
      "dependsOn": ["^build"],
      "outputs": ["coverage/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false,
      "persistent": true
    },
    "type-check": {
      "dependsOn": ["^build"],
      "outputs": []
    }
  }
}

3. Package.json Root Config

{
  "name": "my-monorepo",
  "private": true,
  "workspaces": ["apps/*", "packages/*"],
  "scripts": {
    "build": "turbo build",
    "dev": "turbo dev",
    "lint": "turbo lint",
    "test": "turbo test",
    "type-check": "turbo type-check",
    "clean": "turbo clean && rm -rf node_modules"
  },
  "devDependencies": {
    "turbo": "latest"
  }
}

4. Shared Package Setup (packages/ui)

// packages/ui/package.json
{
  "name": "@repo/ui",
  "version": "0.0.1",
  "exports": {
    "./*": {
      "import": "./src/*.tsx",
      "require": "./src/*.tsx"
    }
  },
  "scripts": {
    "build": "tsc",
    "lint": "eslint src/",
    "dev": "tsc --watch"
  }
}

5. Remote Caching (Vercel)

npx turbo login
npx turbo link

Or with custom remote cache:

turbo build --api="https://your-cache-server.com" --token="$TURBO_TOKEN" --team="your-team"

6. Selective Builds (Filter)

# Build only affected packages
turbo build --filter=...[HEAD^1]

# Build specific app and its dependencies
turbo build --filter=web...

# Exclude a package
turbo build --filter=!docs

7. CI/CD Integration (GitHub Actions)

See references/ci-github-actions.yml for a complete workflow.

Key Principles

  • ^ prefix in dependsOn means "build all dependencies first"
  • outputs defines what gets cached; be explicit
  • cache: false for dev/watch tasks
  • persistent: true for long-running processes
  • Always define exports in package.json for shared packages

Troubleshooting

IssueSolution
-----------------
Cache miss every runCheck outputs paths are correct
Circular dependencyUse turbo graph to visualize
Package not foundVerify workspaces glob in root package.json
Slow cold buildEnable remote caching

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-08 01:39 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 76 📥 182,484
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 681 📥 329,528
office-efficiency

中文周报日报自动生成

goldath
自动从要点输入生成专业的周/日工作报告。适用于需要将原始任务笔记整理成精炼的周报/日报的场景。
★ 0 📥 589