turborepo
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTurborepo 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:
- - Application workspaces (web apps, APIs, mobile apps)
apps/ - - Shared packages (UI components, utilities, configs)
packages/ - - Build tools and configurations (optional)
tooling/
- Keep the root minimal with workspace configuration
package.json - Use consistent naming conventions across all workspaces
- 按照标准 Turborepo 结构组织工作区:
- - 应用工作区(Web 应用、API、移动应用)
apps/ - - 共享包(UI 组件、工具函数、配置文件)
packages/ - - 构建工具与配置(可选)
tooling/
- 保持根目录 简洁,仅包含工作区配置
package.json - 在所有工作区中使用统一的命名规范
Workspace Configuration
工作区配置
- Define workspaces in the root :
package.jsonjson{ "workspaces": ["apps/*", "packages/*"] } - Each workspace should have its own with proper dependencies
package.json - Use internal package references with workspace protocol:
"@repo/ui": "workspace:*"
- 在根目录 中定义工作区:
package.jsonjson{ "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 for caching
outputs - Mark development tasks with and
cache: falsepersistent: 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: falsepersistent: true
Caching Strategy
缓存策略
- Configure remote caching for CI/CD with Vercel or self-hosted solutions
- Define accurate arrays to ensure proper cache hits
outputs - Use to specify which files affect task caching
inputs - Exclude cache directories from outputs (e.g., )
!.next/cache/**
- 为 CI/CD 配置远程缓存,可使用 Vercel 或自托管方案
- 定义精确的 数组以确保缓存命中
outputs - 使用 指定哪些文件会影响任务缓存
inputs - 在输出中排除缓存目录(例如:)
!.next/cache/**
Task Dependencies
任务依赖
- Use to define task relationships:
dependsOn- - Run build in dependencies first
"^build" - - Run lint in the same package
"lint" - - Run build in a specific package
"@repo/ui#build"
- Define proper task ordering for complex build pipelines
- 使用 定义任务关系:
dependsOn- - 先运行依赖项中的构建任务
"^build" - - 运行当前包中的 lint 任务
"lint" - - 运行指定包中的构建任务
"@repo/ui#build"
- 为复杂构建流水线定义合理的任务顺序
Shared Configurations
共享配置
- Create shared config packages:
- - Shared TypeScript configurations
@repo/typescript-config - - Shared ESLint configurations
@repo/eslint-config - - Shared Tailwind configurations
@repo/tailwind-config
- Reference configs using or imports in workspace configs
extends
- 创建共享配置包:
- - 共享 TypeScript 配置
@repo/typescript-config - - 共享 ESLint 配置
@repo/eslint-config - - 共享 Tailwind 配置
@repo/tailwind-config
- 在工作区配置中通过 或导入的方式引用这些共享配置
extends
Development Workflow
开发工作流
- Use to run development servers across workspaces
turbo dev - Filter commands to specific workspaces:
turbo build --filter=web - Use with patterns:
--filterturbo build --filter=./apps/* - Watch mode with for continuous builds
turbo watch
- 使用 在多个工作区中运行开发服务器
turbo dev - 过滤命令以仅作用于特定工作区:
turbo build --filter=web - 使用带模式的 :
--filterturbo build --filter=./apps/* - 使用 开启监听模式以实现持续构建
turbo watch
CI/CD Integration
CI/CD 集成
- Enable remote caching in CI for faster builds
- Use to preview what would be executed
--dry-run - Implement proper environment variable handling with and
globalEnvenv - 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 for package-specific overrides
turbo.json - 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 项目引用以加快类型检查速度
- 为大型代码库实现增量构建