forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstituteClassApplyAtRules.js
More file actions
46 lines (39 loc) · 1.3 KB
/
substituteClassApplyAtRules.js
File metadata and controls
46 lines (39 loc) · 1.3 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
import postcss from 'postcss'
import _ from 'lodash'
import findMixin from '../util/findMixin'
import escapeClassName from '../util/escapeClassName'
function normalizeClassNames(classNames) {
return classNames.map((className) => {
return `.${escapeClassName(_.trimStart(className, '.'))}`
})
}
export default function(config) {
return function (css) {
const options = config()
css.walkRules(function(rule) {
rule.walkAtRules('apply', atRule => {
const mixins = normalizeClassNames(postcss.list.space(atRule.params))
/*
* Don't wreck CSSNext-style @apply rules:
* http://cssnext.io/features/#custom-properties-set-apply
*
* These are deprecated in CSSNext but still playing it safe for now.
* We might consider renaming this at-rule.
*/
const [customProperties, classes] = _.partition(mixins, mixin => {
return _.startsWith(mixin, '--')
})
const decls = _.flatMap(classes, mixin => {
return findMixin(css, mixin, () => {
throw atRule.error(`No ${mixin} class found.`)
})
})
atRule.before(decls)
atRule.params = customProperties.join(' ')
if (_.isEmpty(customProperties)) {
atRule.remove()
}
})
})
}
}