fix(kit): load @nuxt/schema from nuxt package dir (#30774)

This commit is contained in:
Daniel Roe 2025-01-28 22:36:04 +01:00
parent b78da56dd7
commit ca2d91f8e0
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
4 changed files with 27 additions and 14 deletions

View File

@ -27,7 +27,6 @@
"test:attw": "attw --pack"
},
"dependencies": {
"@nuxt/schema": "workspace:*",
"c12": "^2.0.1",
"consola": "^3.4.0",
"defu": "^6.1.4",
@ -50,6 +49,7 @@
"untyped": "^1.5.2"
},
"devDependencies": {
"@nuxt/schema": "workspace:*",
"@rspack/core": "1.2.2",
"@types/lodash-es": "4.17.12",
"@types/semver": "7.5.8",

View File

@ -1,27 +1,21 @@
import { existsSync } from 'node:fs'
import { pathToFileURL } from 'node:url'
import type { JSValue } from 'untyped'
import { applyDefaults } from 'untyped'
import type { ConfigLayer, ConfigLayerMeta, LoadConfigOptions } from 'c12'
import { loadConfig } from 'c12'
import type { NuxtConfig, NuxtOptions } from '@nuxt/schema'
import { NuxtConfigSchema } from '@nuxt/schema'
import { globby } from 'globby'
import defu from 'defu'
import { join } from 'pathe'
import { isWindows } from 'std-env'
import { tryResolveModule } from '../internal/esm'
export interface LoadNuxtConfigOptions extends Omit<LoadConfigOptions<NuxtConfig>, 'overrides'> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
overrides?: Exclude<LoadConfigOptions<NuxtConfig>['overrides'], Promise<any> | Function>
}
const layerSchemaKeys = ['future', 'srcDir', 'rootDir', 'serverDir', 'dir']
const layerSchema = Object.create(null)
for (const key of layerSchemaKeys) {
if (key in NuxtConfigSchema) {
layerSchema[key] = NuxtConfigSchema[key]
}
}
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
// Automatically detect and import layers from `~~/layers/` directory
opts.overrides = defu(opts.overrides, {
@ -54,6 +48,16 @@ export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<Nuxt
nuxtConfig.buildDir = join(nuxtConfig.rootDir!, 'node_modules/.cache/nuxt/.nuxt')
}
const NuxtConfigSchema = await loadNuxtSchema(nuxtConfig.rootDir || cwd || process.cwd())
const layerSchemaKeys = ['future', 'srcDir', 'rootDir', 'serverDir', 'dir']
const layerSchema = Object.create(null)
for (const key of layerSchemaKeys) {
if (key in NuxtConfigSchema) {
layerSchema[key] = NuxtConfigSchema[key]
}
}
const _layers: ConfigLayer<NuxtConfig, ConfigLayerMeta>[] = []
const processedLayers = new Set<string>()
for (const layer of layers) {
@ -89,3 +93,13 @@ export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<Nuxt
// Resolve and apply defaults
return await applyDefaults(NuxtConfigSchema, nuxtConfig as NuxtConfig & Record<string, JSValue>) as unknown as NuxtOptions
}
async function loadNuxtSchema (cwd: string) {
const paths = [cwd]
const nuxtPath = await tryResolveModule('nuxt', cwd) ?? await tryResolveModule('nuxt-nightly', cwd)
if (nuxtPath) {
paths.unshift(nuxtPath)
}
const schemaPath = await tryResolveModule('@nuxt/schema', paths) ?? '@nuxt/schema'
return await import(isWindows ? pathToFileURL(schemaPath).href : schemaPath).then(r => r.NuxtConfigSchema)
}

View File

@ -81,7 +81,6 @@ const nightlies = {
export const keyDependencies = [
'@nuxt/kit',
'@nuxt/schema',
]
let warnedAboutCompatDate = false

View File

@ -230,9 +230,6 @@ importers:
packages/kit:
dependencies:
'@nuxt/schema':
specifier: workspace:*
version: link:../schema
c12:
specifier: ^2.0.1
version: 2.0.1(magicast@0.3.5)
@ -294,6 +291,9 @@ importers:
specifier: ^1.5.2
version: 1.5.2
devDependencies:
'@nuxt/schema':
specifier: workspace:*
version: link:../schema
'@rspack/core':
specifier: 1.2.2
version: 1.2.2