mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 07:05:11 +00:00
fix(kit)!: support loading nuxt 4 and drop support for <=2 (#27837)
This commit is contained in:
parent
49959fd9ad
commit
07ad89cd7f
@ -1,6 +1,7 @@
|
|||||||
import { pathToFileURL } from 'node:url'
|
import { pathToFileURL } from 'node:url'
|
||||||
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
|
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
|
||||||
import type { Nuxt } from '@nuxt/schema'
|
import type { Nuxt } from '@nuxt/schema'
|
||||||
|
import { resolve } from 'pathe'
|
||||||
import { importModule, tryImportModule } from '../internal/esm'
|
import { importModule, tryImportModule } from '../internal/esm'
|
||||||
import type { LoadNuxtConfigOptions } from './config'
|
import type { LoadNuxtConfigOptions } from './config'
|
||||||
|
|
||||||
@ -10,64 +11,29 @@ export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
|
|||||||
|
|
||||||
/** Use lazy initialization of nuxt if set to false */
|
/** Use lazy initialization of nuxt if set to false */
|
||||||
ready?: boolean
|
ready?: boolean
|
||||||
|
|
||||||
/** @deprecated Use cwd option */
|
|
||||||
rootDir?: LoadNuxtConfigOptions['cwd']
|
|
||||||
|
|
||||||
/** @deprecated use overrides option */
|
|
||||||
config?: LoadNuxtConfigOptions['overrides']
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||||
// Backward compatibility
|
// Backward compatibility
|
||||||
opts.cwd = opts.cwd || opts.rootDir
|
opts.cwd = resolve(opts.cwd || (opts as any).rootDir /* backwards compat */ || '.')
|
||||||
opts.overrides = opts.overrides || opts.config || {}
|
opts.overrides = opts.overrides || (opts as any).config as {} /* backwards compat */ || {}
|
||||||
|
|
||||||
// Apply dev as config override
|
// Apply dev as config override
|
||||||
opts.overrides.dev = !!opts.dev
|
opts.overrides.dev = !!opts.dev
|
||||||
|
|
||||||
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt3', 'nuxt', 'nuxt-edge']
|
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt']
|
||||||
.map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null)))
|
.map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null)))
|
||||||
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
|
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
|
||||||
if (!nearestNuxtPkg) {
|
if (!nearestNuxtPkg) {
|
||||||
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
|
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
|
||||||
}
|
}
|
||||||
const pkg = await readPackageJSON(nearestNuxtPkg)
|
const pkg = await readPackageJSON(nearestNuxtPkg)
|
||||||
const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]) : ''
|
|
||||||
|
|
||||||
const rootDir = pathToFileURL(opts.cwd || process.cwd()).href
|
const rootDir = pathToFileURL(opts.cwd!).href
|
||||||
|
|
||||||
// Nuxt 3
|
const { loadNuxt } = await importModule((pkg as any)._name || pkg.name, rootDir)
|
||||||
if (majorVersion === 3) {
|
const nuxt = await loadNuxt(opts)
|
||||||
const { loadNuxt } = await importModule((pkg as any)._name || pkg.name, rootDir)
|
return nuxt
|
||||||
const nuxt = await loadNuxt(opts)
|
|
||||||
return nuxt
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nuxt 2
|
|
||||||
const { loadNuxt } = await tryImportModule('nuxt-edge', rootDir) || await importModule('nuxt', rootDir)
|
|
||||||
const nuxt = await loadNuxt({
|
|
||||||
rootDir: opts.cwd,
|
|
||||||
for: opts.dev ? 'dev' : 'build',
|
|
||||||
configOverrides: opts.overrides,
|
|
||||||
ready: opts.ready,
|
|
||||||
envConfig: opts.dotenv, // TODO: Backward format conversion
|
|
||||||
})
|
|
||||||
|
|
||||||
// Mock new hookable methods
|
|
||||||
nuxt.removeHook ||= nuxt.clearHook.bind(nuxt)
|
|
||||||
nuxt.removeAllHooks ||= nuxt.clearHooks.bind(nuxt)
|
|
||||||
nuxt.hookOnce ||= (name: string, fn: (...args: any[]) => any, ...hookArgs: any[]) => {
|
|
||||||
const unsub = nuxt.hook(name, (...args: any[]) => {
|
|
||||||
unsub()
|
|
||||||
return fn(...args)
|
|
||||||
}, ...hookArgs)
|
|
||||||
return unsub
|
|
||||||
}
|
|
||||||
// https://github.com/nuxt/nuxt/tree/main/packages/kit/src/module/define.ts#L111-L113
|
|
||||||
nuxt.hooks ||= nuxt
|
|
||||||
|
|
||||||
return nuxt as Nuxt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
||||||
|
Loading…
Reference in New Issue
Block a user