ရိုးရိုးလေး ရှင်းပြမယ်
Project references ကို ရိုးရိုးလေး ပြောရရင်
စာအုပ်ကြီးတစ်အုပ်ကို အခန်းတွေခွဲပြီး ရေးသလိုပါပဲ။ အခန်းတစ်ခုက တခြားအခန်းကို ညွှန်းချင်ရင် အဲဒီအခန်းတစ်ခုလုံးကို ပြန်မဖတ်ဘဲ အကျဉ်းချုပ် စာမျက်နှာကိုပဲ ကြည့်ပါတယ်။ အခန်း ၃ မှာ စာလုံးပေါင်း တစ်လုံး ပြင်လိုက်ရင် အခန်း ၃ ကိုပဲ ပြန်ပုံနှိပ်ရပြီး အကျဉ်းချုပ် မပြောင်းသရွေ့ ကျန်အခန်းတွေကို မထိရပါဘူး။
TypeScript မှာ အခန်းတစ်ခုက tsconfig.json ကိုယ်ပိုင်ရှိတဲ့ project တစ်ခုဖြစ်ပြီး အကျဉ်းချုပ် စာမျက်နှာက .d.ts declaration file တွေပါ။ references က ဘယ်အခန်းက ဘယ်အခန်းကို မှီခိုလဲ ပြောပြပြီး tsc --build က မှန်ကန်တဲ့ အစီအစဉ်အတိုင်း ပြောင်းသွားတဲ့ အခန်းတွေကိုပဲ ပြန်ပုံနှိပ်ပေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: tsconfig ကြီးတစ်ခုတည်း
Monorepo တစ်ခုမှာ business rule တွေပါတဲ့ domain package၊ အဲဒါကို သုံးတဲ့ web app၊ test တွေနဲ့ script တွေ အားလုံးက tsconfig.json တစ်ခုတည်းအောက်မှာ ရှိနေပါတယ်။ tsc run တိုင်း file အားလုံးကို type-check လုပ်တဲ့အတွက် test တစ်ခုထဲက တစ်ကြောင်းပဲ ပြင်ထားရင်တောင် CI က မိနစ်နဲ့ချီ ကြာပါတယ်။ ပိုဆိုးတာက domain code က web ထဲက React component ကို import လုပ်တာကို ဘာမှ မတားပါဘူး၊ တစ်ခါ ဖြစ်သွားရင် layer တွေ ရောထွေးသွားပြီး သီးခြား publish ဒါမှမဟုတ် build လုပ်လို့ မရတော့ပါဘူး။
{
"compilerOptions": { "strict": true, "outDir": "dist" },
"include": ["packages/**/*", "scripts/**/*", "tests/**/*"]
}ဘာကြောင့် နှေးပြီး ကျိုးလွယ်တာလဲ
Program ကြီးတစ်ခုမှာ cache ကြီးတစ်ခုပဲ ရှိပါတယ်: ပြောင်းလဲမှု တစ်ခုက ဘယ် file ကိုမဆို ထိခိုက်နိုင်လို့ compiler က အားလုံးကို ထည့်စဉ်းစားရပါတယ်။ Project references က package တစ်ခုစီကို publish လုပ်ထားတဲ့ contract (declaration file တွေ) ပါတဲ့ boundary တစ်ခု ပေးပါတယ်။ ဒါဆို compiler က contract ဒါမှမဟုတ် source ပြောင်းသွားတဲ့ package တွေကိုပဲ ပြန် build လုပ်နိုင်ပြီး references မှာ မကြေညာထားတဲ့ boundary ဖြတ် import က တိတ်တဆိတ် dependency မဖြစ်တော့ဘဲ build error ဖြစ်လာပါတယ်။
Core concept: composite project, declaration နဲ့ build mode
Project reference က TypeScript program ကြီးတစ်ခုကို တစ်ခုနဲ့တစ်ခု သိနေတဲ့ program အသေးလေးတွေအဖြစ် ခွဲပေးပါတယ်။ Package တစ်ခုစီမှာ tsconfig.json ကိုယ်ပိုင် ရှိပြီး တခြား package ကို မှီခိုတဲ့ package က အဲဒါကို references အောက်မှာ စာရင်းသွင်းပါတယ်။ Reference လုပ်ခံရတဲ့ project က composite: true ကို သတ်မှတ်ရပါမယ်၊ ဒါက declaration output ကို ဖွင့်ပေးပြီး သီးခြား build လုပ်လို့ လုံခြုံအောင် စည်းမျဉ်းအချို့ကို ထည့်ပေးပါတယ်: source file တိုင်းက include ဒါမှမဟုတ် files နဲ့ ကိုက်ရမယ်၊ rootDir ရဲ့ default က tsconfig ရှိတဲ့ folder ဖြစ်တယ်။
အဓိက ပြောင်းလဲမှုက boundary ကို ဘာဖြတ်ကျော်လဲ ဆိုတာပါ။ web က domain ကနေ import လုပ်တဲ့အခါ compiler က domain ရဲ့ source file တွေကို ပြန်မစစ်ပါဘူး၊ domain ရဲ့ build က ထုတ်ပေးထားတဲ့ .d.ts declaration file တွေကို ဖတ်ပါတယ်။ Declaration တွေက public type တွေရဲ့ ကျစ်လျစ်တဲ့ အကျဉ်းချုပ် ဖြစ်လို့ domain ဘယ်လောက်ကြီးကြီး web ကို စစ်တာ မြန်နေပါတယ်။ declarationMap: true က .d.ts.map file တွေ ရေးပေးလို့ editor တွေက declaration ကနေ မူရင်း .ts source ဆီ ခုန်သွားနိုင်ပါတယ်။
tsc --build (ဒါမှမဟုတ် tsc -b) က references ကို နားလည်တဲ့ build mode ပါ။ Root tsconfig ကို ပေးလိုက်ရင် dependency graph ကို တွက်ပြီး web မတိုင်ခင် domain ကို build လုပ်ကာ project တစ်ခုစီအတွက် ဘာတွေ compile လုပ်ခဲ့လဲ မှတ်တမ်းတင်တဲ့ .tsbuildinfo file တစ်ခုစီ ရေးပါတယ်။ နောက်တစ်ကြိမ် run တဲ့အခါ input မပြောင်းတဲ့ project တိုင်းကို ကျော်ပါတယ်။ domain ပြောင်းပေမဲ့ သူ့ .d.ts output မပြောင်းရင် web ကို လုံးဝ ပြန် build မလုပ်ဘဲ timestamp တွေကိုပဲ update လုပ်ပါတယ်။ Monorepo ကြီးတွေမှာ မြန်တာ အဲဒီကနေ လာတာပါ။
| Command | လုပ်ဆောင်ချက် |
|---|---|
| tsc -b | ခေတ်မမီတော့တဲ့ project တွေကို dependency order အတိုင်း build |
| tsc -b --verbose | project တစ်ခုစီကို ဘာကြောင့် build/skip လုပ်လဲ ရှင်းပြ |
| tsc -b --dry | မ build ဘဲ ဘာတွေ build မလဲ ပြ |
| tsc -b --clean | project အားလုံးရဲ့ output တွေကို ဖျက် |
| tsc -b --force | up-to-date check ကို လျစ်လျူရှုပြီး အားလုံး ပြန် build |
| tsc -b --watch | file ပြောင်းတိုင်း incremental အနေနဲ့ ပြန် build |
Example ထဲက root tsconfig.json မှာ files: [] နဲ့ references ပဲ ပါပါတယ်။ ဒါက solution file ပါ: ကိုယ်ပိုင် code မရှိဘဲ command တစ်ခုတည်းနဲ့ project အားလုံး build လုပ်နိုင်အောင် စာရင်းပဲ ပြုစုထားတာပါ။ files: [] မပါရင် root config က default အတိုင်း သူ့အောက်က .ts file တိုင်းကို ထည့်ပြီး project structure ရဲ့ အပြင်မှာ နောက်တစ်ကြိမ် compile လုပ်ပါလိမ့်မယ်။
- Package တစ်ခုစီ: tsconfig ကိုယ်ပိုင်
- composite: true က .d.ts ထုတ်ပေး
- references က graph ကို ဆွဲပေး
- tsc -b က ပြောင်းတာကိုပဲ build
Downstream project တွေက package ရဲ့ source ကို မဟုတ်ဘဲ declaration တွေကို မြင်ပါတယ်၊ check တွေ မြန်နေတာ အဲဒါကြောင့်ပါ။
အထွေထွေ ပုံစံ (package tsconfig)
{
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../<dependency>" }]
}Reference လုပ်ခံရနိုင်တဲ့ package တိုင်းမှာ composite: true ထားပါ၊ source နဲ့ output folder ကို ခွဲထားပါ၊ ဒီ package က import လုပ်တဲ့ package တိုင်းကို references ထဲ ထည့်ပါ။ path က tsconfig.json ပါတဲ့ folder (ဒါမှမဟုတ် config file ကိုယ်တိုင်) ကို ညွှန်ပါတယ်။
Worked example: root solution file
ဒါက repository root မှာ ထားတဲ့ tsconfig.json ပါ။ packages/domain နဲ့ packages/web မှာ အောက်က deeper section ထဲကလို composite tsconfig ကိုယ်စီ ရှိပါတယ်။ Root ကနေ tsc --build --verbose ကို run လိုက်ရင် ပထမဆုံးအကြိမ်မှာ အောက်ကလို output မျိုး ရပါတယ် (အချိန်နဲ့ absolute path တွေက ကွာပါလိမ့်မယ်)။
{
"files": [],
"references": [
{ "path": "./packages/domain" },
{ "path": "./packages/web" }
]
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- No files of its own
"files": [],Root config က ဘာ file မှ compile မလုပ်ပါဘူး။ ဒါမှ package source တွေကို project boundary အပြင်မှာ နောက်တစ်ကြိမ် ထပ် compile မလုပ်မိပါဘူး။
- List the projects
"references": [tsc -b က build လုပ်ရမယ့် project တွေပါ။ Build order ကို ဒီစာရင်းရဲ့ အစီအစဉ်ကနေ မဟုတ်ဘဲ project တစ်ခုစီရဲ့ ကိုယ်ပိုင် references ကနေ တွက်ပါတယ်။
- Point at a project folder
{ "path": "./packages/domain" },Path က tsconfig.json ပါတဲ့ folder ကို ညွှန်ပါတယ်။ အဲဒီ project မှာ composite: true မရှိရင် tsc -b က TS6306 နဲ့ ရပ်ပါတယ်။
10:15:02 AM - Projects in this build:
* packages/domain/tsconfig.json
* packages/web/tsconfig.json
* tsconfig.json
10:15:02 AM - Project 'packages/domain/tsconfig.json' is out of date because output file 'packages/domain/tsconfig.tsbuildinfo' does not exist
10:15:02 AM - Building project '/repo/packages/domain/tsconfig.json'...
10:15:03 AM - Project 'packages/web/tsconfig.json' is out of date because output file 'packages/web/tsconfig.tsbuildinfo' does not exist
10:15:03 AM - Building project '/repo/packages/web/tsconfig.json'...Package နှစ်ခုကို setup လုပ်ခြင်း
domain က ဘာကိုမှ မမှီခိုပါဘူး။ composite: true နဲ့ declarationMap: true ကို ထားပြီး source ကို src ထဲ၊ output ကို dist ထဲ ခွဲထားပါတယ်။
{
"compilerOptions": {
"composite": true,
"declarationMap": true,
"strict": true,
"module": "nodenext",
"target": "es2022",
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}web က domain ကို reference လုပ်ပါတယ်။ Workspace package နာမည် (ဥပမာ @app/domain) နဲ့ import လုပ်ပြီး domain ရဲ့ package.json က types နဲ့ exports ကို dist ဆီ ညွှန်ထားရင် compiler က domain ရဲ့ .d.ts ကို ဖတ်ပါတယ်။ domain ကနေ web ကို ပြန် import လုပ်ဖို့ ကြိုးစားရင်တော့ source file က domain ရဲ့ project ထဲ မပါလို့ TS6307 (ဒါမှမဟုတ် rootDir ဆိုင်ရာ TS6059) နဲ့ fail ဖြစ်ပါတယ်။
{
"compilerOptions": {
"composite": true,
"strict": true,
"module": "nodenext",
"target": "es2022",
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [{ "path": "../domain" }]
}npx tsc -b --verbose # first build: domain, then web
npx tsc -b --verbose # nothing changed: both reported "up to date"
npx tsc -b --dry # what would be built, without building
npx tsc -b --clean # delete outputs; next build starts freshdomain ရဲ့ function body ကိုပဲ ပြင်ပြီး ပြန် build ကြည့်ပါ။ --verbose က domain ကို build လုပ်ပြီး web အတွက်တော့ "up to date with .d.ts files from its dependencies" လို့ ပြကာ timestamp တွေကိုပဲ update လုပ်ပါတယ်။ Exported function signature ကို ပြောင်းလိုက်မှသာ web ကို အပြည့် ပြန် build လုပ်ပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ project references
Codebase တစ်ခုမှာ package အများကြီး ရှိလာတာ ဒါမှမဟုတ် type-check နှေးလာတာနဲ့ project references က အရေးကြီးလာပါတယ်:
- Monorepo (pnpm, npm ဒါမှမဟုတ် Yarn workspaces): package တစ်ခုစီမှာ composite tsconfig နဲ့ root solution file ရှိပြီး CI က tsc -b ကို တစ်ကြိမ် run ပါတယ်။ .tsbuildinfo file တွေကို run တစ်ခုနဲ့တစ်ခုကြား cache လုပ်ထားရင် မပြောင်းတဲ့ package တွေကို ကျော်ပါတယ်။
- App code ကို test နဲ့ tooling ကနေ ခွဲခြင်း: src project တစ်ခုနဲ့ အဲဒါကို reference လုပ်တဲ့ test project တစ်ခု ထားရင် test အတွက်ပဲ သုံးတဲ့ type နဲ့ global တွေ production build ထဲ မရောက်ပါဘူး။
- Architecture ကို အတင်းအကျပ် ထိန်းခြင်း: domain က web ကို reference မလုပ်ရင် domain က web ကနေ import လုပ်လို့ မရလို့ business rule တွေ UI code ကနေ ကင်းစင်နေပါတယ်။
- Library publish လုပ်ခြင်း: composite project က ထုတ်ပေးတဲ့ declaration file တွေက package ရဲ့ types field မှာ ship လုပ်ရမယ့် အရာ အတိအကျပါ၊ ဒါကြောင့် အသုံးပြုသူတွေက သင့် source မပါဘဲ type တွေ ရပါတယ်။
Compiler အသစ်အကြောင်း
TypeScript team က ဒါတွေအားလုံးကို ပိုမြန်အောင် native (Go-based) compiler တစ်ခုကိုလည်း တည်ဆောက်နေပါတယ်။ အဲဒါကို အားမကိုးခင် လက်ရှိ status နဲ့ ပံ့ပိုးတဲ့ option တွေကို တရားဝင် release notes မှာ စစ်ကြည့်ပါ။ ဒီ lesson က project reference အခြေခံတွေကတော့ အတူတူပါပဲ။
အမှားများ
Project references မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Reference လုပ်ခံရတဲ့ project မှာ composite: true မေ့ခြင်း
tsc -b က error TS6306: Referenced project ... must have setting "composite": true နဲ့ ရပ်ပါတယ်။ တစ်ယောက်ယောက်ရဲ့ references ထဲ ပါတဲ့ project တိုင်းမှာ အဲဒါ လိုပါတယ်။
tsc -b အစား ရိုးရိုး tsc ကို run ခြင်း
--build မပါရင် tsc က project တစ်ခုတည်းကိုပဲ compile လုပ်ပြီး သူ့ reference တွေကို အရင် မ build ပေးပါဘူး၊ ဒါကြောင့် "Cannot find module" ဒါမှမဟုတ် declaration ပျောက်နေတယ်ဆိုတဲ့ error တွေ ရပါတယ်။ Script နဲ့ CI မှာ tsc -b ကို သုံးပါ။
.tsbuildinfo ကို ချန်ပြီး dist ကို ဖျက်ခြင်း
Output folder တွေကို လက်နဲ့ ဖျက်ပြီး .tsbuildinfo file တွေ ကျန်နေရင် tsc -b က project တွေကို up to date လို့ ထင်ပြီး ဘာမှ build မလုပ်တော့ပါဘူး — dist ပျောက်နေလို့ downstream import တွေ fail ဖြစ်ပါတယ်။ Output တွေကို tsc -b --clean နဲ့ ဖျက်ပါ၊ ဒါမှမဟုတ် --force နဲ့ ပြန် build ပါ၊ build info နဲ့ dist နှစ်ခုလုံးကို git ignore လုပ်ထားပါ။
Package တွေကို စက်ဝိုင်းပုံ reference လုပ်ခြင်း
domain က web ကို reference လုပ်ပြီး web က domain ကို reference လုပ်ရင် tsc -b က TS6202: Project references may not form a circular graph နဲ့ ငြင်းပါတယ်။ နှစ်ခုလုံး လိုတဲ့ code ကို တတိယ package တစ်ခုထဲ ရွှေ့ပါ။
Mini challenge
Mini challenge: tsc -b လို build order ကို တွက်ခြင်း
tsc -b နောက်ကွယ်မှာ ဘာဖြစ်လဲ နားလည်ဖို့ project config တွေရဲ့ map တစ်ခုကို လက်ခံပြီး build order ကို ပြန်ပေးတဲ့ buildOrder function ကို TypeScript နဲ့ ရေးပါ။ Project တိုင်းက သူ reference လုပ်တဲ့ project တွေရဲ့ နောက်မှာ လာရပါမယ်။ tsc လိုပဲ composite: true မပါတဲ့ reference နဲ့ circular reference တွေကို ငြင်းရပါမယ်။
- Project တစ်ခုစီဟာ သူ့ references အားလုံးရဲ့ နောက်မှာ ပေါ်ရမယ်
- Reference လုပ်ခံရတဲ့ project မှာ composite: true မပါရင် error throw လုပ်ပါ
- Cycle ရှိရင် a -> b -> a လို path ပါတဲ့ error throw လုပ်ပါ
- Order မှန်တဲ့ graph တစ်ခုနဲ့ cycle ပါတဲ့ graph တစ်ခုအတွက် ရလဒ်ကို print ထုတ်ပါ
type ProjectConfig = {
composite?: boolean;
references?: string[];
};
const projects: Record<string, ProjectConfig> = {
"packages/web": { composite: true, references: ["packages/domain", "packages/ui"] },
"packages/ui": { composite: true, references: ["packages/domain"] },
"packages/domain": { composite: true },
};
function buildOrder(all: Record<string, ProjectConfig>): string[] {
// TODO: depth-first visit; track a "visiting" / "done" state per project
// TODO: throw if a referenced project is missing composite: true
// TODO: throw with the cycle path if you reach a project that is "visiting"
return [];
}
// TODO: print the order for projects, then try a two-project cycleရလဒ်: packages/domain -> packages/ui -> packages/web ကို print ထုတ်ပြီး နောက်ပြီး Circular reference: a -> b -> a ကို print ထုတ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Project တစ်ခုကို order ထဲ မထည့်ခင် သူ့ references တွေကို recursive visit လုပ်ပါ (depth-first)။ လက်ရှိ လမ်းကြောင်းပေါ်မှာ ရှိနေတဲ့ project တွေကို "visiting" လို့ မှတ်ထားပါ၊ အဲဒီ project ကို ထပ်ရောက်လာရင် cycle ပါ။
အဖြေ ကြည့်ရန်
type ProjectConfig = {
composite?: boolean;
references?: string[];
};
const projects: Record<string, ProjectConfig> = {
"packages/web": { composite: true, references: ["packages/domain", "packages/ui"] },
"packages/ui": { composite: true, references: ["packages/domain"] },
"packages/domain": { composite: true },
};
function buildOrder(all: Record<string, ProjectConfig>): string[] {
const order: string[] = [];
const state = new Map<string, "visiting" | "done">();
function visit(name: string, trail: string[]): void {
if (state.get(name) === "done") return;
if (state.get(name) === "visiting") {
throw new Error(`Circular reference: ${[...trail, name].join(" -> ")}`);
}
const project = all[name];
if (!project) throw new Error(`Unknown project: ${name}`);
state.set(name, "visiting");
for (const dep of project.references ?? []) {
if (!all[dep]?.composite) throw new Error(`${dep} must set "composite": true`);
visit(dep, [...trail, name]);
}
state.set(name, "done");
order.push(name);
}
for (const name of Object.keys(all)) visit(name, []);
return order;
}
console.log(buildOrder(projects).join(" -> "));
try {
buildOrder({
a: { composite: true, references: ["b"] },
b: { composite: true, references: ["a"] },
});
} catch (error) {
console.log(error instanceof Error ? error.message : error);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Package တစ်ခုစီကို composite: true ပါတဲ့ ကိုယ်ပိုင် tsconfig ပေးပြီး မှီခိုတဲ့ package တွေကို references မှာ စာရင်းသွင်းပါ။
- Downstream project တွေက source မဟုတ်ဘဲ .d.ts declaration တွေကို ဖတ်လို့ check တွေ မြန်ပြီး boundary တွေ ထင်ရှားပါတယ်။
- tsc -b က dependency order အတိုင်း build လုပ်ပြီး .tsbuildinfo နဲ့ မပြောင်းတဲ့ project တွေကို ကျော်ပါတယ်။
- Root solution file မှာ files: [] ထားပါ၊ ပြဿနာရှိရင် --verbose နဲ့ ကြည့်ပြီး --clean ဒါမှမဟုတ် --force နဲ့ ပြန်စပါ။
Announcing TypeScript 7.0 — TypeScript