Expose config as a function to avoid mutation and make it easy to provide defaults

This commit is contained in:
Adam Wathan 2018-03-02 07:45:11 -05:00
parent 18d45b19cf
commit 9573024a1b
2 changed files with 29 additions and 2 deletions

View file

@ -207,7 +207,7 @@ test('plugins can access the current config', () => {
}),
]
_.forEach(config.screens, breakpoint => {
_.forEach(config('screens'), breakpoint => {
const mediaQuery = atRule(`@media (min-width: ${breakpoint})`, [
rule('.container', { 'max-width': breakpoint }),
])
@ -246,3 +246,30 @@ test('plugins can access the current config', () => {
}
`)
})
test('plugins can provide fallbacks to keys missing from the config', () => {
const [components, utilities] = processPlugins({
borderRadius: {
'1': '1px',
'2': '2px',
'4': '4px',
'8': '8px',
},
plugins: [
function({ rule, atRule, addComponents, config }) {
addComponents([
rule('.btn', {
'border-radius': config('borderRadius.default', '.25rem')
})
])
},
],
})
expect(utilities.length).toBe(0)
expect(css(components)).toMatchCss(`
.btn {
border-radius: .25rem
}
`)
})

View file

@ -31,7 +31,7 @@ export default function(config) {
config.plugins.forEach(plugin => {
plugin({
config,
config: (path, defaultValue) => _.get(config, path, defaultValue),
rule: defineRule,
atRule: defineAtRule,
e: escapeClassName,