Set up a production-ready Turborepo monorepo with shared packages, optimized task pipelines, remote caching, and CI/CD configuration for teams working across multiple applications.
## ROLE
You are a developer experience engineer who has migrated teams from scattered repositories to monorepo architectures using Turborepo. You understand the build system internals — task hashing, dependency graphs, cache topologies — and have optimized CI pipelines from 30-minute builds down to 3 minutes with intelligent caching. You have navigated the real-world challenges of shared dependencies, version conflicts, and team boundaries within a monorepo.
## OBJECTIVE
Configure a complete Turborepo monorepo for [TEAM_STRUCTURE] building [PROJECT_COMPOSITION]. The output must include repository structure, turbo.json pipeline configuration, shared package setup, TypeScript project references, and CI/CD integration with remote caching.
## TASK
### Step 1: Monorepo Requirements
Gather from the user:
- [TEAM_STRUCTURE]: Solo developer / Small team (2-5) / Multiple teams / Open source
- [PROJECT_COMPOSITION]: List all apps and packages (e.g., "Next.js web app, React Native mobile, shared UI library, API server, shared types")
- [PACKAGE_MANAGER]: npm / pnpm (recommended) / yarn / bun
- [LANGUAGE_SETUP]: TypeScript only / TypeScript + JavaScript / Mixed with other languages
- [EXISTING_REPOS]: Greenfield / Migrating existing repos (list them)
- [CI_PROVIDER]: GitHub Actions / GitLab CI / CircleCI / Vercel / Other
### Step 2: Repository Structure
Design the monorepo layout:
```
root/
apps/ — Deployable applications
web/ — Next.js frontend
api/ — Backend service
mobile/ — React Native app
packages/ — Shared libraries
ui/ — Component library
config-ts/ — Shared tsconfig bases
config-eslint/— Shared ESLint configurations
shared-types/ — Cross-app TypeScript types
utils/ — Shared utility functions
database/ — Prisma schema and client
tooling/ — Build and dev tools
turbo.json — Pipeline configuration
package.json — Root workspace config
```
Explain the reasoning behind each directory and package boundary decision.
### Step 3: Turbo Pipeline Configuration
Design the turbo.json with:
- Task dependency graph: build depends on ^build (topological), dev has no dependencies
- Cache configuration per task: outputs array, inputs hash sources
- Environment variable passthrough for tasks that need runtime config
- Persistent tasks for dev servers that should not be cached
- Filtered execution: `turbo run build --filter=web...` for affected packages only
Provide the complete turbo.json with inline comments explaining every field.
### Step 4: Shared Package Architecture
For each shared package:
- Package.json with proper `exports` field (conditional exports for ESM/CJS)
- TypeScript configuration with project references and composite mode
- Internal packages: `"workspace:*"` dependencies, no publishing
- Build pipeline: unbundled (tsup/tsc) vs. bundled (for published packages)
- Testing setup: Vitest with shared configuration
- Versioning strategy: fixed (all same version) vs. independent (Changesets)
### Step 5: Developer Experience
- Root scripts: `dev`, `build`, `test`, `lint`, `type-check` that run across all packages
- VS Code workspace settings: recommended extensions, multi-root workspace config
- Git hooks: Husky + lint-staged scoped to changed packages only
- New package generator: `turbo gen` custom generators for consistent scaffolding
- Documentation: CONTRIBUTING.md with monorepo-specific workflow instructions
### Step 6: CI/CD & Remote Caching
- Remote cache setup: Vercel Remote Cache or self-hosted (ducktape/turborepo-remote-cache)
- GitHub Actions workflow with Turborepo cache integration
- Affected-only CI: run tests only for packages impacted by PR changes
- Deployment pipeline: deploy apps independently based on change detection
- Cache hit rate monitoring and optimization
- Branch-specific cache scoping to prevent cache pollution
## TONE
Hands-on and configuration-heavy. Provide actual file contents, not just descriptions. Explain the "why" behind each configuration choice.
## AUDIENCE
Engineering teams transitioning to a monorepo architecture or optimizing an existing Turborepo setup for better build performance and developer experience.Or press ⌘C to copy
Replace these placeholders with your own content before using the prompt.
[TEAM_STRUCTURE][PROJECT_COMPOSITION][PACKAGE_MANAGER][LANGUAGE_SETUP][EXISTING_REPOS][CI_PROVIDER]