UNPKG

rollup-plugin-react-scoped-css

Version:

A rollup plugin designed to allow scoped css to be run in react (Compatible with vite and rollup)

92 lines (91 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModernJsxParser = void 0; const ast_utils_1 = require("../ast.utils"); class ModernJsxParser { constructor() { this.reactCalleeNames = [ "_jsxDEV", "_jsx", "_jsxs", "jsxDev", "jsx", "jsxs", "jsxDEV", ]; } isNodeReactFragment(node) { // Always a call expression if (node?.type !== "CallExpression") { return false; } if (node.arguments?.[0]?.type === "Identifier" && ["_Fragment", "Fragment"].includes(node.arguments?.[0]?.name)) { return true; } if (node.arguments?.[0]?.type === "MemberExpression" && ["_Fragment", "Fragment"].includes(node.arguments?.[0]?.property?.name)) { return true; } return false; } isNodeReactElement(node) { // Always a call expression if (node?.type !== "CallExpression") { return false; } if (this.reactCalleeNames.includes(node?.callee?.name)) { return true; } if (node?.callee?.type === "SequenceExpression" && this.reactCalleeNames.includes(node?.callee?.expressions?.[1]?.property?.name)) { return true; } return false; } extendNodeWithAttributes(node, attr) { return { ...node, arguments: node?.arguments.map((v, i) => { // Second argument is attributes so ignore others if (i !== 1) { return v; } const attrNode = this.createAttributeNode(attr); // If it's an object expression, extend it if ((0, ast_utils_1.isNodeObjectExpression)(v)) { return (0, ast_utils_1.extendObjectExpression)(v, attrNode); } // If it's an extender, hack into it if ((0, ast_utils_1.isNodeExtender)(v)) { return (0, ast_utils_1.hackIntoExtenderCallExpression)(v, attrNode); } // If it's an identifier, do Object.assign to be safe if ((0, ast_utils_1.isNodeIdentifier)(v)) { return (0, ast_utils_1.createObjectAssignCallExpressionWithIdentifier)(v, (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode)); } // Worst case override with return (0, ast_utils_1.createObjectExpressionWithAttr)(attrNode); }), }; } createAttributeNode(attr) { return { type: "Property", method: false, shorthand: false, computed: false, key: { type: "Identifier", name: `"${attr}"`, }, value: { type: "Literal", value: true, raw: "true", }, kind: "init", }; } } exports.ModernJsxParser = ModernJsxParser;