berry
berry copied to clipboard
feat(plugin-typescript): automatically setup project references
What's the problem this PR addresses?
Proof of concept of automatically setting up project references based on dependencies https://www.typescriptlang.org/docs/handbook/project-references.html
How did you fix it?
Checklist
- [x] I have read the Contributing Guide.
- [ ] I have set the packages that need to be released for my changes to be effective.
- [x] I will check that all automated PR checks pass before the PR gets reviewed.
Just leaving a note here that tsconfig.json format is JSON+Comments. Seems like the current implementation doesn't handle that.
cc @SimenB since Jest (https://github.com/facebook/jest) is using project references and might find this useful and/or have some thoughts on how they could use it
Woo! 🎉 I recently did https://github.com/facebook/jest/pull/10941 since we had messed up our references, having yarn handle it would be awesome. Sorta relates to https://github.com/microsoft/TypeScript/issues/25376, but solves it from the package manager side instead of from TS
You probably found it while researching, but if not - we use https://github.com/azu/monorepo-utils/tree/master/packages/%40monorepo-utils/workspaces-to-typescript-project-references at work. It properly handles comments as was mentioned above here
Hi guys, what's the status of this feature? I would really love to make it work, is there any help needed?
Not a yarn plugin, but I did something similar here: https://beemo.dev/docs/drivers/typescript#sync-project-refs
Would be nice if it was supported by yarn! Here's the actual code: https://github.com/beemojs/beemo/blob/master/packages/driver-typescript/src/commands/syncProjectRefs.ts
What's missing for this to be merged @merceyz @arcanis ?
Another feature I'd love to see as part of this is the option to opt-in/opt-out from some packages. Is this maybe already possible? @merceyz?
Use cases:
- If a sub-package doesn't use TS
- ...
I'd assume it only sets up references if the module referenced has a tsconfig.json file?
I'd assume it only sets up references if the module referenced has a
tsconfig.jsonfile?
Nope, seems to also create new ones if not existing. But I'd like to exclude even packages beyond that case.
One thing to keep in mind is that typescript doesn't support circular references, which are quite easy to create when using workspaces.
Ref: https://github.com/microsoft/TypeScript/issues/33685
If its useful, I did some testing with this today. I have a couple thoughts which may be worth consideration.
- References are currently added for all referenced workspaces, regardless of if they are typescript projects or not (e.g. packages for shared configs like eslint need not be added to tsconfig references). This may be addressed by checking the referenced workspace to see if it contains a
tsconfig.json. - tsconfigs are created for workspaces that had no
tsconfig.jsonpreviously. This issue would mostly be addressed by resolving the above issue, but may be a good early exit check. - It may be worth exposing a configuration option to specify the
tsconfig.jsonfilename, as it can be customised (e.g.tsc --project tsconfig.custom.json).
FYI found some prior art here https://github.com/Dcard/yarn-plugins/tree/master/packages/tsconfig-references, although this plugin uses the "after workspace addition/removal" hooks. Probably "after all installed" makes more sense as it will update references when dependencies change, in addition to workspace addition/removal.
Any chance this could get picked back up? It would be super helpful :)
Moonrepo
On Thu, 1 Feb 2024 at 23:49, Andrew McClenaghan @.***> wrote:
Any chance this could get picked back up? It would be super helpful :)
— Reply to this email directly, view it on GitHub https://github.com/yarnpkg/berry/pull/2338#issuecomment-1922425581, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGTCZG3OTNZM5HWVDHVNT3YRQLWNAVCNFSM4VXR2H62U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJSGI2DENJVHAYQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>