nitro icon indicating copy to clipboard operation
nitro copied to clipboard

feat: support templates

Open Barbapapazes opened this issue 1 year ago โ€ข 3 comments

๐Ÿ”— 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.

Barbapapazes avatar Jun 10 '24 19:06 Barbapapazes

Nitro supports vfs option for virtual templates have you tried it? what missing features would new templates add?

pi0 avatar Jun 10 '24 19:06 pi0

Nitro supports vfs option 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.

Barbapapazes avatar Jun 10 '24 19:06 Barbapapazes

Thanks for the explanation. I think we need to make the API more relevant to generated types then, not confusing with virtual source files.

pi0 avatar Jun 11 '24 08:06 pi0

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

pi0 avatar Sep 28 '24 06:09 pi0