rollup-plugin-react-scoped-css 1.0.0-alpha.15 → 1.0.0-alpha.17

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/index.js CHANGED
@@ -8,6 +8,7 @@ const pluginutils_1 = require("@rollup/pluginutils");
8
8
  const escodegen_1 = require("escodegen");
9
9
  const ast_program_1 = require("./ast-program");
10
10
  const scope_css_1 = require("./css/scope-css");
11
+ const getImportDeclarationsValue_1 = require("./utils/getImportDeclarationsValue");
11
12
  const getFilenameFromPath = (filePath) => {
12
13
  const parts = filePath.split("/");
13
14
  return parts[parts.length - 1].split("?")[0];
@@ -41,9 +42,6 @@ function reactScopedCssPlugin(optionsIn = {}) {
41
42
  const scopedCssRegex = new RegExp(!scopeStyleByDefault
42
43
  ? `([^\.]+\.${scopedStyleSuffix}\.(${styleFileExtensions.join("|")}))$`
43
44
  : `([^\.]+\.(${styleFileExtensions.join("|")}))$`);
44
- const scopedCssInFileRegex = new RegExp(!scopeStyleByDefault
45
- ? `([^\.]+\.${scopedStyleSuffix}\.(${styleFileExtensions.join("|")}))(\"|\')`
46
- : `([^\.]+\.(${styleFileExtensions.join("|")}))(\"|\')`);
47
45
  const globalCssRegex = new RegExp(`\.${globalStyleSuffix}\.(${styleFileExtensions.join("|")})$`);
48
46
  const jsxRegex = new RegExp(`\.(${jsxFileExtensions.join("|")})$`);
49
47
  return [
@@ -67,32 +65,41 @@ function reactScopedCssPlugin(optionsIn = {}) {
67
65
  {
68
66
  name: "rollup-plugin-react-scoped-css:post",
69
67
  transform(code, id) {
70
- if (!filter(id)) {
68
+ if (!filter(id) || id.includes("node_modules")) {
71
69
  return;
72
70
  }
73
- const matches = code.match(scopedCssInFileRegex);
74
- const shouldScope = matches?.some((match) => {
75
- return !(scopeStyleByDefault && globalCssRegex.test(match));
76
- });
77
- if (shouldScope) {
78
- const importerHash = generateHash(id);
79
- const program = this.parse(code);
80
- const scopedAttr = hashPrefix
81
- ? `data-${hashPrefix}-${importerHash}`
82
- : `data-${importerHash}`;
83
- const newAst = (0, ast_program_1.addHashAttributesToJsxTagsAst)(program, scopedAttr);
84
- const newCode = (0, escodegen_1.generate)(newAst);
85
- return newCode;
71
+ if (jsxRegex.test(id)) {
72
+ const rootNode = this.parse(code);
73
+ const imports = (0, getImportDeclarationsValue_1.getImportDeclarationsValue)(rootNode);
74
+ const shouldScope = !!imports?.some((match) => {
75
+ if (scopeStyleByDefault) {
76
+ return scopedCssRegex.test(match) && !globalCssRegex.test(match);
77
+ }
78
+ else {
79
+ return scopedCssRegex.test(match);
80
+ }
81
+ });
82
+ if (shouldScope) {
83
+ const importerHash = generateHash(id);
84
+ const scopedAttr = hashPrefix
85
+ ? `data-${hashPrefix}-${importerHash}`
86
+ : `data-${importerHash}`;
87
+ const newAst = (0, ast_program_1.addHashAttributesToJsxTagsAst)(rootNode, scopedAttr);
88
+ const newCode = (0, escodegen_1.generate)(newAst);
89
+ return newCode;
90
+ }
86
91
  }
87
- if (scopedCssRegex.test(getFilenameFromPath(id))) {
88
- if (scopeStyleByDefault && globalCssRegex.test(id)) {
89
- return;
92
+ else {
93
+ if (scopedCssRegex.test(getFilenameFromPath(id))) {
94
+ if (scopeStyleByDefault && globalCssRegex.test(id)) {
95
+ return;
96
+ }
97
+ const importerHash = getHashFromPath(id);
98
+ const scopedAttr = options.hashPrefix
99
+ ? `data-${options.hashPrefix}-${importerHash}`
100
+ : `data-${importerHash}`;
101
+ return (0, scope_css_1.scopeCss)(code, getFilenameFromPath(id), scopedAttr);
90
102
  }
91
- const importerHash = getHashFromPath(id);
92
- const scopedAttr = options.hashPrefix
93
- ? `data-${options.hashPrefix}-${importerHash}`
94
- : `data-${importerHash}`;
95
- return (0, scope_css_1.scopeCss)(code, getFilenameFromPath(id), scopedAttr);
96
103
  }
97
104
  },
98
105
  },
@@ -0,0 +1,2 @@
1
+ import { AstNode } from "rollup";
2
+ export declare const getImportDeclarationsValue: (root: AstNode) => any[];
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getImportDeclarationsValue = void 0;
4
+ const ast_iterator_1 = require("../ast-iterator");
5
+ const ast_utils_1 = require("../ast.utils");
6
+ const getImportDeclarationsValue = (root) => {
7
+ const urls = [];
8
+ for (const node of (0, ast_iterator_1.astIterator)(root)) {
9
+ if (!(0, ast_utils_1.isNodeImportOrRequire)(node)) {
10
+ continue;
11
+ }
12
+ const src = (0, ast_utils_1.getSrcFromImportOrRequire)(node);
13
+ urls.push(src);
14
+ }
15
+ return urls;
16
+ };
17
+ exports.getImportDeclarationsValue = getImportDeclarationsValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-react-scoped-css",
3
- "version": "1.0.0-alpha.15",
3
+ "version": "1.0.0-alpha.17",
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",
@@ -63,7 +63,7 @@
63
63
  "eslint-plugin-node": "11.1.0",
64
64
  "eslint-plugin-prettier": "4.2.1",
65
65
  "eslint-plugin-react": "7.32.0",
66
- "jest": "29.3.1",
66
+ "jest": "^29.7.0",
67
67
  "semantic-release": "22.0.7",
68
68
  "typescript": "4.9.4"
69
69
  }