Nuxt/packages/kit/src/config/load.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

import { resolve } from 'path'
import defu from 'defu'
import { applyDefaults } from 'untyped'
import * as rc from 'rc9'
import { tryResolveModule, requireModule, scanRequireTree } from '../utils/cjs'
import { NuxtOptions } from '../types/config'
import nuxtConfigSchema from './schema'
export interface LoadNuxtConfigOptions {
rootDir?: string
configFile?: string
config?: any
}
export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions {
const rootDir = resolve(process.cwd(), opts.rootDir || '.')
const nuxtConfigFile = tryResolveModule(resolve(rootDir, opts.configFile || 'nuxt.config'))
let nuxtConfig: any = {}
if (nuxtConfigFile) {
nuxtConfig = requireModule(nuxtConfigFile, { clearCache: true })
nuxtConfig = { ...nuxtConfig }
nuxtConfig._nuxtConfigFile = nuxtConfigFile
nuxtConfig._nuxtConfigFiles = Array.from(scanRequireTree(nuxtConfigFile))
}
// Combine configs
// Priority: configOverrides > nuxtConfig > .nuxtrc > .nuxtrc (global)
nuxtConfig = defu(
opts.config,
nuxtConfig,
rc.read({ name: '.nuxtrc', dir: rootDir }),
rc.readUser('.nuxtrc')
)
// Set rootDir
if (!nuxtConfig.rootDir) {
nuxtConfig.rootDir = rootDir
}
// Resolve and apply defaults
return applyDefaults(nuxtConfigSchema, nuxtConfig) as NuxtOptions
}