From 342bf0665909090f562a96301d88b017b15fb714 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 23 Apr 2021 22:36:43 +0200 Subject: [PATCH] fix(kit): auto detect version if not specified --- packages/kit/src/nuxt.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/nuxt.ts b/packages/kit/src/nuxt.ts index 5a0c1b63ee..5042c60708 100644 --- a/packages/kit/src/nuxt.ts +++ b/packages/kit/src/nuxt.ts @@ -2,7 +2,7 @@ import { getContext } from 'unctx' import type { Nuxt } from './types/nuxt' import type { NuxtConfig } from './types/config' import type { LoadNuxtConfigOptions } from './config/load' -import { requireModule } from './utils/cjs' +import { requireModule, tryResolveModule } from './utils/cjs' /** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */ export const nuxtCtx = getContext('nuxt') @@ -41,16 +41,23 @@ export interface LoadNuxtOptions extends LoadNuxtConfigOptions { } export async function loadNuxt (opts: LoadNuxtOptions): Promise { + const resolveOpts = { paths: opts.rootDir } + + // Detect version + if (!opts.version) { + opts.version = tryResolveModule('nuxt3', resolveOpts) ? 3 : 2 + } + // Nuxt 3 if (opts.version !== 2) { - const { loadNuxt } = requireModule('nuxt3', { paths: opts.rootDir }) + const { loadNuxt } = requireModule('nuxt3', resolveOpts) const nuxt = await loadNuxt(opts) return nuxt } // Compat // @ts-ignore - const { loadNuxt } = requireModule('nuxt', { paths: opts.rootDir }) + const { loadNuxt } = requireModule('nuxt', resolveOpts) const nuxt = await loadNuxt({ rootDir: opts.rootDir, for: opts.dev ? 'dev' : 'build',