forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (59 loc) · 1.87 KB
/
index.js
File metadata and controls
73 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import path from 'path'
import fs from 'fs'
import _ from 'lodash'
import postcss from 'postcss'
import perfectionist from 'perfectionist'
import registerConfigAsDependency from './lib/registerConfigAsDependency'
import processTailwindFeatures from './processTailwindFeatures'
import resolveConfig from './util/resolveConfig'
import defaultConfig from '../stubs/defaultConfig.stub.js'
function resolveConfigPath(filePath) {
if (_.isObject(filePath)) {
return undefined
}
if (!_.isUndefined(filePath)) {
return path.resolve(filePath)
}
try {
const defaultConfigPath = path.resolve('./tailwind.config.js')
fs.accessSync(defaultConfigPath)
return defaultConfigPath
} catch (err) {
return undefined
}
}
const getConfigFunction = config => () => {
if (_.isUndefined(config) && !_.isObject(config)) {
return resolveConfig([defaultConfig])
}
if (!_.isObject(config)) {
delete require.cache[require.resolve(config)]
}
return resolveConfig([_.isObject(config) ? config : require(config), defaultConfig])
}
const plugin = postcss.plugin('tailwind', config => {
const plugins = []
const resolvedConfigPath = resolveConfigPath(config)
if (!_.isUndefined(resolvedConfigPath)) {
plugins.push(registerConfigAsDependency(resolvedConfigPath))
}
return postcss([
...plugins,
processTailwindFeatures(getConfigFunction(resolvedConfigPath || config)),
perfectionist({
cascade: true,
colorShorthand: true,
indentSize: 2,
maxSelectorLength: 1,
maxValueLength: false,
trimLeadingZero: true,
trimTrailingZeros: true,
zeroLengthNoUnit: false,
}),
])
})
plugin.defaultConfig = function() {
// prettier-ignore
throw new Error("`require('tailwindcss').defaultConfig()` is no longer a function, access it instead as `require('tailwindcss/defaultConfig')()`.")
}
module.exports = plugin