generated from argyleink/vite-postcss-preset-env
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom plugin to combine rules (#412)
* custom plugin to combine rules * fmt * Update postcss-combine-selectors.cjs * Update postcss.config.cjs * removes the postcss plugin from package.json --------- Co-authored-by: Adam Argyle <argyle@google.com>
- Loading branch information
1 parent
0d3cd9e
commit 61c3c54
Showing
4 changed files
with
91 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const creator = () => { | ||
return { | ||
postcssPlugin: 'postcss-combine-selectors', | ||
OnceExit(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} | ||
*/ | ||
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 | ||
} | ||
|
||
function isKeyframesRule(rule) { | ||
if (rule.parent?.type === 'atrule' && rule.parent.name === 'keyframes') { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
module.exports = creator | ||
module.exports.postcss = true |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters