rollup-plugin-react-scoped-css 1.0.0-alpha.14 → 1.0.0-alpha.16

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 CHANGED
@@ -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 customize the stylesheet file pattern
63
- * if undefined or '' is passed, all files will be evaluated
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
- styleFileSuffix?: string
72
+ scopedStyleSuffix?: string;
67
73
 
68
74
  /**
69
- * If you want to customize the attribute prefix that is added to the jsx elements
70
- * Default: 'v'
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
- hashPrefix?: string
79
+ globalStyleSuffix?: string;
73
80
 
74
81
  /**
75
- * If you want to customize the preprocessors
76
- * Default: ['scss', 'css', 'sass', 'less']
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 have jsx in other file extensions
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 customize the stylesheet file pattern
16
- * if undefined or '' is passed, all files will be evaluated
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
- styleFileSuffix?: string;
24
+ scopedStyleSuffix?: string;
20
25
  /**
21
- * If you want to customize the attribute prefix that is added to the jsx elements
22
- * Default: 'v'
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
- hashPrefix?: string;
30
+ globalStyleSuffix?: string;
25
31
  /**
26
- * If you want to customize the stylesheet extensions
27
- * Default: ['scss', 'css', 'sass', 'less']
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 have jsx in other file extensions
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
- styleFileSuffix: "scoped",
27
- styleFileExtensions: ["scss", "css", "sass", "less"],
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
- if (!options.styleFileExtensions || !options.styleFileExtensions.length) {
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 (!options.jsxFileExtensions || !options.jsxFileExtensions.length) {
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)(options.include, options.exclude);
38
- const scopedCssRegex = options.styleFileSuffix
39
- ? new RegExp(`\.${options.styleFileSuffix}\.(${options.styleFileExtensions.join("|")})$`)
40
- : new RegExp(`\.(${options.styleFileExtensions.join("|")})$`);
41
- const scopedCssInFileRegex = options.styleFileSuffix
42
- ? new RegExp(`\.${options.styleFileSuffix}\.(${options.styleFileExtensions.join("|")})(\"|\')`)
43
- : new RegExp(`\.(${options.styleFileExtensions.join("|")})(\"|\')`);
44
- const jsxRegex = new RegExp(`\.(${options.jsxFileExtensions.join("|")})$`);
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;
@@ -60,20 +67,27 @@ function reactScopedCssPlugin(optionsIn = {}) {
60
67
  {
61
68
  name: "rollup-plugin-react-scoped-css:post",
62
69
  transform(code, id) {
63
- if (!filter(id)) {
70
+ if (!filter(id) || id.includes("node_modules")) {
64
71
  return;
65
72
  }
66
- if (scopedCssInFileRegex.test(code)) {
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 = options.hashPrefix
70
- ? `data-${options.hashPrefix}-${importerHash}`
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);
74
85
  return newCode;
75
86
  }
76
87
  if (scopedCssRegex.test(getFilenameFromPath(id))) {
88
+ if (scopeStyleByDefault && globalCssRegex.test(id)) {
89
+ return;
90
+ }
77
91
  const importerHash = getHashFromPath(id);
78
92
  const scopedAttr = options.hashPrefix
79
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.14",
3
+ "version": "1.0.0-alpha.16",
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",