nuxt / module-builder

Complete solution to build and ship Nuxt modules.
MIT License
212 stars 22 forks source link

Imports types that are not defined on `build --stub` #223

Open rchl opened 4 months ago

rchl commented 4 months ago

Running nuxt-module-build build --stub generates types.d.mts like:

import type { ModuleOptions, ModuleHooks, RuntimeModuleHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module'

declare module '#app' {
  interface RuntimeNuxtHooks extends RuntimeModuleHooks {}
}

declare module '@nuxt/schema' {
  interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
  interface NuxtOptions { ['sentry']?: ModuleOptions }
  interface NuxtHooks extends ModuleHooks {}
  interface RuntimeConfig extends ModuleRuntimeConfig {}
  interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

declare module 'nuxt/schema' {
  interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
  interface NuxtOptions { ['sentry']?: ModuleOptions }
  interface NuxtHooks extends ModuleHooks {}
  interface RuntimeConfig extends ModuleRuntimeConfig {}
  interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

export type { default } from './module'

where the "ModuleHooks, RuntimeModuleHooks, ModuleRuntimeConfig" imports are not exported from "./module".


Running nuxt-module-build build generates correct version:

import type { ModuleOptions, ModulePublicRuntimeConfig } from './module'

declare module '@nuxt/schema' {
  interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
  interface NuxtOptions { ['sentry']?: ModuleOptions }
  interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

declare module 'nuxt/schema' {
  interface NuxtConfig { ['sentry']?: Partial<ModuleOptions> }
  interface NuxtOptions { ['sentry']?: ModuleOptions }
  interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
}

export type { ModuleOptions, ModulePublicRuntimeConfig, default } from './module'
rchl commented 4 months ago

With the latest version of @nuxt/module-builder this issue also triggers a warning that can't be avoided:

 WARN  RuntimeModuleHooks is a deprecated naming and will be removed in the future. Please use ModuleRuntimeHooks instead.

Note that I'm not using RuntimeModuleHooks explicitly anywhere. It's the module builder triggering it with the code it itself generates.

YsarocK commented 3 months ago

Confirms too, have the error with https://github.com/YsarocK/nuxt-wp/. Types are not well generated except when removing --stub option of dev:prepare command.

danielroe commented 3 months ago

You can ignore the warning about RuntimeModuleHooks; it's fixed in https://github.com/nuxt/module-builder/pull/228.

The type files in stub mode are deliberately more expansive to contemplate all possible options you may have while developing (because it's not running continuously so it can't update the file if you add a new export).

What is the issue you are experiencing?

rchl commented 3 months ago

I don't remember what the issue was or in which project I saw it...

I guess we can close it until some better reasoning comes up.

rchl commented 3 months ago

But then... It's not so much about which properties are generated but more about what is exported. Why would there be discrepancy in that regard? If ModuleOptions is generated in both cases then why would it only be exported in one case?