rollup-plugin-react-scoped-css 1.0.0-alpha.0 → 1.0.0-alpha.10
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/README.md +55 -52
- package/dist/css/scope-css.js +66 -39
- package/dist/index.js +8 -3
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -4,57 +4,8 @@
|
|
|
4
4
|
While using react in a professional context, I realized that it was lacking the scopped css feature that I learned to love from Vue and Angular. After some reasearch I came across good plugins, but sadly were not compatible with vite and/or rollup. Thus, I decided to create this plugin which was greatly inspired by the amazing work of [gaoxiaoliangz](https://github.com/gaoxiaoliangz) with his [react-scoped-css plugin](https://github.com/gaoxiaoliangz/react-scoped-css).
|
|
5
5
|
|
|
6
6
|
## Requirements
|
|
7
|
-
+ **node** >=
|
|
8
|
-
+ **vite** >= 4
|
|
9
|
-
|
|
10
|
-
## Usage
|
|
11
|
-
|
|
12
|
-
```jsx
|
|
13
|
-
// Component.jsx
|
|
14
|
-
import './Component.scoped.scss'
|
|
15
|
-
|
|
16
|
-
export default function Sub() {
|
|
17
|
-
return (
|
|
18
|
-
<div className="wrap">
|
|
19
|
-
<h1>My Component</h1>
|
|
20
|
-
</div>
|
|
21
|
-
)
|
|
22
|
-
}
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
```scss
|
|
26
|
-
// Component.scoped.scss
|
|
27
|
-
.wrap {
|
|
28
|
-
width: 500px;
|
|
29
|
-
h1 { color: red; }
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
And just like that the styles will be scoped to the component.
|
|
33
|
-
|
|
34
|
-
## Limitations
|
|
35
|
-
Due to the way this plugin is working, it will apply the scope to the file and not the component individually... This may differ from other frameworks since they don't really let you define multiple components in the same file. This then means that if you have 2 components in the same file, the styles might conflict.
|
|
36
|
-
|
|
37
|
-
### Deep selector
|
|
38
|
-
If you want a selector in scoped styles to be "deep", i.e. affecting child components, you can use the ::deep combinator:
|
|
39
|
-
```css
|
|
40
|
-
.a:deep .b { /* ... */ }
|
|
41
|
-
```
|
|
42
|
-
The above will be compiled into:
|
|
43
|
-
```css
|
|
44
|
-
.a[data-v-f3f3eg9] .b { /* ... */ }
|
|
45
|
-
```
|
|
46
|
-
Another exepted format, which will generate the same resutl, is:
|
|
47
|
-
```scss
|
|
48
|
-
.a::v-deep .b { /* ... */ }
|
|
49
|
-
```
|
|
50
|
-
This is primarly for backwards compatibility, we recommend the `:deep` selector.
|
|
51
|
-
### Dynamically Generated Content
|
|
52
|
-
DOM content created with dangerouslySetInnerHTML are not affected by scoped styles, but you can still style them using deep selectors.
|
|
53
|
-
|
|
54
|
-
### Also Keep in Mind
|
|
55
|
-
Scoped styles do not eliminate the need for classes. Due to the way browsers render various CSS selectors, p { color: red } will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in .example { color: red }, then you virtually eliminate that performance hit.
|
|
56
|
-
|
|
57
|
-
Be careful with descendant selectors in recursive components! For a CSS rule with the selector .a .b, if the element that matches .a contains a recursive child component, then all .b in that child component will be matched by the rule.
|
|
7
|
+
+ **node** >= 16
|
|
8
|
+
+ **vite** >= 4
|
|
58
9
|
|
|
59
10
|
## How to install
|
|
60
11
|
|
|
@@ -89,6 +40,8 @@ export default {
|
|
|
89
40
|
};
|
|
90
41
|
```
|
|
91
42
|
|
|
43
|
+
***Notes:** Because this plugin has been built with a majority of it's usecase with vite, this plugin doesn't handle transpiling LESS, SCSS or other preprocessors. For that reason, you will likely need to add your transpilation plugins before reactScopedCssPlugin.*
|
|
44
|
+
|
|
92
45
|
### Customizing the plugin
|
|
93
46
|
There are a few options available to customize how the plugin works
|
|
94
47
|
```ts
|
|
@@ -132,7 +85,7 @@ There are a few options available to customize how the plugin works
|
|
|
132
85
|
}
|
|
133
86
|
```
|
|
134
87
|
|
|
135
|
-
###
|
|
88
|
+
### Advanced rollup usecases
|
|
136
89
|
Since this plugin works in two parts, you might need to expose the first part, then add any other plugin, and then expose the second part of the plugin. This part is automatically handled with vite thanks to the enforce attribute.
|
|
137
90
|
|
|
138
91
|
```js
|
|
@@ -143,5 +96,55 @@ export default {
|
|
|
143
96
|
};
|
|
144
97
|
```
|
|
145
98
|
|
|
99
|
+
## Usage
|
|
100
|
+
|
|
101
|
+
```jsx
|
|
102
|
+
// Component.jsx
|
|
103
|
+
import './Component.scoped.scss'
|
|
104
|
+
|
|
105
|
+
export default function Sub() {
|
|
106
|
+
return (
|
|
107
|
+
<div className="wrap">
|
|
108
|
+
<h1>My Component</h1>
|
|
109
|
+
</div>
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```scss
|
|
115
|
+
// Component.scoped.scss
|
|
116
|
+
.wrap {
|
|
117
|
+
width: 500px;
|
|
118
|
+
h1 { color: red; }
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
And just like that the styles will be scoped to the component.
|
|
122
|
+
|
|
123
|
+
## Limitations
|
|
124
|
+
Due to the way this plugin is working, it will apply the scope to the file and not the component individually... This may differ from other frameworks since they don't really let you define multiple components in the same file. This then means that if you have 2 components in the same file, the styles might conflict.
|
|
125
|
+
|
|
126
|
+
### Deep selector
|
|
127
|
+
If you want a selector in scoped styles to be "deep", i.e. affecting child components, you can use the ::deep combinator:
|
|
128
|
+
```css
|
|
129
|
+
.a::deep .b { /* ... */ }
|
|
130
|
+
```
|
|
131
|
+
The above will be compiled into:
|
|
132
|
+
```css
|
|
133
|
+
.a[data-v-f3f3eg9] .b { /* ... */ }
|
|
134
|
+
```
|
|
135
|
+
Another exepted format, which will generate the same resutl, is:
|
|
136
|
+
```scss
|
|
137
|
+
.a::v-deep .b { /* ... */ }
|
|
138
|
+
```
|
|
139
|
+
This is primarly for backwards compatibility, we recommend the `::deep` selector.
|
|
140
|
+
### Dynamically Generated Content
|
|
141
|
+
DOM content created with dangerouslySetInnerHTML are not affected by scoped styles, but you can still style them using deep selectors.
|
|
142
|
+
|
|
143
|
+
### Also Keep in Mind
|
|
144
|
+
Scoped styles do not eliminate the need for classes. Due to the way browsers render various CSS selectors, p { color: red } will be many times slower when scoped (i.e. when combined with an attribute selector). If you use classes or ids instead, such as in .example { color: red }, then you virtually eliminate that performance hit.
|
|
145
|
+
|
|
146
|
+
Be careful with descendant selectors in recursive components! For a CSS rule with the selector .a .b, if the element that matches .a contains a recursive child component, then all .b in that child component will be matched by the rule.
|
|
147
|
+
|
|
146
148
|
## Contributing
|
|
147
149
|
Anyone is free to open a PR and contribute to this project... just be civilized!
|
|
150
|
+
|
package/dist/css/scope-css.js
CHANGED
|
@@ -2,9 +2,51 @@
|
|
|
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?.data?.type === "PseudoClassSelector") &&
|
|
38
|
+
(item?.next?.data?.type === "ClassSelector" ||
|
|
39
|
+
item?.next?.data?.type === "AttributeSelector" ||
|
|
40
|
+
item?.next?.data?.type === "IdSelector" ||
|
|
41
|
+
item?.next?.data?.type === "PseudoClassSelector"));
|
|
42
|
+
};
|
|
43
|
+
const isCombinator = (item) => {
|
|
44
|
+
return item?.data?.type === "Combinator";
|
|
45
|
+
};
|
|
5
46
|
function scopeCss(css, filename, hash) {
|
|
6
47
|
try {
|
|
7
48
|
const ast = (0, css_tree_1.parse)(css);
|
|
49
|
+
const selectorLists = getSelectors(ast.children);
|
|
8
50
|
const attributeSelector = {
|
|
9
51
|
type: "AttributeSelector",
|
|
10
52
|
name: { type: "Identifier", name: hash },
|
|
@@ -12,49 +54,34 @@ function scopeCss(css, filename, hash) {
|
|
|
12
54
|
value: null,
|
|
13
55
|
flags: null,
|
|
14
56
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
|
|
57
|
+
selectorLists.forEach((selectorList) => {
|
|
58
|
+
selectorList.forEach((selector) => {
|
|
59
|
+
if (selector.type !== "Selector") {
|
|
60
|
+
return;
|
|
19
61
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
62
|
+
let item = selector.children.head;
|
|
63
|
+
while (item !== null) {
|
|
64
|
+
if (isScopedAttributeSelector(item, hash)) {
|
|
65
|
+
item = item?.next ?? null;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (item.next && isScopePiercingPseudoSelector(item.next)) {
|
|
69
|
+
Object.assign(item.next.data, attributeSelector);
|
|
70
|
+
break;
|
|
25
71
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
results.map((node) => {
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
36
|
-
//@ts-ignore: optimization hack, we need to override the object without changing the reference.
|
|
37
|
-
node.children = undefined;
|
|
38
|
-
node.loc = undefined;
|
|
39
|
-
Object.assign(node, attributeSelector);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
selectorList.children.forEach((selector) => {
|
|
44
|
-
if (selector.type === "Selector") {
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
46
|
-
// @ts-ignore: Tail is not exposed via @types/css-tree but does exist.
|
|
47
|
-
let item = selector.children.tail;
|
|
48
|
-
while (item !== null &&
|
|
49
|
-
(item?.data?.type === "PseudoClassSelector" ||
|
|
50
|
-
item?.data?.type === "PseudoElementSelector" ||
|
|
51
|
-
item?.data?.type === "Combinator")) {
|
|
52
|
-
item = item.prev;
|
|
53
|
-
}
|
|
72
|
+
if (isChainedSelector(item) || isCombinator(item)) {
|
|
73
|
+
item = item?.next ?? null;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (item?.next === null) {
|
|
77
|
+
selector.children.appendData(attributeSelector);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
54
80
|
selector.children.insertData(attributeSelector, item.next);
|
|
55
81
|
}
|
|
56
|
-
|
|
57
|
-
|
|
82
|
+
item = item?.next ?? null;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
58
85
|
});
|
|
59
86
|
return (0, css_tree_1.generate)(ast);
|
|
60
87
|
}
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,6 @@ const generateHash = (input, seed = 0) => {
|
|
|
23
23
|
};
|
|
24
24
|
function reactScopedCssPlugin(optionsIn = {}) {
|
|
25
25
|
const options = {
|
|
26
|
-
hashPrefix: "v",
|
|
27
26
|
styleFileSuffix: "scoped",
|
|
28
27
|
styleFileExtensions: ["scss", "css", "sass", "less"],
|
|
29
28
|
jsxFileExtensions: ["jsx", "tsx"],
|
|
@@ -67,12 +66,18 @@ function reactScopedCssPlugin(optionsIn = {}) {
|
|
|
67
66
|
if (scopedCssInFileRegex.test(code)) {
|
|
68
67
|
const importerHash = generateHash(id);
|
|
69
68
|
const program = this.parse(code);
|
|
70
|
-
const
|
|
69
|
+
const scopedAttr = options.hashPrefix
|
|
70
|
+
? `data-${options.hashPrefix}-${importerHash}`
|
|
71
|
+
: `data-${importerHash}`;
|
|
72
|
+
const newAst = (0, ast_program_1.addHashAttributesToJsxTagsAst)(program, scopedAttr);
|
|
71
73
|
return (0, escodegen_1.generate)(newAst);
|
|
72
74
|
}
|
|
73
75
|
if (scopedCssRegex.test(getFilenameFromPath(id))) {
|
|
74
76
|
const importerHash = getHashFromPath(id);
|
|
75
|
-
|
|
77
|
+
const scopedAttr = options.hashPrefix
|
|
78
|
+
? `data-${options.hashPrefix}-${importerHash}`
|
|
79
|
+
: `data-${importerHash}`;
|
|
80
|
+
return (0, scope_css_1.scopeCss)(code, getFilenameFromPath(id), scopedAttr);
|
|
76
81
|
}
|
|
77
82
|
},
|
|
78
83
|
},
|
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.10",
|
|
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",
|
|
@@ -49,7 +50,7 @@
|
|
|
49
50
|
"@semantic-release/github": "8.0.7",
|
|
50
51
|
"@semantic-release/npm": "11.0.1",
|
|
51
52
|
"@semantic-release/release-notes-generator": "10.0.3",
|
|
52
|
-
"@types/css-tree": "
|
|
53
|
+
"@types/css-tree": "2.3.3",
|
|
53
54
|
"@types/escodegen": "0.0.7",
|
|
54
55
|
"@types/estree": "1.0.0",
|
|
55
56
|
"@types/jest": "29.2.5",
|