rollup-plugin-react-scoped-css 1.0.0-alpha.6 → 1.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/css/scope-css.js +68 -63
- package/package.json +3 -2
package/dist/css/scope-css.js
CHANGED
|
@@ -2,29 +2,49 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.scopeCss = void 0;
|
|
4
4
|
const css_tree_1 = require("css-tree");
|
|
5
|
+
const getSelectors = (items) => {
|
|
6
|
+
const selectorLists = items.reduce((acc, node) => {
|
|
7
|
+
if (node.type === "Rule" && node?.prelude?.type === "SelectorList") {
|
|
8
|
+
acc.push(node.prelude.children);
|
|
9
|
+
}
|
|
10
|
+
else if (node.type === "Atrule" && node.name === "media") {
|
|
11
|
+
node.block?.children.forEach((rule) => {
|
|
12
|
+
if (rule.type === "Atrule" && rule.name === "media") {
|
|
13
|
+
const selectors = getSelectors(rule.block?.children);
|
|
14
|
+
acc.push(...selectors);
|
|
15
|
+
}
|
|
16
|
+
if (rule.type === "Rule" && rule.prelude.type === "SelectorList") {
|
|
17
|
+
acc.push(rule.prelude.children);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return acc;
|
|
22
|
+
}, []);
|
|
23
|
+
return selectorLists;
|
|
24
|
+
};
|
|
25
|
+
const isScopePiercingPseudoSelector = (item) => {
|
|
26
|
+
return (item?.data?.type === "PseudoElementSelector" &&
|
|
27
|
+
(item?.data?.name === "v-deep" || item?.data?.name === "deep"));
|
|
28
|
+
};
|
|
29
|
+
const isScopedAttributeSelector = (item, hash) => {
|
|
30
|
+
return (item?.data?.type === "AttributeSelector" && item?.data?.name?.name === hash);
|
|
31
|
+
};
|
|
32
|
+
const isChainedSelector = (item) => {
|
|
33
|
+
return ((item?.data?.type === "TypeSelector" ||
|
|
34
|
+
item?.data?.type === "ClassSelector" ||
|
|
35
|
+
item?.data?.type === "AttributeSelector" ||
|
|
36
|
+
item?.data?.type === "IdSelector") &&
|
|
37
|
+
(item?.next?.data?.type === "ClassSelector" ||
|
|
38
|
+
item?.next?.data?.type === "AttributeSelector" ||
|
|
39
|
+
item?.next?.data?.type === "IdSelector"));
|
|
40
|
+
};
|
|
41
|
+
const isCombinator = (item) => {
|
|
42
|
+
return item?.data?.type === "Combinator";
|
|
43
|
+
};
|
|
5
44
|
function scopeCss(css, filename, hash) {
|
|
6
45
|
try {
|
|
7
46
|
const ast = (0, css_tree_1.parse)(css);
|
|
8
|
-
const
|
|
9
|
-
return (item?.data?.type === "PseudoElementSelector" &&
|
|
10
|
-
(item?.data?.name === "v-deep" || item?.data?.name === "deep"));
|
|
11
|
-
};
|
|
12
|
-
const isScopedAttributeSelector = (item) => {
|
|
13
|
-
return (item?.data?.type === "AttributeSelector" &&
|
|
14
|
-
item?.data?.name?.name === hash);
|
|
15
|
-
};
|
|
16
|
-
const isChainedSelector = (item) => {
|
|
17
|
-
return ((item?.data?.type === "TypeSelector" ||
|
|
18
|
-
item?.data?.type === "ClassSelector" ||
|
|
19
|
-
item?.data?.type === "AttributeSelector" ||
|
|
20
|
-
item?.data?.type === "IdSelector") &&
|
|
21
|
-
(item?.next?.data?.type === "ClassSelector" ||
|
|
22
|
-
item?.next?.data?.type === "AttributeSelector" ||
|
|
23
|
-
item?.next?.data?.type === "IdSelector"));
|
|
24
|
-
};
|
|
25
|
-
const isCombinator = (item) => {
|
|
26
|
-
return item?.data?.type === "Combinator";
|
|
27
|
-
};
|
|
47
|
+
const selectorLists = getSelectors(ast.children);
|
|
28
48
|
const attributeSelector = {
|
|
29
49
|
type: "AttributeSelector",
|
|
30
50
|
name: { type: "Identifier", name: hash },
|
|
@@ -32,53 +52,38 @@ function scopeCss(css, filename, hash) {
|
|
|
32
52
|
value: null,
|
|
33
53
|
flags: null,
|
|
34
54
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
55
|
+
selectorLists.forEach((selectorList) => {
|
|
56
|
+
selectorList.forEach((selector) => {
|
|
57
|
+
if (selector.type !== "Selector") {
|
|
58
|
+
return;
|
|
39
59
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
60
|
+
let hasHitDeep = false;
|
|
61
|
+
let item = selector.children.head;
|
|
62
|
+
while (item !== null || hasHitDeep) {
|
|
63
|
+
// Only to satisfy typescript
|
|
64
|
+
if (item === null) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
if (isScopedAttributeSelector(item, hash)) {
|
|
68
|
+
item = item?.next ?? null;
|
|
69
|
+
continue;
|
|
45
70
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (selector.type === "Selector") {
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
54
|
-
// @ts-ignore: Head is not exposed via @types/css-tree but does exist.
|
|
55
|
-
let item = selector.children.head;
|
|
56
|
-
let hasHitDeep = false;
|
|
57
|
-
while (item !== null) {
|
|
58
|
-
if (isScopedAttributeSelector(item)) {
|
|
59
|
-
item = item?.next ?? null;
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
if (hasHitDeep) {
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
if (item.next && isScopePiercingPseudoSelector(item.next)) {
|
|
66
|
-
hasHitDeep = true;
|
|
67
|
-
Object.assign(item.next.data, attributeSelector);
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
if (isChainedSelector(item) || isCombinator(item)) {
|
|
71
|
-
item = item?.next ?? null;
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
if (item?.next === null) {
|
|
75
|
-
selector.children.appendData(attributeSelector);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
selector.children.insertData(attributeSelector, item.next);
|
|
79
|
-
}
|
|
71
|
+
if (item.next && isScopePiercingPseudoSelector(item.next)) {
|
|
72
|
+
hasHitDeep = true;
|
|
73
|
+
Object.assign(item.next.data, attributeSelector);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
if (isChainedSelector(item) || isCombinator(item)) {
|
|
80
77
|
item = item?.next ?? null;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (item?.next === null) {
|
|
81
|
+
selector.children.appendData(attributeSelector);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
selector.children.insertData(attributeSelector, item.next);
|
|
81
85
|
}
|
|
86
|
+
item = item?.next ?? null;
|
|
82
87
|
}
|
|
83
88
|
});
|
|
84
89
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rollup-plugin-react-scoped-css",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.8",
|
|
4
4
|
"description": "A rollup plugin designed to allow scoped css to be run in react (Compatible with vite and rollup)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"author": "cmseguin",
|
|
18
18
|
"contributors": [
|
|
19
19
|
"cmseguin (https://github.com/cmseguin)",
|
|
20
|
-
"helianthuswhite (https://github.com/helianthuswhite)"
|
|
20
|
+
"helianthuswhite (https://github.com/helianthuswhite)",
|
|
21
|
+
"wirdehall (https://github.com/wirdehall)"
|
|
21
22
|
],
|
|
22
23
|
"keywords": [
|
|
23
24
|
"rollup",
|