UNPKG

@twstyled/util

Version:

Utilities, config and class helpers for twstyled -- the full-featured Tailwind CSS + CSS in JS Compiler

77 lines (76 loc) 3.15 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 core_1 = __importStar(require("@xwind/core")); const _1 = require("."); function splitAtLastOccurence(src, char) { const pos = src.lastIndexOf(char); return [src.substring(0, pos), src.substring(pos + 1)]; } function splitAtLastTwoOccurences(src, char) { const pos2 = src.lastIndexOf(char); const pos1 = src.substring(0, pos2 - 1).lastIndexOf(char); return [ src.substring(0, pos1), src.substring(pos1 + 1, pos2), src.substring(pos2 + 1) ]; } let $twAttributes; let $twVariants; function getTailwindAttributesAndVariants(options) { const twConfigPath = _1.getTwConfigPath(options.configPath); const { twConfig, isNewTwConfig } = _1.getTwConfigCache(twConfigPath); if (!$twAttributes || isNewTwConfig) { const { utilitiesRoot, componentsRoot, screens, variants } = core_1.default(twConfig); const twObjectMap = core_1.createTwClassDictionary(utilitiesRoot, componentsRoot); const TW_COLORS = /^(divide|bg|from|via|to|border|placeholder|ring-offset|ring|text)\-/; const tw = Object.keys(twObjectMap) .filter((key) => !key.startsWith('-') && !key.startsWith('XWIND')) .sort() .reduce((accum, el) => { accum[el] = true; if (TW_COLORS.test(el)) { const components = splitAtLastTwoOccurences(el, '-'); if (components[0]) { accum[components[0]] = accum[components[0]] || true; } else { accum[components[1]] = accum[components[1]] || true; } } else { const components = splitAtLastOccurence(el, '-'); if (components[0]) { accum[components[0]] = accum[components[0]] || true; } else { accum[components[1]] = accum[components[1]] || true; } } return accum; }, {}); $twAttributes = Object.keys(tw); $twVariants = [].concat(...screens, ...variants, 'tw'); } return [$twAttributes, $twVariants]; } exports.default = getTailwindAttributesAndVariants;