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-12-11 21:44:52 +00:00
|
|
|
import type { LoadConfigOptions } from 'c12'
|
|
|
|
import { loadConfig } from 'c12'
|
2023-04-07 16:02:47 +00:00
|
|
|
import type { NuxtConfig, NuxtOptions } 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-09-14 11:10:10 +00:00
|
|
|
(globalThis as any).defineNuxtConfig = (c: any) => c
|
2022-08-22 10:12:02 +00:00
|
|
|
const result = await loadConfig<NuxtConfig>({
|
2022-01-31 21:13:58 +00:00
|
|
|
name: 'nuxt',
|
|
|
|
configFile: 'nuxt.config',
|
|
|
|
rcFile: '.nuxtrc',
|
2022-09-01 10:05:02 +00:00
|
|
|
extend: { extendKey: ['theme', 'extends'] },
|
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
|
|
|
})
|
2022-09-14 11:10:10 +00:00
|
|
|
delete (globalThis as any).defineNuxtConfig
|
2022-08-22 10:12:02 +00:00
|
|
|
const { configFile, layers = [], cwd } = result
|
|
|
|
const nuxtConfig = result.config!
|
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
|
2022-08-22 10:12:02 +00:00
|
|
|
layer.config.srcDir = resolve(layer.config.rootDir!, layer.config.srcDir!)
|
2022-03-09 10:51:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:10:12 +00:00
|
|
|
// Filter layers
|
2022-10-27 10:36:37 +00:00
|
|
|
const _layers = layers.filter(layer => layer.configFile && !layer.configFile.endsWith('.nuxtrc'))
|
|
|
|
;(nuxtConfig as any)._layers = _layers
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2022-09-08 14:15:52 +00:00
|
|
|
// Ensure at least one layer remains (without nuxt.config)
|
2022-10-27 10:36:37 +00:00
|
|
|
if (!_layers.length) {
|
|
|
|
_layers.push({
|
2022-09-08 14:15:52 +00:00
|
|
|
cwd,
|
|
|
|
config: {
|
|
|
|
rootDir: cwd,
|
|
|
|
srcDir: cwd
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-28 20:14:04 +00:00
|
|
|
// Resolve and apply defaults
|
2022-09-12 18:22:41 +00:00
|
|
|
return await applyDefaults(NuxtConfigSchema, nuxtConfig) as NuxtOptions
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|