← Back to TypeScript Course | Chapter 26: Monorepos and Large Projects | Lesson 1 of 5

Monorepo सेट अप करना

एक monorepo कई related packages या applications को एक ही repository में store करता है। TypeScript project configuration packages को types share करने और consistently build करने में help कर सकती है।
Syntax
typescript
// root package.json
{ "workspaces": ["packages/*"] }

// packages/app/tsconfig.json
{
  "references": [{ "path": "../shared" }]
}

Core Concept

एक monorepo कई related TypeScript packages (एक shared library, एक backend, एक frontend) को एक repository में एक dependency tree के साथ रखता है, cross-package refactors को separate repos में coordinated releases की ज़रूरत के बजाय atomic बनाते हुए।

उदाहरण: Core Concept

typescript
// One repo, multiple packages: packages/shared, packages/backend, packages/frontend
console.log("A monorepo makes cross-package refactors atomic, not multi-repo");

Basic Setup

एक basic setup एक workspace tool (npm/yarn/pnpm workspaces या Turborepo) उपयोग करता है जिसमें root package.json में workspaces listed हों, और packages/* के नीचे हर package का अपना package.json और tsconfig.json होता है।

उदाहरण: Basic Setup

typescript
// root package.json: { "workspaces": ["packages/*"] }
// packages/shared/package.json, packages/shared/tsconfig.json, etc.
console.log("Workspace tooling (npm/yarn/pnpm) links packages together");

Typed Example

TypeScript project references (tsconfig.json का references array) एक package के tsconfig को दूसरे की ओर point करने देते हैं, इसलिए tsc --build उन्हें सही dependency order में compile करता है और सिर्फ वही rebuild करता है जो असल में बदला।

उदाहरण: Typed Example

typescript
// packages/app/tsconfig.json
// { "references": [{ "path": "../shared" }] }
console.log("tsc --build compiles referenced packages in dependency order");

Project Usage

एक असली project में, एक monorepo आम तौर पर एक packages/shared-types package रखता है जिस पर frontend और backend दोनों packages depend करते हैं, request/response types को दोनों sides पर copy-paste किए बिना perfectly sync रखते हुए।

उदाहरण: Project Usage

typescript
// packages/shared-types exports API types used by both frontend and backend
console.log("A shared-types package keeps request/response types in sync");

Best Practices

Path aliases को कम उपयोग करें और workspace packages के बीच असली package references को prefer करें, क्योंकि aliases एक package के असली dependency graph को छुपा सकते हैं और tsc --build के incremental ordering को unreliable बना सकते हैं।

उदाहरण: Best Practices

typescript
// Prefer real package references over path aliases between workspace packages
console.log("Aliases can hide the real dependency graph tsc --build relies on");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक package publish करना जो किसी और workspace package पर depend करता है लेकिन इसे अपने dependencies में list करना भूल जाना।
  2. हर package में tsc गलत order में चलाना, project references या एक build tool उपयोग करने के बजाय।
  3. सब कुछ के लिए एक loose root tsconfig.json share करना, जिससे packages को वे options मिलती हैं जो उन्हें नहीं मिलनी चाहिए।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.