Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): merge only existing fields in configs #3010

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 16 additions & 22 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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