Skip to content

Commit

Permalink
fix(core): merge only existing fields in configs (#3010)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
enkot and antfu committed Aug 21, 2023
1 parent 870ea31 commit 580a301
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 42 deletions.
38 changes: 16 additions & 22 deletions packages/core/src/config.ts
Expand Up @@ -171,28 +171,22 @@ export function resolveConfig<Theme extends object = object>(
export function mergeConfigs<Theme extends object = object>(
configs: UserConfig<Theme>[],
): UserConfig<Theme> {
function getMerged<T extends 'rules' | 'variants' | 'extractors' | 'shortcuts' | 'preflights' | 'preprocess' | 'postprocess' | 'extendTheme' | 'safelist' | 'separators' | 'presets'>(key: T): ToArray<Required<UserConfig<Theme>>[T]> {
return uniq(configs.flatMap(p => toArray(p[key] || []) as any[])) as any
}

const merged = Object.assign(
{},
...configs,
{
theme: mergeThemes(configs.map(c => c.theme)),
presets: getMerged('presets'),
safelist: getMerged('safelist'),
preprocess: getMerged('preprocess'),
postprocess: getMerged('postprocess'),
preflights: getMerged('preflights'),
rules: getMerged('rules'),
variants: getMerged('variants'),
shortcuts: getMerged('shortcuts'),
extractors: getMerged('extractors'),
},
)

return merged
const maybeArrays = ['shortcuts', 'preprocess', 'postprocess']
const config = configs.map(config => Object.entries(config)
.reduce<UserConfig<Theme>>((acc, [key, value]) => ({
...acc,
[key]: maybeArrays.includes(key) ? toArray(value) : value,
}), {}))
.reduce<UserConfig<Theme>>(({ theme: themeA, ...a }, { theme: themeB, ...b }) => {
const c = mergeDeep<UserConfig<Theme>>(a, b, true)

if (themeA || themeB)
c.theme = mergeThemes([themeA, themeB])

return c
}, {})

return config
}

function mergeThemes<Theme extends object = object>(themes: (Theme | undefined)[]): Theme {
Expand Down
28 changes: 8 additions & 20 deletions test/config.test.ts
Expand Up @@ -153,13 +153,6 @@ describe('mergeConfigs', () => {
]))
.toMatchInlineSnapshot(`
{
"extractors": [],
"postprocess": [],
"preflights": [],
"preprocess": [],
"presets": [],
"rules": [],
"safelist": [],
"shortcuts": [
{
"foo": "string",
Expand All @@ -172,8 +165,6 @@ describe('mergeConfigs', () => {
[Function],
],
],
"theme": {},
"variants": [],
}
`)
})
Expand All @@ -182,6 +173,7 @@ describe('mergeConfigs', () => {
{
theme: {
fontSize: {
sm: ['0.875rem', '1.125rem'],
md: ['1.125rem', '1.5rem'],
lg: ['1.25rem', '1.5rem'],
},
Expand All @@ -190,21 +182,14 @@ describe('mergeConfigs', () => {
{
theme: {
fontSize: {
sm: ['0.875rem', '1.125rem'],
sm: ['1rem', '1.125rem'],
xl: ['1.5rem', '1.75rem'],
},
},
},
]))
.toMatchInlineSnapshot(`
{
"extractors": [],
"postprocess": [],
"preflights": [],
"preprocess": [],
"presets": [],
"rules": [],
"safelist": [],
"shortcuts": [],
"theme": {
"fontSize": {
"lg": [
Expand All @@ -216,12 +201,15 @@ describe('mergeConfigs', () => {
"1.5rem",
],
"sm": [
"0.875rem",
"1rem",
"1.125rem",
],
"xl": [
"1.5rem",
"1.75rem",
],
},
},
"variants": [],
}
`)
})
Expand Down

0 comments on commit 580a301

Please sign in to comment.