2023-01-23 18:07:21 +00:00
|
|
|
import { existsSync } from 'node:fs'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { mkdir, writeFile } from 'node:fs/promises'
|
2023-04-19 21:02:52 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
2023-01-23 18:07:21 +00:00
|
|
|
import { dirname, resolve } from 'pathe'
|
|
|
|
import chokidar from 'chokidar'
|
2023-04-19 21:02:52 +00:00
|
|
|
import { interopDefault } from 'mlly'
|
2023-01-23 18:07:21 +00:00
|
|
|
import { defu } from 'defu'
|
|
|
|
import { debounce } from 'perfect-debounce'
|
2023-09-19 21:26:15 +00:00
|
|
|
import { createResolver, defineNuxtModule, logger, tryResolveModule } from '@nuxt/kit'
|
2023-01-23 18:07:21 +00:00
|
|
|
import {
|
2023-04-07 16:02:47 +00:00
|
|
|
generateTypes,
|
2024-04-05 18:08:32 +00:00
|
|
|
resolveSchema as resolveUntypedSchema,
|
2023-01-23 18:07:21 +00:00
|
|
|
} from 'untyped'
|
|
|
|
import type { Schema, SchemaDefinition } from 'untyped'
|
|
|
|
import untypedPlugin from 'untyped/babel-plugin'
|
|
|
|
import jiti from 'jiti'
|
|
|
|
|
|
|
|
export default defineNuxtModule({
|
|
|
|
meta: {
|
2024-04-05 18:08:32 +00:00
|
|
|
name: 'nuxt-config-schema',
|
2023-01-23 18:07:21 +00:00
|
|
|
},
|
|
|
|
async setup (_, nuxt) {
|
|
|
|
if (!nuxt.options.experimental.configSchema) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const resolver = createResolver(import.meta.url)
|
|
|
|
|
|
|
|
// Initialize untyped/jiti loader
|
|
|
|
const _resolveSchema = jiti(dirname(import.meta.url), {
|
|
|
|
esmResolve: true,
|
|
|
|
interopDefault: true,
|
|
|
|
cache: false,
|
|
|
|
requireCache: false,
|
|
|
|
transformOptions: {
|
|
|
|
babel: {
|
2024-04-05 18:08:32 +00:00
|
|
|
plugins: [untypedPlugin],
|
|
|
|
},
|
|
|
|
},
|
2023-01-23 18:07:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Register module types
|
2023-03-04 14:39:26 +00:00
|
|
|
nuxt.hook('prepare:types', async (ctx) => {
|
2023-01-23 18:07:21 +00:00
|
|
|
ctx.references.push({ path: 'schema/nuxt.schema.d.ts' })
|
2023-03-04 14:39:26 +00:00
|
|
|
if (nuxt.options._prepare) {
|
|
|
|
await writeSchema(schema)
|
|
|
|
}
|
2023-01-23 18:07:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Resolve schema after all modules initialized
|
|
|
|
let schema: Schema
|
|
|
|
nuxt.hook('modules:done', async () => {
|
|
|
|
schema = await resolveSchema()
|
|
|
|
})
|
|
|
|
|
2023-02-12 19:16:42 +00:00
|
|
|
// Write schema after build to allow further modifications
|
2023-03-22 17:08:27 +00:00
|
|
|
nuxt.hooks.hook('build:done', () => writeSchema(schema))
|
2023-01-23 18:07:21 +00:00
|
|
|
|
|
|
|
// Watch for schema changes in development mode
|
|
|
|
if (nuxt.options.dev) {
|
2023-04-19 21:02:52 +00:00
|
|
|
const onChange = debounce(async () => {
|
|
|
|
schema = await resolveSchema()
|
|
|
|
await writeSchema(schema)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (nuxt.options.experimental.watcher === 'parcel') {
|
|
|
|
const watcherPath = await tryResolveModule('@parcel/watcher', [nuxt.options.rootDir, ...nuxt.options.modulesDir])
|
|
|
|
if (watcherPath) {
|
|
|
|
const { subscribe } = await import(pathToFileURL(watcherPath).href).then(interopDefault) as typeof import('@parcel/watcher')
|
|
|
|
for (const layer of nuxt.options._layers) {
|
|
|
|
const subscription = await subscribe(layer.config.rootDir, onChange, {
|
2024-04-05 18:08:32 +00:00
|
|
|
ignore: ['!nuxt.schema.*'],
|
2023-04-19 21:02:52 +00:00
|
|
|
})
|
|
|
|
nuxt.hook('close', () => subscription.unsubscribe())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-10-22 07:23:03 +00:00
|
|
|
logger.warn('Falling back to `chokidar` as `@parcel/watcher` cannot be resolved in your project.')
|
2023-04-19 21:02:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 18:07:21 +00:00
|
|
|
const filesToWatch = await Promise.all(nuxt.options._layers.map(layer =>
|
2024-04-05 18:08:32 +00:00
|
|
|
resolver.resolve(layer.config.rootDir, 'nuxt.schema.*'),
|
2023-01-23 18:07:21 +00:00
|
|
|
))
|
|
|
|
const watcher = chokidar.watch(filesToWatch, {
|
|
|
|
...nuxt.options.watchers.chokidar,
|
2024-04-05 18:08:32 +00:00
|
|
|
ignoreInitial: true,
|
2023-01-23 18:07:21 +00:00
|
|
|
})
|
|
|
|
watcher.on('all', onChange)
|
|
|
|
nuxt.hook('close', () => watcher.close())
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- utils ---
|
|
|
|
|
|
|
|
async function resolveSchema () {
|
|
|
|
// Global import
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error adding to globalThis for 'auto-import' support within nuxt.config file
|
2023-01-23 18:07:21 +00:00
|
|
|
globalThis.defineNuxtSchema = (val: any) => val
|
|
|
|
|
|
|
|
// Load schema from layers
|
|
|
|
const schemaDefs: SchemaDefinition[] = [nuxt.options.$schema]
|
|
|
|
for (const layer of nuxt.options._layers) {
|
|
|
|
const filePath = await resolver.resolvePath(resolve(layer.config.rootDir, 'nuxt.schema'))
|
|
|
|
if (filePath && existsSync(filePath)) {
|
|
|
|
let loadedConfig: SchemaDefinition
|
|
|
|
try {
|
2023-11-29 18:37:34 +00:00
|
|
|
// TODO: fix type for second argument of `import`
|
|
|
|
loadedConfig = await _resolveSchema.import(filePath, {}) as SchemaDefinition
|
2023-01-23 18:07:21 +00:00
|
|
|
} catch (err) {
|
2023-09-19 21:26:15 +00:00
|
|
|
logger.warn(
|
|
|
|
'Unable to load schema from',
|
2023-01-23 18:07:21 +00:00
|
|
|
filePath,
|
2024-04-05 18:08:32 +00:00
|
|
|
err,
|
2023-01-23 18:07:21 +00:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
schemaDefs.push(loadedConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow hooking to extend custom schemas
|
|
|
|
await nuxt.hooks.callHook('schema:extend', schemaDefs)
|
|
|
|
|
|
|
|
// Resolve and merge schemas
|
|
|
|
const schemas = await Promise.all(
|
2024-04-05 18:08:32 +00:00
|
|
|
schemaDefs.map(schemaDef => resolveUntypedSchema(schemaDef)),
|
2023-01-23 18:07:21 +00:00
|
|
|
)
|
|
|
|
|
2023-02-12 19:16:42 +00:00
|
|
|
// Merge after normalization
|
2023-04-14 12:53:21 +00:00
|
|
|
const schema = defu(...schemas as [Schema, Schema])
|
2023-01-23 18:07:21 +00:00
|
|
|
|
|
|
|
// Allow hooking to extend resolved schema
|
|
|
|
await nuxt.hooks.callHook('schema:resolved', schema)
|
|
|
|
|
|
|
|
return schema
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeSchema (schema: Schema) {
|
2023-03-22 17:08:27 +00:00
|
|
|
await nuxt.hooks.callHook('schema:beforeWrite', schema)
|
2023-01-23 18:07:21 +00:00
|
|
|
// Write it to build dir
|
|
|
|
await mkdir(resolve(nuxt.options.buildDir, 'schema'), { recursive: true })
|
|
|
|
await writeFile(
|
|
|
|
resolve(nuxt.options.buildDir, 'schema/nuxt.schema.json'),
|
|
|
|
JSON.stringify(schema, null, 2),
|
2024-04-05 18:08:32 +00:00
|
|
|
'utf8',
|
2023-01-23 18:07:21 +00:00
|
|
|
)
|
|
|
|
const _types = generateTypes(schema, {
|
|
|
|
addExport: true,
|
|
|
|
interfaceName: 'NuxtCustomSchema',
|
2023-03-28 08:55:20 +00:00
|
|
|
partial: true,
|
2024-04-05 18:08:32 +00:00
|
|
|
allowExtraKeys: false,
|
2023-01-23 18:07:21 +00:00
|
|
|
})
|
|
|
|
const types =
|
|
|
|
_types +
|
|
|
|
`
|
|
|
|
export type CustomAppConfig = Exclude<NuxtCustomSchema['appConfig'], undefined>
|
2023-03-11 22:53:28 +00:00
|
|
|
type _CustomAppConfig = CustomAppConfig
|
2023-01-23 18:07:21 +00:00
|
|
|
|
2023-03-04 14:39:26 +00:00
|
|
|
declare module '@nuxt/schema' {
|
2023-03-11 22:53:28 +00:00
|
|
|
interface NuxtConfig extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
|
|
|
interface NuxtOptions extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
|
|
|
interface CustomAppConfig extends _CustomAppConfig {}
|
2023-03-04 14:39:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 22:42:04 +00:00
|
|
|
declare module 'nuxt/schema' {
|
2023-03-11 22:53:28 +00:00
|
|
|
interface NuxtConfig extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
|
|
|
interface NuxtOptions extends Omit<NuxtCustomSchema, 'appConfig'> {}
|
|
|
|
interface CustomAppConfig extends _CustomAppConfig {}
|
2023-03-04 14:39:26 +00:00
|
|
|
}
|
|
|
|
`
|
2023-01-23 18:07:21 +00:00
|
|
|
const typesPath = resolve(
|
|
|
|
nuxt.options.buildDir,
|
2024-04-05 18:08:32 +00:00
|
|
|
'schema/nuxt.schema.d.ts',
|
2023-01-23 18:07:21 +00:00
|
|
|
)
|
|
|
|
await writeFile(typesPath, types, 'utf8')
|
2023-03-22 17:08:27 +00:00
|
|
|
await nuxt.hooks.callHook('schema:written')
|
2023-01-23 18:07:21 +00:00
|
|
|
}
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2023-01-23 18:07:21 +00:00
|
|
|
})
|