From 9e3700c08a573f4e11857b72fd2c3b2f85785401 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 26 Oct 2020 14:43:31 +0100 Subject: [PATCH] add function presets (#2680) --- __tests__/customConfig.test.js | 115 +++++++++++++++++++++++++++++++++ src/util/getAllConfigs.js | 4 +- 2 files changed, 117 insertions(+), 2 deletions(-) diff --git a/__tests__/customConfig.test.js b/__tests__/customConfig.test.js index 308cb8732bcd..0cc662c26f47 100644 --- a/__tests__/customConfig.test.js +++ b/__tests__/customConfig.test.js @@ -261,6 +261,56 @@ test('the default config can be overridden using the presets key', () => { }) }) +test('presets can be functions', () => { + return postcss([ + tailwind({ + presets: [ + () => ({ + theme: { + extend: { + minHeight: { + 24: '24px', + }, + }, + }, + corePlugins: ['minHeight'], + variants: { minHeight: [] }, + }), + ], + theme: { + extend: { minHeight: { 48: '48px' } }, + }, + }), + ]) + .process( + ` + @tailwind utilities + `, + { from: undefined } + ) + .then((result) => { + const expected = ` + .min-h-0 { + min-height: 0; + } + .min-h-24 { + min-height: 24px; + } + .min-h-48 { + min-height: 48px; + } + .min-h-full { + min-height: 100%; + } + .min-h-screen { + min-height: 100vh; + } + ` + + expect(result.css).toMatchCss(expected) + }) +}) + test('the default config can be removed by using an empty presets key in a preset', () => { return postcss([ tailwind({ @@ -367,3 +417,68 @@ test('presets can have their own presets', () => { expect(result.css).toMatchCss(expected) }) }) + +test('function presets can be mixed with object presets', () => { + return postcss([ + tailwind({ + presets: [ + () => ({ + presets: [], + theme: { + colors: { red: '#dd0000' }, + }, + }), + { + presets: [ + () => ({ + 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' } }, + }, + }), + ]) + .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; + } + ` + + expect(result.css).toMatchCss(expected) + }) +}) diff --git a/src/util/getAllConfigs.js b/src/util/getAllConfigs.js index 5602c7e68bed..3e65d5db1cd2 100644 --- a/src/util/getAllConfigs.js +++ b/src/util/getAllConfigs.js @@ -1,10 +1,10 @@ import defaultConfig from '../../stubs/defaultConfig.stub.js' import { flagEnabled } from '../featureFlags' -import { flatMap, get } from 'lodash' +import { flatMap, get, isFunction } from 'lodash' export default function getAllConfigs(config) { const configs = flatMap([...get(config, 'presets', [defaultConfig])].reverse(), (preset) => { - return getAllConfigs(preset) + return getAllConfigs(isFunction(preset) ? preset() : preset) }) const features = {