TypeScript के Project References
In this page:
{
"references": [{ "path": "./package-a" }],
"files": []
}
Core Concept
Project references आपको एक बड़े TypeScript codebase को छोटे sub-projects में split करने देते हैं, हर एक का अपना tsconfig.json, जबकि tsc --build उन्हें dependency order में compile करता है और किसी भी sub-project को skip करता है जिसके inputs नहीं बदले।
उदाहरण: Core Concept
// core/tsconfig.json and app/tsconfig.json, split from one giant project
console.log("Project references split a codebase into independently built pieces");
Basic Setup
एक basic setup हर referenced sub-project के tsconfig.json में "composite": true जोड़ता है (incremental build info emit होने के लिए required) और consuming project के tsconfig.json में एक references array जो इसकी dependencies के paths की ओर point करे।
उदाहरण: Basic Setup
// core/tsconfig.json: { "compilerOptions": { "composite": true } }
// app/tsconfig.json: { "references": [{ "path": "../core" }] }
console.log("composite: true plus references wires the two projects together");
Typed Example
एक typed example: package core typed utilities export करता है, package app अपने references में { "path": "../core" } list करता है, और app के अंदर core से import करना इसके source को फिर से parse करने के बजाय core की compiled declaration files की ओर resolve होता है।
उदाहरण: Typed Example
// core exports a typed utility, app imports from "core"
// app resolves against core's compiled .d.ts, not core's raw source.
function double(x: number): number { return x * 2; }
console.log(double(5));
Project Usage
एक असली project में, project references एक slow monolithic tsc run को कई छोटे, independently cacheable builds में बदल देते हैं — जो एक codebase कुछ सौ files से बड़ा होने पर critical है।
उदाहरण: Project Usage
// tsc --build builds only what changed, in dependency order
console.log("References turn one slow tsc run into many small cacheable ones");
Best Practices
Project references को हमेशा "composite": true के साथ pair करें और builds को tsc --build (plain tsc नहीं) के through चलाएं, क्योंकि एक composite project का declaration output वह है जो पूरे reference graph को असल में काम करने देता है।
उदाहरण: Best Practices
// Always run: tsc --build (not plain tsc)
console.log("composite + tsc --build is what makes the reference graph work");
composite: trueके बिना एक project reference करना।- एक referenced project edit करना और
tscचलानाtsc -bके बजाय, इसलिए dependents outdated output उपयोग करते हैं। - दो projects के बीच circular references बनाना, जो allowed नहीं है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: