fix(nuxi): resolve kit from nuxt modules dir (#19601)

This commit is contained in:
Daniel Roe 2023-03-11 22:36:43 +00:00 committed by GitHub
parent 4de4de1a71
commit 3684de58f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 3 deletions

View File

@ -1,12 +1,24 @@
import { importModule } from './esm' import { importModule, tryResolveModule } from './esm'
export const loadKit = async (rootDir: string): Promise<typeof import('@nuxt/kit')> => { export const loadKit = async (rootDir: string): Promise<typeof import('@nuxt/kit')> => {
try { try {
return await importModule('@nuxt/kit', rootDir) as typeof import('@nuxt/kit') // Without PNP (or if users have a local install of kit, we bypass resolving from nuxt)
const localKit = await tryResolveModule('@nuxt/kit', rootDir)
// Otherwise, we resolve Nuxt _first_ as it is Nuxt's kit dependency that will be used
const rootURL = localKit ? rootDir : await tryResolveNuxt() || rootDir
return await importModule('@nuxt/kit', rootURL) as typeof import('@nuxt/kit')
} catch (e: any) { } catch (e: any) {
if (e.toString().includes("Cannot find module '@nuxt/kit'")) { if (e.toString().includes("Cannot find module '@nuxt/kit'")) {
throw new Error('nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt3` or `@nuxt/bridge` first.') throw new Error('nuxi requires `@nuxt/kit` to be installed in your project. Try installing `nuxt` v3 or `@nuxt/bridge` first.')
} }
throw e throw e
} }
} }
async function tryResolveNuxt () {
for (const pkg of ['nuxt3', 'nuxt', 'nuxt-edge']) {
const path = await tryResolveModule(pkg)
if (path) { return path }
}
return null
}