fix(nuxt): augment NuxtOptions as well as config (#28747)

This commit is contained in:
Daniel Roe 2024-08-28 22:19:20 +01:00
parent 1f87a4ea50
commit 7b99ecca4d
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -205,7 +205,7 @@ export const schemaTemplate: NuxtTemplate = {
} }
} }
const moduleOptionsInterface = (jsdocTags: boolean) => [ const moduleOptionsInterface = (options: { addJSDocTags: boolean, unresolved: boolean }) => [
...modules.flatMap(([configKey, importName, mod]) => { ...modules.flatMap(([configKey, importName, mod]) => {
let link: string | undefined let link: string | undefined
@ -235,30 +235,32 @@ export const schemaTemplate: NuxtTemplate = {
return [ return [
` /**`, ` /**`,
` * Configuration for \`${importName}\``, ` * Configuration for \`${importName}\``,
...jsdocTags && link ...options.addJSDocTags && link ? [` * @see ${link}`] : [],
? [
` * @see ${link}`,
]
: [],
` */`, ` */`,
` [${configKey}]?: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`, ` [${configKey}]${options.unresolved ? '?' : ''}: typeof ${genDynamicImport(importName, { wrapper: false })}.default extends NuxtModule<infer O> ? ${options.unresolved ? 'Partial<O>' : 'O'} : Record<string, any>`,
] ]
}), }),
modules.length > 0 ? ` modules?: (undefined | null | false | NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName, mod]) => `[${genString(mod.meta?.rawPath || importName)}, Exclude<NuxtConfig[${configKey}], boolean>]`).join(' | ')})[],` : '', modules.length > 0 && options.unresolved ? ` modules?: (undefined | null | false | NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName, mod]) => `[${genString(mod.meta?.rawPath || importName)}, Exclude<NuxtConfig[${configKey}], boolean>]`).join(' | ')})[],` : '',
].filter(Boolean) ].filter(Boolean)
return [ return [
'import { NuxtModule, RuntimeConfig } from \'@nuxt/schema\'', 'import { NuxtModule, RuntimeConfig } from \'@nuxt/schema\'',
'declare module \'@nuxt/schema\' {', 'declare module \'@nuxt/schema\' {',
' interface NuxtOptions {',
...moduleOptionsInterface({ addJSDocTags: false, unresolved: false }),
' }',
' interface NuxtConfig {', ' interface NuxtConfig {',
// TypeScript will duplicate the jsdoc tags if we augment it twice // TypeScript will duplicate the jsdoc tags if we augment it twice
// So here we only generate tags for `nuxt/schema` // So here we only generate tags for `nuxt/schema`
...moduleOptionsInterface(false), ...moduleOptionsInterface({ addJSDocTags: false, unresolved: true }),
' }', ' }',
'}', '}',
'declare module \'nuxt/schema\' {', 'declare module \'nuxt/schema\' {',
' interface NuxtOptions {',
...moduleOptionsInterface({ addJSDocTags: true, unresolved: false }),
' }',
' interface NuxtConfig {', ' interface NuxtConfig {',
...moduleOptionsInterface(true), ...moduleOptionsInterface({ addJSDocTags: true, unresolved: true }),
' }', ' }',
generateTypes(await resolveSchema(privateRuntimeConfig as Record<string, JSValue>), generateTypes(await resolveSchema(privateRuntimeConfig as Record<string, JSValue>),
{ {