fix(kit): avoid behavior change based on NODE_ENV (#3751)

This commit is contained in:
pooya parsa 2022-03-17 21:10:12 +01:00 committed by GitHub
parent 621ce975b4
commit 7d945952d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 36 deletions

View File

@ -1,40 +1,23 @@
import { resolve } from 'pathe' import { resolve } from 'pathe'
import { applyDefaults } from 'untyped' import { applyDefaults } from 'untyped'
import { loadConfig, DotenvOptions } from 'c12' import { loadConfig, LoadConfigOptions } from 'c12'
import type { NuxtOptions } from '@nuxt/schema' import type { NuxtOptions, NuxtConfig } from '@nuxt/schema'
import { NuxtConfigSchema } from '@nuxt/schema' import { NuxtConfigSchema } from '@nuxt/schema'
// TODO
// import { tryResolveModule, requireModule, scanRequireTree } from '../internal/cjs'
export interface LoadNuxtConfigOptions { export interface LoadNuxtConfigOptions extends LoadConfigOptions<NuxtConfig> {}
/** 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<string, any>
/** Configuration for loading dotenv */
dotenv?: DotenvOptions | false
}
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> { export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
const rootDir = resolve(process.cwd(), opts.rootDir || '.') const { config: nuxtConfig, configFile, layers, cwd } = await loadConfig({
const { config: nuxtConfig, configFile, layers } = await loadConfig({
cwd: rootDir,
name: 'nuxt', name: 'nuxt',
configFile: 'nuxt.config', configFile: 'nuxt.config',
rcFile: '.nuxtrc', rcFile: '.nuxtrc',
dotenv: typeof opts.dotenv === 'undefined' ? {} as DotenvOptions : opts.dotenv, dotenv: true,
globalRc: true, globalRc: true,
overrides: opts.config ...opts
}) })
nuxtConfig.rootDir = nuxtConfig.rootDir || rootDir // Fill config
nuxtConfig.rootDir = nuxtConfig.rootDir || cwd
nuxtConfig._nuxtConfigFile = configFile nuxtConfig._nuxtConfigFile = configFile
nuxtConfig._nuxtConfigFiles = [configFile] nuxtConfig._nuxtConfigFiles = [configFile]
@ -44,6 +27,7 @@ export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<Nuxt
layer.config.srcDir = resolve(layer.config.rootDir, layer.config.srcDir) layer.config.srcDir = resolve(layer.config.rootDir, layer.config.srcDir)
} }
// Filter layers
nuxtConfig._layers = layers.filter(layer => layer.configFile && !layer.configFile.endsWith('.nuxtrc')) nuxtConfig._layers = layers.filter(layer => layer.configFile && !layer.configFile.endsWith('.nuxtrc'))
// Resolve and apply defaults // Resolve and apply defaults

View File

@ -1,24 +1,37 @@
import { readPackageJSON, resolvePackageJSON } from 'pkg-types' 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 { importModule, tryImportModule, RequireModuleOptions } from '../internal/cjs'
import type { LoadNuxtConfigOptions } from './config' import type { LoadNuxtConfigOptions } from './config'
export interface LoadNuxtOptions extends LoadNuxtConfigOptions { export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
rootDir: string /** Load nuxt with development mode */
dev?: boolean dev?: boolean
config?: NuxtConfig
configFile?: string /** Use lazy initialization of nuxt if set to false */
ready?: boolean ready?: boolean
/** @deprecated Use cwd option */
rootDir?: LoadNuxtConfigOptions['cwd']
/** @deprecated use overrides option */
config?: LoadNuxtConfigOptions['overrides']
} }
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> { export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
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'] 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]) .then(r => r.filter(Boolean).sort((a, b) => b.length - a.length)[0])
if (!nearestNuxtPkg) { 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 pkg = await readPackageJSON(nearestNuxtPkg)
const majorVersion = parseInt((pkg.version || '').split('.')[0]) const majorVersion = parseInt((pkg.version || '').split('.')[0])
@ -33,9 +46,9 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
// Nuxt 2 // Nuxt 2
const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts) const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts)
const nuxt = await loadNuxt({ const nuxt = await loadNuxt({
rootDir: opts.rootDir, rootDir: opts.cwd,
for: opts.dev ? 'dev' : 'build', for: opts.dev ? 'dev' : 'build',
configOverrides: opts.config, configOverrides: opts.overrides,
ready: opts.ready, ready: opts.ready,
envConfig: opts.dotenv // TODO: Backward format convertion envConfig: opts.dotenv // TODO: Backward format convertion
}) })

View File

@ -49,8 +49,8 @@ export async function loadFixture () {
}) })
ctx.nuxt = await kit.loadNuxt({ ctx.nuxt = await kit.loadNuxt({
rootDir: ctx.options.rootDir, cwd: ctx.options.rootDir,
config: ctx.options.nuxtConfig, overrides: ctx.options.nuxtConfig,
configFile: ctx.options.configFile configFile: ctx.options.configFile
}) })