From 71225e50c5bc45c3feb7b189997659d880fe03f3 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Sat, 4 Mar 2023 15:39:26 +0100 Subject: [PATCH] feat(nuxt): enable config schema by default (#19172) --- packages/nuxt/src/core/schema.ts | 35 ++++++++++------------ packages/schema/src/config/experimental.ts | 4 +-- packages/schema/src/types/config.ts | 7 +++-- test/fixtures/basic/nuxt.config.ts | 4 +-- test/fixtures/basic/types.ts | 3 +- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/packages/nuxt/src/core/schema.ts b/packages/nuxt/src/core/schema.ts index 4544bfe920..77fa41f094 100644 --- a/packages/nuxt/src/core/schema.ts +++ b/packages/nuxt/src/core/schema.ts @@ -1,5 +1,5 @@ import { existsSync } from 'node:fs' -import { writeFile, mkdir, rm } from 'node:fs/promises' +import { writeFile, mkdir } from 'node:fs/promises' import { dirname, resolve } from 'pathe' import chokidar from 'chokidar' import { defu } from 'defu' @@ -7,7 +7,6 @@ import { debounce } from 'perfect-debounce' import { defineNuxtModule, createResolver } from '@nuxt/kit' import { resolveSchema as resolveUntypedSchema, - generateMarkdown, generateTypes } from 'untyped' import type { Schema, SchemaDefinition } from 'untyped' @@ -39,9 +38,14 @@ export default defineNuxtModule({ }) // Register module types - nuxt.hook('prepare:types', (ctx) => { + nuxt.hook('prepare:types', async (ctx) => { ctx.references.push({ path: 'nuxt-config-schema' }) ctx.references.push({ path: 'schema/nuxt.schema.d.ts' }) + if (nuxt.options._prepare) { + await nuxt.hooks.callHook('schema:beforeWrite', schema) + await writeSchema(schema) + await nuxt.hooks.callHook('schema:written') + } }) // Resolve schema after all modules initialized @@ -122,13 +126,6 @@ export default defineNuxtModule({ } async function writeSchema (schema: Schema) { - // Avoid writing empty schema - const isEmptySchema = !schema.properties || Object.keys(schema.properties).length === 0 - if (isEmptySchema) { - await rm(resolve(nuxt.options.buildDir, 'schema'), { recursive: true }).catch(() => { }) - return - } - // Write it to build dir await mkdir(resolve(nuxt.options.buildDir, 'schema'), { recursive: true }) await writeFile( @@ -136,12 +133,6 @@ export default defineNuxtModule({ JSON.stringify(schema, null, 2), 'utf8' ) - const markdown = '# Nuxt Custom Config Schema' + generateMarkdown(schema) - await writeFile( - resolve(nuxt.options.buildDir, 'schema/nuxt.schema.md'), - markdown, - 'utf8' - ) const _types = generateTypes(schema, { addExport: true, interfaceName: 'NuxtCustomSchema', @@ -152,12 +143,18 @@ export default defineNuxtModule({ ` export type CustomAppConfig = Exclude +declare module '@nuxt/schema' { + interface NuxtConfig extends NuxtCustomSchema {} + interface NuxtOptions extends NuxtCustomSchema {} + interface CustomAppConfig extends CustomAppConfig {} +} + declare module 'nuxt/schema' { interface NuxtConfig extends NuxtCustomSchema {} interface NuxtOptions extends NuxtCustomSchema {} - interface AppConfigInput extends CustomAppConfig {} - interface AppConfig extends CustomAppConfig {} -}` + interface CustomAppConfig extends CustomAppConfig {} +} +` const typesPath = resolve( nuxt.options.buildDir, 'schema/nuxt.schema.d.ts' diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index 00694d45ac..112233deab 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -115,10 +115,10 @@ export default defineUntypedSchema({ componentIslands: false, /** - * Enable experimental config schema support + * Config schema support * * @see https://github.com/nuxt/nuxt/issues/15592 */ - configSchema: false + configSchema: true } }) diff --git a/packages/schema/src/types/config.ts b/packages/schema/src/types/config.ts index ed9827f875..9bc2602e17 100644 --- a/packages/schema/src/types/config.ts +++ b/packages/schema/src/types/config.ts @@ -135,7 +135,10 @@ export interface RuntimeConfig extends RuntimeConfigNamespace { } // -- App Config -- -export interface AppConfigInput extends Record { + +export interface CustomAppConfig extends Record { } + +export interface AppConfigInput extends CustomAppConfig { /** @deprecated reserved */ private?: never /** @deprecated reserved */ @@ -153,4 +156,4 @@ export interface NuxtAppConfig { keepalive: boolean | KeepAliveProps } -export interface AppConfig { } +export interface AppConfig extends CustomAppConfig { } diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts index df9b888ac2..f09079b074 100644 --- a/test/fixtures/basic/nuxt.config.ts +++ b/test/fixtures/basic/nuxt.config.ts @@ -11,6 +11,7 @@ declare module 'nitropack' { } export default defineNuxtConfig({ + typescript: { strict: true }, app: { pageTransition: true, layoutTransition: true, @@ -161,8 +162,7 @@ export default defineNuxtConfig({ componentIslands: true, reactivityTransform: true, treeshakeClientOnly: true, - payloadExtraction: true, - configSchema: true + payloadExtraction: true }, appConfig: { fromNuxtConfig: true, diff --git a/test/fixtures/basic/types.ts b/test/fixtures/basic/types.ts index 9052f0dcc7..c1ba1b0df7 100644 --- a/test/fixtures/basic/types.ts +++ b/test/fixtures/basic/types.ts @@ -247,8 +247,9 @@ describe('app config', () => { val: number }, userConfig: number + [key: string]: any } - expectTypeOf().toMatchTypeOf() + expectTypeOf().toEqualTypeOf() }) })