fix(nuxt): handle schema types for relative module paths (#7946)

This commit is contained in:
Daniel Roe 2022-10-03 14:32:01 +01:00 committed by GitHub
parent e2212ee106
commit 4823b17836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema' import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema'
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork' import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork'
import { isAbsolute, join, relative } from 'pathe' import { isAbsolute, join, relative, resolve } from 'pathe'
import { resolveSchema, generateTypes } from 'untyped' import { resolveSchema, generateTypes } from 'untyped'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
import { hash } from 'ohash' import { hash } from 'ohash'
@ -120,12 +120,14 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
importName: m.entryPath || m.meta?.name importName: m.entryPath || m.meta?.name
})).filter(m => m.configKey && m.name && !adHocModules.includes(m.name)) })).filter(m => m.configKey && m.name && !adHocModules.includes(m.name))
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
return [ return [
"import { NuxtModule } from '@nuxt/schema'", "import { NuxtModule } from '@nuxt/schema'",
"declare module '@nuxt/schema' {", "declare module '@nuxt/schema' {",
' interface NuxtConfig {', ' interface NuxtConfig {',
...moduleInfo.filter(Boolean).map(meta => ...moduleInfo.filter(Boolean).map(meta =>
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>` ` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName.startsWith('.') ? './' + join(relativeRoot, meta.importName) : meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
), ),
' }', ' }',
generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))), generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))),