Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit ee4b418

Browse files
committed
WIP
Not exactly the expected behavior yet.
1 parent f6ba638 commit ee4b418

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

src/lib/expandApplyAtRules.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,24 @@ function expandApplyAtRules(context) {
4343
let applyCandidates = new Set()
4444

4545
// Collect all @apply rules and candidates
46-
let applies = []
46+
let ruleApplies = new Map()
4747
root.walkAtRules('apply', (rule) => {
4848
let [candidates, important] = extractApplyCandidates(rule.params)
4949

50+
if (!ruleApplies.has(rule.parent)) {
51+
ruleApplies.set(rule.parent, new Set())
52+
}
53+
54+
let applies = ruleApplies.get(rule.parent)
5055
for (let util of candidates) {
56+
applies.add([util, important])
5157
applyCandidates.add(util)
5258
}
53-
applies.push(rule)
59+
rule.remove()
5460
})
5561

5662
// Start the @apply process if we have rules with @apply in them
57-
if (applies.length > 0) {
63+
if (ruleApplies.size > 0) {
5864
// Fill up some caches!
5965
let applyClassCache = buildApplyCache(applyCandidates, context)
6066

@@ -96,11 +102,9 @@ function expandApplyAtRules(context) {
96102
.join(', ')
97103
}
98104

99-
for (let apply of applies) {
105+
for (let [applyParentRule, applyCandidates] of ruleApplies.entries()) {
100106
let siblings = []
101-
let [applyCandidates, important] = extractApplyCandidates(apply.params)
102-
103-
for (let applyCandidate of applyCandidates) {
107+
for (let [applyCandidate, important] of applyCandidates) {
104108
if (!applyClassCache.has(applyCandidate)) {
105109
throw apply.error(
106110
`The \`${applyCandidate}\` class does not exist. If \`${applyCandidate}\` is a custom class, make sure it is defined within a \`@layer\` directive.`
@@ -113,7 +117,11 @@ function expandApplyAtRules(context) {
113117
let root = postcss.root({ nodes: [node.clone()] })
114118

115119
root.walkRules((rule) => {
116-
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)
120+
rule.selector = replaceSelector(
121+
applyParentRule.selector,
122+
rule.selector,
123+
applyCandidate
124+
)
117125
rule.walkDecls((d) => {
118126
d.important = important
119127
})
@@ -125,16 +133,13 @@ function expandApplyAtRules(context) {
125133

126134
// Inject the rules, sorted, correctly
127135
for (let [, sibling] of siblings.sort(([a], [z]) => bigSign(z.sort - a.sort))) {
128-
// `apply.parent` is referring to the node at `.abc` in: .abc { @apply mt-2 }
129-
apply.parent.after(sibling)
136+
// `applyParentRule` is referring to the node at `.abc` in: .abc { @apply mt-2 }
137+
applyParentRule.after(sibling)
130138
}
131139

132-
// If there are left-over declarations, just remove the @apply
133-
if (apply.parent.nodes.length > 1) {
134-
apply.remove()
135-
} else {
136-
// The node is empty, drop the full node
137-
apply.parent.remove()
140+
// The node is empty, drop the full node
141+
if (applyParentRule.nodes.length === 0) {
142+
applyParentRule.remove()
138143
}
139144
}
140145
}

tests/10-apply.test.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,29 @@
178178
font-weight: 400;
179179
}
180180
}
181+
.multiple-apply-directives {
182+
text-align: center;
183+
font-weight: 600;
184+
}
185+
.multiple-apply-directives:hover {
186+
font-weight: 700;
187+
}
188+
.multiple-apply-directives:focus {
189+
font-weight: 500;
190+
}
191+
@media (min-width: 1024px) {
192+
.multiple-apply-directives {
193+
font-weight: 300;
194+
}
195+
}
196+
@media (min-width: 1280px) {
197+
.multiple-apply-directives:focus {
198+
font-weight: 900;
199+
}
200+
}
201+
.apply-sandwich {
202+
font-weight: 300;
203+
text-align: right;
204+
font-weight: 500;
205+
text-align: center;
206+
}

tests/10-apply.test.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@
2323
<div class="basic-nesting-parent">
2424
<div class="basic-nesting-child"></div>
2525
</div>
26+
<div class="multiple-apply-directives"></div>
27+
<div class="apply-sandwich"></div>
2628
</body>
2729
</html>

tests/10-apply.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ test('@apply', () => {
6565
@apply font-bold hover:font-normal;
6666
}
6767
}
68+
.multiple-apply-directives {
69+
@apply xl:focus:font-black hover:font-bold;
70+
@apply lg:font-light focus:font-medium;
71+
@apply font-semibold text-center;
72+
}
73+
.apply-sandwich {
74+
@apply p-4;
75+
padding-top: 11px;
76+
@apply py-3;
77+
}
6878
}
6979
7080
@layer utilities {
@@ -82,4 +92,5 @@ test('@apply', () => {
8292
})
8393
})
8494

95+
// TODO: Test @apply !important
8596
// TODO: Test stuff that should throw

0 commit comments

Comments
 (0)