rollup-plugin-react-scoped-css 0.4.1 → 0.6.0

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.
Files changed (41) hide show
  1. package/.eslintrc.yml +29 -0
  2. package/.github/workflows/pull-request.yml +20 -0
  3. package/.github/workflows/release.yml +30 -0
  4. package/.releaserc +13 -0
  5. package/.vscode/settings.json +8 -0
  6. package/README.md +55 -7
  7. package/babel.config.js +6 -2
  8. package/dist/ast-iterator.d.ts +1 -0
  9. package/dist/ast-iterator.js +114 -0
  10. package/dist/ast-transformer.d.ts +1 -0
  11. package/dist/ast-transformer.js +50 -0
  12. package/dist/ast.utils.d.ts +4 -0
  13. package/dist/ast.utils.js +49 -108
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.js +24 -23
  16. package/dist/parsers/classic-parser.d.ts +3 -2
  17. package/dist/parsers/classic-parser.js +87 -24
  18. package/dist/parsers/jsx-parser.model.d.ts +0 -3
  19. package/dist/parsers/new-parser.d.ts +3 -2
  20. package/dist/parsers/new-parser.js +86 -23
  21. package/package.json +18 -3
  22. package/src/__mocks__/{compiled-jsx-with-attr.js → classic-compiled-jsx-with-attr.js} +0 -0
  23. package/src/__mocks__/{compiled-jsx.js → classic-compiled-jsx.js} +0 -0
  24. package/src/__mocks__/new-compiled-jsx-with-attr.js +101 -0
  25. package/src/__mocks__/new-compiled-jsx.js +64 -0
  26. package/src/__mocks__/sample-code-file.js +4 -0
  27. package/src/ast-iterator.spec.ts +22 -0
  28. package/src/ast-iterator.ts +32 -0
  29. package/src/ast-transformer.spec.ts +23 -0
  30. package/src/ast-transformer.ts +28 -0
  31. package/src/ast.utils.spec.ts +59 -20
  32. package/src/ast.utils.ts +41 -99
  33. package/src/index.ts +77 -57
  34. package/src/parsers/classic-parser.ts +72 -0
  35. package/src/parsers/jsx-parser.model.ts +10 -0
  36. package/src/parsers/new-parser.ts +74 -0
  37. package/tsconfig.json +2 -1
  38. package/dist/parsers/base-parser.d.ts +0 -15
  39. package/dist/parsers/base-parser.js +0 -109
  40. package/dist/parsers/jsx-parser.interface.d.ts +0 -6
  41. package/dist/parsers/jsx-parser.interface.js +0 -2
@@ -0,0 +1,74 @@
1
+ import { JsxParser } from "./jsx-parser.model";
2
+
3
+ export class NewJsxParser implements JsxParser {
4
+ isNodeReactFragment(node: any) {
5
+ return (
6
+ node.arguments?.[0]?.type === "Identifier" &&
7
+ ["_Fragment", "Fragment"].includes(node.arguments?.[0]?.name)
8
+ );
9
+ }
10
+
11
+ isNodeReactElement(node: any) {
12
+ return (
13
+ node?.type === "CallExpression" &&
14
+ ["_jsxDEV", "_jsx", "_jsxs", "jsxDev", "jsx", "jsxs"].includes(
15
+ node?.callee?.name
16
+ )
17
+ );
18
+ }
19
+
20
+ public extendNodeWithAttributes(node: any, attr: string) {
21
+ const arg = node?.arguments?.[1];
22
+ return {
23
+ ...node,
24
+ arguments: node?.arguments.map((v: any, i: number) => {
25
+ if (i === 1) {
26
+ if (v.type === "ObjectExpression") {
27
+ return {
28
+ ...v,
29
+ properties: [...arg.properties, this.createAttributeNode(attr)],
30
+ };
31
+ } else if (
32
+ v.type === "CallExpression" &&
33
+ v.callee.name === "_objectSpread"
34
+ ) {
35
+ const args = [...v.arguments];
36
+ args[0].properties = [
37
+ ...args[0].properties,
38
+ this.createAttributeNode(attr),
39
+ ];
40
+ return {
41
+ ...v,
42
+ arguments: args,
43
+ };
44
+ } else {
45
+ return {
46
+ type: "ObjectExpression",
47
+ properties: [this.createAttributeNode(attr)],
48
+ };
49
+ }
50
+ }
51
+ return v;
52
+ }),
53
+ };
54
+ }
55
+
56
+ private createAttributeNode(attr: string) {
57
+ return {
58
+ type: "Property",
59
+ method: false,
60
+ shorthand: false,
61
+ computed: false,
62
+ key: {
63
+ type: "Identifier",
64
+ name: `"${attr}"`,
65
+ },
66
+ value: {
67
+ type: "Literal",
68
+ value: true,
69
+ raw: "true",
70
+ },
71
+ kind: "init",
72
+ };
73
+ }
74
+ }
package/tsconfig.json CHANGED
@@ -6,7 +6,8 @@
6
6
  "declaration": true,
7
7
  "outDir": "dist",
8
8
  "strict": true,
9
- "esModuleInterop": true
9
+ "esModuleInterop": true,
10
+ "downlevelIteration": true
10
11
  },
11
12
  "exclude": [
12
13
  "node_modules",
@@ -1,15 +0,0 @@
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
- }
@@ -1,109 +0,0 @@
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;
@@ -1,6 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });