From 43b2882739e7f9c6380c994393e1e7a295f1f997 Mon Sep 17 00:00:00 2001 From: Romain Menke Date: Wed, 20 Sep 2023 18:26:42 +0200 Subject: [PATCH] fmt --- build/postcss-combine-selectors.cjs | 158 ++++++++++++++-------------- postcss.config.cjs | 2 +- 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/build/postcss-combine-selectors.cjs b/build/postcss-combine-selectors.cjs index 47218978..0fa3cbc0 100644 --- a/build/postcss-combine-selectors.cjs +++ b/build/postcss-combine-selectors.cjs @@ -1,88 +1,88 @@ const creator = () => { - return { - postcssPlugin: 'postcss-combine-selectors', - Once(root) { - const rulesToCombine = new Map(); - - root.walkRules(rule => { - if (isKeyframesRule(rule)) { - return; - } - - const key = ruleKey(rule); - const existing = rulesToCombine.get(key); - - // Existing group: - // - add rule to the group - if (existing) { - existing.rules.push(rule); - return; - } - - // New group: - // - first rule is the one we're going to combine into - // - create an empty slice for other rules to be added to - rulesToCombine.set(key, { - first: rule, - rules: [] - }); - }); - - // Iterate over all groups - for (const { first, rules } of rulesToCombine.values()) { - // If there was only one rule for a given group, there's nothing to combine - if (rules.length === 0) { - continue; - } - - // Append all contents of all subsequent rules to the first rule - for (const rule of rules) { - rule.each((child) => { - child.remove(); - first.append(child); - }) - - // Remove the now-empty rule - rule.remove(); - } - } - }, - }; -}; + return { + postcssPlugin: 'postcss-combine-selectors', + Once(root) { + const rulesToCombine = new Map() + + root.walkRules(rule => { + if (isKeyframesRule(rule)) { + return + } + + const key = ruleKey(rule) + const existing = rulesToCombine.get(key) + + // Existing group: + // - add rule to the group + if (existing) { + existing.rules.push(rule) + return + } + + // New group: + // - first rule is the one we're going to combine into + // - create an empty slice for other rules to be added to + rulesToCombine.set(key, { + first: rule, + rules: [] + }) + }) + + // Iterate over all groups + for (const { first, rules } of rulesToCombine.values()) { + // If there was only one rule for a given group, there's nothing to combine + if (rules.length === 0) { + continue + } + + // Append all contents of all subsequent rules to the first rule + for (const rule of rules) { + rule.each((child) => { + child.remove() + first.append(child) + }) + + // Remove the now-empty rule + rule.remove() + } + } + }, + } +} /** - * Construct a key that is specific to the AST ancestry of the rule. - * Only rules with the same key can be combined. - * - * @param {import('postcss').Rule} rule - * @returns {string} - */ +* Construct a key that is specific to the AST ancestry of the rule. +* Only rules with the same key can be combined. +* +* @param {import('postcss').Rule} rule +* @returns {string} +*/ function ruleKey(rule) { - let key = `[rule ${rule.selector}]`; - - let ancestor = rule.parent; - while (ancestor) { - if (ancestor.type === 'atrule') { - key = `[${ancestor.name} ${ancestor.params}]${key}`; - } else if (ancestor.type === 'rule') { - key = `[rule ${ancestor.selector}]${key}`; - } else if (ancestor.type === 'root') { - break; - } - - ancestor = ancestor.parent; - } - - return key; + let key = `[rule ${rule.selector}]` + + let ancestor = rule.parent + while (ancestor) { + if (ancestor.type === 'atrule') { + key = `[${ancestor.name} ${ancestor.params}]${key}` + } else if (ancestor.type === 'rule') { + key = `[rule ${ancestor.selector}]${key}` + } else if (ancestor.type === 'root') { + break + } + + ancestor = ancestor.parent + } + + return key } function isKeyframesRule(rule) { - if (rule.parent?.type === 'atrule' && rule.parent.name === 'keyframes') { - return true; - } - - return false; + if (rule.parent?.type === 'atrule' && rule.parent.name === 'keyframes') { + return true + } + + return false } -module.exports = creator; -module.exports.postcss = true; +module.exports = creator +module.exports.postcss = true diff --git a/postcss.config.cjs b/postcss.config.cjs index 6db7ba91..5d258721 100644 --- a/postcss.config.cjs +++ b/postcss.config.cjs @@ -1,7 +1,7 @@ const postcssPresetEnv = require('postcss-preset-env') const postcssImport = require('postcss-import') const cssnano = require('cssnano') -const combineSelectors = require('./build/postcss-combine-selectors.cjs') +const combineSelectors = require('./build/postcss-combine-selectors.cjs') const lib = process.env.npm_lifecycle_event