rollup-plugin-react-scoped-css 0.4.0 → 0.4.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.
package/dist/ast.utils.js CHANGED
@@ -58,7 +58,7 @@ function isElementWithOldJsxTransform(node) {
58
58
  function isElementWithNewJsxTransform(node) {
59
59
  var _a;
60
60
  return (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
61
- ['_jsxDEV', '_jsx'].includes((_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.name);
61
+ ['_jsxDEV', '_jsx', '_jsxs'].includes((_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.name);
62
62
  }
63
63
  function isFragmentWithOldJsxTransform(node) {
64
64
  var _a, _b, _c, _d, _e, _f;
@@ -0,0 +1,15 @@
1
+ import { JsxParser, ParserImplementations } from "./jsx-parser.model";
2
+ export declare class BaseParser implements Pick<JsxParser, 'isNode' & 'traverse'> {
3
+ isNode(obj: any): boolean;
4
+ /**
5
+ * Traverses the AST tree and calls callback for each node.
6
+ * -
7
+ * @param ast The ast tree
8
+ * @param callback Callback function to be called for each node
9
+ * @returns
10
+ */
11
+ traverse(ast: any, callback: Function): any;
12
+ findImplementation(ast: any): ParserImplementations.classic;
13
+ extendNodeWithAttributes(node: any, attr: string): any;
14
+ private createAttributeNode;
15
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
14
+ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
15
+ to[j] = from[i];
16
+ return to;
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.BaseParser = void 0;
20
+ var jsx_parser_model_1 = require("./jsx-parser.model");
21
+ var BaseParser = /** @class */ (function () {
22
+ function BaseParser() {
23
+ }
24
+ BaseParser.prototype.isNode = function (obj) {
25
+ var _a, _b, _c;
26
+ return typeof obj === 'object' &&
27
+ typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.end) === 'number' &&
28
+ typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.start) === 'number' &&
29
+ typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.type) === 'string';
30
+ };
31
+ /**
32
+ * Traverses the AST tree and calls callback for each node.
33
+ * -
34
+ * @param ast The ast tree
35
+ * @param callback Callback function to be called for each node
36
+ * @returns
37
+ */
38
+ BaseParser.prototype.traverse = function (ast, callback) {
39
+ for (var _i = 0, _a = Object.keys(ast); _i < _a.length; _i++) {
40
+ var key = _a[_i];
41
+ if (['end', 'start', 'type'].includes(key)) {
42
+ continue;
43
+ }
44
+ if (Array.isArray(ast[key])) {
45
+ for (var nodeIndex in ast[key]) {
46
+ ast[key][nodeIndex] = this.traverse(ast[key][nodeIndex], callback);
47
+ }
48
+ }
49
+ else if (this.isNode(ast[key])) {
50
+ ast[key] = this.traverse(ast[key], callback);
51
+ }
52
+ }
53
+ var result = callback(ast);
54
+ return typeof result !== 'undefined' ? result : ast;
55
+ };
56
+ BaseParser.prototype.findImplementation = function (ast) {
57
+ var implementation = jsx_parser_model_1.ParserImplementations.classic;
58
+ this.traverse(ast, function (node) {
59
+ if (node.type !== 'ImportDeclaration') {
60
+ return;
61
+ }
62
+ if (node.source.value === 'react/jsx-runtime') {
63
+ implementation = jsx_parser_model_1.ParserImplementations.new;
64
+ }
65
+ });
66
+ return implementation;
67
+ };
68
+ BaseParser.prototype.extendNodeWithAttributes = function (node, attr) {
69
+ var _this = this;
70
+ var _a;
71
+ var arg = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[1];
72
+ return __assign(__assign({}, node), { arguments: node === null || node === void 0 ? void 0 : node.arguments.map(function (v, i) {
73
+ if (i === 1) {
74
+ if (v.type === 'ObjectExpression') {
75
+ return __assign(__assign({}, v), { properties: __spreadArray(__spreadArray([], arg.properties), [
76
+ _this.createAttributeNode(attr)
77
+ ]) });
78
+ }
79
+ else {
80
+ return {
81
+ type: 'ObjectExpression',
82
+ properties: [_this.createAttributeNode(attr)]
83
+ };
84
+ }
85
+ }
86
+ return v;
87
+ }) });
88
+ };
89
+ BaseParser.prototype.createAttributeNode = function (attr) {
90
+ return {
91
+ type: 'Property',
92
+ method: false,
93
+ shorthand: false,
94
+ computed: false,
95
+ key: {
96
+ type: 'Identifier',
97
+ name: "\"" + attr + "\""
98
+ },
99
+ value: {
100
+ type: 'Literal',
101
+ value: true,
102
+ raw: "true"
103
+ },
104
+ kind: 'init'
105
+ };
106
+ };
107
+ return BaseParser;
108
+ }());
109
+ exports.BaseParser = BaseParser;
@@ -0,0 +1,6 @@
1
+ import { BaseParser } from "./base-parser";
2
+ import { JsxParser } from "./jsx-parser.model";
3
+ export declare class ClassicJsxParser extends BaseParser implements JsxParser {
4
+ isNodeReactFragment(node: any): boolean;
5
+ isNodeReactElement(node: any): boolean;
6
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.ClassicJsxParser = void 0;
19
+ var base_parser_1 = require("./base-parser");
20
+ var ClassicJsxParser = /** @class */ (function (_super) {
21
+ __extends(ClassicJsxParser, _super);
22
+ function ClassicJsxParser() {
23
+ return _super !== null && _super.apply(this, arguments) || this;
24
+ }
25
+ ClassicJsxParser.prototype.isNodeReactFragment = function (node) {
26
+ var _a, _b, _c, _d, _e, _f;
27
+ return ((_c = (_b = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.object) === null || _c === void 0 ? void 0 : _c.name) === "React" &&
28
+ ((_f = (_e = (_d = node === null || node === void 0 ? void 0 : node.arguments) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.property) === null || _f === void 0 ? void 0 : _f.name) === "Fragment";
29
+ };
30
+ ClassicJsxParser.prototype.isNodeReactElement = function (node) {
31
+ var _a, _b, _c, _d;
32
+ return (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
33
+ ((_b = (_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.object) === null || _b === void 0 ? void 0 : _b.name) === 'React' &&
34
+ ((_d = (_c = node === null || node === void 0 ? void 0 : node.callee) === null || _c === void 0 ? void 0 : _c.property) === null || _d === void 0 ? void 0 : _d.name) === 'createElement';
35
+ };
36
+ return ClassicJsxParser;
37
+ }(base_parser_1.BaseParser));
38
+ exports.ClassicJsxParser = ClassicJsxParser;
@@ -0,0 +1,6 @@
1
+ export interface JsxParser {
2
+ isNode: (node: any) => boolean;
3
+ isNodeReactElement: (node: any) => boolean;
4
+ isNodeReactFragment: (node: any) => boolean;
5
+ traverse: (ast: any, callback: (ast: any) => void) => void;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ export interface JsxParser {
2
+ isNodeReactElement: (node: any) => boolean;
3
+ isNodeReactFragment: (node: any) => boolean;
4
+ isNode: (node: any) => boolean;
5
+ traverse: (ast: any, callback: (ast: any) => void) => void;
6
+ findImplementation: (ast: any) => ParserImplementations;
7
+ extendNodeWithAttributes: (node: any, attr: string) => any;
8
+ }
9
+ export declare enum ParserImplementations {
10
+ new = "new",
11
+ classic = "classic"
12
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParserImplementations = void 0;
4
+ var ParserImplementations;
5
+ (function (ParserImplementations) {
6
+ ParserImplementations["new"] = "new";
7
+ ParserImplementations["classic"] = "classic";
8
+ })(ParserImplementations = exports.ParserImplementations || (exports.ParserImplementations = {}));
@@ -0,0 +1,6 @@
1
+ import { BaseParser } from "./base-parser";
2
+ import { JsxParser } from "./jsx-parser.model";
3
+ export declare class NewJsxParser extends BaseParser implements JsxParser {
4
+ isNodeReactFragment(node: any): boolean;
5
+ isNodeReactElement(node: any): boolean;
6
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.NewJsxParser = void 0;
19
+ var base_parser_1 = require("./base-parser");
20
+ var NewJsxParser = /** @class */ (function (_super) {
21
+ __extends(NewJsxParser, _super);
22
+ function NewJsxParser() {
23
+ return _super !== null && _super.apply(this, arguments) || this;
24
+ }
25
+ NewJsxParser.prototype.isNodeReactFragment = function (node) {
26
+ var _a, _b, _c, _d;
27
+ return ((_b = (_a = node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.type) === "Identifier" &&
28
+ ((_d = (_c = node.arguments) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.name) === "_Fragment";
29
+ };
30
+ NewJsxParser.prototype.isNodeReactElement = function (node) {
31
+ var _a;
32
+ return (node === null || node === void 0 ? void 0 : node.type) === 'CallExpression' &&
33
+ ['_jsxDEV', '_jsx', '_jsxs'].includes((_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.name);
34
+ };
35
+ return NewJsxParser;
36
+ }(base_parser_1.BaseParser));
37
+ exports.NewJsxParser = NewJsxParser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-react-scoped-css",
3
- "version": "0.4.0",
3
+ "version": "0.4.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",
package/src/ast.utils.ts CHANGED
@@ -38,7 +38,7 @@ function isElementWithOldJsxTransform(node: any) {
38
38
 
39
39
  function isElementWithNewJsxTransform(node: any) {
40
40
  return node?.type === 'CallExpression' &&
41
- ['_jsxDEV', '_jsx'].includes(node?.callee?.name)
41
+ ['_jsxDEV', '_jsx', '_jsxs'].includes(node?.callee?.name)
42
42
  }
43
43
 
44
44
  function isFragmentWithOldJsxTransform(node: any) {