Skip to content

Commit

Permalink
fix(core): only deep merge theme by one level, fix #1298
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 19, 2022
1 parent dc9b27d commit 8f4f6b7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Expand Up @@ -79,7 +79,7 @@ export function resolveConfig(
const theme = clone([
...sortedPresets.map(p => p.theme || {}),
config.theme || {},
].reduce((a, p) => mergeDeep(a, p), {}))
].reduce((a, p) => mergeDeep(a, p, 1), {}))

;(mergePresets('extendTheme') as ThemeExtender<any>[]).forEach(extendTheme => extendTheme(theme))

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils/object.ts
Expand Up @@ -47,7 +47,7 @@ export function isObject(item: any): item is Record<string, any> {
return (item && typeof item === 'object' && !Array.isArray(item))
}

export function mergeDeep<T>(original: T, patch: DeepPartial<T>): T {
export function mergeDeep<T>(original: T, patch: DeepPartial<T>, level = Infinity): T {
const o = original as any
const p = patch as any

Expand All @@ -60,8 +60,8 @@ export function mergeDeep<T>(original: T, patch: DeepPartial<T>): T {
const output = { ...o }
if (isObject(o) && isObject(p)) {
Object.keys(p).forEach((key) => {
if ((isObject(o[key]) && isObject(p[key])) || (Array.isArray(o[key]) && Array.isArray(p[key])))
output[key] = mergeDeep(o[key], p[key])
if (level > 0 && ((isObject(o[key]) && isObject(p[key])) || (Array.isArray(o[key]) && Array.isArray(p[key]))))
output[key] = mergeDeep(o[key], p[key], level - 1)
else
Object.assign(output, { [key]: p[key] })
})
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/preset-mini.test.ts.snap
Expand Up @@ -17,6 +17,14 @@ exports[`preset-mini > dark customizing selector 1`] = `
[data-mode=\\"light\\"] .light\\\\:disabled\\\\:w-full:disabled{width:100%;}"
`;

exports[`preset-mini > fontSize theme 1`] = `
"/* layer: default */
.text-lg{font-size:3rem;line-height:1.5em;}
.text-medium{font-size:2rem;line-height:1.5em;}
.text-small{font-size:1rem;line-height:1;}
.text-xs{font-size:2rem;line-height:1;}"
`;

exports[`preset-mini > nested theme colors 1`] = `
"/* layer: default */
.bg-a-b-c{--un-bg-opacity:1;background-color:rgba(81,69,67,var(--un-bg-opacity));}
Expand Down
27 changes: 27 additions & 0 deletions test/preset-mini.test.ts
Expand Up @@ -111,4 +111,31 @@ describe('preset-mini', () => {
expect(css).toMatchInlineSnapshot('""')
expect([...matched]).toEqual([])
})

test('fontSize theme', async () => {
const uno = createGenerator({
presets: [
presetMini(),
],
theme: {
fontSize: {
small: '1rem',
medium: ['2rem', '1.5em'],
xs: '2rem',
lg: ['3rem', '1.5em'],
},
},
})

const { css } = await uno.generate([
'text-small',
'text-medium',
'text-xs',
'text-lg',
].join(' '), { preflights: false })

// @ts-expect-error types
expect(uno.config.theme.fontSize.lg).toEqual(['3rem', '1.5em'])
expect(css).toMatchSnapshot()
})
})

0 comments on commit 8f4f6b7

Please sign in to comment.