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

This commit is contained in:
Daniel Roe 2025-01-21 12:40:28 +00:00
parent b3f0c140be
commit 1797bbe4d0
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,7 +1,7 @@
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'
@ -10,9 +10,11 @@ import { MODE_RE } from './utils'
* *
* Requires Nuxt 2.13+ * Requires Nuxt 2.13+
*/ */
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) })
@ -27,9 +29,12 @@ export type AddComponentOptions = { name: string, filePath: string } & Partial<E
* *
* Requires Nuxt 2.13+ * Requires Nuxt 2.13+
*/ */
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) {