@twstyled/babel-preset
Version:
Babel plugin for twstyled -- the full-featured Tailwind CSS + CSS in JS Compiler
21 lines (20 loc) • 993 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
function visitorPreprocessToClass({ types: t }, item, state, value) {
// Check if a className props already exists
const classNameAttribute = item.parent.attributes.find((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name, { name: 'className' }));
const rightExpression = (classNameAttribute === null || classNameAttribute === void 0 ? void 0 : classNameAttribute.value) ||
t.stringLiteral('');
const result = util_1.combineExpressions({ types: t }, value, rightExpression);
item.remove();
if (t.isJSXAttribute(classNameAttribute)) {
classNameAttribute.value = result;
}
else {
/* Handle the case where no className exists yet */
;
item.parent.attributes.unshift(t.jsxAttribute(t.jsxIdentifier('className'), t.isStringLiteral(result) ? result : result));
}
}
exports.default = visitorPreprocessToClass;