Configure a monorepo package
This guide configures one package in a workspace with tsconfig/monorepo,
and references another workspace package so tsc --build compiles them in order.
-
Add the package to the workspace package you are configuring:
Terminal window pnpm add -D typescript @repobuddy/typescript -
Create
tsconfig.jsonin the package. Extend the preset and set the folders:{"extends": "@repobuddy/typescript/tsconfig/monorepo","compilerOptions": {"outDir": "esm","rootDir": "src"},"include": ["src"]}The preset sets
composite, so every source file must be matched byinclude. -
If the package imports another workspace package, add a
referencesentry with the path to that package’s folder (or to its tsconfig file):{"references": [{"path": "../other-package"}]}The referenced package must also have
compositeon. If it extendstsconfig/monorepo, it does. -
Build with
tsc --build, which builds referenced projects first:Terminal window pnpm exec tsc --build -
Add
*.tsbuildinfoand the output folder to.gitignore.compositeturns onincremental, which writes a.tsbuildinfofile.
Finished config
Section titled “Finished config”testcases/build-ts/tsconfig.json in this repository:
{ "extends": "@repobuddy/typescript/tsconfig/monorepo", "compilerOptions": { "outDir": "esm", "rootDir": "src" }, "include": ["src"], "references": [ { "path": "../../packages/typescript" } ]}Verify
Section titled “Verify”- Run
pnpm exec tsc --showConfig. The output includes"composite": trueand"module": "node16". - Run
pnpm exec tsc --build. It exits with code0and writesesm/with.js,.d.ts, and.mapfiles.
- If a tool that reads your tsconfig cannot follow an
extendsarray, extendtsconfig/legacy/monorepoinstead. - In a large solution, add
projects/large-projectto theextendsarray so the editor does not load every referenced project at startup.