Skip to content

build: typescript project references #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2025

Conversation

BarryThePenguin
Copy link
Contributor

@BarryThePenguin BarryThePenguin commented Mar 28, 2025

This is heavily based on the https://github.com/typescript-eslint/typescript-eslint monorepo. The TypeScript documentation for Project References is also useful.

There are 3 main tsconfig files:

  1. tsconfig.base.json the base configuration that everything else extends from
  2. tsconfig.json that references all the workspaces in the monorepo
  3. tsconfig.tsup.json that is used by tsup when building a single workspace

Then each workspace has it's own tsconfig.json file that is referenced by the root tsconfig.json to include the following files:

  1. package/*/tsconfig.build.json used to build/typecheck the workspace files
  2. package/*/tsconfig.spec.json used to typecheck test files

The benefits of using project references include:

  • A single yarn typecheck command to check the types across the whole monorepo
  • While also limiting yarn typecheck to an individual workspaces, which is useful for keeping CI quick
  • Also allows type checking between workspaces, eg; @hono/zod-openapi and @hono/zod-validator
  • Also opens up linting with type information using the projectService configuration.

This is a lot of extra configuration, so I understand if the overhead of managing it all outweighs the benefits of running typescript across the entire monorepo.

The alternative, which is basically what exists now, would be just running typescript in individual workspaces. Though it doesn't come with the benefits that I mentioned above..

@codecov-commenter
Copy link

