mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-21 16:09:52 +00:00
fix(kit): ensure nuxt is loaded from cwd rather than parent dir (#30910)
This commit is contained in:
parent
88b2642dad
commit
7393c255c3
@ -2,6 +2,7 @@ import { pathToFileURL } from 'node:url'
|
||||
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
|
||||
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
|
||||
import { resolve } from 'pathe'
|
||||
import { withTrailingSlash } from 'ufo'
|
||||
import { importModule, tryImportModule } from '../internal/esm'
|
||||
import { runWithNuxtContext } from '../context'
|
||||
import type { LoadNuxtConfigOptions } from './config'
|
||||
@ -22,23 +23,23 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||
// Apply dev as config override
|
||||
opts.overrides.dev = !!opts.dev
|
||||
|
||||
const rootDir = withTrailingSlash(pathToFileURL(opts.cwd!).href)
|
||||
|
||||
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt']
|
||||
.map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null)))
|
||||
.map(pkg => resolvePackageJSON(pkg, { url: rootDir }).catch(() => null)))
|
||||
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
|
||||
if (!nearestNuxtPkg) {
|
||||
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
|
||||
}
|
||||
const pkg = await readPackageJSON(nearestNuxtPkg)
|
||||
|
||||
const rootDir = pathToFileURL(opts.cwd!).href
|
||||
|
||||
const { loadNuxt } = await importModule<typeof import('nuxt')>((pkg as any)._name || pkg.name, { paths: rootDir })
|
||||
const nuxt = await loadNuxt(opts)
|
||||
return nuxt
|
||||
}
|
||||
|
||||
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
||||
const rootDir = pathToFileURL(nuxt.options.rootDir).href
|
||||
const rootDir = withTrailingSlash(pathToFileURL(nuxt.options.rootDir).href)
|
||||
|
||||
const { build } = await tryImportModule<typeof import('nuxt')>('nuxt-nightly', { paths: rootDir }) || await importModule<typeof import('nuxt')>('nuxt', { paths: rootDir })
|
||||
return runWithNuxtContext(nuxt, () => build(nuxt))
|
||||
|
33
packages/kit/test/load-nuxt.test.ts
Normal file
33
packages/kit/test/load-nuxt.test.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { mkdir, rm, writeFile } from 'node:fs/promises'
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
||||
import { join, normalize } from 'pathe'
|
||||
import { withoutTrailingSlash } from 'ufo'
|
||||
import { x } from 'tinyexec'
|
||||
|
||||
import { loadNuxt } from '../src'
|
||||
|
||||
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
|
||||
|
||||
describe('loadNuxt', () => {
|
||||
const tempDir = join(repoRoot, 'temp')
|
||||
|
||||
beforeAll(async () => {
|
||||
await mkdir(join(tempDir, 'nuxt'), { recursive: true })
|
||||
await writeFile(join(tempDir, 'package.json'), '{"dependencies":{"nuxt":"file:./nuxt"}}')
|
||||
await writeFile(join(tempDir, 'nuxt', 'package.json'), '{"name":"nuxt","type":"module","exports":{".":"./index.js"}}')
|
||||
await writeFile(join(tempDir, 'nuxt', 'index.js'), 'export const loadNuxt = (opts) => ({ name: "it me" })')
|
||||
await x('npm', ['install'], { nodeOptions: { cwd: tempDir } })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await rm(tempDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('respects correct directory', async () => {
|
||||
const nuxt = await loadNuxt({ cwd: tempDir })
|
||||
expect(nuxt).toStrictEqual({
|
||||
name: 'it me',
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user