2023-07-30 09:16:13 +00:00
|
|
|
import type { JSValue } from 'untyped'
|
2021-03-28 20:14:04 +00:00
|
|
|
import { applyDefaults } from 'untyped'
|
2024-05-02 13:24:31 +00:00
|
|
|
import type { ConfigLayer, ConfigLayerMeta, LoadConfigOptions } from 'c12'
|
2022-12-11 21:44:52 +00:00
|
|
|
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'
|
2024-05-15 10:51:14 +00:00
|
|
|
import { globby } from 'globby'
|
|
|
|
import defu from 'defu'
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2024-06-12 19:55:40 +00:00
|
|
|
export interface LoadNuxtConfigOptions extends Omit<LoadConfigOptions<NuxtConfig>, 'overrides'> {
|
|
|
|
overrides?: Exclude<LoadConfigOptions<NuxtConfig>['overrides'], Promise<any> | Function>
|
|
|
|
}
|
2021-03-28 20:14:04 +00:00
|
|
|
|
2024-07-16 14:12:45 +00:00
|
|
|
const layerSchemaKeys = ['future', 'srcDir', 'rootDir', 'serverDir', 'dir']
|
2024-06-13 21:52:57 +00:00
|
|
|
const layerSchema = Object.create(null)
|
|
|
|
for (const key of layerSchemaKeys) {
|
|
|
|
if (key in NuxtConfigSchema) {
|
|
|
|
layerSchema[key] = NuxtConfigSchema[key]
|
|
|
|
}
|
|
|
|
}
|
2024-05-02 13:24:31 +00:00
|
|
|
|
2021-10-18 09:03:39 +00:00
|
|
|
export async function loadNuxtConfig (opts: LoadNuxtConfigOptions): Promise<NuxtOptions> {
|
2024-05-15 10:51:14 +00:00
|
|
|
// Automatically detect and import layers from `~~/layers/` directory
|
|
|
|
opts.overrides = defu(opts.overrides, {
|
|
|
|
_extends: await globby('layers/*', {
|
|
|
|
onlyDirectories: true,
|
|
|
|
cwd: opts.cwd || process.cwd(),
|
|
|
|
}),
|
|
|
|
});
|
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',
|
2024-05-15 10:51:14 +00:00
|
|
|
extend: { extendKey: ['theme', 'extends', '_extends'] },
|
2022-03-17 20:10:12 +00:00
|
|
|
dotenv: true,
|
2022-01-31 21:13:58 +00:00
|
|
|
globalRc: true,
|
2024-04-05 18:08:32 +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
|
|
|
|
2024-05-02 13:24:31 +00:00
|
|
|
const _layers: ConfigLayer<NuxtConfig, ConfigLayerMeta>[] = []
|
2024-06-13 11:51:00 +00:00
|
|
|
const processedLayers = new Set<string>()
|
2022-03-09 10:51:32 +00:00
|
|
|
for (const layer of layers) {
|
2024-05-02 13:24:31 +00:00
|
|
|
// Resolve `rootDir` & `srcDir` of layers
|
2022-08-12 17:47:58 +00:00
|
|
|
layer.config = layer.config || {}
|
2024-06-13 11:51:00 +00:00
|
|
|
layer.config.rootDir = layer.config.rootDir ?? layer.cwd!
|
|
|
|
|
|
|
|
// Only process/resolve layers once
|
|
|
|
if (processedLayers.has(layer.config.rootDir)) { continue }
|
|
|
|
processedLayers.add(layer.config.rootDir)
|
2024-05-02 13:24:31 +00:00
|
|
|
|
|
|
|
// Normalise layer directories
|
|
|
|
layer.config = await applyDefaults(layerSchema, layer.config as NuxtConfig & Record<string, JSValue>) as unknown as NuxtConfig
|
|
|
|
|
|
|
|
// Filter layers
|
|
|
|
if (!layer.configFile || layer.configFile.endsWith('.nuxtrc')) { continue }
|
|
|
|
_layers.push(layer)
|
2022-03-09 10:51:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 10:36:37 +00:00
|
|
|
;(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,
|
2024-04-05 18:08:32 +00:00
|
|
|
srcDir: cwd,
|
|
|
|
},
|
2022-09-08 14:15:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-28 20:14:04 +00:00
|
|
|
// Resolve and apply defaults
|
2023-07-30 09:16:13 +00:00
|
|
|
return await applyDefaults(NuxtConfigSchema, nuxtConfig as NuxtConfig & Record<string, JSValue>) as unknown as NuxtOptions
|
2021-03-28 20:14:04 +00:00
|
|
|
}
|