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.

71 lines (69 loc) 3.2 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/processTaggedTemplateExpression.ts var processTaggedTemplateExpression_exports = {}; __export(processTaggedTemplateExpression_exports, { processTaggedTemplateExpression: () => processTaggedTemplateExpression }); module.exports = __toCommonJS(processTaggedTemplateExpression_exports); var import_ts_morph = require("ts-morph"); var import_sheet = require("@kuma-ui/sheet"); var extractClassName = (templateLiteral) => { if (import_ts_morph.Node.isNoSubstitutionTemplateLiteral(templateLiteral)) { const cssString = templateLiteral.getLiteralText(); return cssString ? import_sheet.sheet.parseCSS(cssString) : void 0; } return void 0; }; var processTaggedTemplateExpression = (node, bindings) => { const tag = node.getTag(); if (import_ts_morph.Node.isIdentifier(tag) && tag.getText() === bindings["css"]) { const className = extractClassName(node.getTemplate()); if (className) { node.replaceWithText(JSON.stringify(className)); } } else if (import_ts_morph.Node.isCallExpression(tag) && tag.getExpressionIfKind(import_ts_morph.SyntaxKind.Identifier)?.getText() === bindings["styled"]) { const componentArg = tag.getArguments()[0]; if (import_ts_morph.Node.isStringLiteral(componentArg)) { const componentName = componentArg.getLiteralText(); replaceTaggedTemplate(node, getBoxComponent(componentName, bindings)); } else { replaceTaggedTemplate(node, componentArg.getFullText()); } } else if (import_ts_morph.Node.isPropertyAccessExpression(tag) && tag.getExpressionIfKind(import_ts_morph.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); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { processTaggedTemplateExpression });