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

46 lines
1.4 KiB
TypeScript
Raw Normal View History

import { resolve } from 'pathe'
import { applyDefaults } from 'untyped'
import { loadConfig, DotenvOptions } from 'c12'
import type { NuxtOptions } from '@nuxt/schema'
import { NuxtConfigSchema } from '@nuxt/schema'
// TODO
// import { tryResolveModule, requireModule, scanRequireTree } from '../internal/cjs'
export interface LoadNuxtConfigOptions {
2021-04-15 18:49:29 +00:00
/** Your project root directory (either absolute or relative to the current working directory). */
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`). */
configFile?: string
2021-04-15 18:49:29 +00:00
/** Any overrides to your Nuxt configuration. */
config?: Record<string, any>
/** Configuration for loading dotenv */
dotenv?: DotenvOptions | false
}
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
const rootDir = resolve(process.cwd(), opts.rootDir || '.')
const { config: nuxtConfig, configFile, layers } = await loadConfig({
cwd: rootDir,
name: 'nuxt',
configFile: 'nuxt.config',
rcFile: '.nuxtrc',
dotenv: opts.dotenv,
globalRc: true,
overrides: opts.config
})
nuxtConfig.rootDir = nuxtConfig.rootDir || rootDir
nuxtConfig._nuxtConfigFile = configFile
nuxtConfig._nuxtConfigFiles = [configFile]
nuxtConfig.layers = layers
// Resolve and apply defaults
return applyDefaults(NuxtConfigSchema, nuxtConfig) as NuxtOptions
}