UNPKG

@lcap/builder

Version:
149 lines (148 loc) 5.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const postcss = __importStar(require("postcss")); const ComponentAnnotation = '@component'; const UseGlobalTokensAnnotation = '@useGlobalTokens'; const AliasAnnotation = '@alias'; const HiddenAnnotation = '@hidden'; const parseCssVars = (nodes) => { const variables = []; let variable = { title: '', desc: '', type: 'input', name: '', value: '', }; nodes.forEach((n) => { if (n.type === 'comment') { n.text.replace(/\*/g, '') .trim() .split('\n') .map((str) => str.trim()) .filter((str) => str && str.startsWith('@')) .forEach((str) => { if (str === '@hidden') { variable.hidden = true; } const spaceIndex = str.indexOf(' '); if (spaceIndex === -1) { return; } const key = str.substring(1, spaceIndex); const valueStr = str.substring(spaceIndex).trim(); if (valueStr.startsWith('{') || valueStr.startsWith('[')) { variable[key] = JSON.parse(valueStr); } else { variable[key] = valueStr; } }); return; } if (n.type !== 'decl') { return; } variable.name = n.prop; variable.value = n.value; if ((['color', 'bg', 'background'].some((key) => variable.name.toLocaleLowerCase().includes(key)) || variable.name.toLocaleLowerCase().endsWith('outline')) && (!variable.type || variable.type === 'input')) { variable.type = 'color'; } if (!variable.type || !['color', 'size', 'input'].includes(variable.type)) { variable.type = 'input'; } variables.push(variable); variable = { title: '', desc: '', type: 'input', name: '', value: '', }; }); return variables; }; exports.default = (cssContent) => { const themeInfo = { global: { selector: '', variables: [], }, components: [], }; const root = postcss.parse(cssContent); let componentInfo = null; root.nodes.forEach((n) => { if (n.type === 'comment') { if (n.text.includes('@component')) { componentInfo = { name: '', useGlobalTokens: [], selector: '', variables: [], }; n.text.replace(/\*/g, '') .trim() .split('\n') .map((str) => str.trim()) .filter((str) => str && str.startsWith('@')) .forEach((str) => { if (!componentInfo) { return; } if (str.startsWith(ComponentAnnotation)) { componentInfo.name = str.substring(ComponentAnnotation.length).trim(); } else if (str.startsWith(UseGlobalTokensAnnotation)) { componentInfo.useGlobalTokens = JSON.parse(str.substring(UseGlobalTokensAnnotation.length).trim()); } else if (str.startsWith(AliasAnnotation)) { componentInfo.alias = JSON.parse(str.substring(AliasAnnotation.length).trim()); } else if (str.startsWith(HiddenAnnotation)) { componentInfo.hidden = true; } }); } } else if (n.type === 'rule') { const variables = parseCssVars(n.nodes); if (componentInfo) { componentInfo.variables = variables; componentInfo.selector = n.selector; if (themeInfo.components.findIndex((c) => c.name === (componentInfo === null || componentInfo === void 0 ? void 0 : componentInfo.name)) === -1) { themeInfo.components.push(componentInfo); } componentInfo = null; } else if (!themeInfo.global.selector) { themeInfo.global.selector = n.selector; themeInfo.global.variables = variables; } } }); return themeInfo; };