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

Commit 5644eb4

Browse files
committed
implement naive recursive @apply
Currently what it will do is re-run the apply code. If it turns out that there are @apply rules left, then it will re-apply those. If there are no @apply rules left, this means that we are "done". One big downside is that we are (at the time of this commit) running the apply code for the test at least 4 times. The good part is that we are doing it on the exact same tree so we don't need to re-parse it. This is the first implementation which can be improved!
1 parent 66c47d3 commit 5644eb4

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/lib/expandApplyAtRules.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ function buildApplyCache(applyCandidates, context) {
99
}
1010

1111
if (context.classCache.has(candidate)) {
12-
context.applyClassCache.set(candidate, context.classCache.get(candidate))
12+
context.applyClassCache.set(
13+
candidate,
14+
context.classCache.get(candidate).map(([meta, rule]) => [meta, rule.clone()])
15+
)
1316
continue
1417
}
1518

@@ -38,7 +41,7 @@ function extractApplyCandidates(params) {
3841
}
3942

4043
function expandApplyAtRules(context) {
41-
return (root) => {
44+
return function processApply(root) {
4245
let applyCandidates = new Set()
4346

4447
// Collect all @apply rules and candidates
@@ -79,12 +82,16 @@ function expandApplyAtRules(context) {
7982
*/
8083
// TODO: Should we use postcss-selector-parser for this instead?
8184
function replaceSelector(selector, utilitySelectors, candidate) {
85+
let needle = `.${escapeClassName(candidate)}`
86+
let utilitySelectorsList = utilitySelectors.split(/\s*,\s*/g)
87+
8288
return selector
8389
.split(/\s*,\s*/g)
8490
.map((s) => {
8591
let replaced = []
86-
for (let utilitySelector of utilitySelectors.split(/\s*,\s*/g)) {
87-
let replacedSelector = utilitySelector.replace(`.${escapeClassName(candidate)}`, s)
92+
93+
for (let utilitySelector of utilitySelectorsList) {
94+
let replacedSelector = utilitySelector.replace(needle, s)
8895
if (replacedSelector === utilitySelector) {
8996
continue
9097
}
@@ -136,6 +143,9 @@ function expandApplyAtRules(context) {
136143
apply.parent.remove()
137144
}
138145
}
146+
147+
// Do it again, in case we have other `@apply` rules
148+
processApply(root)
139149
}
140150
}
141151
}

0 commit comments

Comments
 (0)