UNPKG

@kuma-ui/compiler

Version:

🐻 Kuma UI is a utility-first, zero-runtime CSS-in-JS library that offers an outstanding developer experience and optimized performance.

50 lines (48 loc) 1.95 kB
// src/processTaggedTemplateExpression.ts import { Node, SyntaxKind } from "ts-morph"; import { sheet } from "@kuma-ui/sheet"; var extractClassName = (templateLiteral) => { if (Node.isNoSubstitutionTemplateLiteral(templateLiteral)) { const cssString = templateLiteral.getLiteralText(); return cssString ? sheet.parseCSS(cssString) : void 0; } return void 0; }; var processTaggedTemplateExpression = (node, bindings) => { const tag = node.getTag(); if (Node.isIdentifier(tag) && tag.getText() === bindings["css"]) { const className = extractClassName(node.getTemplate()); if (className) { node.replaceWithText(JSON.stringify(className)); } } else if (Node.isCallExpression(tag) && tag.getExpressionIfKind(SyntaxKind.Identifier)?.getText() === bindings["styled"]) { const componentArg = tag.getArguments()[0]; if (Node.isStringLiteral(componentArg)) { const componentName = componentArg.getLiteralText(); replaceTaggedTemplate(node, getBoxComponent(componentName, bindings)); } else { replaceTaggedTemplate(node, componentArg.getFullText()); } } else if (Node.isPropertyAccessExpression(tag) && tag.getExpressionIfKind(SyntaxKind.Identifier)?.getText() === bindings["styled"]) { replaceTaggedTemplate(node, getBoxComponent(tag.getName(), bindings)); } }; function getBoxComponent(intrinsicComponentName, bindings) { return `${bindings["Box"]} as="${intrinsicComponentName}"`; } function replaceTaggedTemplate(node, component) { const className = extractClassName(node.getTemplate()); if (className) { const replacement = `__KUMA_REACT__.forwardRef((props, ref) => { const combinedClassName = [props.className, "${className}"].filter(Boolean).join(" "); return <${component} {...props} ref={ref} className={combinedClassName} IS_KUMA_DEFAULT />; })`; node.replaceWithText(replacement); } } export { processTaggedTemplateExpression };