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-03-17 20:10:12 +00:00
|
|
|
import { loadConfig, LoadConfigOptions } from 'c12'
|
|
|
|
import type { NuxtOptions, NuxtConfig } from '@nuxt/schema'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { NuxtConfigSchema } from '@nuxt/schema'
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2022-03-17 20:10:12 +00:00
|
|
|
export interface LoadNuxtConfigOptions extends LoadConfigOptions<NuxtConfig> {}
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2021-10-18 09:03:39 +00:00
|
|
|
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
|
2022-08-12 17:47:58 +00:00
|
|
|
const { config: nuxtConfig, configFile, layers, cwd } = await loadConfig<NuxtConfig>({
|
2022-01-31 21:13:58 +00:00
|
|
|
name: 'nuxt',
|
|
|
|
configFile: 'nuxt.config',
|
|
|
|
rcFile: '.nuxtrc',
|
2022-03-17 20:10:12 +00:00
|
|
|
dotenv: true,
|
2022-01-31 21:13:58 +00:00
|
|
|
globalRc: true,
|
2022-03-17 20:10:12 +00:00
|
|
|
...opts
|
2022-01-31 21:13:58 +00:00
|
|
|
})
|
2021-10-18 09:03:39 +00:00
|
|
|
|
2022-03-17 20:10:12 +00:00
|
|
|
// Fill config
|
|
|
|
nuxtConfig.rootDir = nuxtConfig.rootDir || cwd
|
2022-01-31 21:13:58 +00:00
|
|
|
nuxtConfig._nuxtConfigFile = configFile
|
|
|
|
nuxtConfig._nuxtConfigFiles = [configFile]
|
2022-03-09 10:51:32 +00:00
|
|
|
|
|
|
|
// Resolve `rootDir` & `srcDir` of layers
|
|
|
|
for (const layer of layers) {
|
2022-08-12 17:47:58 +00:00
|
|
|
layer.config = layer.config || {}
|
2022-03-09 10:51:32 +00:00
|
|
|
layer.config.rootDir = layer.config.rootDir ?? layer.cwd
|
|
|
|
layer.config.srcDir = resolve(layer.config.rootDir, layer.config.srcDir)
|
|
|
|
}
|
|
|
|
|
2022-03-17 20:10:12 +00:00
|
|
|
// Filter layers
|
2022-03-16 20:36:30 +00:00
|
|
|
nuxtConfig._layers = layers.filter(layer => layer.configFile && !layer.configFile.endsWith('.nuxtrc'))
|
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
|
|
|
}
|