@@ -10,7 +10,7 @@ export interface LoadConfigOptions<T extends ConfigT=ConfigT> {
10
10
name ?: string
11
11
cwd ?: string
12
12
13
- configFile ?: false | string
13
+ configFile ?: string
14
14
15
15
rcFile ?: false | string
16
16
globalRc ?: boolean
@@ -50,7 +50,7 @@ export async function loadConfig<T extends ConfigT=ConfigT> (opts: LoadConfigOpt
50
50
}
51
51
52
52
// Load config file
53
- const { config, configPath } = await loadConfigFile ( opts )
53
+ const { config, configPath } = await loadConfigFile ( opts . cwd , opts . configFile )
54
54
ctx . configPath = configPath
55
55
56
56
// Load rc files
@@ -70,24 +70,54 @@ export async function loadConfig<T extends ConfigT=ConfigT> (opts: LoadConfigOpt
70
70
opts . defaults
71
71
) as T
72
72
73
+ // Allow extending
74
+ await extendConfig ( ctx . config , opts . configFile ! , opts . cwd )
75
+ ctx . config = defu (
76
+ ctx . config ,
77
+ ...ctx . config . _extends . map ( e => e . config )
78
+ ) as T
79
+
73
80
// Return resolved context
74
81
return ctx
75
82
}
76
83
84
+ async function extendConfig ( config , configFile : string , cwd : string ) {
85
+ console . log ( 'Extending from' , cwd )
86
+ config . _extends = config . _extends || [ ]
87
+
88
+ const extendSources = ( Array . isArray ( config . extends ) ? config . extends : [ config . extends ] ) . filter ( Boolean )
89
+ for ( const extendSource of extendSources ) {
90
+ // TODO: Assuming extendSource is dir
91
+ const _cwd = resolve ( cwd , extendSource )
92
+ const _config = await loadConfigFile ( _cwd , configFile )
93
+ await extendConfig ( _config . config , configFile , _cwd )
94
+ delete _config . config . _extends
95
+ config . _extends . push ( {
96
+ config : _config . config ,
97
+ meta : {
98
+ cwd : _cwd ,
99
+ configPath : _config . configPath
100
+ }
101
+ } )
102
+ }
103
+
104
+ return config
105
+ }
106
+
77
107
const jiti = createJiti ( null , { cache : false , interopDefault : true } )
78
108
79
- async function loadConfigFile ( opts : LoadConfigOptions ) {
109
+ async function loadConfigFile ( cwd : string , configFile : string | false ) {
80
110
const res = {
81
111
configPath : null ,
82
112
config : null
83
113
}
84
114
85
- if ( ! opts . configFile ) {
115
+ if ( ! configFile ) {
86
116
return res
87
117
}
88
118
89
119
try {
90
- res . configPath = jiti . resolve ( resolve ( opts . cwd , opts . configFile ) , { paths : [ opts . cwd ] } )
120
+ res . configPath = jiti . resolve ( resolve ( cwd , configFile ) , { paths : [ cwd ] } )
91
121
res . config = jiti ( res . configPath )
92
122
if ( typeof res . config === 'function' ) {
93
123
res . config = await res . config ( )
0 commit comments