Skip to content

Commit 3b8ca9d

Browse files
Recursively collapse adjacent rules (#7565)
* Recursively collapse adjacent rules * Update changelog
1 parent af64d71 commit 3b8ca9d

File tree

7 files changed

+49
-7
lines changed

7 files changed

+49
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
- Prevent nesting plugin from breaking other plugins ([#7563](https://github.com/tailwindlabs/tailwindcss/pull/7563))
11+
- Recursively collapse adjacent rules ([#7565](https://github.com/tailwindlabs/tailwindcss/pull/7565))
1112

1213
## [3.0.23] - 2022-02-16
1314

src/lib/collapseAdjacentRules.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let comparisonMap = {
55
let types = new Set(Object.keys(comparisonMap))
66

77
export default function collapseAdjacentRules() {
8-
return (root) => {
8+
function collapseRulesIn(root) {
99
let currentRule = null
1010
root.each((node) => {
1111
if (!types.has(node.type)) {
@@ -35,5 +35,20 @@ export default function collapseAdjacentRules() {
3535
currentRule = node
3636
}
3737
})
38+
39+
// After we've collapsed adjacent rules & at-rules, we need to collapse
40+
// adjacent rules & at-rules that are children of at-rules.
41+
// We do not care about nesting rules because Tailwind CSS
42+
// explicitly does not handle rule nesting on its own as
43+
// the user is expected to use a nesting plugin
44+
root.each((node) => {
45+
if (node.type === 'atrule') {
46+
collapseRulesIn(node)
47+
}
48+
})
49+
}
50+
51+
return (root) => {
52+
collapseRulesIn(root)
3853
}
3954
}

tests/apply.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,9 +1289,6 @@ it('apply partitioning works with media queries', async () => {
12891289
body {
12901290
--tw-text-opacity: 1;
12911291
color: rgb(220 38 38 / var(--tw-text-opacity));
1292-
}
1293-
html,
1294-
body {
12951292
font-size: 2rem;
12961293
}
12971294
}

tests/collapse-adjacent-rules.test.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@
7272
color: black;
7373
font-weight: 700;
7474
}
75+
@supports (foo: bar) {
76+
.some-apply-thing {
77+
font-weight: 700;
78+
--tw-text-opacity: 1;
79+
color: rgb(0 0 0 / var(--tw-text-opacity));
80+
}
81+
}
82+
@media (min-width: 768px) {
83+
.some-apply-thing {
84+
font-weight: 700;
85+
--tw-text-opacity: 1;
86+
color: rgb(0 0 0 / var(--tw-text-opacity));
87+
}
88+
}
89+
@supports (foo: bar) {
90+
@media (min-width: 768px) {
91+
.some-apply-thing {
92+
font-weight: 700;
93+
--tw-text-opacity: 1;
94+
color: rgb(0 0 0 / var(--tw-text-opacity));
95+
}
96+
}
97+
}
7598
@media (min-width: 640px) {
7699
.sm\:text-center {
77100
text-align: center;

tests/collapse-adjacent-rules.test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<div class="md:text-center"></div>
2020
<div class="lg:font-bold"></div>
2121
<div class="lg:text-center"></div>
22+
<div class="some-apply-thing"></div>
2223
</body>
2324
</html>

tests/collapse-adjacent-rules.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ test('collapse adjacent rules', () => {
88
content: [path.resolve(__dirname, './collapse-adjacent-rules.test.html')],
99
corePlugins: { preflight: false },
1010
theme: {},
11-
plugins: [],
11+
plugins: [
12+
function ({ addVariant }) {
13+
addVariant('foo-bar', '@supports (foo: bar)')
14+
},
15+
],
1216
}
1317

1418
let input = css`
@@ -45,6 +49,9 @@ test('collapse adjacent rules', () => {
4549
.bar {
4650
font-weight: 700;
4751
}
52+
.some-apply-thing {
53+
@apply foo-bar:md:text-black foo-bar:md:font-bold foo-bar:text-black foo-bar:font-bold md:font-bold md:text-black;
54+
}
4855
`
4956

5057
return run(input, config).then((result) => {

tests/kitchen-sink.test.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,6 @@ div {
498498
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
499499
transition-duration: 150ms;
500500
}
501-
}
502-
@media (prefers-reduced-motion: no-preference) {
503501
.dark .md\:dark\:motion-safe\:foo\:active\:custom-util:active {
504502
background: #abcdef !important;
505503
}

0 commit comments

Comments
 (0)