UNPKG

@dr.pogodin/babel-plugin-react-css-modules

Version:

Transforms styleName to className using compile time CSS module resolution.

33 lines 1.66 kB
import { NodePath } from '@babel/traverse'; import { cloneNode, // type Expression, memberExpression, binaryExpression, conditionalExpression, stringLiteral, logicalExpression, identifier, isJSXSpreadAttribute } from '@babel/types'; import optionsDefaults from "./schemas/optionsDefaults.js"; const createSpreadMapper = (path, stats) => { const result = {}; let { attributeNames } = optionsDefaults; if (stats.opts && stats.opts.attributeNames) { attributeNames = { ...attributeNames, ...stats.opts.attributeNames }; } const attributes = Object.entries(attributeNames).filter(pair => pair[1]); const attributeKeys = attributes.map(pair => pair[0]); const spreadAttributes = path.node.openingElement.attributes.filter(attribute => isJSXSpreadAttribute(attribute)); spreadAttributes.forEach(spread => { attributeKeys.forEach(attributeKey => { const destinationName = attributeNames[attributeKey]; if (result[destinationName]) { result[destinationName] = binaryExpression('+', result[destinationName], conditionalExpression(cloneNode(spread.argument), binaryExpression('+', stringLiteral(' '), logicalExpression('||', memberExpression(cloneNode(spread.argument), identifier(destinationName)), stringLiteral(''))), stringLiteral(''))); } else { result[destinationName] = conditionalExpression(cloneNode(spread.argument), logicalExpression('||', memberExpression(cloneNode(spread.argument), identifier(destinationName)), stringLiteral('')), stringLiteral('')); } }); }); return result; }; export default createSpreadMapper; //# sourceMappingURL=createSpreadMapper.js.map