2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2021-03-28 20:14:04 +00:00
|
|
|
import { applyDefaults } from 'untyped'
|
2022-01-31 21:13:58 +00:00
|
|
|
import { loadConfig, DotenvOptions } from 'c12'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { NuxtOptions } from '@nuxt/schema'
|
|
|
|
import { NuxtConfigSchema } from '@nuxt/schema'
|
2022-01-31 21:13:58 +00:00
|
|
|
// TODO
|
|
|
|
// import { tryResolveModule, requireModule, scanRequireTree } from '../internal/cjs'
|
2021-03-28 20:14:04 +00:00
|
|
|
|
|
|
|
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-11-21 16:14:46 +00:00
|
|
|
|
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-11-21 16:14:46 +00:00
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Any overrides to your Nuxt configuration. */
|
|
|
|
config?: Record<string, any>
|
2021-11-21 16:14:46 +00:00
|
|
|
|
|
|
|
/** Configuration for loading dotenv */
|
|
|
|
dotenv?: DotenvOptions | false
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 09:03:39 +00:00
|
|
|
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
|
2021-03-28 20:14:04 +00:00
|
|
|
const rootDir = resolve(process.cwd(), opts.rootDir || '.')
|
|
|
|
|
2022-01-31 21:13:58 +00:00
|
|
|
const { config: nuxtConfig, configFile, layers } = await loadConfig({
|
|
|
|
cwd: rootDir,
|
|
|
|
name: 'nuxt',
|
|
|
|
configFile: 'nuxt.config',
|
|
|
|
rcFile: '.nuxtrc',
|
2022-02-02 09:59:00 +00:00
|
|
|
dotenv: typeof opts.dotenv === 'undefined' ? {} as DotenvOptions : opts.dotenv,
|
2022-01-31 21:13:58 +00:00
|
|
|
globalRc: true,
|
|
|
|
overrides: opts.config
|
|
|
|
})
|
2021-10-18 09:03:39 +00:00
|
|
|
|
2022-01-31 21:13:58 +00:00
|
|
|
nuxtConfig.rootDir = nuxtConfig.rootDir || rootDir
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2022-01-31 21:13:58 +00:00
|
|
|
nuxtConfig._nuxtConfigFile = configFile
|
|
|
|
nuxtConfig._nuxtConfigFiles = [configFile]
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2022-01-31 21:13:58 +00:00
|
|
|
nuxtConfig.layers = layers
|
2021-03-28 20:14:04 +00:00
|
|
|
|
|
|
|
// Resolve and apply defaults
|
2021-11-21 16:14:46 +00:00
|
|
|
return applyDefaults(NuxtConfigSchema, nuxtConfig) as NuxtOptions
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|