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

Commit d4c1787

Browse files
committed
simplify the @apply implementation
Creating multiple siblings for each utility guarantees the order. We can optimize this by squashing adjacent rules with the same selector.
1 parent b9c3d45 commit d4c1787

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

src/index.test.css

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
}
1212
.apply-test {
1313
margin-top: 1.5rem;
14+
}
15+
.apply-test {
1416
--tw-bg-opacity: 1;
1517
background-color: rgba(236, 72, 153, var(--tw-bg-opacity));
1618
}
@@ -32,8 +34,6 @@
3234
}
3335
.apply-components {
3436
width: 100%;
35-
margin-left: auto;
36-
margin-right: auto;
3737
}
3838
@media (min-width: 640px) {
3939
.apply-components {
@@ -60,6 +60,10 @@
6060
max-width: 1536px;
6161
}
6262
}
63+
.apply-components {
64+
margin-left: auto;
65+
margin-right: auto;
66+
}
6367
.drop-empty-rules:hover {
6468
font-weight: 700;
6569
}
@@ -99,6 +103,26 @@
99103
font-weight: 400;
100104
}
101105
}
106+
.apply-order-a {
107+
margin: 1.25rem;
108+
}
109+
.apply-order-a {
110+
margin-top: 1.5rem;
111+
}
112+
.apply-order-b {
113+
margin: 1.25rem;
114+
}
115+
.apply-order-b {
116+
margin-top: 1.5rem;
117+
}
118+
.dark .dark\:group:hover .apply-dark-group-example-a {
119+
--tw-bg-opacity: 1;
120+
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
121+
}
122+
.group:hover .group-hover\:dark .apply-dark-group-example-b {
123+
--tw-bg-opacity: 1;
124+
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
125+
}
102126
@media (min-width: 640px) {
103127
@media (prefers-reduced-motion: no-preference) {
104128
.group:active .crazy-example:focus {

src/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ test('it works', () => {
104104
@apply font-bold hover:font-normal;
105105
}
106106
}
107+
.apply-order-a {
108+
@apply m-5 mt-6;
109+
}
110+
.apply-order-b {
111+
@apply mt-6 m-5;
112+
}
113+
.apply-dark-group-example-a {
114+
@apply dark:group-hover:bg-green-500;
115+
}
116+
.apply-dark-group-example-b {
117+
@apply group-hover:dark:bg-green-500;
118+
}
107119
.crazy-example {
108120
@apply sm:motion-safe:group-active:focus:opacity-10;
109121
}

src/lib/expandApplyAtRules.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,21 @@ function expandApplyAtRules(context) {
6969

7070
let [layerName, rules] = context.classCache.get(applyCandidate)
7171
for (let [sort, [selector, rule]] of rules) {
72-
// Nested rules...
73-
if (!isPlainObject(rule)) {
74-
siblings.push([
75-
sort,
76-
toPostCssNode(
77-
[selector, updateSelectors(rule, apply, applyCandidate)],
78-
context.postCssNodeCache
79-
),
80-
])
81-
} else {
82-
let appliedSelector = replaceSelector(apply.parent.selector, selector, applyCandidate)
83-
84-
if (appliedSelector !== apply.parent.selector) {
85-
siblings.push([
86-
sort,
87-
toPostCssNode([appliedSelector, rule], context.postCssNodeCache),
88-
])
89-
continue
90-
}
91-
92-
// Add declarations directly
93-
for (let property in rule) {
94-
apply.before(postcss.decl({ prop: property, value: rule[property] }))
95-
}
96-
}
72+
siblings.push([
73+
sort,
74+
toPostCssNode(
75+
!isPlainObject(rule)
76+
? [selector, updateSelectors(rule, apply, applyCandidate)]
77+
: [replaceSelector(apply.parent.selector, selector, applyCandidate), rule],
78+
context.postCssNodeCache
79+
),
80+
])
9781
}
9882
}
9983

10084
// Inject the rules, sorted, correctly
10185
for (let [sort, sibling] of siblings.sort(([a], [z]) => bigSign(z - a))) {
102-
// `apply.parent` is refering to the node at `.abc` in: .abc { @apply mt-2 }
86+
// `apply.parent` is referring to the node at `.abc` in: .abc { @apply mt-2 }
10387
apply.parent.after(sibling)
10488
}
10589

0 commit comments

Comments
 (0)