codecov-commenter commented Mar 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.50%. Comparing base (5dd598f) to head (4ce55d1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1077   +/-   ##
=======================================
  Coverage   79.50%   79.50%           
=======================================
  Files          77       77           
  Lines        2279     2279           
  Branches      577      577           
=======================================
  Hits         1812     1812           
  Misses        391      391           
  Partials       76       76           
Flag Coverage Δ
ajv-validator 0.74% <ø> (-0.01%) ⬇️
arktype-validator 0.57% <ø> (-0.01%) ⬇️
auth-js 2.98% <ø> (-0.02%) ⬇️
casbin 1.44% <ø> (-0.01%) ⬇️
class-validator 0.83% <ø> (-0.01%) ⬇️
clerk-auth 0.92% <ø> (-0.01%) ⬇️
cloudflare-access 2.72% <ø> (ø)
conform-validator 1.09% <ø> (-0.01%) ⬇️
effect-validator 0.39% <ø> (-0.01%) ⬇️
esbuild-transpiler 0.96% <ø> (-0.01%) ⬇️
event-emitter 2.50% <ø> (-0.02%) ⬇️
firebase-auth 2.45% <ø> (-0.02%) ⬇️
graphql-server 4.12% <ø> (-0.03%) ⬇️
hello 0.17% <ø> (-0.01%) ⬇️
medley-router 0.43% <ø> (-0.01%) ⬇️
node-ws 2.36% <ø> (ø)
oauth-providers 26.28% <ø> (ø)
oidc-auth 9.96% <ø> (-0.06%) ⬇️
otel 0.87% <ø> (-0.01%) ⬇️
prometheus 1.57% <ø> (-0.01%) ⬇️
react-renderer 1.36% <ø> (-0.01%) ⬇️
sentry 0.74% <ø> (-0.01%) ⬇️
standard-validator 1.40% <ø> (-0.01%) ⬇️
swagger-editor 0.61% <ø> (-0.01%) ⬇️
swagger-ui 1.66% <ø> (-0.01%) ⬇️
trpc-server 0.52% <ø> (-0.01%) ⬇️
tsyringe 0.26% <ø> (-0.01%) ⬇️
typebox-validator 1.36% <ø> (-0.01%) ⬇️
typia-validator 1.88% <ø> (-0.02%) ⬇️
valibot-validator 0.48% <ø> (-0.01%) ⬇️
zod-openapi 5.17% <ø> (-0.03%) ⬇️
zod-validator 0.57% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BarryThePenguin BarryThePenguin force-pushed the refactor/project-references branch from e440863 to b9ff262 Compare March 28, 2025 11:46
Copy link

changeset-bot bot commented Mar 28, 2025

⚠️ No Changeset found

Latest commit: 4ce55d1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@BarryThePenguin BarryThePenguin force-pushed the refactor/project-references branch 2 times, most recently from c1f764f to 056fd2c Compare March 28, 2025 11:52
@yusukebe
Copy link
Member

Hi @BarryThePenguin

I may be wrong, but even though it does not throw errors when I run the yarn typecheck command, it throws errors when I run yarn build. My understanding is that it should throw errors even with yarn typecheck. What do you think? The following is an example of @hono/node-ws.

CleanShot 2025-03-30 at 18 06 48@2x

@BarryThePenguin
Copy link
Contributor Author

@hono/node-ws is a tricky one as it uses types from @types/ws. From what I understand, the typecheck command and tsup might be resolving these types from different places which is why they give different results. Thanks for taking a look!

@BarryThePenguin BarryThePenguin force-pushed the refactor/project-references branch 4 times, most recently from ed01644 to c2d3fd8 Compare March 31, 2025 22:23
Copy link
Contributor Author

@BarryThePenguin BarryThePenguin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure what is wrong with @hono/node-ws... it looks like it might be an existing issue? Though I can't reproduce it outside of tsup 😩

I noticed there were a few older issues like honojs/hono#3327, might be related?

"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsBuildInfoFile is used along with incremental (enabled with composite: true) to provide some performance improvements when building the project. They don't have to go in the dist directory, it's just where the typescript-eslint project puts them

"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/tsconfig.build.tsbuildinfo",
"emitDeclarationOnly": false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change this as we use tsup to build the output.

One benefit of having tcs emit declaration files during the build is it can generate declaration maps. They enable features like "Go to Definition" in tools like vscode. There are some conversations as to if these files are worth publishing to npm, tsup also mentions it in their documentation.

There are also a few issues about this:

Comment on lines +9 to +10
"include": ["src/**/*.ts"],
"exclude": ["**/*.test.ts"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only include src files in the build, while excluding .test.ts files.

The big benefit of this is it prevents test related globals from appearing as valid references in src files, eg; describe, test, expect from vitest

"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc/packages/ajv-validator",
"types": ["vitest/globals"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow .test.ts files to reference vitest/globals

"outDir": "../../dist/out-tsc/packages/ajv-validator",
"types": ["vitest/globals"]
},
"include": ["**/*.test.ts"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only include .test.ts files in this project

Comment on lines +8 to +12
"references": [
{
"path": "./tsconfig.build.json"
}
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Along with a reference to the project that includes the src files

Comment on lines +4 to +6
"composite": false,
"jsx": "react",
"types": ["@cloudflare/workers-types", "node", "ws"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsup struggles to understand composite projects, so it's simpler to just disable them

It also needs a reference to any types needed during build, though the jsx configuration can probably just go in tsconfig.base.json 🤔

@yusukebe
Copy link
Member

yusukebe commented Apr 1, 2025

Hey @BarryThePenguin

The @hono/node-ws issue may be fixed by honojs/hono#4036.

@yusukebe
Copy link
Member

yusukebe commented Apr 1, 2025

Oops. Accidentally closed. Reopened.

@yusukebe
Copy link
Member

yusukebe commented Apr 1, 2025

Hi @BarryThePenguin

The @hono/node-ws issue has been resolved by #1094 (Thanks @nakasyou!)

@BarryThePenguin BarryThePenguin force-pushed the refactor/project-references branch from 3c14afa to 4ce55d1 Compare April 1, 2025 19:47
@BarryThePenguin
Copy link
Contributor Author

Awesome, looks good now 👍🏻 Thanks both!

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

yusukebe commented Apr 2, 2025

@BarryThePenguin

Looks good! I'll merge this now. Thank you!

@yusukebe yusukebe merged commit e8512f0 into honojs:main Apr 2, 2025
35 checks passed
@BarryThePenguin BarryThePenguin deleted the refactor/project-references branch April 2, 2025 09:43
TimBarley pushed a commit to TimBarley/honojs-middleware that referenced this pull request Apr 9, 2025
* build: typescript project references

* chore: remove duplicate keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants