import { resolve , dirname } from 'path' ; import { existsSync , realpathSync } from 'fs' ; import Hookable , { configHooksT } from 'hookable' ; import defu from 'defu' ; import jiti from 'jiti' ; import type { NuxtConfig } from '@nuxt/types' ; declare module '@nuxt/types' { interface NuxtConfig { hooks ? : configHooksT ; name ? : string ; extends ? : string ; alias ? : { [ key ] : string ; } ; } } export function nuxtConfig ( config ) { config = resolveConfig ( config , 0 ) ; delete config . _file ; delete config . _dir ; delete config . name ; delete config . extends ; return config ; } function resolveConfig ( config , level ) { if ( typeof config === 'function' ) { throw new TypeError ( 'extending is not possible with nuxt config as a function' ) ; } const dir = config . srcDir || config . rootDir || config . _dir || process . cwd ( ) ; if ( dir && config . name ) { config . alias = config . alias || { } ; config . alias [ config . name ] = dir ; } if ( dir && config . components === undefined ) { config . components = [ ] ; const componentsDir = resolve ( dir , 'components' ) ; if ( existsSync ( componentsDir ) ) { config . components . push ( { path : componentsDir } ) ; } const globalComponentsDir = resolve ( dir , 'components/global' ) ; if ( existsSync ( globalComponentsDir ) ) { config . components . push ( { path : globalComponentsDir , global : true } ) ; } } if ( config . extends ) { const base = loadConfig ( config . extends , dir ) ; return mergeConfig ( config , resolveConfig ( base , level + 1 ) ) ; } return config ; } function loadConfig ( configFile , from ) { const _require = jiti ( from ) ; configFile = realpathSync ( _require . resolve ( configFile ) ) ; let config = _require ( configFile ) ; config = ( config . default || config ) as NuxtConfig ; config . _file = configFile ; config . _dir = dirname ( configFile ) ; return config ; } function mergeConfig ( target , base ) { const override = { } ; override . hooks = Hookable . mergeHooks ( base . hooks || { } , target . hooks || { } ) ; if ( base . components || target . components ) { override . components = [ ... normalizeComponents ( target . components ) , ... normalizeComponents ( base . components , true ) ] ; } return { ... defu . arrayFn ( target , base ) , ... override } ; } function normalizeComponents ( components , isBase ? ) { if ( typeof components === 'boolean' || ! components ) { components = [ ] ; } if ( ! Array . isArray ( components ) ) { throw new TypeError ( '`components` should be an array: ' + typeof components ) ; } const componentsArr = components . map ( dir => ( { ... ( typeof dir === 'string' ? { path : dir } : dir ) } ) ) ; for ( const component of componentsArr ) { component . level = ( component . level || 0 ) + ( isBase ? 1 : 0 ) ; } return componentsArr ; }