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
87 lines (69 loc) · 2.6 KB
/
substituteClassApplyAtRules.js
File metadata and controls
87 lines (69 loc) · 2.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import _ from 'lodash'
import postcss from 'postcss'
import escapeClassName from '../util/escapeClassName'
function buildClassTable(css) {
const classTable = {}
css.walkRules(rule => {
if (!_.has(classTable, rule.selector)) {
classTable[rule.selector] = []
}
classTable[rule.selector].push(rule)
})
return classTable
}
function normalizeClassName(className) {
return `.${escapeClassName(_.trimStart(className, '.'))}`
}
function findMixin(classTable, mixin, onError) {
const matches = _.get(classTable, mixin, [])
if (_.isEmpty(matches)) {
// prettier-ignore
onError(`\`@apply\` cannot be used with \`${mixin}\` because \`${mixin}\` either cannot be found, or it's actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that \`${mixin}\` exists, make sure that any \`@import\` statements are being properly processed *before* Tailwind CSS sees your CSS, as \`@apply\` can only be used for classes in the same CSS tree.`)
}
if (matches.length > 1) {
// prettier-ignore
onError(`\`@apply\` cannot be used with ${mixin} because ${mixin} is included in multiple rulesets.`)
}
const [match] = matches
if (match.parent.type !== 'root') {
// prettier-ignore
onError(`\`@apply\` cannot be used with ${mixin} because ${mixin} is nested inside of an at-rule (@${match.parent.name}).`)
}
return match.clone().nodes
}
export default function() {
return function(css) {
const classLookup = buildClassTable(css)
css.walkRules(rule => {
rule.walkAtRules('apply', atRule => {
const mixins = 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 = _(classes)
.reject(mixin => mixin === '!important')
.flatMap(mixin => {
return findMixin(classLookup, normalizeClassName(mixin), message => {
throw atRule.error(message)
})
})
.value()
_.tap(_.last(mixins) === '!important', important => {
decls.forEach(decl => (decl.important = important))
})
atRule.before(decls)
atRule.params = customProperties.join(' ')
if (_.isEmpty(customProperties)) {
atRule.remove()
}
})
})
}
}