2021-03-28 20:14:04 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import defu from 'defu'
|
|
|
|
import jiti from 'jiti'
|
|
|
|
import { applyDefaults } from 'untyped'
|
|
|
|
import * as rc from 'rc9'
|
2021-04-02 11:47:01 +00:00
|
|
|
import { NuxtOptions } from '../types/config'
|
2021-03-28 20:14:04 +00:00
|
|
|
import nuxtConfigSchema from './schema'
|
|
|
|
|
|
|
|
export interface LoadNuxtConfigOptions {
|
|
|
|
rootDir?: string
|
|
|
|
configFile?: string
|
|
|
|
config?: any
|
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions {
|
2021-03-28 20:14:04 +00:00
|
|
|
const rootDir = resolve(process.cwd(), opts.rootDir || '.')
|
|
|
|
|
|
|
|
const _require = jiti(rootDir)
|
|
|
|
|
|
|
|
let configFile
|
|
|
|
try {
|
|
|
|
configFile = _require.resolve(resolve(rootDir, opts.configFile || 'nuxt.config'))
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
throw (e)
|
|
|
|
}
|
|
|
|
// Skip if cannot resolve
|
|
|
|
configFile = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
let nuxtConfig: any = {}
|
|
|
|
|
|
|
|
if (configFile) {
|
|
|
|
// clearRequireCache(configFile) TODO
|
|
|
|
nuxtConfig = _require(configFile) || {}
|
|
|
|
if (nuxtConfig.default) {
|
|
|
|
nuxtConfig = nuxtConfig.default
|
|
|
|
}
|
|
|
|
nuxtConfig = { ...nuxtConfig }
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-04-02 11:47:01 +00:00
|
|
|
return applyDefaults(nuxtConfigSchema, nuxtConfig) as NuxtOptions
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|