forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessPlugins.js
More file actions
70 lines (59 loc) · 1.8 KB
/
processPlugins.js
File metadata and controls
70 lines (59 loc) · 1.8 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
import _ from 'lodash'
import postcss from 'postcss'
import escapeClassName from '../util/escapeClassName'
import prefixSelector from '../util/prefixSelector'
import wrapWithVariants from '../util/wrapWithVariants'
function defineRule(selector, properties) {
const decls = _.map(properties, (value, property) => {
return postcss.decl({
prop: `${property}`,
value: `${value}`,
})
})
return postcss.rule({ selector }).append(decls)
}
function defineUtility(selector, properties, options) {
if (selector.startsWith('.')) {
return defineUtility(selector.slice(1), properties, options)
}
const rule = defineRule(
prefixSelector(options.prefix, `.${escapeClassName(selector)}`),
properties
)
if (options.important) {
rule.walkDecls(decl => (decl.important = true))
}
return rule
}
function defineAtRule(atRule, rules) {
const [name, ...params] = atRule.split(' ')
return postcss
.atRule({
name: name.startsWith('@') ? name.slice(1) : name,
params: params.join(' '),
})
.append(rules)
}
export default function(config) {
const pluginComponents = []
const pluginUtilities = []
config.plugins.forEach(plugin => {
plugin({
config: (path, defaultValue) => _.get(config, path, defaultValue),
rule: defineRule,
utility: (selector, properties) => defineUtility(selector, properties, config.options),
atRule: defineAtRule,
e: escapeClassName,
addUtilities: (utilities, variants = []) => {
pluginUtilities.push(wrapWithVariants(utilities, variants))
},
addComponents: components => {
pluginComponents.push(...components)
},
prefix: selector => {
return prefixSelector(config.options.prefix, selector)
},
})
})
return [pluginComponents, pluginUtilities]
}