diff --git a/packages/kit/src/compatibility.ts b/packages/kit/src/compatibility.ts index e650dc0710..a7a5568ad0 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 b10fd98f16..79088591b0 100644 --- a/packages/kit/src/components.ts +++ b/packages/kit/src/components.ts @@ -1,16 +1,18 @@ 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' /** * 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() - 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) }) @@ -23,9 +25,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) {