feat: support templates
๐ Linked issue
/
โ Type of change
- [ ] ๐ Documentation (updates to the documentation, readme, or JSdoc annotations)
- [ ] ๐ Bug fix (a non-breaking change that fixes an issue)
- [x] ๐ Enhancement (improving an existing functionality like performance)
- [ ] โจ New feature (a non-breaking change that adds functionality)
- [ ] ๐งน Chore (updates to the build process or auxiliary tools and libraries)
- [ ] โ ๏ธ Breaking change (fix or feature that would cause existing functionality to change)
๐ Description
Hello ๐,
This PR is closely related to #2505.
With templates, modules authors could add d.ts file into the buildDir in order to augment some types. (or more usage like in Nuxt).
This is a first bare-bone implementation.
๐ Checklist
- [ ] I have linked an issue or discussion.
- [ ] I have updated the documentation accordingly.
Nitro supports vfs option for virtual templates have you tried it? what missing features would new templates add?
Nitro supports
vfsoption for virtual templates have you tried it? what missing features would new templates add?
I'm trying to augment some Nitro types.
Here a Nitro plugin:
export default defineNitroPlugin((nitro) => {
nitro.log = console.log;
});
And I want to use it into a route:
export default eventHandler(async (event) => {
const nitroApp = useNitroApp();
nitroApp.log("Hello from NitroPack!");
return 'hello';
});
My main issue is how to type correctly the new log. In a Nuxt application, adding more types to the NuxtApp is pretty easy by writing a new file into the buildDir but with Nitro, I do not know how to do it.
In the same time, I want to be able to do it from a Nitro module in order to add this new type to developer that use the module.
With a template, I can generate do:
import { defineNitroConfig } from "nitropack/config";
export default defineNitroConfig({
modules: [
(nitro) => {
nitro.options.templates ||= [];
nitro.options.templates.push({
filename: "data.mjs",
getContents: () => {
return `export default { hello: 'coucou' }`;
},
});
nitro.options.templates.push({
filename: "types/log.d.ts",
getContents: () => {
return `
declare module "nitropack/types" {
interface NitroApp {
log: typeof console.log;
}
}
export {};
`;
},
});
},
],
});
With #2505, I can add log.d.ts to the references to make everything works.
Thanks for the explanation. I think we need to make the API more relevant to generated types then, not confusing with virtual source files.
not planned for now and can be done in userland for actually writing custom files to the filesystem.
Two core functions:
- VFS to extend virtual importable files
- Typegen (to be extendable) to auto-generate types