rollup-plugin-react-scoped-css 0.6.0 → 0.7.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.
package/README.md CHANGED
@@ -27,6 +27,9 @@ export default function Sub() {
27
27
  ```
28
28
  And just like that the styles will be scoped to the component.
29
29
 
30
+ ## Limitations
31
+ Due to the way this plugin is working, it will apply the scope to the file and not the component individually... This may differ from other frameworks since they don't really let you define multiple components in the same file. This then means that if you have 2 components in the same file, the styles might conflict.
32
+
30
33
  ## Technicality
31
34
  since this plugin uses the engine of vue to scope the css to the components, the following sections come straight from the [vue documentation](https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors).
32
35
 
@@ -65,12 +68,12 @@ in vite:
65
68
  ```js
66
69
  // vite.config.js
67
70
  import { defineConfig } from 'vite'
68
- import reactRefresh from '@vitejs/plugin-react-refresh'
69
- import reactScopedCssPlugin from 'rollup-plugin-react-scoped-css'
71
+ import react from '@vitejs/plugin-react'
72
+ import { reactScopedCssPlugin } from 'rollup-plugin-react-scoped-css'
70
73
 
71
74
  // https://vitejs.dev/config/
72
75
  export default defineConfig({
73
- plugins: [reactRefresh(), reactScopedCssPlugin()]
76
+ plugins: [react(), reactScopedCssPlugin()]
74
77
  })
75
78
  ```
76
79
 
@@ -0,0 +1,5 @@
1
+ import { ParserImplementations } from "./parsers/jsx-parser.model";
2
+ declare global {
3
+ var implementation: null | ParserImplementations;
4
+ }
5
+ export declare function addHashAttributesToJsxTagsAst(program: any, attr: string): any;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.addHashAttributesToJsxTagsAst = void 0;
15
+ var ast_iterator_1 = require("./ast-iterator");
16
+ var ast_transformer_1 = require("./ast-transformer");
17
+ var ast_utils_1 = require("./ast.utils");
18
+ var classic_parser_1 = require("./parsers/classic-parser");
19
+ var jsx_parser_model_1 = require("./parsers/jsx-parser.model");
20
+ var new_parser_1 = require("./parsers/new-parser");
21
+ global.implementation = null;
22
+ var classicParser = new classic_parser_1.ClassicJsxParser();
23
+ var newParser = new new_parser_1.NewJsxParser();
24
+ var parser = classicParser;
25
+ var findImplementation = function (program) {
26
+ var e_1, _a;
27
+ var implementation = jsx_parser_model_1.ParserImplementations.classic;
28
+ try {
29
+ for (var _b = __values((0, ast_iterator_1.astIterator)(program)), _c = _b.next(); !_c.done; _c = _b.next()) {
30
+ var node = _c.value;
31
+ if (!(0, ast_utils_1.isNodeImportOrRequire)(node)) {
32
+ continue;
33
+ }
34
+ var src = (0, ast_utils_1.getSrcFromImportOrRequire)(node);
35
+ if (["react/jsx-dev-runtime", "react/jsx-runtime"].includes(src)) {
36
+ return jsx_parser_model_1.ParserImplementations.new;
37
+ }
38
+ }
39
+ }
40
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
41
+ finally {
42
+ try {
43
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
44
+ }
45
+ finally { if (e_1) throw e_1.error; }
46
+ }
47
+ return implementation;
48
+ };
49
+ function addHashAttributesToJsxTagsAst(program, attr) {
50
+ // Once in the program, we can determine which parser to use
51
+ if (global.implementation === null) {
52
+ global.implementation = findImplementation(program);
53
+ if (global.implementation === jsx_parser_model_1.ParserImplementations.new) {
54
+ parser = newParser;
55
+ }
56
+ }
57
+ return (0, ast_transformer_1.astTransformer)(program, function (node) {
58
+ if (parser.isNodeReactElement(node) && !parser.isNodeReactFragment(node)) {
59
+ return parser.extendNodeWithAttributes(node, attr);
60
+ }
61
+ });
62
+ }
63
+ exports.addHashAttributesToJsxTagsAst = addHashAttributesToJsxTagsAst;
@@ -1,5 +1,32 @@
1
- import { ParserImplementations } from "./parsers/jsx-parser.model";
2
- declare global {
3
- var implementation: null | ParserImplementations;
4
- }
5
- export declare function addHashAttributesToJsxTagsAst(program: any, attr: string): any;
1
+ export declare const createObjectAssignCallExpressionWithIdentifier: (identifier: any, objectExpression: any) => {
2
+ type: string;
3
+ callee: {
4
+ type: string;
5
+ object: {
6
+ type: string;
7
+ name: string;
8
+ };
9
+ property: {
10
+ type: string;
11
+ name: string;
12
+ };
13
+ computed: boolean;
14
+ optional: boolean;
15
+ };
16
+ arguments: any[];
17
+ optional: boolean;
18
+ };
19
+ export declare const createObjectExpressionWithAttr: (attrNode: any) => {
20
+ type: string;
21
+ properties: any[];
22
+ };
23
+ export declare const extendObjectExpression: (objectExpression: any, prop: any) => any;
24
+ export declare const hackIntoExtenderCallExpression: (callExpression: any, prop: any) => any;
25
+ export declare const isNodeCallExpression: (node: any) => boolean;
26
+ export declare const isNodeObjectExpression: (node: any) => boolean;
27
+ export declare const isNodeExtender: (node: any) => boolean;
28
+ export declare const isNodeIdentifier: (node: any) => boolean;
29
+ export declare const isNodeImportDeclaration: (node: any) => boolean;
30
+ export declare const isNodeRequireCallExpression: (node: any) => boolean;
31
+ export declare const isNodeImportOrRequire: (node: any) => boolean;
32
+ export declare const getSrcFromImportOrRequire: (node: any) => any;
package/dist/ast.utils.js CHANGED
@@ -1,64 +1,127 @@
1
1
  "use strict";
2
- var __values = (this && this.__values) || function(o) {
3
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
- if (m) return m.call(o);
5
- if (o && typeof o.length === "number") return {
6
- next: function () {
7
- if (o && i >= o.length) o = void 0;
8
- return { value: o && o[i++], done: !o };
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];
9
8
  }
9
+ return t;
10
10
  };
11
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
11
+ return __assign.apply(this, arguments);
12
12
  };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.addHashAttributesToJsxTagsAst = void 0;
15
- var ast_iterator_1 = require("./ast-iterator");
16
- var ast_transformer_1 = require("./ast-transformer");
17
- var classic_parser_1 = require("./parsers/classic-parser");
18
- var jsx_parser_model_1 = require("./parsers/jsx-parser.model");
19
- var new_parser_1 = require("./parsers/new-parser");
20
- global.implementation = null;
21
- var classicParser = new classic_parser_1.ClassicJsxParser();
22
- var newParser = new new_parser_1.NewJsxParser();
23
- var parser = classicParser;
24
- var findImplementation = function (program) {
25
- var e_1, _a;
26
- var implementation = jsx_parser_model_1.ParserImplementations.classic;
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
27
17
  try {
28
- for (var _b = __values((0, ast_iterator_1.astIterator)(program)), _c = _b.next(); !_c.done; _c = _b.next()) {
29
- var node = _c.value;
30
- if (node.type !== "ImportDeclaration") {
31
- continue;
32
- }
33
- if (node.specifiers.some(function (n) {
34
- return ["_jsxDEV", "jsxRuntime"].includes(n.local.name);
35
- }) ||
36
- ["react/jsx-dev-runtime", "react/jsx-runtime"].includes(node.source.value)) {
37
- return jsx_parser_model_1.ParserImplementations.new;
38
- }
39
- }
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
40
19
  }
41
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
20
+ catch (error) { e = { error: error }; }
42
21
  finally {
43
22
  try {
44
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
45
24
  }
46
- finally { if (e_1) throw e_1.error; }
25
+ finally { if (e) throw e.error; }
47
26
  }
48
- return implementation;
27
+ return ar;
49
28
  };
50
- function addHashAttributesToJsxTagsAst(program, attr) {
51
- // Once in the program, we can determine which parser to use
52
- if (global.implementation === null) {
53
- global.implementation = findImplementation(program);
54
- if (global.implementation === jsx_parser_model_1.ParserImplementations.new) {
55
- parser = newParser;
29
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
+ if (ar || !(i in from)) {
32
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
+ ar[i] = from[i];
56
34
  }
57
35
  }
58
- return (0, ast_transformer_1.astTransformer)(program, function (node) {
59
- if (parser.isNodeReactElement(node) && !parser.isNodeReactFragment(node)) {
60
- return parser.extendNodeWithAttributes(node, attr);
61
- }
62
- });
63
- }
64
- exports.addHashAttributesToJsxTagsAst = addHashAttributesToJsxTagsAst;
36
+ return to.concat(ar || Array.prototype.slice.call(from));
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ 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;
40
+ var createObjectAssignCallExpressionWithIdentifier = function (identifier, objectExpression) {
41
+ return {
42
+ type: "CallExpression",
43
+ callee: {
44
+ type: "MemberExpression",
45
+ object: {
46
+ type: "Identifier",
47
+ name: "Object",
48
+ },
49
+ property: {
50
+ type: "Identifier",
51
+ name: "assign",
52
+ },
53
+ computed: false,
54
+ optional: false,
55
+ },
56
+ arguments: [
57
+ {
58
+ type: "ObjectExpression",
59
+ start: 14,
60
+ end: 16,
61
+ properties: [],
62
+ },
63
+ identifier,
64
+ objectExpression,
65
+ ],
66
+ optional: false,
67
+ };
68
+ };
69
+ exports.createObjectAssignCallExpressionWithIdentifier = createObjectAssignCallExpressionWithIdentifier;
70
+ var createObjectExpressionWithAttr = function (attrNode) {
71
+ return {
72
+ type: "ObjectExpression",
73
+ properties: [attrNode],
74
+ };
75
+ };
76
+ exports.createObjectExpressionWithAttr = createObjectExpressionWithAttr;
77
+ var extendObjectExpression = function (objectExpression, prop) {
78
+ return __assign(__assign({}, objectExpression), { properties: __spreadArray(__spreadArray([], __read(objectExpression.properties), false), [prop], false) });
79
+ };
80
+ exports.extendObjectExpression = extendObjectExpression;
81
+ var hackIntoExtenderCallExpression = function (callExpression, prop) {
82
+ return __assign(__assign({}, callExpression), { arguments: __spreadArray(__spreadArray([], __read(callExpression.arguments), false), [
83
+ (0, exports.createObjectExpressionWithAttr)(prop),
84
+ ], false) });
85
+ };
86
+ exports.hackIntoExtenderCallExpression = hackIntoExtenderCallExpression;
87
+ var isNodeCallExpression = function (node) {
88
+ return node.type === "CallExpression";
89
+ };
90
+ exports.isNodeCallExpression = isNodeCallExpression;
91
+ var isNodeObjectExpression = function (node) {
92
+ return node.type === "ObjectExpression";
93
+ };
94
+ exports.isNodeObjectExpression = isNodeObjectExpression;
95
+ var isNodeExtender = function (node) {
96
+ return ((0, exports.isNodeCallExpression)(node) &&
97
+ (node.callee.name === "_objectSpread" || node.callee.name === "_extends"));
98
+ };
99
+ exports.isNodeExtender = isNodeExtender;
100
+ var isNodeIdentifier = function (node) {
101
+ return node.type === "Identifier";
102
+ };
103
+ exports.isNodeIdentifier = isNodeIdentifier;
104
+ var isNodeImportDeclaration = function (node) {
105
+ return node.type === "ImportDeclaration";
106
+ };
107
+ exports.isNodeImportDeclaration = isNodeImportDeclaration;
108
+ var isNodeRequireCallExpression = function (node) {
109
+ return ((0, exports.isNodeCallExpression)(node) &&
110
+ node.callee.name === "require" &&
111
+ node.arguments.length === 1);
112
+ };
113
+ exports.isNodeRequireCallExpression = isNodeRequireCallExpression;
114
+ var isNodeImportOrRequire = function (node) {
115
+ return (0, exports.isNodeImportDeclaration)(node) || (0, exports.isNodeRequireCallExpression)(node);
116
+ };
117
+ exports.isNodeImportOrRequire = isNodeImportOrRequire;
118
+ var getSrcFromImportOrRequire = function (node) {
119
+ if ((0, exports.isNodeImportDeclaration)(node)) {
120
+ return node.source.value;
121
+ }
122
+ if ((0, exports.isNodeRequireCallExpression)(node)) {
123
+ return node.arguments[0].value;
124
+ }
125
+ return null;
126
+ };
127
+ exports.getSrcFromImportOrRequire = getSrcFromImportOrRequire;
package/dist/index.d.ts CHANGED
@@ -36,4 +36,4 @@ export interface ReactScopedCssPluginOptions {
36
36
  export interface VitePartialPlugin extends Plugin {
37
37
  enforce?: "pre" | "post";
38
38
  }
39
- export default function reactScopedCssPlugin(optionsIn?: ReactScopedCssPluginOptions): VitePartialPlugin[];
39
+ export declare function reactScopedCssPlugin(optionsIn?: ReactScopedCssPluginOptions): VitePartialPlugin[];
package/dist/index.js CHANGED
@@ -30,13 +30,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
30
30
  return result;
31
31
  };
32
32
  Object.defineProperty(exports, "__esModule", { value: true });
33
+ exports.reactScopedCssPlugin = void 0;
33
34
  /* eslint-disable no-useless-escape */
34
35
  var path = __importStar(require("path"));
35
36
  var js_xxhash_1 = require("js-xxhash");
36
37
  var pluginutils_1 = require("@rollup/pluginutils");
37
38
  var component_compiler_utils_1 = require("@vue/component-compiler-utils");
38
39
  var escodegen_1 = require("escodegen");
39
- var ast_utils_1 = require("./ast.utils");
40
+ var ast_program_1 = require("./ast-program");
40
41
  var getFilenameFromPath = function (filePath) {
41
42
  var parts = filePath.split("/");
42
43
  return parts[parts.length - 1].split("?")[0];
@@ -102,7 +103,7 @@ function reactScopedCssPlugin(optionsIn) {
102
103
  if (scopedCssInFileRegex.test(code)) {
103
104
  var importerHash = generateHash(id);
104
105
  var program = this.parse(code);
105
- var newAst = (0, ast_utils_1.addHashAttributesToJsxTagsAst)(program, "data-".concat(options.hashPrefix, "-").concat(importerHash));
106
+ var newAst = (0, ast_program_1.addHashAttributesToJsxTagsAst)(program, "data-".concat(options.hashPrefix, "-").concat(importerHash));
106
107
  return (0, escodegen_1.generate)(newAst);
107
108
  }
108
109
  if (scopedCssRegex.test(getFilenameFromPath(id))) {
@@ -113,4 +114,4 @@ function reactScopedCssPlugin(optionsIn) {
113
114
  },
114
115
  ];
115
116
  }
116
- exports.default = reactScopedCssPlugin;
117
+ exports.reactScopedCssPlugin = reactScopedCssPlugin;
@@ -10,72 +10,47 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
- if (ar || !(i in from)) {
32
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
- ar[i] = from[i];
34
- }
35
- }
36
- return to.concat(ar || Array.prototype.slice.call(from));
37
- };
38
13
  Object.defineProperty(exports, "__esModule", { value: true });
39
14
  exports.ClassicJsxParser = void 0;
15
+ var ast_utils_1 = require("../ast.utils");
40
16
  var ClassicJsxParser = /** @class */ (function () {
41
17
  function ClassicJsxParser() {
42
18
  }
43
19
  ClassicJsxParser.prototype.isNodeReactFragment = function (node) {
44
- var _a, _b, _c, _d, _e, _f;
45
- 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" &&
46
- ((_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");
20
+ var _a, _b, _c, _d, _e, _f, _g, _h;
21
+ return ((((_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" ||
22
+ ((_e = (_d = (_c = node === null || node === void 0 ? void 0 : node.callee) === null || _c === void 0 ? void 0 : _c.object) === null || _d === void 0 ? void 0 : _d.object) === null || _e === void 0 ? void 0 : _e.name) === "_react") &&
23
+ ((_h = (_g = (_f = node === null || node === void 0 ? void 0 : node.arguments) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.property) === null || _h === void 0 ? void 0 : _h.name) === "Fragment");
47
24
  };
48
25
  ClassicJsxParser.prototype.isNodeReactElement = function (node) {
49
- var _a, _b, _c, _d;
26
+ var _a, _b, _c, _d, _e, _f, _g;
50
27
  return ((node === null || node === void 0 ? void 0 : node.type) === "CallExpression" &&
51
- ((_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" &&
52
- ((_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");
28
+ (((_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" ||
29
+ ((_e = (_d = (_c = node === null || node === void 0 ? void 0 : node.callee) === null || _c === void 0 ? void 0 : _c.object) === null || _d === void 0 ? void 0 : _d.object) === null || _e === void 0 ? void 0 : _e.name) === "_react") &&
30
+ ((_g = (_f = node === null || node === void 0 ? void 0 : node.callee) === null || _f === void 0 ? void 0 : _f.property) === null || _g === void 0 ? void 0 : _g.name) === "createElement");
53
31
  };
54
32
  ClassicJsxParser.prototype.extendNodeWithAttributes = function (node, attr) {
55
33
  var _this = this;
56
- var _a;
57
- var arg = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[1];
58
34
  return __assign(__assign({}, node), { arguments: node === null || node === void 0 ? void 0 : node.arguments.map(function (v, i) {
59
- if (i === 1) {
60
- if (v.type === "ObjectExpression") {
61
- return __assign(__assign({}, v), { properties: __spreadArray(__spreadArray([], __read(arg.properties), false), [_this.createAttributeNode(attr)], false) });
62
- }
63
- else if (v.type === "CallExpression" &&
64
- v.callee.name === "_objectSpread") {
65
- var args = __spreadArray([], __read(v.arguments), false);
66
- args[0].properties = __spreadArray(__spreadArray([], __read(args[0].properties), false), [
67
- _this.createAttributeNode(attr),
68
- ], false);
69
- return __assign(__assign({}, v), { arguments: args });
70
- }
71
- else {
72
- return {
73
- type: "ObjectExpression",
74
- properties: [_this.createAttributeNode(attr)],
75
- };
76
- }
35
+ // Second argument is attributes so ignore others
36
+ if (i !== 1) {
37
+ return v;
38
+ }
39
+ var attrNode = _this.createAttributeNode(attr);
40
+ // If it's an object expression, extend it
41
+ if ((0, ast_utils_1.isNodeObjectExpression)(v)) {
42
+ return (0, ast_utils_1.extendObjectExpression)(v, attrNode);
43
+ }
44
+ // If it's an extender, hack into it
45
+ if ((0, ast_utils_1.isNodeExtender)(v)) {
46
+ return (0, ast_utils_1.hackIntoExtenderCallExpression)(v, attrNode);
47
+ }
48
+ // If it's an identifier, do Object.assign to be safe
49
+ if ((0, ast_utils_1.isNodeIdentifier)(v)) {
50
+ return (0, ast_utils_1.createObjectAssignCallExpressionWithIdentifier)(v, (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode));
77
51
  }
78
- return v;
52
+ // Worst case override with
53
+ return (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode);
79
54
  }) });
80
55
  };
81
56
  ClassicJsxParser.prototype.createAttributeNode = function (attr) {
@@ -10,71 +10,65 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
- if (ar || !(i in from)) {
32
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
- ar[i] = from[i];
34
- }
35
- }
36
- return to.concat(ar || Array.prototype.slice.call(from));
37
- };
38
13
  Object.defineProperty(exports, "__esModule", { value: true });
39
14
  exports.NewJsxParser = void 0;
15
+ var ast_utils_1 = require("../ast.utils");
40
16
  var NewJsxParser = /** @class */ (function () {
41
17
  function NewJsxParser() {
42
18
  }
43
19
  NewJsxParser.prototype.isNodeReactFragment = function (node) {
44
- var _a, _b, _c, _d;
45
- return (((_b = (_a = node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.type) === "Identifier" &&
46
- ["_Fragment", "Fragment"].includes((_d = (_c = node.arguments) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.name));
20
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
21
+ // Always a call expression
22
+ if ((node === null || node === void 0 ? void 0 : node.type) !== "CallExpression") {
23
+ return false;
24
+ }
25
+ if (((_b = (_a = node.arguments) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.type) === "Identifier" &&
26
+ ["_Fragment", "Fragment"].includes((_d = (_c = node.arguments) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.name)) {
27
+ return true;
28
+ }
29
+ if (((_f = (_e = node.arguments) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.type) === "MemberExpression" &&
30
+ ["_Fragment", "Fragment"].includes((_j = (_h = (_g = node.arguments) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.property) === null || _j === void 0 ? void 0 : _j.name)) {
31
+ return true;
32
+ }
33
+ return false;
47
34
  };
48
35
  NewJsxParser.prototype.isNodeReactElement = function (node) {
49
- var _a;
50
- return ((node === null || node === void 0 ? void 0 : node.type) === "CallExpression" &&
51
- ["_jsxDEV", "_jsx", "_jsxs", "jsxDev", "jsx", "jsxs"].includes((_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.name));
36
+ var _a, _b, _c, _d, _e, _f;
37
+ // Always a call expression
38
+ if ((node === null || node === void 0 ? void 0 : node.type) !== "CallExpression") {
39
+ return false;
40
+ }
41
+ if (["_jsxDEV", "_jsx", "_jsxs", "jsxDev", "jsx", "jsxs"].includes((_a = node === null || node === void 0 ? void 0 : node.callee) === null || _a === void 0 ? void 0 : _a.name)) {
42
+ return true;
43
+ }
44
+ if (((_b = node === null || node === void 0 ? void 0 : node.callee) === null || _b === void 0 ? void 0 : _b.type) === "SequenceExpression" &&
45
+ ["_jsxDEV", "_jsx", "_jsxs", "jsxDev", "jsx", "jsxs"].includes((_f = (_e = (_d = (_c = node === null || node === void 0 ? void 0 : node.callee) === null || _c === void 0 ? void 0 : _c.expressions) === null || _d === void 0 ? void 0 : _d[1]) === null || _e === void 0 ? void 0 : _e.property) === null || _f === void 0 ? void 0 : _f.name)) {
46
+ return true;
47
+ }
48
+ return false;
52
49
  };
53
50
  NewJsxParser.prototype.extendNodeWithAttributes = function (node, attr) {
54
51
  var _this = this;
55
- var _a;
56
- var arg = (_a = node === null || node === void 0 ? void 0 : node.arguments) === null || _a === void 0 ? void 0 : _a[1];
57
52
  return __assign(__assign({}, node), { arguments: node === null || node === void 0 ? void 0 : node.arguments.map(function (v, i) {
58
- if (i === 1) {
59
- if (v.type === "ObjectExpression") {
60
- return __assign(__assign({}, v), { properties: __spreadArray(__spreadArray([], __read(arg.properties), false), [_this.createAttributeNode(attr)], false) });
61
- }
62
- else if (v.type === "CallExpression" &&
63
- v.callee.name === "_objectSpread") {
64
- var args = __spreadArray([], __read(v.arguments), false);
65
- args[0].properties = __spreadArray(__spreadArray([], __read(args[0].properties), false), [
66
- _this.createAttributeNode(attr),
67
- ], false);
68
- return __assign(__assign({}, v), { arguments: args });
69
- }
70
- else {
71
- return {
72
- type: "ObjectExpression",
73
- properties: [_this.createAttributeNode(attr)],
74
- };
75
- }
53
+ // Second argument is attributes so ignore others
54
+ if (i !== 1) {
55
+ return v;
56
+ }
57
+ var attrNode = _this.createAttributeNode(attr);
58
+ // If it's an object expression, extend it
59
+ if ((0, ast_utils_1.isNodeObjectExpression)(v)) {
60
+ return (0, ast_utils_1.extendObjectExpression)(v, attrNode);
61
+ }
62
+ // If it's an extender, hack into it
63
+ if ((0, ast_utils_1.isNodeExtender)(v)) {
64
+ return (0, ast_utils_1.hackIntoExtenderCallExpression)(v, attrNode);
65
+ }
66
+ // If it's an identifier, do Object.assign to be safe
67
+ if ((0, ast_utils_1.isNodeIdentifier)(v)) {
68
+ return (0, ast_utils_1.createObjectAssignCallExpressionWithIdentifier)(v, (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode));
76
69
  }
77
- return v;
70
+ // Worst case override with
71
+ return (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode);
78
72
  }) });
79
73
  };
80
74
  NewJsxParser.prototype.createAttributeNode = function (attr) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rollup-plugin-react-scoped-css",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
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",
@@ -40,7 +40,9 @@
40
40
  "rollup": "^2.56.2"
41
41
  },
42
42
  "devDependencies": {
43
+ "@babel/core": "^7.16.0",
43
44
  "@babel/preset-env": "^7.15.0",
45
+ "@babel/preset-react": "^7.16.0",
44
46
  "@babel/preset-typescript": "^7.15.0",
45
47
  "@semantic-release/commit-analyzer": "^9.0.2",
46
48
  "@semantic-release/git": "^10.0.1",
@@ -1,20 +0,0 @@
1
- name: Pull Request Workflow
2
-
3
- on:
4
- - pull_request
5
-
6
- jobs:
7
- build:
8
- runs-on: ubuntu-latest
9
-
10
- steps:
11
- - uses: actions/checkout@v2
12
- - name: Use Node.js 16.x
13
- uses: actions/setup-node@v2
14
- with:
15
- node-version: 16.x
16
- cache: 'npm'
17
- - run: npm ci
18
- - run: npm run build --if-present
19
- - run: npm test
20
- - run: npm run lint
@@ -1,30 +0,0 @@
1
- name: Release Workflow
2
-
3
- on:
4
- push:
5
- branches:
6
- - 'main'
7
- paths-ignore:
8
- - '.github/**'
9
-
10
- jobs:
11
- release:
12
- runs-on: ubuntu-latest
13
-
14
- steps:
15
- - uses: actions/checkout@v2
16
- - name: Use Node.js 16.x
17
- uses: actions/setup-node@v2
18
- with:
19
- node-version: 16.x
20
- registry-url: 'https://registry.npmjs.org'
21
- cache: 'npm'
22
- - run: npm ci
23
- - run: npm run build --if-present
24
- - name: Release
25
- env:
26
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27
- NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
28
- run: npx semantic-release
29
-
30
-