Skip to content
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

feat: support templates #2506

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

Barbapapazes
Copy link
Member

@Barbapapazes Barbapapazes commented Jun 10, 2024

πŸ”— Linked issue

/

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ 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.

@pi0 pi0 marked this pull request as draft June 10, 2024 19:43
@pi0
Copy link
Member

pi0 commented Jun 10, 2024

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

@Barbapapazes
Copy link
Member Author

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.

src/core/build/types.ts Outdated Show resolved Hide resolved
@pi0
Copy link
Member

pi0 commented Jun 11, 2024

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

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.

2 participants