mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
feat(nuxt): enable config schema by default (#19172)
This commit is contained in:
parent
44d4aba283
commit
71225e50c5
@ -1,5 +1,5 @@
|
|||||||
import { existsSync } from 'node:fs'
|
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 { dirname, resolve } from 'pathe'
|
||||||
import chokidar from 'chokidar'
|
import chokidar from 'chokidar'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
@ -7,7 +7,6 @@ import { debounce } from 'perfect-debounce'
|
|||||||
import { defineNuxtModule, createResolver } from '@nuxt/kit'
|
import { defineNuxtModule, createResolver } from '@nuxt/kit'
|
||||||
import {
|
import {
|
||||||
resolveSchema as resolveUntypedSchema,
|
resolveSchema as resolveUntypedSchema,
|
||||||
generateMarkdown,
|
|
||||||
generateTypes
|
generateTypes
|
||||||
} from 'untyped'
|
} from 'untyped'
|
||||||
import type { Schema, SchemaDefinition } from 'untyped'
|
import type { Schema, SchemaDefinition } from 'untyped'
|
||||||
@ -39,9 +38,14 @@ export default defineNuxtModule({
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Register module types
|
// 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: '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) {
|
||||||
|
await nuxt.hooks.callHook('schema:beforeWrite', schema)
|
||||||
|
await writeSchema(schema)
|
||||||
|
await nuxt.hooks.callHook('schema:written')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Resolve schema after all modules initialized
|
// Resolve schema after all modules initialized
|
||||||
@ -122,13 +126,6 @@ export default defineNuxtModule({
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function writeSchema (schema: Schema) {
|
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
|
// 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(
|
||||||
@ -136,12 +133,6 @@ export default defineNuxtModule({
|
|||||||
JSON.stringify(schema, null, 2),
|
JSON.stringify(schema, null, 2),
|
||||||
'utf8'
|
'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, {
|
const _types = generateTypes(schema, {
|
||||||
addExport: true,
|
addExport: true,
|
||||||
interfaceName: 'NuxtCustomSchema',
|
interfaceName: 'NuxtCustomSchema',
|
||||||
@ -152,12 +143,18 @@ export default defineNuxtModule({
|
|||||||
`
|
`
|
||||||
export type CustomAppConfig = Exclude<NuxtCustomSchema['appConfig'], undefined>
|
export type CustomAppConfig = Exclude<NuxtCustomSchema['appConfig'], undefined>
|
||||||
|
|
||||||
|
declare module '@nuxt/schema' {
|
||||||
|
interface NuxtConfig extends NuxtCustomSchema {}
|
||||||
|
interface NuxtOptions extends NuxtCustomSchema {}
|
||||||
|
interface CustomAppConfig extends CustomAppConfig {}
|
||||||
|
}
|
||||||
|
|
||||||
declare module 'nuxt/schema' {
|
declare module 'nuxt/schema' {
|
||||||
interface NuxtConfig extends NuxtCustomSchema {}
|
interface NuxtConfig extends NuxtCustomSchema {}
|
||||||
interface NuxtOptions extends NuxtCustomSchema {}
|
interface NuxtOptions extends NuxtCustomSchema {}
|
||||||
interface AppConfigInput extends CustomAppConfig {}
|
interface CustomAppConfig extends CustomAppConfig {}
|
||||||
interface AppConfig extends CustomAppConfig {}
|
}
|
||||||
}`
|
`
|
||||||
const typesPath = resolve(
|
const typesPath = resolve(
|
||||||
nuxt.options.buildDir,
|
nuxt.options.buildDir,
|
||||||
'schema/nuxt.schema.d.ts'
|
'schema/nuxt.schema.d.ts'
|
||||||
|
@ -115,10 +115,10 @@ export default defineUntypedSchema({
|
|||||||
componentIslands: false,
|
componentIslands: false,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable experimental config schema support
|
* Config schema support
|
||||||
*
|
*
|
||||||
* @see https://github.com/nuxt/nuxt/issues/15592
|
* @see https://github.com/nuxt/nuxt/issues/15592
|
||||||
*/
|
*/
|
||||||
configSchema: false
|
configSchema: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -135,7 +135,10 @@ export interface RuntimeConfig extends RuntimeConfigNamespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -- App Config --
|
// -- App Config --
|
||||||
export interface AppConfigInput extends Record<string, any> {
|
|
||||||
|
export interface CustomAppConfig extends Record<string, any> { }
|
||||||
|
|
||||||
|
export interface AppConfigInput extends CustomAppConfig {
|
||||||
/** @deprecated reserved */
|
/** @deprecated reserved */
|
||||||
private?: never
|
private?: never
|
||||||
/** @deprecated reserved */
|
/** @deprecated reserved */
|
||||||
@ -153,4 +156,4 @@ export interface NuxtAppConfig {
|
|||||||
keepalive: boolean | KeepAliveProps
|
keepalive: boolean | KeepAliveProps
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AppConfig { }
|
export interface AppConfig extends CustomAppConfig { }
|
||||||
|
4
test/fixtures/basic/nuxt.config.ts
vendored
4
test/fixtures/basic/nuxt.config.ts
vendored
@ -11,6 +11,7 @@ declare module 'nitropack' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
|
typescript: { strict: true },
|
||||||
app: {
|
app: {
|
||||||
pageTransition: true,
|
pageTransition: true,
|
||||||
layoutTransition: true,
|
layoutTransition: true,
|
||||||
@ -161,8 +162,7 @@ export default defineNuxtConfig({
|
|||||||
componentIslands: true,
|
componentIslands: true,
|
||||||
reactivityTransform: true,
|
reactivityTransform: true,
|
||||||
treeshakeClientOnly: true,
|
treeshakeClientOnly: true,
|
||||||
payloadExtraction: true,
|
payloadExtraction: true
|
||||||
configSchema: true
|
|
||||||
},
|
},
|
||||||
appConfig: {
|
appConfig: {
|
||||||
fromNuxtConfig: true,
|
fromNuxtConfig: true,
|
||||||
|
3
test/fixtures/basic/types.ts
vendored
3
test/fixtures/basic/types.ts
vendored
@ -247,8 +247,9 @@ describe('app config', () => {
|
|||||||
val: number
|
val: number
|
||||||
},
|
},
|
||||||
userConfig: number
|
userConfig: number
|
||||||
|
[key: string]: any
|
||||||
}
|
}
|
||||||
expectTypeOf<AppConfig>().toMatchTypeOf<ExpectedMergedAppConfig>()
|
expectTypeOf<AppConfig>().toEqualTypeOf<ExpectedMergedAppConfig>()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user