rollup-plugin-react-scoped-css 1.0.0-alpha.13 → 1.0.0-alpha.15
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 +28 -15
- package/dist/index.d.ts +20 -9
- package/dist/index.js +31 -16
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -4,8 +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** >=
|
|
7
|
+
+ **node** >= 18
|
|
8
|
+
+ **vite** >= 5
|
|
9
9
|
|
|
10
10
|
## How to install
|
|
11
11
|
|
|
@@ -50,38 +50,51 @@ There are a few options available to customize how the plugin works
|
|
|
50
50
|
* Which files should be included and parsed by the plugin
|
|
51
51
|
* Default: undefined
|
|
52
52
|
*/
|
|
53
|
-
include?: FilterPattern
|
|
53
|
+
include?: FilterPattern;
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* Which files should be exluded and that should not be parsed by the plugin
|
|
57
57
|
* Default: undefined
|
|
58
58
|
*/
|
|
59
|
-
exclude?: FilterPattern
|
|
59
|
+
exclude?: FilterPattern;
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* If you want to
|
|
63
|
-
*
|
|
62
|
+
* If you want regular files to be scoped & global files to be .global.css
|
|
63
|
+
* Default: false
|
|
64
|
+
*/
|
|
65
|
+
scopeStyleByDefault?: boolean;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* If you want to customize the pattern for scoped styles.
|
|
69
|
+
* This will only work if scopeStyleByDefault is false
|
|
64
70
|
* Default: 'scoped'
|
|
65
71
|
*/
|
|
66
|
-
|
|
72
|
+
scopedStyleSuffix?: string;
|
|
67
73
|
|
|
68
74
|
/**
|
|
69
|
-
* If you want to customize the
|
|
70
|
-
*
|
|
75
|
+
* If you want to customize the pattern for global styles.
|
|
76
|
+
* This will only work if scopeStyleByDefault is true
|
|
77
|
+
* Default: 'global'
|
|
71
78
|
*/
|
|
72
|
-
|
|
79
|
+
globalStyleSuffix?: string;
|
|
73
80
|
|
|
74
81
|
/**
|
|
75
|
-
* If you want to customize the
|
|
76
|
-
* Default: ['
|
|
82
|
+
* If you want to customize the pattern for style files.
|
|
83
|
+
* Default: ['css', 'scss', 'sass', 'less']
|
|
77
84
|
*/
|
|
78
|
-
styleFileExtensions?: string[]
|
|
85
|
+
styleFileExtensions?: string[];
|
|
79
86
|
|
|
80
87
|
/**
|
|
81
|
-
* If you
|
|
88
|
+
* If you want to customize the pattern for jsx files.
|
|
82
89
|
* Default: ['jsx', 'tsx']
|
|
83
90
|
*/
|
|
84
|
-
jsxFileExtensions?: string[]
|
|
91
|
+
jsxFileExtensions?: string[];
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* If you want to customize the attribute prefix that is added to the jsx elements
|
|
95
|
+
* Default: 'v'
|
|
96
|
+
*/
|
|
97
|
+
hashPrefix?: string;
|
|
85
98
|
}
|
|
86
99
|
```
|
|
87
100
|
|
package/dist/index.d.ts
CHANGED
|
@@ -12,26 +12,37 @@ export interface ReactScopedCssPluginOptions {
|
|
|
12
12
|
*/
|
|
13
13
|
exclude?: FilterPattern;
|
|
14
14
|
/**
|
|
15
|
-
* If you want to
|
|
16
|
-
*
|
|
15
|
+
* If you want regular files to be scoped & global files to be .global.css
|
|
16
|
+
* Default: false
|
|
17
|
+
*/
|
|
18
|
+
scopeStyleByDefault?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* If you want to customize the pattern for scoped styles.
|
|
21
|
+
* This will only work if scopeStyleByDefault is false
|
|
17
22
|
* Default: 'scoped'
|
|
18
23
|
*/
|
|
19
|
-
|
|
24
|
+
scopedStyleSuffix?: string;
|
|
20
25
|
/**
|
|
21
|
-
* If you want to customize the
|
|
22
|
-
*
|
|
26
|
+
* If you want to customize the pattern for global styles.
|
|
27
|
+
* This will only work if scopeStyleByDefault is true
|
|
28
|
+
* Default: 'global'
|
|
23
29
|
*/
|
|
24
|
-
|
|
30
|
+
globalStyleSuffix?: string;
|
|
25
31
|
/**
|
|
26
|
-
* If you want to customize the
|
|
27
|
-
* Default: ['
|
|
32
|
+
* If you want to customize the pattern for style files.
|
|
33
|
+
* Default: ['css', 'scss', 'sass', 'less']
|
|
28
34
|
*/
|
|
29
35
|
styleFileExtensions?: string[];
|
|
30
36
|
/**
|
|
31
|
-
* If you
|
|
37
|
+
* If you want to customize the pattern for jsx files.
|
|
32
38
|
* Default: ['jsx', 'tsx']
|
|
33
39
|
*/
|
|
34
40
|
jsxFileExtensions?: string[];
|
|
41
|
+
/**
|
|
42
|
+
* If you want to customize the attribute prefix that is added to the jsx elements
|
|
43
|
+
* Default: undefined
|
|
44
|
+
*/
|
|
45
|
+
hashPrefix?: string;
|
|
35
46
|
}
|
|
36
47
|
export interface VitePartialPlugin extends Plugin {
|
|
37
48
|
enforce?: "pre" | "post";
|
package/dist/index.js
CHANGED
|
@@ -23,25 +23,29 @@ const generateHash = (input, seed = 0) => {
|
|
|
23
23
|
};
|
|
24
24
|
function reactScopedCssPlugin(optionsIn = {}) {
|
|
25
25
|
const options = {
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
scopeStyleByDefault: false,
|
|
27
|
+
scopedStyleSuffix: "scoped",
|
|
28
|
+
globalStyleSuffix: "global",
|
|
29
|
+
styleFileExtensions: ["css", "scss", "sass", "less"],
|
|
28
30
|
jsxFileExtensions: ["jsx", "tsx"],
|
|
29
31
|
...optionsIn,
|
|
30
32
|
};
|
|
31
|
-
|
|
33
|
+
const { exclude, globalStyleSuffix, hashPrefix, jsxFileExtensions, include, scopeStyleByDefault, scopedStyleSuffix, styleFileExtensions, } = options;
|
|
34
|
+
if (!styleFileExtensions || !styleFileExtensions.length) {
|
|
32
35
|
throw new Error("You need to provide at least one style file extension");
|
|
33
36
|
}
|
|
34
|
-
if (!
|
|
37
|
+
if (!jsxFileExtensions || !jsxFileExtensions.length) {
|
|
35
38
|
throw new Error("You need to provide at least one jsx file extension");
|
|
36
39
|
}
|
|
37
|
-
const filter = (0, pluginutils_1.createFilter)(
|
|
38
|
-
const scopedCssRegex =
|
|
39
|
-
?
|
|
40
|
-
:
|
|
41
|
-
const scopedCssInFileRegex =
|
|
42
|
-
?
|
|
43
|
-
:
|
|
44
|
-
const
|
|
40
|
+
const filter = (0, pluginutils_1.createFilter)(include, exclude);
|
|
41
|
+
const scopedCssRegex = new RegExp(!scopeStyleByDefault
|
|
42
|
+
? `([^\.]+\.${scopedStyleSuffix}\.(${styleFileExtensions.join("|")}))$`
|
|
43
|
+
: `([^\.]+\.(${styleFileExtensions.join("|")}))$`);
|
|
44
|
+
const scopedCssInFileRegex = new RegExp(!scopeStyleByDefault
|
|
45
|
+
? `([^\.]+\.${scopedStyleSuffix}\.(${styleFileExtensions.join("|")}))(\"|\')`
|
|
46
|
+
: `([^\.]+\.(${styleFileExtensions.join("|")}))(\"|\')`);
|
|
47
|
+
const globalCssRegex = new RegExp(`\.${globalStyleSuffix}\.(${styleFileExtensions.join("|")})$`);
|
|
48
|
+
const jsxRegex = new RegExp(`\.(${jsxFileExtensions.join("|")})$`);
|
|
45
49
|
return [
|
|
46
50
|
{
|
|
47
51
|
name: "rollup-plugin-react-scoped-css:pre",
|
|
@@ -50,6 +54,9 @@ function reactScopedCssPlugin(optionsIn = {}) {
|
|
|
50
54
|
return;
|
|
51
55
|
}
|
|
52
56
|
if (scopedCssRegex.test(source) && jsxRegex.test(importer)) {
|
|
57
|
+
if (scopeStyleByDefault && globalCssRegex.test(source)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
53
60
|
const importerHash = generateHash(importer);
|
|
54
61
|
const url = (0, path_1.resolve)((0, path_1.dirname)(importer), `${source}?scope=${importerHash}`);
|
|
55
62
|
return url;
|
|
@@ -63,16 +70,24 @@ function reactScopedCssPlugin(optionsIn = {}) {
|
|
|
63
70
|
if (!filter(id)) {
|
|
64
71
|
return;
|
|
65
72
|
}
|
|
66
|
-
|
|
73
|
+
const matches = code.match(scopedCssInFileRegex);
|
|
74
|
+
const shouldScope = matches?.some((match) => {
|
|
75
|
+
return !(scopeStyleByDefault && globalCssRegex.test(match));
|
|
76
|
+
});
|
|
77
|
+
if (shouldScope) {
|
|
67
78
|
const importerHash = generateHash(id);
|
|
68
79
|
const program = this.parse(code);
|
|
69
|
-
const scopedAttr =
|
|
70
|
-
? `data-${
|
|
80
|
+
const scopedAttr = hashPrefix
|
|
81
|
+
? `data-${hashPrefix}-${importerHash}`
|
|
71
82
|
: `data-${importerHash}`;
|
|
72
83
|
const newAst = (0, ast_program_1.addHashAttributesToJsxTagsAst)(program, scopedAttr);
|
|
73
|
-
|
|
84
|
+
const newCode = (0, escodegen_1.generate)(newAst);
|
|
85
|
+
return newCode;
|
|
74
86
|
}
|
|
75
87
|
if (scopedCssRegex.test(getFilenameFromPath(id))) {
|
|
88
|
+
if (scopeStyleByDefault && globalCssRegex.test(id)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
76
91
|
const importerHash = getHashFromPath(id);
|
|
77
92
|
const scopedAttr = options.hashPrefix
|
|
78
93
|
? `data-${options.hashPrefix}-${importerHash}`
|
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.15",
|
|
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",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://github.com/cmseguin/rollup-plugin-react-scoped-css#readme",
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@rollup/pluginutils": "5.0
|
|
37
|
+
"@rollup/pluginutils": "5.1.0",
|
|
38
38
|
"css-tree": "2.3.1",
|
|
39
39
|
"escodegen": "2.0.0",
|
|
40
40
|
"js-xxhash": "2.0.0",
|
|
41
|
-
"rollup": "
|
|
41
|
+
"rollup": "4.6.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@babel/core": "7.20.12",
|