From 7d945952d83c826ee15ff483bbbdf7aed58e6a9a Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 17 Mar 2022 21:10:12 +0100 Subject: [PATCH] fix(kit): avoid behavior change based on NODE_ENV (#3751) --- packages/kit/src/loader/config.ts | 34 ++++++++----------------------- packages/kit/src/loader/nuxt.ts | 31 ++++++++++++++++++++-------- packages/test-utils/src/nuxt.ts | 4 ++-- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/packages/kit/src/loader/config.ts b/packages/kit/src/loader/config.ts index a783b7465a..84a85f5cbb 100644 --- a/packages/kit/src/loader/config.ts +++ b/packages/kit/src/loader/config.ts @@ -1,40 +1,23 @@ import { resolve } from 'pathe' import { applyDefaults } from 'untyped' -import { loadConfig, DotenvOptions } from 'c12' -import type { NuxtOptions } from '@nuxt/schema' +import { loadConfig, LoadConfigOptions } from 'c12' +import type { NuxtOptions, NuxtConfig } from '@nuxt/schema' import { NuxtConfigSchema } from '@nuxt/schema' -// TODO -// import { tryResolveModule, requireModule, scanRequireTree } from '../internal/cjs' -export interface LoadNuxtConfigOptions { - /** Your project root directory (either absolute or relative to the current working directory). */ - rootDir?: string - - /** The path to your `nuxt.config` file (either absolute or relative to your project `rootDir`). */ - configFile?: string - - /** Any overrides to your Nuxt configuration. */ - config?: Record - - /** Configuration for loading dotenv */ - dotenv?: DotenvOptions | false -} +export interface LoadNuxtConfigOptions extends LoadConfigOptions {} export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise { - const rootDir = resolve(process.cwd(), opts.rootDir || '.') - - const { config: nuxtConfig, configFile, layers } = await loadConfig({ - cwd: rootDir, + const { config: nuxtConfig, configFile, layers, cwd } = await loadConfig({ name: 'nuxt', configFile: 'nuxt.config', rcFile: '.nuxtrc', - dotenv: typeof opts.dotenv === 'undefined' ? {} as DotenvOptions : opts.dotenv, + dotenv: true, globalRc: true, - overrides: opts.config + ...opts }) - nuxtConfig.rootDir = nuxtConfig.rootDir || rootDir - + // Fill config + nuxtConfig.rootDir = nuxtConfig.rootDir || cwd nuxtConfig._nuxtConfigFile = configFile nuxtConfig._nuxtConfigFiles = [configFile] @@ -44,6 +27,7 @@ export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise layer.configFile && !layer.configFile.endsWith('.nuxtrc')) // Resolve and apply defaults diff --git a/packages/kit/src/loader/nuxt.ts b/packages/kit/src/loader/nuxt.ts index 323aa559ee..840b7109be 100644 --- a/packages/kit/src/loader/nuxt.ts +++ b/packages/kit/src/loader/nuxt.ts @@ -1,24 +1,37 @@ import { readPackageJSON, resolvePackageJSON } from 'pkg-types' -import type { Nuxt, NuxtConfig } from '@nuxt/schema' +import type { Nuxt } from '@nuxt/schema' import { importModule, tryImportModule, RequireModuleOptions } from '../internal/cjs' import type { LoadNuxtConfigOptions } from './config' export interface LoadNuxtOptions extends LoadNuxtConfigOptions { - rootDir: string + /** Load nuxt with development mode */ dev?: boolean - config?: NuxtConfig - configFile?: string + + /** Use lazy initialization of nuxt if set to false */ ready?: boolean + + /** @deprecated Use cwd option */ + rootDir?: LoadNuxtConfigOptions['cwd'] + + /** @deprecated use overrides option */ + config?: LoadNuxtConfigOptions['overrides'] } export async function loadNuxt (opts: LoadNuxtOptions): Promise { - const resolveOpts: RequireModuleOptions = { paths: opts.rootDir } + // Backward compatibility + opts.cwd = opts.cwd || opts.rootDir + opts.overrides = opts.overrides || opts.config || {} + + const resolveOpts: RequireModuleOptions = { paths: opts.cwd } + + // Apply dev as config override + opts.overrides.dev = !!opts.dev const nearestNuxtPkg = await Promise.all(['nuxt3', 'nuxt-edge', 'nuxt'] - .map(pkg => resolvePackageJSON(pkg, { url: opts.rootDir }).catch(() => null))) + .map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null))) .then(r => r.filter(Boolean).sort((a, b) => b.length - a.length)[0]) if (!nearestNuxtPkg) { - throw new Error(`Cannot find any nuxt version from ${opts.rootDir}`) + throw new Error(`Cannot find any nuxt version from ${opts.cwd}`) } const pkg = await readPackageJSON(nearestNuxtPkg) const majorVersion = parseInt((pkg.version || '').split('.')[0]) @@ -33,9 +46,9 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise { // Nuxt 2 const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts) const nuxt = await loadNuxt({ - rootDir: opts.rootDir, + rootDir: opts.cwd, for: opts.dev ? 'dev' : 'build', - configOverrides: opts.config, + configOverrides: opts.overrides, ready: opts.ready, envConfig: opts.dotenv // TODO: Backward format convertion }) diff --git a/packages/test-utils/src/nuxt.ts b/packages/test-utils/src/nuxt.ts index acf6b1ae02..fb77328508 100644 --- a/packages/test-utils/src/nuxt.ts +++ b/packages/test-utils/src/nuxt.ts @@ -49,8 +49,8 @@ export async function loadFixture () { }) ctx.nuxt = await kit.loadNuxt({ - rootDir: ctx.options.rootDir, - config: ctx.options.nuxtConfig, + cwd: ctx.options.rootDir, + overrides: ctx.options.nuxtConfig, configFile: ctx.options.configFile })