rollup-plugin-react-scoped-css
Version:
A rollup plugin designed to allow scoped css to be run in react (Compatible with vite and rollup)
63 lines (62 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LegacyJsxParser = void 0;
const ast_utils_1 = require("../ast.utils");
class LegacyJsxParser {
isNodeReactFragment(node) {
return ((node?.callee?.object?.name === "React" ||
node?.callee?.object?.object?.name === "_react") &&
node?.arguments?.[0]?.property?.name === "Fragment");
}
isNodeReactElement(node) {
return (node?.type === "CallExpression" &&
(node?.callee?.object?.name === "React" ||
node?.callee?.object?.object?.name === "_react") &&
node?.callee?.property?.name === "createElement");
}
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.LegacyJsxParser = LegacyJsxParser;