Skills infra-monorepo-turborepo
Turborepo, workspaces, package architecture, @repo/* naming, exports, tree-shaking
git clone https://github.com/agents-inc/skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/agents-inc/skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/dist/plugins/infra-monorepo-turborepo/skills/infra-monorepo-turborepo" ~/.claude/skills/agents-inc-skills-infra-monorepo-turborepo && rm -rf "$T"
dist/plugins/infra-monorepo-turborepo/skills/infra-monorepo-turborepo/SKILL.mdMonorepo Orchestration with Turborepo
Quick Guide: Turborepo 2.x for monorepo orchestration. Task pipelines with dependency ordering. Local + remote caching for massive speed gains. Workspaces for package linking. Syncpack for dependency version consistency. Internal packages use
naming, explicit@repo/*fields, andexportsprotocol.workspace:*
<critical_requirements>
CRITICAL: Before Using This Skill
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
, named constants)import type
(You MUST define task dependencies using
in turbo.json to ensure topological ordering)dependsOn: ["^build"]
(You MUST declare all environment variables in the
array of turbo.json tasks for proper cache invalidation)env
(You MUST set
for tasks with side effects like dev servers and code generation)cache: false
(You MUST use Bun workspaces protocol
for internal package dependencies)workspace:*
(You MUST use
naming convention for ALL internal packages)@repo/*
(You MUST define explicit
field in package.json - never allow importing internal paths)exports
(You MUST mark React as
NOT peerDependencies
in component packages)dependencies
</critical_requirements>
Auto-detection: Turborepo configuration, turbo.json, monorepo setup, workspaces, Bun workspaces, syncpack, task pipelines, @repo/* packages, package.json exports, workspace dependencies, shared configurations
When to use:
- Configuring Turborepo task pipeline and caching strategies
- Setting up workspaces for monorepo package linking
- Enabling remote caching for team/CI cache sharing
- Synchronizing dependency versions across workspace packages
- Creating new internal packages in
packages/ - Configuring package.json exports for tree-shaking
- Setting up shared configuration packages (@repo/eslint-config, @repo/typescript-config)
When NOT to use:
- Single application projects (use standard build tools directly)
- Projects without shared packages (no monorepo benefits)
- Very small projects where setup overhead exceeds caching benefits
- Polyrepo architecture is preferred over monorepo
- Projects already using Nx or Lerna (don't mix monorepo tools)
- App-specific code that won't be shared (keep in app directory)
Key patterns covered:
- Turborepo 2.x task pipeline (dependsOn, outputs, inputs, cache)
- Local and remote caching strategies
- Bun workspaces for package linking
- Syncpack for dependency version consistency
- Environment variable handling in turbo.json
- Package structure and @repo/* naming conventions
- package.json exports for tree-shaking
- Named exports and barrel file patterns
- Internal dependencies with workspace protocol
Detailed Resources:
- For code examples, see examples/core.md (always start here)
- examples/caching.md - Remote caching, CI/CD integration
- examples/workspaces.md - Workspace protocol, syncpack, dependency boundaries
- examples/packages.md - Internal package conventions, exports, creating packages
- For decision frameworks and anti-patterns, see reference.md
<philosophy>
Philosophy
Turborepo is a high-performance build system designed for JavaScript/TypeScript monorepos. It provides intelligent task scheduling, caching, and remote cache sharing to dramatically reduce build times. Combined with Bun workspaces, it enables efficient package management with automatic dependency linking.
When to use Turborepo:
- Managing monorepos with multiple apps and shared packages
- Teams need to share build cache across developers and CI
- Build times are slow and need optimization through caching
- Projects with complex task dependencies requiring topological ordering
When NOT to use Turborepo:
- Single application projects (use standard build tools)
- Projects without shared packages (no monorepo needed)
- Very small projects where setup overhead exceeds benefits
- Polyrepo architecture is preferred over monorepo
<patterns>
Core Patterns
Pattern 1: Turborepo Task Pipeline with Dependency Ordering
Define task dependencies and caching behavior in turbo.json to enable intelligent build orchestration and caching.
Key Concepts
- Run dependency tasks first (topological order)dependsOn: ["^build"]
- Define what files to cacheoutputs
- Specify which files trigger cache invalidationinputs
- Disable caching for tasks with side effectscache: false
- Keep dev servers runningpersistent: true
Configuration
// Good Example - Proper task configuration with dependencies { "tasks": { "build": { "dependsOn": ["^build"], "env": ["DATABASE_URL", "NODE_ENV"], "outputs": ["dist/**", ".next/**", "!.next/cache/**"] }, "test": { "dependsOn": ["^build"], "inputs": [ "$TURBO_DEFAULT$", "src/**/*.tsx", "src/**/*.ts", "test/**/*.ts", "test/**/*.tsx" ] }, "dev": { "cache": false, "persistent": true }, "generate": { "dependsOn": ["^generate"], "cache": false }, "lint": {} } }
Why good:
dependsOn: ["^build"] ensures topological task execution (dependencies build first), env array includes all environment variables for proper cache invalidation, cache: false prevents caching tasks with side effects (dev servers, code generation), outputs specifies cacheable artifacts while excluding cache directories
See examples/core.md for good/bad comparison examples.
Pattern 2: Caching Strategies
Turborepo's caching system dramatically speeds up builds by reusing previous task outputs when inputs haven't changed.
What Gets Cached
- Build outputs (
,dist/
).next/ - Test results (when
)cache: true - Lint results
What Doesn't Get Cached
- Dev servers (
)cache: false - Code generation (
- generates files)cache: false - Tasks with side effects
Cache Invalidation Triggers
- Source file changes
- Dependency changes
- Environment variable changes (when in
array)env - Global dependencies changes (
,.env
)tsconfig.json
Setup: Link Vercel account, set
TURBO_TOKEN and TURBO_TEAM environment variables to enable remote cache sharing.
See examples/caching.md for remote caching configuration and CI integration examples.
Pattern 3: Bun Workspaces for Package Management
Configure Bun workspaces to enable package linking and dependency sharing across monorepo packages.
Workspace Configuration
// Good Example - Properly configured workspaces { "workspaces": ["apps/*", "packages/*"], "dependencies": { "@repo/ui": "workspace:*", "@repo/types": "workspace:*" } }
Why good:
workspace:* protocol links local packages automatically, glob patterns apps/* and packages/* discover all packages dynamically, Bun hoists common dependencies to root reducing duplication
Directory Structure
my-monorepo/ ├── apps/ │ ├── web/ # Frontend application │ ├── admin/ # Admin dashboard │ └── server/ # Backend server ├── packages/ │ ├── ui/ # Shared UI components │ ├── api/ # API client │ ├── eslint-config/ # Shared ESLint config │ ├── prettier-config/ # Shared Prettier config │ └── typescript-config/ # Shared TypeScript config ├── turbo.json # Turborepo configuration └── package.json # Root package.json with workspaces
See examples/workspaces.md for workspace protocol good/bad comparison examples.
</patterns><performance>
Performance Optimization
Cache Hit Metrics:
- First build: ~45s (5 packages, no cache)
- Cached build: ~1s (97% faster with local cache)
- Affected build: ~12s (73% faster, only changed packages rebuild)
- Team savings: Hours per week with remote cache enabled
Optimization Strategies:
- Set
for files affecting all packages (globalDependencies
,.env
) to prevent unnecessary cache invalidationtsconfig.json - Use
array to fine-tune what triggers cache invalidation for specific tasksinputs - Enable remote caching to share artifacts across team and CI
- Use
with affected detection (--filter
) to only run tasks for changed packages--filter=...[HEAD^] - Set
carefully to exclude cache directories (e.g.,outputs
)!.next/cache/**
Force Cache Bypass:
</performance># Ignore cache when needed bun run build --force # Only build affected packages bun run build --filter=...[HEAD^1]
<decision_framework>
Decision Framework
When to Create a New Package
New code to write? ├─ Is it a deployable application? → apps/ ├─ Is it shared across 2+ apps? → packages/ ├─ Is it app-specific? → Keep in app directory └─ Is it a build tool? → tools/
When to Use Turborepo
Is this a monorepo with multiple packages/apps? ├─ NO → Use standard build tools └─ YES → Do builds take > 30s or is caching important? ├─ YES → Use Turborepo └─ NO → Standard tools may suffice
For comprehensive decision trees and package creation criteria, see reference.md.
</decision_framework>
<red_flags>
RED FLAGS
High Priority Issues:
- Missing
for build tasks (breaks topological ordering)dependsOn: ["^build"] - Missing
array in turbo.json (causes cache misses across environments)env - Caching dev servers or code generation (incorrect outputs reused)
- Default exports in library packages (breaks tree-shaking)
- Missing
field in package.json (allows internal path imports)exports
Common Mistakes:
- Hardcoded versions instead of
for internal depsworkspace:* - React in
instead ofdependenciespeerDependencies - Giant barrel files re-exporting everything (negates tree-shaking)
- Running full test suite without
affected detection--filter=...[HEAD^]
Gotchas:
runs dependencies' tasks;dependsOn: ["^task"]
runs same package's taskdependsOn: ["task"]
requires--filter=...[HEAD^]
in GitHub Actionsfetch-depth: 2- Exclude cache directories in outputs:
!.next/cache/**
For detailed anti-patterns and checklists, see reference.md.
</red_flags>
<critical_reminders>
CRITICAL REMINDERS
All code must follow project conventions in CLAUDE.md
(You MUST define task dependencies using
in turbo.json to ensure topological ordering)dependsOn: ["^build"]
(You MUST declare all environment variables in the
array of turbo.json tasks for proper cache invalidation)env
(You MUST set
for tasks with side effects like dev servers and code generation)cache: false
(You MUST use Bun workspaces protocol
for internal package dependencies)workspace:*
(You MUST use
naming convention for ALL internal packages)@repo/*
(You MUST define explicit
field in package.json - never allow importing internal paths)exports
(You MUST mark React as
NOT peerDependencies
in component packages)dependencies
Failure to follow these rules will cause incorrect builds, cache misses, broken dependency resolution, and tree-shaking failures.
</critical_reminders>