rollup-plugin-react-scoped-css 0.8.0 → 0.8.1

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.
@@ -1,3 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.astIterator = void 0;
1
4
  const isNode = (key, value) => {
2
5
  if (key === "loc")
3
6
  return false;
@@ -6,7 +9,7 @@ const isNode = (key, value) => {
6
9
  typeof value?.start === "number" &&
7
10
  typeof value?.type === "string");
8
11
  };
9
- export const astIterator = function* (ast) {
12
+ const astIterator = function* (ast) {
10
13
  if (!ast) {
11
14
  return;
12
15
  }
@@ -18,12 +21,13 @@ export const astIterator = function* (ast) {
18
21
  // If key value is an array, iterate over it
19
22
  if (Array.isArray(ast[key])) {
20
23
  for (const nodeIndex in ast[key]) {
21
- yield* astIterator(ast[key][nodeIndex]);
24
+ yield* (0, exports.astIterator)(ast[key][nodeIndex]);
22
25
  }
23
26
  }
24
27
  else if (isNode(key, ast[key])) {
25
- yield* astIterator(ast[key]);
28
+ yield* (0, exports.astIterator)(ast[key]);
26
29
  }
27
30
  }
28
31
  yield ast;
29
32
  };
33
+ exports.astIterator = astIterator;
@@ -1,37 +1,41 @@
1
- import { astIterator } from "./ast-iterator";
2
- import { astTransformer } from "./ast-transformer";
3
- import { getSrcFromImportOrRequire, isNodeImportOrRequire } from "./ast.utils";
4
- import { LegacyJsxParser } from "./parsers/legacy-jsx-parser";
5
- import { ModernJsxParser } from "./parsers/modern-jsx-parser";
6
- import { ParserImplementations } from "./parsers/jsx-parser.model";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addHashAttributesToJsxTagsAst = void 0;
4
+ const ast_iterator_1 = require("./ast-iterator");
5
+ const ast_transformer_1 = require("./ast-transformer");
6
+ const ast_utils_1 = require("./ast.utils");
7
+ const legacy_jsx_parser_1 = require("./parsers/legacy-jsx-parser");
8
+ const modern_jsx_parser_1 = require("./parsers/modern-jsx-parser");
9
+ const jsx_parser_model_1 = require("./parsers/jsx-parser.model");
7
10
  global.implementation = null;
8
- const legacyJsxParser = new LegacyJsxParser();
9
- const modernJsxParser = new ModernJsxParser();
11
+ const legacyJsxParser = new legacy_jsx_parser_1.LegacyJsxParser();
12
+ const modernJsxParser = new modern_jsx_parser_1.ModernJsxParser();
10
13
  let parser = legacyJsxParser;
11
14
  const findImplementation = (program) => {
12
- const implementation = ParserImplementations.legacy;
13
- for (const node of astIterator(program)) {
14
- if (!isNodeImportOrRequire(node)) {
15
+ const implementation = jsx_parser_model_1.ParserImplementations.legacy;
16
+ for (const node of (0, ast_iterator_1.astIterator)(program)) {
17
+ if (!(0, ast_utils_1.isNodeImportOrRequire)(node)) {
15
18
  continue;
16
19
  }
17
- const src = getSrcFromImportOrRequire(node);
20
+ const src = (0, ast_utils_1.getSrcFromImportOrRequire)(node);
18
21
  if (["react/jsx-dev-runtime", "react/jsx-runtime"].includes(src)) {
19
- return ParserImplementations.modern;
22
+ return jsx_parser_model_1.ParserImplementations.modern;
20
23
  }
21
24
  }
22
25
  return implementation;
23
26
  };
24
- export function addHashAttributesToJsxTagsAst(program, attr) {
27
+ function addHashAttributesToJsxTagsAst(program, attr) {
25
28
  // Once in the program, we can determine which parser to use
26
29
  if (global.implementation === null) {
27
30
  global.implementation = findImplementation(program);
28
- if (global.implementation === ParserImplementations.modern) {
31
+ if (global.implementation === jsx_parser_model_1.ParserImplementations.modern) {
29
32
  parser = modernJsxParser;
30
33
  }
31
34
  }
32
- return astTransformer(program, (node) => {
35
+ return (0, ast_transformer_1.astTransformer)(program, (node) => {
33
36
  if (parser.isNodeReactElement(node) && !parser.isNodeReactFragment(node)) {
34
37
  return parser.extendNodeWithAttributes(node, attr);
35
38
  }
36
39
  });
37
40
  }
41
+ exports.addHashAttributesToJsxTagsAst = addHashAttributesToJsxTagsAst;
@@ -1,23 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.astTransformer = void 0;
1
4
  const isNode = (obj) => {
2
5
  return (typeof obj === "object" &&
3
6
  typeof obj?.end === "number" &&
4
7
  typeof obj?.start === "number" &&
5
8
  typeof obj?.type === "string");
6
9
  };
7
- export const astTransformer = (ast, callback) => {
10
+ const astTransformer = (ast, callback) => {
8
11
  for (const key of Object.keys(ast)) {
9
12
  if (["end", "start", "type"].includes(key)) {
10
13
  continue;
11
14
  }
12
15
  if (Array.isArray(ast[key])) {
13
16
  for (const nodeIndex in ast[key]) {
14
- ast[key][nodeIndex] = astTransformer(ast[key][nodeIndex], callback);
17
+ ast[key][nodeIndex] = (0, exports.astTransformer)(ast[key][nodeIndex], callback);
15
18
  }
16
19
  }
17
20
  else if (isNode(ast[key])) {
18
- ast[key] = astTransformer(ast[key], callback);
21
+ ast[key] = (0, exports.astTransformer)(ast[key], callback);
19
22
  }
20
23
  }
21
24
  const result = callback(ast);
22
25
  return typeof result !== "undefined" ? result : ast;
23
26
  };
27
+ exports.astTransformer = astTransformer;
package/dist/ast.utils.js CHANGED
@@ -1,4 +1,7 @@
1
- export const createObjectAssignCallExpressionWithIdentifier = (identifier, objectExpression) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSrcFromImportOrRequire = exports.isNodeImportOrRequire = exports.isNodeRequireCallExpression = exports.isNodeImportDeclaration = exports.isNodeIdentifier = exports.isNodeExtender = exports.isNodeObjectExpression = exports.isNodeCallExpression = exports.hackIntoExtenderCallExpression = exports.extendObjectExpression = exports.createObjectExpressionWithAttr = exports.createObjectAssignCallExpressionWithIdentifier = void 0;
4
+ const createObjectAssignCallExpressionWithIdentifier = (identifier, objectExpression) => {
2
5
  return {
3
6
  type: "CallExpression",
4
7
  callee: {
@@ -27,57 +30,69 @@ export const createObjectAssignCallExpressionWithIdentifier = (identifier, objec
27
30
  optional: false,
28
31
  };
29
32
  };
30
- export const createObjectExpressionWithAttr = (attrNode) => {
33
+ exports.createObjectAssignCallExpressionWithIdentifier = createObjectAssignCallExpressionWithIdentifier;
34
+ const createObjectExpressionWithAttr = (attrNode) => {
31
35
  return {
32
36
  type: "ObjectExpression",
33
37
  properties: [attrNode],
34
38
  };
35
39
  };
36
- export const extendObjectExpression = (objectExpression, prop) => {
40
+ exports.createObjectExpressionWithAttr = createObjectExpressionWithAttr;
41
+ const extendObjectExpression = (objectExpression, prop) => {
37
42
  return {
38
43
  ...objectExpression,
39
44
  properties: [...objectExpression.properties, prop],
40
45
  };
41
46
  };
42
- export const hackIntoExtenderCallExpression = (callExpression, prop) => {
47
+ exports.extendObjectExpression = extendObjectExpression;
48
+ const hackIntoExtenderCallExpression = (callExpression, prop) => {
43
49
  return {
44
50
  ...callExpression,
45
51
  arguments: [
46
52
  ...callExpression.arguments,
47
- createObjectExpressionWithAttr(prop),
53
+ (0, exports.createObjectExpressionWithAttr)(prop),
48
54
  ],
49
55
  };
50
56
  };
51
- export const isNodeCallExpression = (node) => {
57
+ exports.hackIntoExtenderCallExpression = hackIntoExtenderCallExpression;
58
+ const isNodeCallExpression = (node) => {
52
59
  return node.type === "CallExpression";
53
60
  };
54
- export const isNodeObjectExpression = (node) => {
61
+ exports.isNodeCallExpression = isNodeCallExpression;
62
+ const isNodeObjectExpression = (node) => {
55
63
  return node.type === "ObjectExpression";
56
64
  };
57
- export const isNodeExtender = (node) => {
58
- return (isNodeCallExpression(node) &&
65
+ exports.isNodeObjectExpression = isNodeObjectExpression;
66
+ const isNodeExtender = (node) => {
67
+ return ((0, exports.isNodeCallExpression)(node) &&
59
68
  (node.callee.name === "_objectSpread" || node.callee.name === "_extends"));
60
69
  };
61
- export const isNodeIdentifier = (node) => {
70
+ exports.isNodeExtender = isNodeExtender;
71
+ const isNodeIdentifier = (node) => {
62
72
  return node.type === "Identifier";
63
73
  };
64
- export const isNodeImportDeclaration = (node) => {
74
+ exports.isNodeIdentifier = isNodeIdentifier;
75
+ const isNodeImportDeclaration = (node) => {
65
76
  return node.type === "ImportDeclaration";
66
77
  };
67
- export const isNodeRequireCallExpression = (node) => {
68
- return (isNodeCallExpression(node) &&
78
+ exports.isNodeImportDeclaration = isNodeImportDeclaration;
79
+ const isNodeRequireCallExpression = (node) => {
80
+ return ((0, exports.isNodeCallExpression)(node) &&
69
81
  node.callee.name === "require" &&
70
82
  node.arguments.length === 1);
71
83
  };
72
- export const isNodeImportOrRequire = (node) => {
73
- return isNodeImportDeclaration(node) || isNodeRequireCallExpression(node);
84
+ exports.isNodeRequireCallExpression = isNodeRequireCallExpression;
85
+ const isNodeImportOrRequire = (node) => {
86
+ return (0, exports.isNodeImportDeclaration)(node) || (0, exports.isNodeRequireCallExpression)(node);
74
87
  };
75
- export const getSrcFromImportOrRequire = (node) => {
76
- if (isNodeImportDeclaration(node)) {
88
+ exports.isNodeImportOrRequire = isNodeImportOrRequire;
89
+ const getSrcFromImportOrRequire = (node) => {
90
+ if ((0, exports.isNodeImportDeclaration)(node)) {
77
91
  return node.source.value;
78
92
  }
79
- if (isNodeRequireCallExpression(node)) {
93
+ if ((0, exports.isNodeRequireCallExpression)(node)) {
80
94
  return node.arguments[0].value;
81
95
  }
82
96
  return null;
83
97
  };
98
+ exports.getSrcFromImportOrRequire = getSrcFromImportOrRequire;
package/dist/index.js CHANGED
@@ -1,10 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reactScopedCssPlugin = void 0;
1
4
  /* eslint-disable no-useless-escape */
2
- import { resolve, dirname } from "path";
3
- import { xxHash32 } from "js-xxhash";
4
- import { createFilter } from "@rollup/pluginutils";
5
- import { compileStyle } from "@vue/component-compiler-utils";
6
- import { generate } from "escodegen";
7
- import { addHashAttributesToJsxTagsAst } from "./ast-program";
5
+ const path_1 = require("path");
6
+ const js_xxhash_1 = require("js-xxhash");
7
+ const pluginutils_1 = require("@rollup/pluginutils");
8
+ const component_compiler_utils_1 = require("@vue/component-compiler-utils");
9
+ const escodegen_1 = require("escodegen");
10
+ const ast_program_1 = require("./ast-program");
8
11
  const getFilenameFromPath = (filePath) => {
9
12
  const parts = filePath.split("/");
10
13
  return parts[parts.length - 1].split("?")[0];
@@ -15,11 +18,11 @@ const getHashFromPath = (filePath) => {
15
18
  return hash;
16
19
  };
17
20
  const generateHash = (input, seed = 0) => {
18
- const hashNum = xxHash32(Buffer.from(input, "utf8"), seed);
21
+ const hashNum = (0, js_xxhash_1.xxHash32)(Buffer.from(input, "utf8"), seed);
19
22
  return hashNum.toString(16);
20
23
  };
21
24
  const addAttributesToCss = (src, fileName, hash) => {
22
- const { code } = compileStyle({
25
+ const { code } = (0, component_compiler_utils_1.compileStyle)({
23
26
  source: src,
24
27
  filename: fileName,
25
28
  id: hash,
@@ -28,7 +31,7 @@ const addAttributesToCss = (src, fileName, hash) => {
28
31
  });
29
32
  return code;
30
33
  };
31
- export function reactScopedCssPlugin(optionsIn = {}) {
34
+ function reactScopedCssPlugin(optionsIn = {}) {
32
35
  const options = {
33
36
  hashPrefix: "v",
34
37
  styleFileSuffix: "scoped",
@@ -42,7 +45,7 @@ export function reactScopedCssPlugin(optionsIn = {}) {
42
45
  if (!options.jsxFileExtensions || !options.jsxFileExtensions.length) {
43
46
  throw new Error("You need to provide at least one jsx file extension");
44
47
  }
45
- const filter = createFilter(options.include, options.exclude);
48
+ const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
46
49
  const scopedCssRegex = options.styleFileSuffix
47
50
  ? new RegExp(`\.${options.styleFileSuffix}\.(${options.styleFileExtensions.join("|")})$`)
48
51
  : new RegExp(`\.(${options.styleFileExtensions.join("|")})$`);
@@ -59,7 +62,7 @@ export function reactScopedCssPlugin(optionsIn = {}) {
59
62
  }
60
63
  if (scopedCssRegex.test(source) && jsxRegex.test(importer)) {
61
64
  const importerHash = generateHash(importer);
62
- const url = resolve(dirname(importer), `${source}?scope=${importerHash}`);
65
+ const url = (0, path_1.resolve)((0, path_1.dirname)(importer), `${source}?scope=${importerHash}`);
63
66
  return url;
64
67
  }
65
68
  },
@@ -74,8 +77,8 @@ export function reactScopedCssPlugin(optionsIn = {}) {
74
77
  if (scopedCssInFileRegex.test(code)) {
75
78
  const importerHash = generateHash(id);
76
79
  const program = this.parse(code);
77
- const newAst = addHashAttributesToJsxTagsAst(program, `data-${options.hashPrefix}-${importerHash}`);
78
- return generate(newAst);
80
+ const newAst = (0, ast_program_1.addHashAttributesToJsxTagsAst)(program, `data-${options.hashPrefix}-${importerHash}`);
81
+ return (0, escodegen_1.generate)(newAst);
79
82
  }
80
83
  if (scopedCssRegex.test(getFilenameFromPath(id))) {
81
84
  const importerHash = getHashFromPath(id);
@@ -85,3 +88,4 @@ export function reactScopedCssPlugin(optionsIn = {}) {
85
88
  },
86
89
  ];
87
90
  }
91
+ exports.reactScopedCssPlugin = reactScopedCssPlugin;
@@ -1,5 +1,8 @@
1
- export var ParserImplementations;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParserImplementations = void 0;
4
+ var ParserImplementations;
2
5
  (function (ParserImplementations) {
3
6
  ParserImplementations["modern"] = "modern";
4
7
  ParserImplementations["legacy"] = "legacy";
5
- })(ParserImplementations || (ParserImplementations = {}));
8
+ })(ParserImplementations = exports.ParserImplementations || (exports.ParserImplementations = {}));
@@ -1,5 +1,8 @@
1
- import { createObjectAssignCallExpressionWithIdentifier, createObjectExpressionWithAttr, extendObjectExpression, hackIntoExtenderCallExpression, isNodeExtender, isNodeIdentifier, isNodeObjectExpression, } from "../ast.utils";
2
- export class LegacyJsxParser {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LegacyJsxParser = void 0;
4
+ const ast_utils_1 = require("../ast.utils");
5
+ class LegacyJsxParser {
3
6
  isNodeReactFragment(node) {
4
7
  return ((node?.callee?.object?.name === "React" ||
5
8
  node?.callee?.object?.object?.name === "_react") &&
@@ -21,19 +24,19 @@ export class LegacyJsxParser {
21
24
  }
22
25
  const attrNode = this.createAttributeNode(attr);
23
26
  // If it's an object expression, extend it
24
- if (isNodeObjectExpression(v)) {
25
- return extendObjectExpression(v, attrNode);
27
+ if ((0, ast_utils_1.isNodeObjectExpression)(v)) {
28
+ return (0, ast_utils_1.extendObjectExpression)(v, attrNode);
26
29
  }
27
30
  // If it's an extender, hack into it
28
- if (isNodeExtender(v)) {
29
- return hackIntoExtenderCallExpression(v, attrNode);
31
+ if ((0, ast_utils_1.isNodeExtender)(v)) {
32
+ return (0, ast_utils_1.hackIntoExtenderCallExpression)(v, attrNode);
30
33
  }
31
34
  // If it's an identifier, do Object.assign to be safe
32
- if (isNodeIdentifier(v)) {
33
- return createObjectAssignCallExpressionWithIdentifier(v, createObjectExpressionWithAttr(attrNode));
35
+ if ((0, ast_utils_1.isNodeIdentifier)(v)) {
36
+ return (0, ast_utils_1.createObjectAssignCallExpressionWithIdentifier)(v, (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode));
34
37
  }
35
38
  // Worst case override with
36
- return createObjectExpressionWithAttr(attrNode);
39
+ return (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode);
37
40
  }),
38
41
  };
39
42
  }
@@ -56,3 +59,4 @@ export class LegacyJsxParser {
56
59
  };
57
60
  }
58
61
  }
62
+ exports.LegacyJsxParser = LegacyJsxParser;
@@ -1,5 +1,8 @@
1
- import { createObjectAssignCallExpressionWithIdentifier, createObjectExpressionWithAttr, extendObjectExpression, hackIntoExtenderCallExpression, isNodeExtender, isNodeIdentifier, isNodeObjectExpression, } from "../ast.utils";
2
- export class ModernJsxParser {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModernJsxParser = void 0;
4
+ const ast_utils_1 = require("../ast.utils");
5
+ class ModernJsxParser {
3
6
  constructor() {
4
7
  this.reactCalleeNames = [
5
8
  "_jsxDEV",
@@ -50,19 +53,19 @@ export class ModernJsxParser {
50
53
  }
51
54
  const attrNode = this.createAttributeNode(attr);
52
55
  // If it's an object expression, extend it
53
- if (isNodeObjectExpression(v)) {
54
- return extendObjectExpression(v, attrNode);
56
+ if ((0, ast_utils_1.isNodeObjectExpression)(v)) {
57
+ return (0, ast_utils_1.extendObjectExpression)(v, attrNode);
55
58
  }
56
59
  // If it's an extender, hack into it
57
- if (isNodeExtender(v)) {
58
- return hackIntoExtenderCallExpression(v, attrNode);
60
+ if ((0, ast_utils_1.isNodeExtender)(v)) {
61
+ return (0, ast_utils_1.hackIntoExtenderCallExpression)(v, attrNode);
59
62
  }
60
63
  // If it's an identifier, do Object.assign to be safe
61
- if (isNodeIdentifier(v)) {
62
- return createObjectAssignCallExpressionWithIdentifier(v, createObjectExpressionWithAttr(attrNode));
64
+ if ((0, ast_utils_1.isNodeIdentifier)(v)) {
65
+ return (0, ast_utils_1.createObjectAssignCallExpressionWithIdentifier)(v, (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode));
63
66
  }
64
67
  // Worst case override with
65
- return createObjectExpressionWithAttr(attrNode);
68
+ return (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode);
66
69
  }),
67
70
  };
68
71
  }
@@ -85,3 +88,4 @@ export class ModernJsxParser {
85
88
  };
86
89
  }
87
90
  }
91
+ exports.ModernJsxParser = ModernJsxParser;
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "rollup-plugin-react-scoped-css",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
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",
7
- "type": "module",
8
7
  "scripts": {
9
8
  "build": "tsc",
10
9
  "build:watch": "tsc --watch",
package/tsconfig.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "target": "ES2021",
4
- "module": "ESNext",
4
+ "module": "CommonJS",
5
5
  "moduleResolution": "node",
6
6
  "declaration": true,
7
7
  "outDir": "dist",