From 1797bbe4d00dec01f3eafad4172b94b1d2280936 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 21 Jan 2025 12:40:28 +0000 Subject: [PATCH] perf(kit): run components compat check synchronously (#30685) --- packages/kit/src/compatibility.ts | 7 ++++++- packages/kit/src/components.ts | 15 ++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/kit/src/compatibility.ts b/packages/kit/src/compatibility.ts index 58c4a4a90c..2b24d932f1 100644 --- a/packages/kit/src/compatibility.ts +++ b/packages/kit/src/compatibility.ts @@ -14,6 +14,11 @@ const builderMap = { '@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 */ @@ -23,7 +28,7 @@ export async function checkNuxtCompatibility (constraints: NuxtCompatibility, nu // Nuxt version check if (constraints.nuxt) { const nuxtVersion = getNuxtVersion(nuxt) - if (!satisfies(normalizeSemanticVersion(nuxtVersion), constraints.nuxt, { includePrerelease: true })) { + if (!checkNuxtVersion(constraints.nuxt, nuxt)) { issues.push({ name: 'nuxt', message: `Nuxt version \`${constraints.nuxt}\` is required but currently using \`${nuxtVersion}\``, diff --git a/packages/kit/src/components.ts b/packages/kit/src/components.ts index f8415780ff..c735509f26 100644 --- a/packages/kit/src/components.ts +++ b/packages/kit/src/components.ts @@ -1,7 +1,7 @@ import { kebabCase, pascalCase } from 'scule' import type { Component, ComponentsDir } from '@nuxt/schema' import { useNuxt } from './context' -import { assertNuxtCompatibility } from './compatibility' +import { checkNuxtVersion } from './compatibility' import { logger } from './logger' import { MODE_RE } from './utils' @@ -10,9 +10,11 @@ import { MODE_RE } from './utils' * * Requires Nuxt 2.13+ */ -export async function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) { +export function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) { 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 ||= [] dir.priority ||= 0 nuxt.hook('components:dirs', (dirs) => { dirs[opts.prepend ? 'unshift' : 'push'](dir) }) @@ -27,9 +29,12 @@ export type AddComponentOptions = { name: string, filePath: string } & Partial=2.13' }, nuxt) + if (!checkNuxtVersion('>=2.13', nuxt)) { + throw new Error(`\`addComponent\` requires Nuxt 2.13 or higher.`) + } + nuxt.options.components ||= [] if (!opts.mode) {