Skip to content

Commit 4c25ca5

Browse files
committed
Apply config modifications to default config, before resolving config
1 parent e130771 commit 4c25ca5

File tree

4 files changed

+122
-8
lines changed

4 files changed

+122
-8
lines changed

__tests__/resolveConfig.test.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,3 +1359,105 @@ test('more than two config objects can be resolved', () => {
13591359
},
13601360
})
13611361
})
1362+
test('plugin config modifications are applied', () => {
1363+
const userConfig = {
1364+
plugins: [
1365+
{
1366+
modifyConfig: function (config) {
1367+
return {
1368+
...config,
1369+
prefix: 'tw-'
1370+
}
1371+
},
1372+
handler: function () {}
1373+
}
1374+
]
1375+
}
1376+
1377+
const defaultConfig = {
1378+
prefix: '',
1379+
important: false,
1380+
separator: ':',
1381+
theme: {
1382+
screens: {
1383+
mobile: '400px',
1384+
},
1385+
},
1386+
variants: {
1387+
appearance: ['responsive'],
1388+
borderCollapse: [],
1389+
borderColors: ['responsive', 'hover', 'focus'],
1390+
},
1391+
}
1392+
1393+
const result = resolveConfig([userConfig, defaultConfig])
1394+
1395+
expect(result).toEqual({
1396+
prefix: 'tw-',
1397+
important: false,
1398+
separator: ':',
1399+
theme: {
1400+
screens: {
1401+
mobile: '400px',
1402+
},
1403+
},
1404+
variants: {
1405+
appearance: ['responsive'],
1406+
borderCollapse: [],
1407+
borderColors: ['responsive', 'hover', 'focus'],
1408+
},
1409+
plugins: userConfig.plugins,
1410+
})
1411+
})
1412+
1413+
test('user config takes precedence over plugin config modifications', () => {
1414+
const userConfig = {
1415+
prefix: 'user-',
1416+
plugins: [
1417+
{
1418+
modifyConfig: function (config) {
1419+
return {
1420+
...config,
1421+
prefix: 'plugin-'
1422+
}
1423+
},
1424+
handler: function () {}
1425+
}
1426+
]
1427+
}
1428+
1429+
const defaultConfig = {
1430+
prefix: '',
1431+
important: false,
1432+
separator: ':',
1433+
theme: {
1434+
screens: {
1435+
mobile: '400px',
1436+
},
1437+
},
1438+
variants: {
1439+
appearance: ['responsive'],
1440+
borderCollapse: [],
1441+
borderColors: ['responsive', 'hover', 'focus'],
1442+
},
1443+
}
1444+
1445+
const result = resolveConfig([userConfig, defaultConfig])
1446+
1447+
expect(result).toEqual({
1448+
prefix: 'user-',
1449+
important: false,
1450+
separator: ':',
1451+
theme: {
1452+
screens: {
1453+
mobile: '400px',
1454+
},
1455+
},
1456+
variants: {
1457+
appearance: ['responsive'],
1458+
borderCollapse: [],
1459+
borderColors: ['responsive', 'hover', 'focus'],
1460+
},
1461+
plugins: userConfig.plugins,
1462+
})
1463+
})

__tmp_4/tailwind.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
theme: {
3+
screens: {
4+
mobile: '400px',
5+
},
6+
},
7+
}

src/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ 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-
5347
const getConfigFunction = config => () => {
5448
if (_.isUndefined(config) && !_.isObject(config)) {
5549
return resolveConfig([defaultConfig])
@@ -63,7 +57,7 @@ const getConfigFunction = config => () => {
6357

6458
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
6559

66-
return resolveConfig([applyPluginConfigModifications(configObject), defaultConfig])
60+
return resolveConfig([configObject, defaultConfig])
6761
}
6862

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

src/util/resolveConfig.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import mergeWith from 'lodash/mergeWith'
33
import isFunction from 'lodash/isFunction'
44
import isUndefined from 'lodash/isUndefined'
55
import defaults from 'lodash/defaults'
6+
import identity from 'lodash/identity'
7+
import get from 'lodash/get'
68
import map from 'lodash/map'
79
import get from 'lodash/get'
810
import toPath from 'lodash/toPath'
@@ -22,6 +24,12 @@ const configUtils = {
2224
},
2325
}
2426

27+
function applyPluginConfigModifications(config, plugins) {
28+
return plugins.reduce((modified, plugin) => {
29+
return get(plugin, 'modifyConfig', identity)(modified)
30+
}, config)
31+
}
32+
2533
function value(valueToResolve, ...args) {
2634
return isFunction(valueToResolve) ? valueToResolve(...args) : valueToResolve
2735
}
@@ -94,7 +102,10 @@ function resolveFunctionKeys(object) {
94102
}, {})
95103
}
96104

97-
export default function resolveConfig(configs) {
105+
export default function resolveConfig([userConfig, defaultConfig]) {
106+
const modifiedDefaultConfig = applyPluginConfigModifications(defaultConfig, get(userConfig, 'plugins', []))
107+
const configs = [userConfig, modifiedDefaultConfig]
108+
98109
return defaults(
99110
{
100111
// Need to get a default empty object if the config has no theme

0 commit comments

Comments
 (0)