diff --git a/packages/kit/src/loader/nuxt.ts b/packages/kit/src/loader/nuxt.ts index adf5446fa1..1e54af14fd 100644 --- a/packages/kit/src/loader/nuxt.ts +++ b/packages/kit/src/loader/nuxt.ts @@ -1,6 +1,7 @@ import { pathToFileURL } from 'node:url' import { readPackageJSON, resolvePackageJSON } from 'pkg-types' import type { Nuxt } from '@nuxt/schema' +import { withTrailingSlash } from 'ufo' import { importModule, tryImportModule } from '../internal/esm' import { runWithNuxtContext } from '../context' import type { LoadNuxtConfigOptions } from './config' @@ -27,8 +28,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { // Apply dev as config override opts.overrides.dev = !!opts.dev + const rootDir = withTrailingSlash(pathToFileURL(opts.cwd!).href) + const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt3', 'nuxt', 'nuxt-edge'] - .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}`) @@ -36,8 +39,6 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { const pkg = await readPackageJSON(nearestNuxtPkg) const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]!) : '' - const rootDir = pathToFileURL(opts.cwd || process.cwd()).href - // Nuxt 3 if (majorVersion && majorVersion >= 3) { const { loadNuxt } = await importModule((pkg as any)._name || pkg.name, { paths: rootDir }) @@ -72,7 +73,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { } export async function buildNuxt (nuxt: Nuxt): Promise { - const rootDir = pathToFileURL(nuxt.options.rootDir).href + const rootDir = withTrailingSlash(pathToFileURL(nuxt.options.rootDir).href) // Nuxt 3 if (nuxt.options._majorVersion === 3) { diff --git a/packages/kit/test/load-nuxt.test.ts b/packages/kit/test/load-nuxt.test.ts new file mode 100644 index 0000000000..25b6c8cc50 --- /dev/null +++ b/packages/kit/test/load-nuxt.test.ts @@ -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', + }) + }) +})