mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(kit): avoid behavior change based on NODE_ENV (#3751)
This commit is contained in:
parent
621ce975b4
commit
7d945952d8
@ -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<string, any>
|
||||
|
||||
/** Configuration for loading dotenv */
|
||||
dotenv?: DotenvOptions | false
|
||||
}
|
||||
export interface LoadNuxtConfigOptions extends LoadConfigOptions<NuxtConfig> {}
|
||||
|
||||
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
|
||||
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<Nuxt
|
||||
layer.config.srcDir = resolve(layer.config.rootDir, layer.config.srcDir)
|
||||
}
|
||||
|
||||
// Filter layers
|
||||
nuxtConfig._layers = layers.filter(layer => layer.configFile && !layer.configFile.endsWith('.nuxtrc'))
|
||||
|
||||
// Resolve and apply defaults
|
||||
|
@ -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<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']
|
||||
.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> {
|
||||
// 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
|
||||
})
|
||||
|
@ -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
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user