Skip to content

Commit e130771

Browse files
committed
Allow plugins to modify the config
1 parent 4b72c73 commit e130771

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

__tests__/modifyConfig.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import postcss from 'postcss'
2+
import tailwind from '../src/index'
3+
4+
test('plugins can add new theme values', () => {
5+
return postcss([
6+
tailwind({
7+
corePlugins: [],
8+
plugins: [
9+
{
10+
modifyConfig: function(config) {
11+
return {
12+
...config,
13+
theme: {
14+
...config.theme,
15+
rotate: {
16+
'0': '0deg',
17+
'90': '90deg',
18+
'180': '180deg',
19+
'270': '270deg',
20+
}
21+
}
22+
}
23+
},
24+
handler: function({ addUtilities, theme }) {
25+
addUtilities(Object.entries(theme('rotate')).map(([key, value]) => {
26+
return {
27+
[`.rotate-${key}`]: {
28+
transform: `rotate(${value})`,
29+
},
30+
}
31+
}))
32+
},
33+
}
34+
]
35+
}),
36+
])
37+
.process('@tailwind utilities;',
38+
{ from: undefined }
39+
)
40+
.then(result => {
41+
const expected = `
42+
.rotate-0 {
43+
transform: rotate(0deg)
44+
}
45+
.rotate-90 {
46+
transform: rotate(90deg)
47+
}
48+
.rotate-180 {
49+
transform: rotate(180deg)
50+
}
51+
.rotate-270 {
52+
transform: rotate(270deg)
53+
}
54+
`
55+
56+
expect(result.css).toMatchCss(expected)
57+
})
58+
})

__tests__/processPlugins.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test('plugins can create utilities with object syntax', () => {
4747
object-fit: cover
4848
}
4949
}
50-
`)
50+
`)
5151
})
5252

5353
test('plugins can create utilities with arrays of objects', () => {
@@ -1206,3 +1206,41 @@ test('prefix will prefix all classes in a selector', () => {
12061206
}
12071207
`)
12081208
})
1209+
1210+
test('plugins can be provided as an object with a handler function', () => {
1211+
const { components, utilities } = processPlugins(
1212+
[
1213+
{
1214+
handler: function({ addUtilities }) {
1215+
addUtilities({
1216+
'.object-fill': {
1217+
'object-fit': 'fill',
1218+
},
1219+
'.object-contain': {
1220+
'object-fit': 'contain',
1221+
},
1222+
'.object-cover': {
1223+
'object-fit': 'cover',
1224+
},
1225+
})
1226+
},
1227+
}
1228+
],
1229+
makeConfig()
1230+
)
1231+
1232+
expect(components.length).toBe(0)
1233+
expect(css(utilities)).toMatchCss(`
1234+
@variants {
1235+
.object-fill {
1236+
object-fit: fill
1237+
}
1238+
.object-contain {
1239+
object-fit: contain
1240+
}
1241+
.object-cover {
1242+
object-fit: cover
1243+
}
1244+
}
1245+
`)
1246+
})

src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ function resolveConfigPath(filePath) {
4444
}
4545
}
4646

47+
function applyPluginConfigModifications(config) {
48+
return [...config.plugins].reduce((modified, plugin) => {
49+
return _.get(plugin, 'modifyConfig', _.identity)(modified)
50+
}, config)
51+
}
52+
4753
const getConfigFunction = config => () => {
4854
if (_.isUndefined(config) && !_.isObject(config)) {
4955
return resolveConfig([defaultConfig])
@@ -55,10 +61,9 @@ const getConfigFunction = config => () => {
5561
})
5662
}
5763

58-
return resolveConfig([
59-
_.isObject(config) ? _.get(config, 'config', config) : require(config),
60-
defaultConfig,
61-
])
64+
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
65+
66+
return resolveConfig([applyPluginConfigModifications(configObject), defaultConfig])
6267
}
6368

6469
const plugin = postcss.plugin('tailwind', config => {

src/util/processPlugins.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash'
22
import postcss from 'postcss'
33
import Node from 'postcss/lib/node'
4+
import isFunction from 'lodash/isFunction'
45
import escapeClassName from '../util/escapeClassName'
56
import generateVariantFunction from '../util/generateVariantFunction'
67
import parseObjectStyles from '../util/parseObjectStyles'
@@ -28,7 +29,9 @@ export default function(plugins, config) {
2829
const getConfigValue = (path, defaultValue) => _.get(config, path, defaultValue)
2930

3031
plugins.forEach(plugin => {
31-
plugin({
32+
const handler = isFunction(plugin) ? plugin : plugin.handler
33+
34+
handler({
3235
postcss,
3336
config: getConfigValue,
3437
theme: (path, defaultValue) => getConfigValue(`theme.${path}`, defaultValue),

0 commit comments

Comments
 (0)