UNPKG

@tiny-css/compiler

Version:

The compiler for compiling & reducing the bundle size of tiny-css

94 lines (93 loc) 4.6 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveSelectorTypes = exports.arrayedSet = void 0; var groupSelectors_1 = require("./groupSelectors"); var htmltagsRegexp_1 = require("./htmltagsRegexp"); var isTruthyArray = function (array) { return !!(array && array.length); }; function arrayedSet(arr) { return Array.from(new Set(arr)); } exports.arrayedSet = arrayedSet; var uniqueIfy = function (arr, type) { return arrayedSet(arr .filter(function (selectorObj) { return selectorObj.type === groupSelectors_1.SelectorType[type]; }) .map(function (selectorObj) { return selectorObj.selector; }) .flat(1)) .map(function (selector) { return ({ type: groupSelectors_1.SelectorType[type], selector: [selector] }); }); }; /** * it's the lighter version of `groupSelector` which doesn't * resolve selector relation resolution. * Also it takes multiple selector string & it can return selector types with unique selectors * @author KR Tirtho * @param {string[]} src array of selector strings * @param {{ unique: boolean }} [opts={ unique: true }] default {unique: true} * @return {*} {SelectorObject[]} */ function resolveSelectorTypes(src, opts) { if (opts === void 0) { opts = { unique: true }; } var selectorStore = []; src.forEach(function (str) { var _a, _b; var classType = { type: groupSelectors_1.SelectorType.class, /* only returns any classes */ selector: arrayedSet((_a = str.match(groupSelectors_1.classSelectors)) === null || _a === void 0 ? void 0 : _a.filter(Boolean)), }; var idType = { type: groupSelectors_1.SelectorType.id, //returns all id selectors selector: arrayedSet((_b = str.match(groupSelectors_1.idSelectors)) === null || _b === void 0 ? void 0 : _b.filter(Boolean)), }; var onlyTags = str.replace(groupSelectors_1.classSelectors, "class").replace(groupSelectors_1.idSelectors, "id").replace(groupSelectors_1.sudoAttrSelectorRegex, "").replace(groupSelectors_1.nestOpRegex, " ") .split(" "); var tagType = { type: groupSelectors_1.SelectorType.tag, selector: arrayedSet(onlyTags .filter(function (tag) { return htmltagsRegexp_1.HtmlTags.split("|").includes(tag); })), }; var pseudoType = { type: groupSelectors_1.SelectorType.pseudo, selector: arrayedSet(str.replace(groupSelectors_1.nestOpRegex, " ").split(" ").filter(function (selector) { var isMatch = groupSelectors_1.pseudoSelectorRegex.test(selector); var isGlobMatch = groupSelectors_1.globSelectorRegex.test(selector); return isMatch || isGlobMatch; })), }; // eslint-disable-next-line @typescript-eslint/no-unused-expressions isTruthyArray(classType.selector) && selectorStore.push(classType); // eslint-disable-next-line @typescript-eslint/no-unused-expressions isTruthyArray(idType.selector) && selectorStore.push(idType); // eslint-disable-next-line @typescript-eslint/no-unused-expressions isTruthyArray(tagType.selector) && selectorStore.push(tagType); // eslint-disable-next-line @typescript-eslint/no-unused-expressions isTruthyArray(pseudoType.selector) && selectorStore.push(pseudoType); }); if (!opts.unique) { return selectorStore; } var uniqueClasses = uniqueIfy(selectorStore, groupSelectors_1.SelectorType.class); var uniqueIds = uniqueIfy(selectorStore, groupSelectors_1.SelectorType.id); var uniqueTags = uniqueIfy(selectorStore, groupSelectors_1.SelectorType.tag); var uniquePseudos = uniqueIfy(selectorStore, groupSelectors_1.SelectorType.pseudo); return __spread(uniqueClasses, uniqueIds, uniqueTags, uniquePseudos); } exports.resolveSelectorTypes = resolveSelectorTypes;