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

Merge extend objects deeply by default #2679

Merged
merged 1 commit into from
Oct 26, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions __tests__/resolveConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,55 @@ test('theme key is merged instead of replaced', () => {
})
})

test('theme key is deeply merged instead of replaced', () => {
const userConfig = {
theme: {
extend: {
colors: {
grey: {
darker: '#606f7b',
dark: '#8795a1',
},
},
},
},
}

const defaultConfig = {
prefix: '-',
important: false,
separator: ':',
theme: {
colors: {
grey: {
grey: '#b8c2cc',
light: '#dae1e7',
lighter: '#f1f5f8',
},
},
},
}

const result = resolveConfig([userConfig, defaultConfig])

expect(result).toMatchObject({
prefix: '-',
important: false,
separator: ':',
theme: {
colors: {
grey: {
darker: '#606f7b',
dark: '#8795a1',
grey: '#b8c2cc',
light: '#dae1e7',
lighter: '#f1f5f8',
},
},
},
})
})

test('variants key is merged instead of replaced', () => {
const userConfig = {
variants: {
Expand Down Expand Up @@ -1779,6 +1828,70 @@ test('variants can be extended', () => {
})
})

test('extensions are applied in the right order', () => {
const userConfig = {
theme: {
extend: {
colors: {
grey: {
light: '#eee',
},
},
},
},
}

const otherConfig = {
theme: {
extend: {
colors: {
grey: {
light: '#ddd',
darker: '#111',
},
},
},
},
}

const anotherConfig = {
theme: {
extend: {
colors: {
grey: {
darker: '#222',
},
},
},
},
}

const defaultConfig = {
theme: {
colors: {
grey: {
light: '#ccc',
dark: '#333',
},
},
},
}

const result = resolveConfig([userConfig, otherConfig, anotherConfig, defaultConfig])

expect(result).toMatchObject({
theme: {
colors: {
grey: {
light: '#eee',
dark: '#333',
darker: '#111',
},
},
},
})
})

test('variant sort order can be customized', () => {
const userConfig = {
variantOrder: [
Expand Down
19 changes: 11 additions & 8 deletions src/util/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,23 @@ function mergeThemes(themes) {
}
}

function mergeExtensionCustomizer(_merged, value) {
if (Array.isArray(value)) return value
}

function mergeExtensions({ extend, ...theme }) {
return mergeWith(theme, extend, (themeValue, extensions) => {
// The `extend` property is an array, so we need to check if it contains any functions
if (!isFunction(themeValue) && !some(extensions, isFunction)) {
return {
...themeValue,
...Object.assign({}, ...extensions),
}
return mergeWith({}, themeValue, ...extensions, mergeExtensionCustomizer)
}

return (resolveThemePath, utils) => ({
...value(themeValue, resolveThemePath, utils),
...Object.assign({}, ...extensions.map((e) => value(e, resolveThemePath, utils))),
})
return (resolveThemePath, utils) =>
mergeWith(
{},
...[themeValue, ...extensions].map((e) => value(e, resolveThemePath, utils)),
mergeExtensionCustomizer
)
})
}

Expand Down