|
| 1 | +/* eslint-env node */ |
| 2 | +import postcss from "postcss"; |
| 3 | +import Tokenizer from "css-selector-tokenizer"; |
| 4 | +import valueParser from "postcss-value-parser"; |
| 5 | +import { extractICSS, createICSSRules } from "icss-utils"; |
| 6 | + |
| 7 | +const plugin = "postcss-icss-composes"; |
| 8 | + |
| 9 | +const flatten = outer => outer.reduce((acc, inner) => [...acc, ...inner], []); |
| 10 | + |
| 11 | +const includes = (array, value) => array.indexOf(value) !== -1; |
| 12 | + |
| 13 | +const isSingular = node => node.nodes.length === 1; |
| 14 | + |
| 15 | +const isLocal = node => |
| 16 | + node.type === "nested-pseudo-class" && node.name === "local"; |
| 17 | + |
| 18 | +const isClass = node => node.type === "class"; |
| 19 | + |
| 20 | +const getSelectorIdentifier = selector => { |
| 21 | + if (!isSingular(selector)) { |
| 22 | + return null; |
| 23 | + } |
| 24 | + const [node] = selector.nodes; |
| 25 | + if (isLocal(node)) { |
| 26 | + const local = node.nodes[0]; |
| 27 | + if (isSingular(local) && isClass(local.nodes[0])) { |
| 28 | + return local.nodes[0].name; |
| 29 | + } |
| 30 | + return null; |
| 31 | + } |
| 32 | + if (isClass(node)) { |
| 33 | + return node.name; |
| 34 | + } |
| 35 | + return null; |
| 36 | +}; |
| 37 | + |
| 38 | +const getIdentifiers = (rule, result) => { |
| 39 | + const selectors = Tokenizer.parse(rule.selector).nodes; |
| 40 | + return selectors |
| 41 | + .map(selector => { |
| 42 | + const identifier = getSelectorIdentifier(selector); |
| 43 | + if (identifier === null) { |
| 44 | + result.warn( |
| 45 | + `composition is only allowed in single class selector, not in '${Tokenizer.stringify(selector)}'`, |
| 46 | + { node: rule } |
| 47 | + ); |
| 48 | + } |
| 49 | + return identifier; |
| 50 | + }) |
| 51 | + .filter(identifier => identifier !== null); |
| 52 | +}; |
| 53 | + |
| 54 | +const isComposes = node => |
| 55 | + node.type === "decl" && |
| 56 | + (node.prop === "composes" || node.prop === "compose-with"); |
| 57 | + |
| 58 | +const walkRules = (css, callback) => |
| 59 | + css.walkRules(rule => { |
| 60 | + if (rule.some(node => isComposes(node))) { |
| 61 | + callback(rule); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | +const walkDecls = (rule, callback) => |
| 66 | + rule.each(node => { |
| 67 | + if (isComposes(node)) { |
| 68 | + callback(node); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | +const isMedia = node => |
| 73 | + (node.type === "atrule" && node.name === "media") || |
| 74 | + (node.parent && isMedia(node.parent)); |
| 75 | + |
| 76 | +const splitBy = (array, cond) => |
| 77 | + array.reduce( |
| 78 | + (acc, item) => |
| 79 | + cond(item) |
| 80 | + ? [...acc, []] |
| 81 | + : [...acc.slice(0, -1), [...acc[acc.length - 1], item]], |
| 82 | + [[]] |
| 83 | + ); |
| 84 | + |
| 85 | +const parseComposes = value => { |
| 86 | + const parsed = valueParser(value); |
| 87 | + const [names, path] = splitBy( |
| 88 | + parsed.nodes, |
| 89 | + node => node.type === "word" && node.value === "from" |
| 90 | + ); |
| 91 | + return { |
| 92 | + names: names.filter(node => node.type === "word").map(node => node.value), |
| 93 | + path: path && |
| 94 | + path.filter(node => node.type === "string").map(node => node.value)[0] |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +const combineIntoMessages = (classes, composed) => |
| 99 | + flatten( |
| 100 | + classes.map(name => |
| 101 | + composed.map(value => ({ |
| 102 | + plugin, |
| 103 | + type: "icss-composed", |
| 104 | + name, |
| 105 | + value |
| 106 | + })) |
| 107 | + ) |
| 108 | + ); |
| 109 | + |
| 110 | +const invertObject = obj => |
| 111 | + Object.keys(obj).reduce( |
| 112 | + (acc, key) => Object.assign({}, acc, { [obj[key]]: key }), |
| 113 | + {} |
| 114 | + ); |
| 115 | + |
| 116 | +const combineImports = (icss, composed) => |
| 117 | + Object.keys(composed).reduce( |
| 118 | + (acc, path) => |
| 119 | + Object.assign({}, acc, { |
| 120 | + [`'${path}'`]: Object.assign( |
| 121 | + {}, |
| 122 | + acc[`'${path}'`], |
| 123 | + invertObject(composed[path]) |
| 124 | + ) |
| 125 | + }), |
| 126 | + Object.assign({}, icss) |
| 127 | + ); |
| 128 | + |
| 129 | +const convertMessagesToExports = (messages, aliases) => |
| 130 | + messages |
| 131 | + .map(msg => msg.name) |
| 132 | + .reduce((acc, name) => (includes(acc, name) ? acc : [...acc, name]), []) |
| 133 | + .reduce( |
| 134 | + (acc, name) => |
| 135 | + Object.assign({}, acc, { |
| 136 | + [name]: [ |
| 137 | + aliases[name] || name, |
| 138 | + ...messages |
| 139 | + .filter(msg => msg.name === name) |
| 140 | + .map(msg => aliases[msg.value] || msg.value) |
| 141 | + ].join(" ") |
| 142 | + }), |
| 143 | + {} |
| 144 | + ); |
| 145 | + |
| 146 | +const getScopedClasses = messages => |
| 147 | + messages |
| 148 | + .filter(msg => msg.type === "icss-scoped") |
| 149 | + .reduce( |
| 150 | + (acc, msg) => Object.assign({}, acc, { [msg.name]: msg.value }), |
| 151 | + {} |
| 152 | + ); |
| 153 | + |
| 154 | +module.exports = postcss.plugin(plugin, () => (css, result) => { |
| 155 | + const scopedClasses = getScopedClasses(result.messages); |
| 156 | + const composedMessages = []; |
| 157 | + const composedImports = {}; |
| 158 | + |
| 159 | + let importedIndex = 0; |
| 160 | + const getImportedName = (path, name) => { |
| 161 | + if (!composedImports[path]) { |
| 162 | + composedImports[path] = {}; |
| 163 | + } |
| 164 | + if (composedImports[path][name]) { |
| 165 | + return composedImports[path][name]; |
| 166 | + } |
| 167 | + const importedName = `__composed__${name}__${importedIndex}`; |
| 168 | + composedImports[path][name] = importedName; |
| 169 | + importedIndex += 1; |
| 170 | + return importedName; |
| 171 | + }; |
| 172 | + |
| 173 | + const { icssImports, icssExports } = extractICSS(css); |
| 174 | + |
| 175 | + walkRules(css, rule => { |
| 176 | + const classes = getIdentifiers(rule, result); |
| 177 | + if (isMedia(rule)) { |
| 178 | + result.warn( |
| 179 | + "composition cannot be conditional and is not allowed in media queries", |
| 180 | + { node: rule } |
| 181 | + ); |
| 182 | + } |
| 183 | + walkDecls(rule, decl => { |
| 184 | + const { names, path } = parseComposes(decl.value); |
| 185 | + const composed = path |
| 186 | + ? names.map(name => getImportedName(path, name)) |
| 187 | + : names; |
| 188 | + composedMessages.push(...combineIntoMessages(classes, composed)); |
| 189 | + decl.remove(); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + const composedExports = convertMessagesToExports( |
| 194 | + composedMessages, |
| 195 | + scopedClasses |
| 196 | + ); |
| 197 | + const exports = Object.assign({}, icssExports, composedExports); |
| 198 | + const imports = combineImports(icssImports, composedImports); |
| 199 | + css.prepend(createICSSRules(imports, exports)); |
| 200 | + result.messages.push(...composedMessages); |
| 201 | +}); |
0 commit comments