1
- import { stat } from 'node:fs/promises '
2
- import { loadConfig , createDefineConfig } from 'c12 '
1
+ import { loadConfig , watchConfig , createDefineConfig } from 'c12 '
2
+ import { relative } from 'pathe '
3
3
import type { Nuxt } from '@nuxt/schema'
4
- import { join } from 'pathe'
5
- import type { ResolvedCollection , DefinedCollection } from '../types'
4
+ import type { DefinedCollection } from '../types'
6
5
import { defineCollection , resolveCollections } from './collection'
7
6
import { logger } from './dev'
8
7
@@ -21,47 +20,41 @@ const defaultConfig: NuxtContentConfig = {
21
20
22
21
export const defineContentConfig = createDefineConfig < NuxtContentConfig > ( )
23
22
24
- export async function loadContentConfig ( rootDir : string , opts : { defaultFallback ?: boolean } = { } ) {
23
+ export async function loadContentConfig ( nuxt : Nuxt , options : { defaultFallback ?: boolean } = { } ) {
24
+ const loader : typeof watchConfig = nuxt . options . dev
25
+ ? opts => watchConfig ( {
26
+ ...opts ,
27
+ onWatch : ( e ) => {
28
+ logger . info ( relative ( nuxt . options . rootDir , e . path ) + ' ' + e . type + ', restarting the Nuxt server...' )
29
+ nuxt . hooks . callHook ( 'restart' , { hard : true } )
30
+ } ,
31
+ } )
32
+ : loadConfig as typeof watchConfig ;
33
+
25
34
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26
35
( globalThis as any ) . defineContentConfig = ( c : any ) => c
27
- const { config, configFile } = await loadConfig < NuxtContentConfig > ( { name : 'content' , cwd : rootDir } )
36
+
37
+ const contentConfigs = await Promise . all (
38
+ nuxt . options . _layers . reverse ( ) . map (
39
+ layer => loader < NuxtContentConfig > ( { name : 'content' , cwd : layer . config . rootDir , defaultConfig : options . defaultFallback ? defaultConfig : undefined } ) ,
40
+ ) ,
41
+ )
42
+
28
43
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29
44
delete ( globalThis as any ) . defineContentConfig
30
45
31
- if ( ( ! configFile || configFile === 'content.config' ) && opts . defaultFallback ) {
32
- logger . warn ( '`content.config.ts` is not found, falling back to default collection. In order to have full control over your collections, create the config file in project root. See: https://content.nuxt.com/getting-started/installation' )
33
- return {
34
- collections : resolveCollections ( defaultConfig . collections ) ,
35
- }
46
+ if ( nuxt . options . dev ) {
47
+ nuxt . hook ( 'close' , ( ) => Promise . all ( contentConfigs . map ( c => c . unwatch ( ) ) ) . then ( ( ) => { } ) )
36
48
}
37
49
38
- return {
39
- collections : resolveCollections ( config . collections || { } ) ,
40
- }
41
- }
50
+ const collectionsConfig = contentConfigs . reduce ( ( acc , curr ) => ( { ...acc , ...curr . config ?. collections } ) , { } as Record < string , DefinedCollection > )
51
+ const hasNoCollections = Object . keys ( collectionsConfig || { } ) . length === 0
42
52
43
- export async function loadLayersConfig ( nuxt : Nuxt ) {
44
- const collectionMap = { } as Record < string , ResolvedCollection >
45
- const _layers = [ ...nuxt . options . _layers ] . reverse ( )
46
- for ( const layer of _layers ) {
47
- const rootDir = layer . config . rootDir
48
- const configStat = await stat ( join ( rootDir , 'content.config.ts' ) ) . catch ( ( ) => null )
49
- if ( configStat && configStat . isFile ( ) ) {
50
- const { collections } = await loadContentConfig ( rootDir , { defaultFallback : false } )
51
- for ( const collection of collections ) {
52
- collectionMap [ collection . name ] = collection
53
- }
54
- }
53
+ if ( hasNoCollections ) {
54
+ logger . warn ( 'No content configuration found, falling back to default collection. In order to have full control over your collections, create the config file in project root. See: https://content.nuxt.com/getting-started/installation' )
55
55
}
56
56
57
- if ( Object . keys ( collectionMap ) . length === 0 ) {
58
- const collections = resolveCollections ( defaultConfig . collections )
59
- for ( const collection of collections ) {
60
- collectionMap [ collection . name ] = collection
61
- }
62
- }
57
+ const collections = resolveCollections ( hasNoCollections ? defaultConfig . collections : collectionsConfig )
63
58
64
- return {
65
- collections : Object . values ( collectionMap ) ,
66
- }
59
+ return { collections }
67
60
}
0 commit comments