fix(cli): support nuxt2-edge

This commit is contained in:
Pooya Parsa 2021-04-28 17:51:42 +02:00
parent 325c6b8fea
commit f0dfb42a58

View File

@ -2,7 +2,7 @@ import { getContext } from 'unctx'
import type { Nuxt } from './types/nuxt' import type { Nuxt } from './types/nuxt'
import type { NuxtConfig } from './types/config' import type { NuxtConfig } from './types/config'
import type { LoadNuxtConfigOptions } from './config/load' import type { LoadNuxtConfigOptions } from './config/load'
import { requireModule, tryResolveModule } from './utils/cjs' import { requireModule, tryRequireModule, tryResolveModule } from './utils/cjs'
/** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */ /** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */
export const nuxtCtx = getContext<Nuxt>('nuxt') export const nuxtCtx = getContext<Nuxt>('nuxt')
@ -57,7 +57,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
// Compat // Compat
// @ts-ignore // @ts-ignore
const { loadNuxt } = requireModule('nuxt', resolveOpts) const { loadNuxt } = tryRequireModule('nuxt-edge', resolveOpts) || requireModule('nuxt', resolveOpts)
const nuxt = await loadNuxt({ const nuxt = await loadNuxt({
rootDir: opts.rootDir, rootDir: opts.rootDir,
for: opts.dev ? 'dev' : 'build', for: opts.dev ? 'dev' : 'build',
@ -68,14 +68,16 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
} }
export function buildNuxt (nuxt: Nuxt): Promise<any> { export function buildNuxt (nuxt: Nuxt): Promise<any> {
const resolveOpts = { paths: nuxt.options.rootDir }
// Nuxt 3 // Nuxt 3
if (nuxt.options._majorVersion === 3) { if (nuxt.options._majorVersion === 3) {
const { build } = requireModule('nuxt3', { paths: nuxt.options.rootDir }) const { build } = requireModule('nuxt3', resolveOpts)
return build(nuxt) return build(nuxt)
} }
// Compat // Compat
// @ts-ignore // @ts-ignore
const { build } = requireModule('nuxt', { paths: nuxt.options.rootDir }) const { build } = tryRequireModule('nuxt-edge', resolveOpts) || requireModule('nuxt', resolveOpts)
return build(nuxt) return build(nuxt)
} }