Monorepo chứa nhiều packages/apps trong một repo — Turborepo/Nx cho task orchestration với caching (chỉ rebuild package thay đổi); pnpm workspaces là package manager tốt nhất cho monorepo.
- Monorepo là single repository chứa nhiều packages/apps — ngược với polyrepo (mỗi service có repo riêng).
- Lợi ích: code sharing dễ dàng (shared UI library, utils), atomic commits across packages (thay đổi API và consumer trong một commit), unified tooling và CI, dễ refactor cross-package.
- Task orchestration: Turborepo và Nx chạy tasks (build, test, lint) song song và theo dependency order — nếu package B depends on package A, A phải build trước B.
- Affected commands: Nx có nx affected --target=test chỉ chạy tests cho packages bị ảnh hưởng bởi changes — critical cho CI performance trong large monorepos.
- Caching: Turborepo cache build outputs locally và remotely (Vercel Remote Cache) — nếu inputs không thay đổi, reuse cache thay vì rebuild; có thể chia sẻ cache giữa developers và CI.
- Internal packages: packages/ui, packages/utils không publish lên npm, chỉ dùng trong repo — workspace:* protocol trong pnpm để reference.
- Versioning: Changesets tool quản lý versioning và changelogs cho publishable packages.
- Trade-offs monorepo: repo size lớn, CI phức tạp hơn, cần tooling investment; polyrepo dễ hơn cho independent teams và deployments.
- Turborepo vs Nx: Turborepo đơn giản hơn, Nx có nhiều features hơn (generators, devtools, cloud).
A monorepo contains multiple packages/apps in one repository — Turborepo/Nx handle task orchestration with caching (only rebuild changed packages); pnpm workspaces is the best package manager for monorepos.
- A monorepo is a single repository containing multiple packages/apps — the opposite of a polyrepo (each service has its own repo).
- Benefits: easy code sharing (shared UI library, utils), atomic commits across packages (change an API and its consumer in one commit), unified tooling and CI, easier cross-package refactoring.
- Task orchestration: Turborepo and Nx run tasks (build, test, lint) in parallel and in dependency order — if package B depends on package A, A must be built first.
- Affected commands: Nx provides nx affected --target=test to only run tests for packages affected by changes — critical for CI performance in large monorepos.
- Caching: Turborepo caches build outputs locally and remotely (Vercel Remote Cache) — if inputs have not changed, reuses the cache instead of rebuilding; cache can be shared between developers and CI.
- Internal packages: packages/ui and packages/utils are not published to npm, used only within the repo — the workspace:* protocol in pnpm is used to reference them.
- Versioning: the Changesets tool manages versioning and changelogs for publishable packages.
- Trade-offs of monorepo: large repo size, more complex CI, requires tooling investment; polyrepo is simpler for independent teams and deployments.
- Turborepo vs Nx: Turborepo is simpler, Nx has more features (generators, devtools, cloud).