Skip to content

Commit 84fc496

Browse files
committed
Split :has from optimized default rules
1 parent 3bada2e commit 84fc496

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

has-fixup.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This is a very temporary fix to split :has() selectors
2+
// into their own rules for `optimizeUniversalDefaults`
3+
module.exports = function () {
4+
return {
5+
postcssPlugin: 'has-fixup',
6+
RuleExit(rule, { Rule }) {
7+
if (rule.selectors.length < 2) {
8+
return
9+
}
10+
11+
if (!rule.selectors.some(s => s.includes(':has'))) {
12+
return
13+
}
14+
15+
let withHas = []
16+
let withoutHas = []
17+
18+
// Find every selector with the :has() pseudo-class
19+
for (let sel of rule.selectors) {
20+
if (sel.includes(':has')) {
21+
withHas.push(sel)
22+
} else {
23+
withoutHas.push(sel)
24+
}
25+
}
26+
27+
// Remove the :has() selectors from the original rule
28+
rule.selectors = withoutHas
29+
30+
// Add the :has() selectors to a new rule
31+
const newRule = new Rule({
32+
selectors: withHas,
33+
nodes: rule.clone().nodes,
34+
})
35+
36+
rule.parent.insertAfter(rule, newRule)
37+
}
38+
}
39+
};
40+
41+
module.exports.postcss = true;

postcss.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
'tailwindcss',
66
'postcss-focus-visible',
77
'autoprefixer',
8+
require.resolve('./has-fixup.js'),
89
],
910
}

0 commit comments

Comments
 (0)