From 75dfa4ea547383a9e602604023c15da3d94b8290 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Sun, 4 Oct 2020 15:12:21 -0400 Subject: [PATCH] Drop array format for `presets` key instead --- __tests__/customConfig.test.js | 109 +++++++++++++++++++-------------- resolveConfig.js | 3 - src/index.js | 8 --- src/util/getAllConfigs.js | 8 ++- 4 files changed, 69 insertions(+), 59 deletions(-) diff --git a/__tests__/customConfig.test.js b/__tests__/customConfig.test.js index c8be6bd0ee70..ae00c0ba61e2 100644 --- a/__tests__/customConfig.test.js +++ b/__tests__/customConfig.test.js @@ -211,26 +211,26 @@ test('tailwind.config.js is picked up by default when passing an empty object', }) }) -test('when custom config is an array the default config is not included', () => { +test('the default config can be overridden using the presets key', () => { return postcss([ - tailwind([ - { - theme: { - extend: { - colors: { - black: 'black', + tailwind({ + presets: [ + { + theme: { + extend: { + colors: { + black: 'black', + }, + backgroundColor: theme => theme('colors'), }, - backgroundColor: theme => theme('colors'), }, + corePlugins: ['backgroundColor'], }, - corePlugins: ['backgroundColor'], - }, - { - theme: { - extend: { colors: { white: 'white' } }, - }, + ], + theme: { + extend: { colors: { white: 'white' } }, }, - ]), + }), ]) .process( ` @@ -252,48 +252,65 @@ test('when custom config is an array the default config is not included', () => }) }) -test('when custom config is an array in a file the default config is not included', () => { - return inTempDirectory(() => { - fs.writeFileSync( - path.resolve(defaultConfigFile), - `module.exports = [ +test('presets can have their own presets', () => { + return postcss([ + tailwind({ + presets: [ + { + theme: { + colors: { red: '#dd0000' }, + }, + }, { + presets: [ + { + theme: { + colors: { + transparent: 'transparent', + red: '#ff0000', + }, + }, + }, + ], theme: { extend: { colors: { black: 'black', + red: '#ee0000', }, backgroundColor: theme => theme('colors'), }, }, corePlugins: ['backgroundColor'], }, - { - theme: { - extend: { colors: { white: 'white' } }, - }, - } - ]` + ], + theme: { + extend: { colors: { white: 'white' } }, + }, + }), + ]) + .process( + ` + @tailwind utilities + `, + { from: undefined } ) + .then(result => { + const expected = ` + .bg-transparent { + background-color: transparent; + } + .bg-red { + background-color: #ee0000; + } + .bg-black { + background-color: black; + } + .bg-white { + background-color: white; + } + ` - return postcss([tailwind()]) - .process( - ` - @tailwind utilities - `, - { from: undefined } - ) - .then(result => { - const expected = ` - .bg-black { - background-color: black; - } - .bg-white { - background-color: white; - } - ` - - expect(result.css).toMatchCss(expected) - }) - }) + expect(result.css).toMatchCss(expected) + }) }) diff --git a/resolveConfig.js b/resolveConfig.js index 5a65b6f73616..6fb5c15fb243 100644 --- a/resolveConfig.js +++ b/resolveConfig.js @@ -2,9 +2,6 @@ const resolveConfigObjects = require('./lib/util/resolveConfig').default const getAllConfigs = require('./lib/util/getAllConfigs').default module.exports = function resolveConfig(...configs) { - if (configs.length === 1 && Array.isArray(configs[0])) { - return resolveConfigObjects([...configs[0]].reverse()) - } const [, ...defaultConfigs] = getAllConfigs(configs[0]) return resolveConfigObjects([...configs, ...defaultConfigs]) } diff --git a/src/index.js b/src/index.js index ae4f06b5b46b..d2def911941f 100644 --- a/src/index.js +++ b/src/index.js @@ -14,10 +14,6 @@ import { defaultConfigFile } from './constants' import defaultConfig from '../stubs/defaultConfig.stub.js' function resolveConfigPath(filePath) { - // require('tailwindcss')([{ theme: ..., variants: ... }, {...}]) - if (Array.isArray(filePath)) { - return undefined - } // require('tailwindcss')({ theme: ..., variants: ... }) if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) { return undefined @@ -64,10 +60,6 @@ const getConfigFunction = config => () => { const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config) - if (Array.isArray(configObject)) { - return resolveConfig([...configObject].reverse()) - } - return resolveConfig([...getAllConfigs(configObject)]) } diff --git a/src/util/getAllConfigs.js b/src/util/getAllConfigs.js index b48c003a6128..ef67e06ce8c8 100644 --- a/src/util/getAllConfigs.js +++ b/src/util/getAllConfigs.js @@ -7,9 +7,13 @@ import extendedFontSizeScale from '../flagged/extendedFontSizeScale.js' import darkModeVariant from '../flagged/darkModeVariant.js' import standardFontWeights from '../flagged/standardFontWeights' import additionalBreakpoint from '../flagged/additionalBreakpoint' +import { flatMap, get } from 'lodash' + +export default function getAllConfigs(config, defaultPresets = [defaultConfig]) { + const configs = flatMap([...get(config, 'presets', defaultPresets)].reverse(), preset => { + return getAllConfigs(preset, []) + }) -export default function getAllConfigs(config) { - const configs = [defaultConfig] const features = { uniformColorPalette, extendedSpacingScale,