Skip to content

Monorepo - Turborepo ​

Turborepo 是 Vercel 出品的高性能 Monorepo 构建工具。

核心能力 ​

┌─────────────┐     ┌──────────────┐     ┌──────────────┐
│  并行执行    │     │  增量构建    │     │  远程缓存    │
│  build/test │     │  只改啥跑啥  │     │  团队共享    │
└─────────────┘     └──────────────┘     └──────────────┘
┌─────────────┐     ┌──────────────┐     ┌──────────────┐
│  并行执行    │     │  增量构建    │     │  远程缓存    │
│  build/test │     │  只改啥跑啥  │     │  团队共享    │
└─────────────┘     └──────────────┘     └──────────────┘

快速开始 ​

bash
npx create-turbo@latest
npx create-turbo@latest

turbo.json ​

json
{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": [".env"],
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],           // 先构建依赖
      "outputs": ["dist/**"],            // 缓存输出目录
      "inputs": ["src/**", "tsconfig.json"]  // 缓存输入
    },
    "test": {
      "dependsOn": ["build"],
      "inputs": ["src/**", "tests/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false,                    // 开发模式不缓存
      "persistent": true                 // 持续运行
    },
    "typecheck": {
      "dependsOn": ["^build"]
    }
  }
}
{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": [".env"],
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],           // 先构建依赖
      "outputs": ["dist/**"],            // 缓存输出目录
      "inputs": ["src/**", "tsconfig.json"]  // 缓存输入
    },
    "test": {
      "dependsOn": ["build"],
      "inputs": ["src/**", "tests/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false,                    // 开发模式不缓存
      "persistent": true                 // 持续运行
    },
    "typecheck": {
      "dependsOn": ["^build"]
    }
  }
}

常用命令 ​

bash
# 构建所有包(并行 + 缓存)
turbo run build

# 只构建改动的包(增量)
turbo run build --filter=@my/web

# 从远端拉取缓存
turbo run build --remote-only

# 清除本地缓存
turbo clean

# 图形化依赖图
turbo run build --graph
# 构建所有包(并行 + 缓存)
turbo run build

# 只构建改动的包(增量)
turbo run build --filter=@my/web

# 从远端拉取缓存
turbo run build --remote-only

# 清除本地缓存
turbo clean

# 图形化依赖图
turbo run build --graph

缓存策略 ​

本地缓存 ​

默认存储在 node_modules/.cache/turbo/,根据文件内容 hash 判断是否命中。

远程缓存(Vercel Remote Caching) ​

bash
# 登录 Vercel 后自动启用
turbo login
turbo link

# 或使用自定义缓存服务器
# turbo.json 中配置 remoteCache
# 登录 Vercel 后自动启用
turbo login
turbo link

# 或使用自定义缓存服务器
# turbo.json 中配置 remoteCache

与 pnpm 整合 ​

yaml
# pnpm-workspace.yaml
packages:
  - 'apps/*'
  - 'packages/*'
# pnpm-workspace.yaml
packages:
  - 'apps/*'
  - 'packages/*'
json
// package.json
{
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev",
    "lint": "turbo run lint",
    "test": "turbo run test",
    "clean": "turbo clean"
  }
}
// package.json
{
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev",
    "lint": "turbo run lint",
    "test": "turbo run test",
    "clean": "turbo clean"
  }
}

Nx 对比 ​

能力TurborepoNx
配置复杂度低(turbo.json)中高
构建缓存✅ 本地 + Vercel 远程✅ 本地 + Nx Cloud
任务编排✅ 基础 pipeline✅ 高级
依赖图可视化✅✅ 更详细
生成器❌✅ 代码生成
迁移工具❌✅ 从其他 Monorepo 迁移
插件生态较小丰富
学习曲线低中等

最佳实践 ​

json
{
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**"],
      "inputs": ["src/**", "tsconfig.json", "package.json"]
    },
    "dev": {
      "cache": false,
      "persistent": true
    },
    "//#build": {           // 根目录 build
      "dependsOn": ["^build"]
    }
  }
}
{
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**"],
      "inputs": ["src/**", "tsconfig.json", "package.json"]
    },
    "dev": {
      "cache": false,
      "persistent": true
    },
    "//#build": {           // 根目录 build
      "dependsOn": ["^build"]
    }
  }
}

注意事项:

  • .env 和 tsconfig.json 加入 globalDependencies,改变时全局失效
  • outputs 只缓存声明目录,不要漏掉
  • CI 中配置 --filter 只构建受影响的包