Skip to content

Commit 600523d

Browse files
committed
Support array for Tailwind config
1 parent 12d52c2 commit 600523d

File tree

9 files changed

+117
-9
lines changed

9 files changed

+117
-9
lines changed

__tests__/customConfig.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,90 @@ test('tailwind.config.js is picked up by default when passing an empty object',
210210
})
211211
})
212212
})
213+
214+
test('when custom config is an array the default config is not included', () => {
215+
return postcss([
216+
tailwind([
217+
{
218+
theme: {
219+
extend: {
220+
colors: {
221+
black: 'black',
222+
},
223+
backgroundColor: theme => theme('colors'),
224+
},
225+
},
226+
corePlugins: ['backgroundColor'],
227+
},
228+
{
229+
theme: {
230+
extend: { colors: { white: 'white' } },
231+
},
232+
},
233+
]),
234+
])
235+
.process(
236+
`
237+
@tailwind utilities
238+
`,
239+
{ from: undefined }
240+
)
241+
.then(result => {
242+
const expected = `
243+
.bg-black {
244+
background-color: black;
245+
}
246+
.bg-white {
247+
background-color: white;
248+
}
249+
`
250+
251+
expect(result.css).toMatchCss(expected)
252+
})
253+
})
254+
255+
test('when custom config is an array in a file the default config is not included', () => {
256+
return inTempDirectory(() => {
257+
fs.writeFileSync(
258+
path.resolve(defaultConfigFile),
259+
`module.exports = [
260+
{
261+
theme: {
262+
extend: {
263+
colors: {
264+
black: 'black',
265+
},
266+
backgroundColor: theme => theme('colors'),
267+
},
268+
},
269+
corePlugins: ['backgroundColor'],
270+
},
271+
{
272+
theme: {
273+
extend: { colors: { white: 'white' } },
274+
},
275+
}
276+
]`
277+
)
278+
279+
return postcss([tailwind()])
280+
.process(
281+
`
282+
@tailwind utilities
283+
`,
284+
{ from: undefined }
285+
)
286+
.then(result => {
287+
const expected = `
288+
.bg-black {
289+
background-color: black;
290+
}
291+
.bg-white {
292+
background-color: white;
293+
}
294+
`
295+
296+
expect(result.css).toMatchCss(expected)
297+
})
298+
})
299+
})

__tests__/resolveConfig.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,6 @@ test('variants can be defined as a function', () => {
17961796
rotate: ['responsive', 'focus'],
17971797
cursor: ['focus', 'checked', 'hover'],
17981798
},
1799-
plugins: userConfig.plugins,
18001799
})
18011800
})
18021801

@@ -1831,6 +1830,12 @@ test('core plugin configurations stack', () => {
18311830
corePlugins: { display: false },
18321831
}
18331832

1833+
const otherConfig = {
1834+
corePlugins: ({ corePlugins }) => {
1835+
return [...corePlugins, 'margin']
1836+
},
1837+
}
1838+
18341839
const defaultConfig = {
18351840
prefix: '',
18361841
important: false,
@@ -1840,14 +1845,14 @@ test('core plugin configurations stack', () => {
18401845
corePlugins: ['float', 'display', 'padding'],
18411846
}
18421847

1843-
const result = resolveConfig([userConfig, defaultConfig])
1848+
const result = resolveConfig([userConfig, otherConfig, defaultConfig])
18441849

18451850
expect(result).toMatchObject({
18461851
prefix: '',
18471852
important: false,
18481853
separator: ':',
18491854
theme: {},
18501855
variants: {},
1851-
corePlugins: ['float', 'padding'],
1856+
corePlugins: ['float', 'padding', 'margin'],
18521857
})
18531858
})

jest/runInTempDirectory.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import path from 'path'
33

44
import rimraf from 'rimraf'
55

6+
let id = 0
7+
68
export default function(callback) {
79
return new Promise(resolve => {
8-
const workerId = process.env.JEST_WORKER_ID
10+
const workerId = `${process.env.JEST_WORKER_ID}-${id++}`
911
const tmpPath = path.resolve(__dirname, `../__tmp_${workerId}`)
1012
const currentPath = process.cwd()
1113

resolveConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const resolveConfigObjects = require('./lib/util/resolveConfig').default
22
const getAllConfigs = require('./lib/util/getAllConfigs').default
33

44
module.exports = function resolveConfig(...configs) {
5+
if (configs.length === 1 && Array.isArray(configs[0])) {
6+
return resolveConfigObjects([...configs[0]].reverse())
7+
}
58
const [, ...defaultConfigs] = getAllConfigs(configs[0])
6-
79
return resolveConfigObjects([...configs, ...defaultConfigs])
810
}

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { defaultConfigFile } from './constants'
1414
import defaultConfig from '../stubs/defaultConfig.stub.js'
1515

1616
function resolveConfigPath(filePath) {
17+
// require('tailwindcss')([{ theme: ..., variants: ... }, {...}])
18+
if (Array.isArray(filePath)) {
19+
return undefined
20+
}
1721
// require('tailwindcss')({ theme: ..., variants: ... })
1822
if (_.isObject(filePath) && !_.has(filePath, 'config') && !_.isEmpty(filePath)) {
1923
return undefined
@@ -45,7 +49,7 @@ function resolveConfigPath(filePath) {
4549
}
4650

4751
const getConfigFunction = config => () => {
48-
if (_.isUndefined(config) && !_.isObject(config)) {
52+
if (_.isUndefined(config)) {
4953
return resolveConfig([...getAllConfigs(defaultConfig)])
5054
}
5155

@@ -60,6 +64,10 @@ const getConfigFunction = config => () => {
6064

6165
const configObject = _.isObject(config) ? _.get(config, 'config', config) : require(config)
6266

67+
if (Array.isArray(configObject)) {
68+
return resolveConfig([...configObject].reverse())
69+
}
70+
6371
return resolveConfig([...getAllConfigs(configObject)])
6472
}
6573

src/processTailwindFeatures.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function(getConfig) {
3737
[
3838
...corePlugins(config),
3939
...[flagEnabled(config, 'darkModeVariant') ? darkModeVariantPlugin : () => {}],
40-
...config.plugins,
40+
..._.get(config, 'plugins', []),
4141
],
4242
config
4343
)

src/util/prefixSelector.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import parser from 'postcss-selector-parser'
22
import tap from 'lodash/tap'
33

44
export default function(prefix, selector) {
5-
const getPrefix = typeof prefix === 'function' ? prefix : () => prefix
5+
const getPrefix =
6+
typeof prefix === 'function' ? prefix : () => (prefix === undefined ? '' : prefix)
67

78
return parser(selectors => {
89
selectors.walkClasses(classSelector => {

src/util/processPlugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default function(plugins, config) {
7474
return config.target === 'browserslist' ? browserslistTarget : config.target
7575
}
7676

77-
const [defaultTarget, targetOverrides] = getConfigValue('target')
77+
const [defaultTarget, targetOverrides] = getConfigValue('target', 'relaxed')
7878

7979
const target = _.get(targetOverrides, path, defaultTarget)
8080

src/util/resolveConfig.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ function resolveVariants([firstConfig, ...variantConfigs]) {
191191

192192
function resolveCorePlugins(corePluginConfigs) {
193193
const result = [...corePluginConfigs].reverse().reduce((resolved, corePluginConfig) => {
194+
if (isFunction(corePluginConfig)) {
195+
return corePluginConfig({ corePlugins: resolved })
196+
}
194197
return configurePlugins(corePluginConfig, resolved)
195198
}, Object.keys(corePluginList))
196199

0 commit comments

Comments
 (0)