Skip to content

Commit 33cb210

Browse files
committed
feat: allow extending from multiple keys
resolves #24
1 parent e0cdc8d commit 33cb210

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/loader.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface LoadConfigOptions<T extends InputConfig=InputConfig> {
4040
resolve?: (id: string, opts: LoadConfigOptions) => null | ResolvedConfig | Promise<ResolvedConfig | null>
4141

4242
extend?: false | {
43-
extendKey?: string
43+
extendKey?: string | string[]
4444
}
4545
}
4646

@@ -128,12 +128,14 @@ export async function loadConfig<T extends InputConfig=InputConfig> (opts: LoadC
128128

129129
async function extendConfig (config, opts: LoadConfigOptions) {
130130
config._layers = config._layers || []
131-
if (!opts.extend) {
132-
return
131+
if (!opts.extend) { return }
132+
let keys = opts.extend.extendKey
133+
if (typeof keys === 'string') { keys = [keys] }
134+
const extendSources = []
135+
for (const key of keys) {
136+
extendSources.push(...(Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean))
137+
delete config[key]
133138
}
134-
const key = opts.extend.extendKey
135-
const extendSources = (Array.isArray(config[key]) ? config[key] : [config[key]]).filter(Boolean)
136-
delete config[key]
137139
for (const extendSource of extendSources) {
138140
const _config = await resolveConfig(extendSource, opts)
139141
if (!_config.config) {

test/fixture/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
2+
theme: './theme',
23
extends: [
3-
'./theme',
44
'./config.dev',
55
'c12-npm-test'
66
],

test/index.test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('c12', () => {
1111
const { config, layers } = await loadConfig({
1212
cwd: r('./fixture'),
1313
dotenv: true,
14+
extend: {
15+
extendKey: ['theme', 'extends']
16+
},
1417
resolve: (id) => {
1518
if (id === 'virtual') {
1619
return { config: { virtual: true } }
@@ -67,11 +70,11 @@ describe('c12', () => {
6770
},
6871
"configFile": true,
6972
"extends": [
70-
"./theme",
7173
"./config.dev",
7274
"c12-npm-test",
7375
],
7476
"overriden": false,
77+
"theme": "./theme",
7578
},
7679
"configFile": "config",
7780
"cwd": "<path>/fixture",
@@ -82,11 +85,6 @@ describe('c12', () => {
8285
},
8386
"configFile": ".configrc",
8487
},
85-
{
86-
"config": {
87-
"virtual": true,
88-
},
89-
},
9088
{
9189
"config": {
9290
"colors": {
@@ -111,6 +109,11 @@ describe('c12', () => {
111109
"configFile": "<path>/fixture/base/config.ts",
112110
"cwd": "<path>/fixture/base",
113111
},
112+
{
113+
"config": {
114+
"virtual": true,
115+
},
116+
},
114117
{
115118
"config": {
116119
"devConfig": true,

0 commit comments

Comments
 (0)