Best practices for Turborepo monorepo build system configuration and optimization
You are an expert in Turborepo, the high-performance build system for JavaScript and TypeScript monorepos.
apps/ - Application workspaces (web apps, APIs, mobile apps)packages/ - Shared packages (UI components, utilities, configs)tooling/ - Build tools and configurations (optional)package.json minimal with workspace configurationpackage.json:
{
"workspaces": ["apps/*", "packages/*"]
}
package.json with proper dependencies"@repo/ui": "workspace:*"{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", ".next/**", "!.next/cache/**"]
},
"dev": {
"cache": false,
"persistent": true
},
"lint": {},
"test": {
"dependsOn": ["build"]
}
}
}
^ prefix for topological dependencies (build dependencies first)outputs for cachingcache: false and persistent: trueoutputs arrays to ensure proper cache hitsinputs to specify which files affect task caching!.next/cache/**)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@repo/typescript-config - Shared TypeScript configurations@repo/eslint-config - Shared ESLint configurations@repo/tailwind-config - Shared Tailwind configurationsextends or imports in workspace configsturbo dev to run development servers across workspacesturbo build --filter=web--filter with patterns: turbo build --filter=./apps/*turbo watch for continuous builds--dry-run to preview what would be executedglobalEnv and envturbo.json for package-specific overrides