From 4c25ca5ed6e563088aed5c92a59f76cfd182aec7 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 11 Oct 2019 14:25:29 -0400 Subject: [PATCH] Apply config modifications to default config, before resolving config --- __tests__/resolveConfig.test.js | 102 ++++++++++++++++++++++++++++++++ __tmp_4/tailwind.config.js | 7 +++ src/index.js | 8 +-- src/util/resolveConfig.js | 13 +++- 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 __tmp_4/tailwind.config.js diff --git a/__tests__/resolveConfig.test.js b/__tests__/resolveConfig.test.js index 927ed2338..cd9d9dc73 100644 --- a/__tests__/resolveConfig.test.js +++ b/__tests__/resolveConfig.test.js @@ -1359,3 +1359,105 @@ test('more than two config objects can be resolved', () => { }, }) }) +test('plugin config modifications are applied', () => { + const userConfig = { + plugins: [ + { + modifyConfig: function (config) { + return { + ...config, + prefix: 'tw-' + } + }, + handler: function () {} + } + ] + } + + const defaultConfig = { + prefix: '', + important: false, + separator: ':', + theme: { + screens: { + mobile: '400px', + }, + }, + variants: { + appearance: ['responsive'], + borderCollapse: [], + borderColors: ['responsive', 'hover', 'focus'], + }, + } + + const result = resolveConfig([userConfig, defaultConfig]) + + expect(result).toEqual({ + prefix: 'tw-', + important: false, + separator: ':', + theme: { + screens: { + mobile: '400px', + }, + }, + variants: { + appearance: ['responsive'], + borderCollapse: [], + borderColors: ['responsive', 'hover', 'focus'], + }, + plugins: userConfig.plugins, + }) +}) + +test('user config takes precedence over plugin config modifications', () => { + const userConfig = { + prefix: 'user-', + plugins: [ + { + modifyConfig: function (config) { + return { + ...config, + prefix: 'plugin-' + } + }, + handler: function () {} + } + ] + } + + const defaultConfig = { + prefix: '', + important: false, + separator: ':', + theme: { + screens: { + mobile: '400px', + }, + }, + variants: { + appearance: ['responsive'], + borderCollapse: [], + borderColors: ['responsive', 'hover', 'focus'], + }, + } + + const result = resolveConfig([userConfig, defaultConfig]) + + expect(result).toEqual({ + prefix: 'user-', + important: false, + separator: ':', + theme: { + screens: { + mobile: '400px', + }, + }, + variants: { + appearance: ['responsive'], + borderCollapse: [], + borderColors: ['responsive', 'hover', 'focus'], + }, + plugins: userConfig.plugins, + }) +}) diff --git a/__tmp_4/tailwind.config.js b/__tmp_4/tailwind.config.js new file mode 100644 index 000000000..42791f2cf --- /dev/null +++ b/__tmp_4/tailwind.config.js @@ -0,0 +1,7 @@ +module.exports = { + theme: { + screens: { + mobile: '400px', + }, + }, + } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 32225d99f..9dd2ab582 100644 --- a/src/index.js +++ b/src/index.js @@ -44,12 +44,6 @@ function resolveConfigPath(filePath) { } } -function applyPluginConfigModifications(config) { - return [...config.plugins].reduce((modified, plugin) => { - return _.get(plugin, 'modifyConfig', _.identity)(modified) - }, config) -} - const getConfigFunction = config => () => { if (_.isUndefined(config) && !_.isObject(config)) { return resolveConfig([defaultConfig]) @@ -63,7 +57,7 @@ const getConfigFunction = config => () => { const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config) - return resolveConfig([applyPluginConfigModifications(configObject), defaultConfig]) + return resolveConfig([configObject, defaultConfig]) } const plugin = postcss.plugin('tailwind', config => { diff --git a/src/util/resolveConfig.js b/src/util/resolveConfig.js index cbf2f93d3..dc649816b 100644 --- a/src/util/resolveConfig.js +++ b/src/util/resolveConfig.js @@ -3,6 +3,8 @@ import mergeWith from 'lodash/mergeWith' import isFunction from 'lodash/isFunction' import isUndefined from 'lodash/isUndefined' import defaults from 'lodash/defaults' +import identity from 'lodash/identity' +import get from 'lodash/get' import map from 'lodash/map' import get from 'lodash/get' import toPath from 'lodash/toPath' @@ -22,6 +24,12 @@ const configUtils = { }, } +function applyPluginConfigModifications(config, plugins) { + return plugins.reduce((modified, plugin) => { + return get(plugin, 'modifyConfig', identity)(modified) + }, config) +} + function value(valueToResolve, ...args) { return isFunction(valueToResolve) ? valueToResolve(...args) : valueToResolve } @@ -94,7 +102,10 @@ function resolveFunctionKeys(object) { }, {}) } -export default function resolveConfig(configs) { +export default function resolveConfig([userConfig, defaultConfig]) { + const modifiedDefaultConfig = applyPluginConfigModifications(defaultConfig, get(userConfig, 'plugins', [])) + const configs = [userConfig, modifiedDefaultConfig] + return defaults( { // Need to get a default empty object if the config has no theme