2021-03-28 20:14:04 +00:00
|
|
|
import { resolve } from 'path'
|
2021-04-15 19:17:44 +00:00
|
|
|
import { existsSync } from 'fs'
|
2021-03-28 20:14:04 +00:00
|
|
|
import defu from 'defu'
|
|
|
|
import { applyDefaults } from 'untyped'
|
|
|
|
import * as rc from 'rc9'
|
2021-04-04 12:28:31 +00:00
|
|
|
import { tryResolveModule, requireModule, scanRequireTree } from '../utils/cjs'
|
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 {
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Your project root directory (either absolute or relative to the current working directory). */
|
2021-03-28 20:14:04 +00:00
|
|
|
rootDir?: string
|
2021-04-15 18:49:29 +00:00
|
|
|
/** The path to your `nuxt.config` file (either absolute or relative to your project `rootDir`). */
|
2021-03-28 20:14:04 +00:00
|
|
|
configFile?: string
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Any overrides to your Nuxt configuration. */
|
|
|
|
config?: Record<string, any>
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|
|
|
|
|
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 || '.')
|
|
|
|
|
2021-04-04 12:28:31 +00:00
|
|
|
const nuxtConfigFile = tryResolveModule(resolve(rootDir, opts.configFile || 'nuxt.config'))
|
2021-03-28 20:14:04 +00:00
|
|
|
|
|
|
|
let nuxtConfig: any = {}
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
if (nuxtConfigFile && existsSync(nuxtConfigFile)) {
|
2021-04-04 12:28:31 +00:00
|
|
|
nuxtConfig = requireModule(nuxtConfigFile, { clearCache: true })
|
2021-03-28 20:14:04 +00:00
|
|
|
nuxtConfig = { ...nuxtConfig }
|
2021-04-04 12:28:31 +00:00
|
|
|
nuxtConfig._nuxtConfigFile = nuxtConfigFile
|
|
|
|
nuxtConfig._nuxtConfigFiles = Array.from(scanRequireTree(nuxtConfigFile))
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|