2017-10-15 13:52:51 -04:00
|
|
|
import path from 'path'
|
2019-03-04 16:19:07 -05:00
|
|
|
import fs from 'fs'
|
2017-10-15 13:52:51 -04:00
|
|
|
|
2017-08-26 14:36:40 -05:00
|
|
|
import _ from 'lodash'
|
|
|
|
|
import postcss from 'postcss'
|
2018-01-18 22:58:08 -05:00
|
|
|
import perfectionist from 'perfectionist'
|
2017-08-27 18:42:10 -04:00
|
|
|
|
2017-11-03 09:54:15 -04:00
|
|
|
import registerConfigAsDependency from './lib/registerConfigAsDependency'
|
2018-06-22 20:52:59 -04:00
|
|
|
import processTailwindFeatures from './processTailwindFeatures'
|
2019-02-05 20:36:54 -05:00
|
|
|
import resolveConfig from './util/resolveConfig'
|
2019-03-15 07:08:34 -05:00
|
|
|
import { defaultConfigFile } from './constants'
|
2017-11-22 13:26:18 -05:00
|
|
|
|
2019-03-14 15:51:21 -05:00
|
|
|
import defaultConfig from '../stubs/defaultConfig.stub.js'
|
|
|
|
|
|
2019-03-04 16:19:07 -05:00
|
|
|
function resolveConfigPath(filePath) {
|
|
|
|
|
if (_.isObject(filePath)) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_.isUndefined(filePath)) {
|
|
|
|
|
return path.resolve(filePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2019-03-15 07:08:34 -05:00
|
|
|
const defaultConfigPath = path.resolve(defaultConfigFile)
|
2019-03-04 16:19:07 -05:00
|
|
|
fs.accessSync(defaultConfigPath)
|
|
|
|
|
return defaultConfigPath
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-03 09:54:15 -04:00
|
|
|
|
2019-03-04 16:19:07 -05:00
|
|
|
const getConfigFunction = config => () => {
|
|
|
|
|
if (_.isUndefined(config) && !_.isObject(config)) {
|
2019-03-14 15:51:21 -05:00
|
|
|
return resolveConfig([defaultConfig])
|
2017-10-26 09:29:23 -04:00
|
|
|
}
|
|
|
|
|
|
2019-03-04 16:19:07 -05:00
|
|
|
if (!_.isObject(config)) {
|
|
|
|
|
delete require.cache[require.resolve(config)]
|
|
|
|
|
}
|
2017-11-03 09:54:15 -04:00
|
|
|
|
2019-03-14 15:51:21 -05:00
|
|
|
return resolveConfig([_.isObject(config) ? config : require(config), defaultConfig])
|
2019-03-04 16:19:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const plugin = postcss.plugin('tailwind', config => {
|
|
|
|
|
const plugins = []
|
|
|
|
|
const resolvedConfigPath = resolveConfigPath(config)
|
2018-06-22 21:01:18 -04:00
|
|
|
|
2019-03-04 16:19:07 -05:00
|
|
|
if (!_.isUndefined(resolvedConfigPath)) {
|
|
|
|
|
plugins.push(registerConfigAsDependency(resolvedConfigPath))
|
2017-10-15 13:52:51 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 20:52:59 -04:00
|
|
|
return postcss([
|
2017-11-04 00:35:59 -05:00
|
|
|
...plugins,
|
2019-03-05 07:41:26 -05:00
|
|
|
processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)),
|
2018-06-22 20:52:59 -04:00
|
|
|
perfectionist({
|
|
|
|
|
cascade: true,
|
|
|
|
|
colorShorthand: true,
|
|
|
|
|
indentSize: 2,
|
|
|
|
|
maxSelectorLength: 1,
|
|
|
|
|
maxValueLength: false,
|
|
|
|
|
trimLeadingZero: true,
|
|
|
|
|
trimTrailingZeros: true,
|
2018-08-14 13:37:08 -04:00
|
|
|
zeroLengthNoUnit: false,
|
2018-06-22 20:52:59 -04:00
|
|
|
}),
|
|
|
|
|
])
|
2017-08-24 19:38:25 -04:00
|
|
|
})
|
2017-08-28 15:02:50 -05:00
|
|
|
|
|
|
|
|
module.exports = plugin
|