perf(kit): run components compat check synchronously (#30685)

This commit is contained in:
Daniel Roe 2025-01-21 12:40:28 +00:00
parent 4cce6bf8d5
commit 7cceb25c16
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 16 additions and 6 deletions

View File

@ -14,6 +14,11 @@ const builderMap = {
'@nuxt/webpack-builder': 'webpack', '@nuxt/webpack-builder': 'webpack',
} }
export function checkNuxtVersion (version: string, nuxt: Nuxt = useNuxt()) {
const nuxtVersion = getNuxtVersion(nuxt)
return satisfies(normalizeSemanticVersion(nuxtVersion), version, { includePrerelease: true })
}
/** /**
* Check version constraints and return incompatibility issues as an array * Check version constraints and return incompatibility issues as an array
*/ */
@ -23,7 +28,7 @@ export async function checkNuxtCompatibility (constraints: NuxtCompatibility, nu
// Nuxt version check // Nuxt version check
if (constraints.nuxt) { if (constraints.nuxt) {
const nuxtVersion = getNuxtVersion(nuxt) const nuxtVersion = getNuxtVersion(nuxt)
if (!satisfies(normalizeSemanticVersion(nuxtVersion), constraints.nuxt, { includePrerelease: true })) { if (!checkNuxtVersion(constraints.nuxt, nuxt)) {
issues.push({ issues.push({
name: 'nuxt', name: 'nuxt',
message: `Nuxt version \`${constraints.nuxt}\` is required but currently using \`${nuxtVersion}\``, message: `Nuxt version \`${constraints.nuxt}\` is required but currently using \`${nuxtVersion}\``,

View File

@ -1,16 +1,18 @@
import { kebabCase, pascalCase } from 'scule' import { kebabCase, pascalCase } from 'scule'
import type { Component, ComponentsDir } from '@nuxt/schema' import type { Component, ComponentsDir } from '@nuxt/schema'
import { useNuxt } from './context' import { useNuxt } from './context'
import { assertNuxtCompatibility } from './compatibility' import { checkNuxtVersion } from './compatibility'
import { logger } from './logger' import { logger } from './logger'
import { MODE_RE } from './utils' import { MODE_RE } from './utils'
/** /**
* Register a directory to be scanned for components and imported only when used. * Register a directory to be scanned for components and imported only when used.
*/ */
export async function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) { export function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) {
const nuxt = useNuxt() const nuxt = useNuxt()
await assertNuxtCompatibility({ nuxt: '>=2.13' }, nuxt) if (!checkNuxtVersion('>=2.13', nuxt)) {
throw new Error(`\`addComponentsDir\` requires Nuxt 2.13 or higher.`)
}
nuxt.options.components ||= [] nuxt.options.components ||= []
dir.priority ||= 0 dir.priority ||= 0
nuxt.hook('components:dirs', (dirs) => { dirs[opts.prepend ? 'unshift' : 'push'](dir) }) nuxt.hook('components:dirs', (dirs) => { dirs[opts.prepend ? 'unshift' : 'push'](dir) })
@ -23,9 +25,12 @@ export type AddComponentOptions = { name: string, filePath: string } & Partial<E
/** /**
* Register a component by its name and filePath. * Register a component by its name and filePath.
*/ */
export async function addComponent (opts: AddComponentOptions) { export function addComponent (opts: AddComponentOptions) {
const nuxt = useNuxt() const nuxt = useNuxt()
await assertNuxtCompatibility({ nuxt: '>=2.13' }, nuxt) if (!checkNuxtVersion('>=2.13', nuxt)) {
throw new Error(`\`addComponent\` requires Nuxt 2.13 or higher.`)
}
nuxt.options.components ||= [] nuxt.options.components ||= []
if (!opts.mode) { if (!opts.mode) {