unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.89k stars 496 forks source link

feat: add hook before prepare:types #2505

Open Barbapapazes opened 3 months ago

Barbapapazes commented 3 months ago

๐Ÿ”— Linked issue

related to #1710

โ“ Type of change

๐Ÿ“š Description

Hello ๐Ÿ‘‹,

This PR allows modules developers to add references or tweak the tsconfig file before saving it.

I also refactor how references are build to easily add a new one (just a path).

This hook is similar to the one in Nuxt.

๐Ÿ“ Checklist

pi0 commented 3 months ago

types:extend hook was added via #1715 to allow extending types via a hook (#1710)

Allow extending more items like declarations is good idea, thinking to do few structure changes to allow it (we don't need a new hook, i hope)

Barbapapazes commented 3 months ago

types:extend hook was added via #1715 to allow extending types via a hook (#1710)

Allow extending more items like declarations is good idea, thinking to do few structure changes to allow it (we don't need a new hook, i hope)

Still in my module, explained here https://github.com/unjs/nitro/pull/2506#issuecomment-2159167362

Yes! My TypeScript knowledge led me updating what I thought was the main file .nuxt/nuxt.d.ts to augment Nuxt and Vue types. After, I tried to do the same to Nitro but it was not possible so I propose this change, similar to the current behavior of Nuxt.

But, after deep diving into the tsconfig file and the files created by Nuxt, it seems that Nuxt automatically add a path to the current module and because it's a directory, it's possible to add .d.ts file which is way more elegant than a template file since it's not dynamic.

The Nuxt module author guide, https://nuxt.com/docs/guide/going-further/modules#adding-type-declarations, is very misleading about the usage of template so typescript and I will open an issue.

While writing these lines, I haven't check if Nitro add a similar line, a reference to the module to allow .d.ts, or if it's come from Nuxt but it could help to have the same (well documented) in Nitro. But there is still the question for dynamic .d.ts file, like the one Nitro generate for the app.config or the routes definitions.