@dr.pogodin/babel-plugin-react-css-modules
Version:
Transforms styleName to className using compile time CSS module resolution.
28 lines • 1.55 kB
JavaScript
import { NodePath } from '@babel/traverse';
import { isJSXExpressionContainer, isStringLiteral, JSXAttribute, stringLiteral } from '@babel/types';
import conditionalClassMerge from "./conditionalClassMerge.js";
import getClassName from "./getClassName.js";
/**
* Updates the className value of a JSX element using a provided
* styleName attribute.
*/
export default (path, styleModuleImportMap, sourceAttribute, destinationName, options) => {
const resolvedStyleName = getClassName(sourceAttribute.value.value, styleModuleImportMap, options);
const destinationAttribute = path.node.openingElement.attributes.find(attribute => typeof attribute.name !== 'undefined' && attribute.name.name === destinationName);
if (destinationAttribute) {
if (isStringLiteral(destinationAttribute.value)) {
destinationAttribute.value.value += ` ${resolvedStyleName}`;
} else if (isJSXExpressionContainer(destinationAttribute.value)) {
destinationAttribute.value.expression = conditionalClassMerge(destinationAttribute.value.expression, stringLiteral(resolvedStyleName));
} else {
throw new Error(`Unexpected attribute value:${destinationAttribute.value}`);
}
path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(sourceAttribute), 1);
} else {
/* eslint-disable no-param-reassign */
sourceAttribute.name.name = destinationName;
sourceAttribute.value.value = resolvedStyleName;
/* eslint-enable no-param-reassign */
}
};
//# sourceMappingURL=resolveStringLiteral.js.map