refactor: update tsconfig to use TypeScript project references#16505
refactor: update tsconfig to use TypeScript project references#16505ematipico merged 4 commits intowithastro:mainfrom
Conversation
f335cb5 to
b5c76bf
Compare
|
d48b758 to
fceae66
Compare
| async function clean(outdir, cleanDts) { | ||
| const files = await glob('**', { | ||
| cwd: outdir, | ||
| dot: true, |
There was a problem hiding this comment.
Note for reviewers:
By default, glob doesn't include files or directories that start with ., but we want to clean the ._cache/ under dist/.
| "allowJs": true | ||
| }, | ||
| "extends": "./tsconfig.base.json" | ||
| "extends": "./configs/tsconfig.base.json" |
There was a problem hiding this comment.
Note for reviewers:
We no longer need a separate tsconfig.eslint.json for ESLint. It can use the root tsconfig.json to scan the entire monorepo, with the added benefit of the TypeScript cache from pnpm build or pnpm typecheck.
However, I’d prefer to address this in a separate PR to keep this PR a bit smaller and easier to review.
| "test:e2e:match": "cd packages/astro && pnpm playwright install firefox && pnpm run test:e2e:match", | ||
| "test:e2e:hosts": "turbo run test:hosted", | ||
| "typecheck:tests": "pnpm -r run --sequential typecheck:tests", | ||
| "typecheck": "tsc -b", |
There was a problem hiding this comment.
Note for reviewers:
We don’t need a standalone typecheck:tests script in each package. The root typecheck already covers both src/ and test/ across all packages.
0d10ea4 to
1f781dc
Compare
1f781dc to
824ce06
Compare
commit: |
|
I like this, awesome work |
ematipico
left a comment
There was a problem hiding this comment.
Just tested it locally. It's freaking fast on my machine. Amazing work!
|
@ematipico Thanks. I will open a new PR to update |
WHAT is TypeScript project reference?
TypeScript project reference is a TypeScript feature that lets you split a TypeScript codebase into smaller, independently buildable projects. The mental model is similar to a PNPM monorepo: each project declares its dependencies on other projects, and the compiler builds them in the correct order. PNPM monorepo use
package.jsonanddependencies/devDependenciesfields to declare dependency graph, which TypeScript usestsconfig.jsonandreferencesfield to declare the graph.To use TypeScript project references, the following changes are needed:
tsc --buildortsc -binstead oftscortsc --projectcomposite: trueto tsconfignoEmit: falsesince you need to emit and cache declaration filesreferencesfield in tsconfig to declare the dependency graphWHY is it better?
Faster builds. With project references, TypeScript only rebuilds the projects that have actually changed (and their dependents), instead of re-typechecking the entire codebase from scratch on every run. This significantly reduces build times in a large monorepo like this one.
Better editor support. "Go to Definition" can jump straight to the original
.tssource across project boundaries, even beforepnpm build, and across different pnpm packages. See the demo below: it's a fresh clone of the repo with nodist/, and I can still jump fromcloudflare/test/sessions.test.tstocloudflare/src/index.ts(a source file in the same pnpm package) and on topackages/astro/src/types/public/config.ts(a source file in a different pnpm package).astro-jump-to-def.mp4
HOW are tsconfigs organized in this repo?
Shared configs live under
configs/at the repo root:configs/tsconfig.base.json: The base tsconfig that every other tsconfig extends from.configs/tsconfig.build.json: The tsconfig used to build packages. It includessrc/and writes declaration files todist/.configs/tsconfig.test.json: The tsconfig used to typecheck tests. It includestest/and excludestest/fixtures/.configs/tsconfig.language-tools.json: The tsconfig used to build packages underpackages/language-tools/. The main difference fromtsconfig.build.jsonis that it targets commonjs.A classic package at
packages/xx/then has three tsconfig files:The repo-root
tsconfig.jsonis the top-level solution file that references every package:Testing
This PR touches a large number of tsconfig files, which are part of the build pipeline, so it's important to verify that the build artifacts are unchanged after the refactor.
I wrote a script that compares the packed output of this branch against the target branch. The two things it checks are:
dist/directory contains the exact same.jsand.d.tsfiles in both branches..tsbuildinfocache files do not leak into the packed output.The script and its output are available here: https://gist.github.com/ocavue/065faf832d2c45137c93dc2d0f1c72e5
Docs
Not included in this PR yet, but the plan is to add a section to
CONTRIBUTING.mdbased on a refined version of this PR description.