fix(nuxt): ignore schema types that eval to any (#19835)

This commit is contained in:
Daniel Roe 2023-03-22 17:08:27 +00:00 committed by GitHub
parent fc7867fb0e
commit 9d8c230132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 13 deletions

View File

@ -42,9 +42,7 @@ export default defineNuxtModule({
ctx.references.push({ path: 'nuxt-config-schema' }) ctx.references.push({ path: 'nuxt-config-schema' })
ctx.references.push({ path: 'schema/nuxt.schema.d.ts' }) ctx.references.push({ path: 'schema/nuxt.schema.d.ts' })
if (nuxt.options._prepare) { if (nuxt.options._prepare) {
await nuxt.hooks.callHook('schema:beforeWrite', schema)
await writeSchema(schema) await writeSchema(schema)
await nuxt.hooks.callHook('schema:written')
} }
}) })
@ -55,11 +53,7 @@ export default defineNuxtModule({
}) })
// Write schema after build to allow further modifications // Write schema after build to allow further modifications
nuxt.hooks.hook('build:done', async () => { nuxt.hooks.hook('build:done', () => writeSchema(schema))
await nuxt.hooks.callHook('schema:beforeWrite', schema)
await writeSchema(schema)
await nuxt.hooks.callHook('schema:written')
})
// Watch for schema changes in development mode // Watch for schema changes in development mode
if (nuxt.options.dev) { if (nuxt.options.dev) {
@ -72,9 +66,7 @@ export default defineNuxtModule({
}) })
const onChange = debounce(async () => { const onChange = debounce(async () => {
schema = await resolveSchema() schema = await resolveSchema()
await nuxt.hooks.callHook('schema:beforeWrite', schema)
await writeSchema(schema) await writeSchema(schema)
await nuxt.hooks.callHook('schema:written')
}) })
watcher.on('all', onChange) watcher.on('all', onChange)
nuxt.hook('close', () => watcher.close()) nuxt.hook('close', () => watcher.close())
@ -126,6 +118,7 @@ export default defineNuxtModule({
} }
async function writeSchema (schema: Schema) { async function writeSchema (schema: Schema) {
await nuxt.hooks.callHook('schema:beforeWrite', schema)
// Write it to build dir // Write it to build dir
await mkdir(resolve(nuxt.options.buildDir, 'schema'), { recursive: true }) await mkdir(resolve(nuxt.options.buildDir, 'schema'), { recursive: true })
await writeFile( await writeFile(
@ -161,6 +154,7 @@ declare module 'nuxt/schema' {
'schema/nuxt.schema.d.ts' 'schema/nuxt.schema.d.ts'
) )
await writeFile(typesPath, types, 'utf8') await writeFile(typesPath, types, 'utf8')
await nuxt.hooks.callHook('schema:written')
} }
} }
}) })

View File

@ -204,11 +204,14 @@ ${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from $
declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)} declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
type ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id: string, index: number) => `typeof cfg${index}`).join(', ')}]> type ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id: string, index: number) => `typeof cfg${index}`).join(', ')}]>
type IsAny<T> = 0 extends 1 & T ? true : false
type MergedAppConfig<Resolved extends Record<string, any>, Custom extends Record<string, any>> = { type MergedAppConfig<Resolved extends Record<string, any>, Custom extends Record<string, any>> = {
[K in keyof Resolved]: K extends keyof Custom [K in keyof Resolved]: K extends keyof Custom
? Custom[K] extends Record<string, any> ? Custom[K] extends Record<string, any>
? Resolved[K] extends Record<string, any> ? IsAny<Custom[K]> extends true
? Resolved[K]
: Resolved[K] extends Record<string, any>
? MergedAppConfig<Resolved[K], Custom[K]> ? MergedAppConfig<Resolved[K], Custom[K]>
: Exclude<Custom[K], undefined> : Exclude<Custom[K], undefined>
: Exclude<Custom[K], undefined> : Exclude<Custom[K], undefined>