|
| 1 | +import * as fs from "fs-extra"; |
| 2 | +import { ParsedSelector, SelectorCache } from "opticss"; |
| 3 | +import * as postcss from "postcss"; |
| 4 | + |
| 5 | +import { BemSelector, BlockClassSelector } from "./interface"; |
| 6 | +import { findLcs } from "./utils"; |
| 7 | +export declare type PostcssAny = unknown; |
| 8 | + |
| 9 | +type BemSelectorMap = Map<string, BemSelector>; |
| 10 | +type ElementToBemSelectorMap = Map<string, BemSelector[]>; |
| 11 | +type BlockToBemSelectorMap = Map<string, ElementToBemSelectorMap>; |
| 12 | +type BemToBlockClassMap = WeakMap<BemSelector, BlockClassSelector>; |
| 13 | + |
| 14 | +const EMPTY_ELEMENT_PLACEHOLDER = "EMPTY-ELEMENT-PLACEHOLDER"; |
| 15 | + |
| 16 | +export function convertBemToBlocks(files: Array<string>): Promise<void>[] { |
| 17 | + let promises: Promise<void>[] = []; |
| 18 | + files.forEach(file => { |
| 19 | + fs.readFile(file, (_err, css) => { |
| 20 | + let output = postcss([bemToBlocksPlugin]) |
| 21 | + .process(css, { from: file }); |
| 22 | + // rewrite the file with the processed output |
| 23 | + promises.push(fs.writeFile(file, output.toString())); |
| 24 | + }); |
| 25 | + }); |
| 26 | + return promises; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Iterates through a cache of bemSelectors and returns a map of bemSelector to |
| 31 | + * the blockClassName. This function optimises the states and subStates from the |
| 32 | + * name of the modifier present in the BEM selector |
| 33 | + * @param bemSelectorCache weakmap - BemSelectorMap |
| 34 | + BemSelector { |
| 35 | + block: 'jobs-hero', |
| 36 | + element: 'image-container', |
| 37 | + modifier: undefined } |
| 38 | + => |
| 39 | + BlockClassName { |
| 40 | + class: // name of the element if present. If this is not present, then it is on the :scope |
| 41 | + state: // name of the modifiers HCF |
| 42 | + subState: // null if HCF is null |
| 43 | + }, // written to a file with blockname.block.css |
| 44 | + */ |
| 45 | +export function constructBlocksMap(bemSelectorCache: BemSelectorMap): BemToBlockClassMap { |
| 46 | + let blockListMap: BlockToBemSelectorMap = new Map(); |
| 47 | + let resultMap: BemToBlockClassMap = new WeakMap(); |
| 48 | + |
| 49 | + // create the resultMap and the blockListMap |
| 50 | + for (let bemSelector of bemSelectorCache.values()) { |
| 51 | + // create the new blockClass instance |
| 52 | + let blockClass: BlockClassSelector = new BlockClassSelector(); |
| 53 | + if (bemSelector.element) { |
| 54 | + blockClass.class = bemSelector.element; |
| 55 | + } |
| 56 | + if (bemSelector.modifier) { |
| 57 | + blockClass.state = bemSelector.modifier; |
| 58 | + } |
| 59 | + // add this blockClass to the resultMap |
| 60 | + resultMap.set(bemSelector, blockClass); |
| 61 | + |
| 62 | + // add this selector to the blockList based on the block, and then the |
| 63 | + // element value |
| 64 | + let block = blockListMap.get(bemSelector.block); |
| 65 | + if (block) { |
| 66 | + if (bemSelector.element) { |
| 67 | + if (block.has(bemSelector.element)) { |
| 68 | + (block.get(bemSelector.element) as BemSelector[]).push(bemSelector); |
| 69 | + } else { |
| 70 | + block.set(bemSelector.element, new Array(bemSelector)); |
| 71 | + } |
| 72 | + } else { |
| 73 | + // the modifier is on the block itself |
| 74 | + if (block.has(EMPTY_ELEMENT_PLACEHOLDER)) { |
| 75 | + (block.get(EMPTY_ELEMENT_PLACEHOLDER) as BemSelector[]).push(bemSelector); |
| 76 | + } else { |
| 77 | + block.set(EMPTY_ELEMENT_PLACEHOLDER, new Array(bemSelector)); |
| 78 | + } |
| 79 | + } |
| 80 | + } else { |
| 81 | + // if there is no existing block, create the elementMap and the add it to |
| 82 | + // the blockMap |
| 83 | + let elementListMap = new Map(); |
| 84 | + if (bemSelector.element) { |
| 85 | + elementListMap.set(bemSelector.element, new Array(bemSelector)); |
| 86 | + } else { |
| 87 | + elementListMap.set(EMPTY_ELEMENT_PLACEHOLDER, new Array(bemSelector)); |
| 88 | + } |
| 89 | + blockListMap.set(bemSelector.block, elementListMap); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // optimize the blocks for sub-states, iterate through the blocks |
| 94 | + for (let elementListMap of blockListMap.values()) { |
| 95 | + // iterate through the elements |
| 96 | + for (let selList of elementListMap.values()) { |
| 97 | + let lcs: string; |
| 98 | + // find the longest common substring(LCS) in the list of selectors |
| 99 | + let modifiers = selList.length && selList.filter(sel => sel.modifier !== undefined); |
| 100 | + if (modifiers) { |
| 101 | + if (modifiers.length > 1) { |
| 102 | + lcs = findLcs(modifiers.map(sel => sel.modifier as string)); |
| 103 | + } |
| 104 | + // update the states and substates with the LCS |
| 105 | + modifiers.forEach(sel => { |
| 106 | + let blockClass = resultMap.get(sel); |
| 107 | + if (blockClass && lcs) { |
| 108 | + blockClass.subState = (blockClass.state as string).replace(lcs, ""); |
| 109 | + blockClass.state = lcs.replace(/-$/, ""); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + // TODO: detect if there is a scope node, if not create a new empty scope node |
| 116 | + return resultMap; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * PostCSS plugin for transforming BEM to CSS blocks |
| 121 | + */ |
| 122 | +export const bemToBlocksPlugin: postcss.Plugin<PostcssAny> = postcss.plugin("bem-to-blocks-plugin", (options) => { |
| 123 | + options = options || {}; |
| 124 | + |
| 125 | + return (root, result) => { |
| 126 | + const cache = new SelectorCache(); |
| 127 | + const bemSelectorCache: BemSelectorMap = new Map(); |
| 128 | + |
| 129 | + // in this pass, we collect all the selectors |
| 130 | + root.walkRules(rule => { |
| 131 | + let parsedSelList = cache.getParsedSelectors(rule); |
| 132 | + parsedSelList.forEach(parsedSel => { |
| 133 | + parsedSel.eachSelectorNode(node => { |
| 134 | + if (node.value) { |
| 135 | + let bemSelector = new BemSelector(node.value); |
| 136 | + if (bemSelector) { |
| 137 | + // add it to the cache so it's available for the next pass |
| 138 | + bemSelectorCache.set(node.value, bemSelector); |
| 139 | + } else { |
| 140 | + console.error(`${parsedSel} does not comply with BEM standards. Consider a refactor`); |
| 141 | + } |
| 142 | + } |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + // convert selectors to block selectors |
| 148 | + let bemToBlockClassMap: BemToBlockClassMap = constructBlocksMap(bemSelectorCache); |
| 149 | + |
| 150 | + // rewrite into a CSS block |
| 151 | + root.walkRules(rule => { |
| 152 | + // iterate through each rule |
| 153 | + let parsedSelList = cache.getParsedSelectors(rule); |
| 154 | + let modifiedSelList: ParsedSelector[] = new Array(); |
| 155 | + parsedSelList.forEach(sel => { |
| 156 | + // this contains the selector combinators |
| 157 | + let modifiedCompoundSelector = sel.clone(); |
| 158 | + |
| 159 | + modifiedCompoundSelector.eachSelectorNode(node => { |
| 160 | + // we only need to modify class names. We can ignore everything else, |
| 161 | + // like existing attributes, pseudo selectors, comments, imports, |
| 162 | + // exports, etc |
| 163 | + if (node.type === "class" && node.value) { |
| 164 | + let bemSelector = bemSelectorCache.get(node.value); |
| 165 | + // get the block class from the bemSelector |
| 166 | + let blockClassName = bemSelector && bemToBlockClassMap.get(bemSelector); |
| 167 | + |
| 168 | + // if the selector was previously cached |
| 169 | + if (blockClassName) { |
| 170 | + // we need to use the below method instead of node.value as the |
| 171 | + // attributes brackets get escaped when doing node.value = blockClassName.toString() |
| 172 | + node.setPropertyWithoutEscape("value", blockClassName.toString()); |
| 173 | + } |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + modifiedSelList.push(modifiedCompoundSelector); |
| 178 | + }); |
| 179 | + |
| 180 | + // if the selector nodes were modified, then create a new rule for it |
| 181 | + if (modifiedSelList.toString()) { |
| 182 | + let newRule = rule.clone(); |
| 183 | + newRule.selector = modifiedSelList.toString(); |
| 184 | + rule.replaceWith(newRule); |
| 185 | + } |
| 186 | + }); |
| 187 | + result.root = root; |
| 188 | + }; |
| 189 | +}); |
0 commit comments