-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.mjs
More file actions
113 lines (111 loc) · 3.38 KB
/
plugin.mjs
File metadata and controls
113 lines (111 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/** @type {import('lightningcss').Visitor<any>} */
export default {
Rule: {
style(rule) {
const flatSelectors = rule.value.selectors.flat();
if (
flatSelectors.every(
(selector) =>
selector.type === "universal" || selector.type === "pseudo-element"
)
) {
return rule;
}
if (
flatSelectors.some(
(selector) =>
selector.type === "pseudo-element" ||
(selector.type === "pseudo-class" &&
(selector.kind === "where" || selector.kind === "is"))
)
) {
/** @type {import('lightningcss').SelectorList} */
let selectors = [];
for (let selectorGroup of rule.value.selectors) {
if (
selectorGroup.every(
(selector) =>
selector.type === "pseudo-class" && selector.kind === "where"
)
) {
selectors.push(selectorGroup);
} else if (
selectorGroup.every(
(selector) =>
selector.type === "pseudo-class" && selector.kind === "is"
)
) {
selectors.push(
selectorGroup.map(
(selector) =>
/** @type {import('lightningcss').SelectorComponent} */
({
...selector,
kind: "where",
})
)
);
} else if (
selectorGroup.some((selector) => selector.type === "pseudo-element")
) {
// Pseudo elements are not supported inside :where so we need to move them outside
// @ts-expect-error waiting for TS 5.0
let outsideWhereIndex = selectorGroup.findLastIndex(
(
/** @type {import('lightningcss').SelectorComponent} */
part
) => part.type === "pseudo-element"
);
while (
selectorGroup[outsideWhereIndex - 1]?.type === "combinator"
) {
outsideWhereIndex--;
}
const insideWhere = selectorGroup.slice(0, outsideWhereIndex);
const outsideWhere = selectorGroup.slice(outsideWhereIndex);
// * selectors already have 0 specificity
if (
insideWhere.every(
(selector) =>
selector.type === "universal" ||
(selector.type === "pseudo-class" &&
selector.kind === "where")
)
) {
selectors.push([...insideWhere, ...outsideWhere]);
} else {
selectors.push([
{
type: "pseudo-class",
kind: "where",
selectors: [insideWhere],
},
...outsideWhere,
]);
}
} else {
selectors.push([
{
type: "pseudo-class",
kind: "where",
selectors: [selectorGroup],
},
]);
}
}
rule.value.selectors = selectors;
} else {
rule.value.selectors = [
[
{
type: "pseudo-class",
kind: "where",
selectors: rule.value.selectors,
},
],
];
}
return rule;
},
},
};