perf(kit): skip extra module resolution step in loadNuxt (#31176)

This commit is contained in:
Daniel Roe 2025-03-01 12:45:17 +00:00 committed by GitHub
parent 594286f258
commit 66f19ccf41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,8 @@
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import { pathToFileURL } from 'node:url'
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import { resolve } from 'pathe'
import { resolveModulePath } from 'exsolve'
import { interopDefault } from 'mlly'
import { directoryToURL, importModule, tryImportModule } from '../internal/esm'
import { runWithNuxtContext } from '../context'
import type { LoadNuxtConfigOptions } from './config'
@ -21,17 +23,15 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
// Apply dev as config override
opts.overrides.dev = !!opts.dev
const rootURL = directoryToURL(opts.cwd!)
const resolvedPath = ['nuxt-nightly', 'nuxt']
.map(pkg => resolveModulePath(pkg, { try: true, from: [directoryToURL(opts.cwd!)] }))
.filter((p): p is NonNullable<typeof p> => !!p)
.sort((a, b) => b.length - a.length)[0]
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt']
.map(pkg => resolvePackageJSON(pkg, { url: rootURL }).catch(() => null)))
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
if (!nearestNuxtPkg) {
if (!resolvedPath) {
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
}
const pkg = await readPackageJSON(nearestNuxtPkg)
const { loadNuxt } = await importModule<typeof import('nuxt')>((pkg as any)._name || pkg.name, { url: rootURL })
const { loadNuxt } = await import(pathToFileURL(resolvedPath).href).then(r => interopDefault(r)) as typeof import('nuxt')
const nuxt = await loadNuxt(opts)
return nuxt
}