| 1 | 'use strict';
|
|---|
| 2 | const OVERRIDABLE_RULES = new Set(['keyframes', 'counter-style']);
|
|---|
| 3 | const SCOPE_RULES = new Set(['media', 'supports']);
|
|---|
| 4 |
|
|---|
| 5 | /**
|
|---|
| 6 | * @param {string} prop
|
|---|
| 7 | * @return {string}
|
|---|
| 8 | */
|
|---|
| 9 | function vendorUnprefixed(prop) {
|
|---|
| 10 | return prop.replace(/^-\w+-/, '');
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * @param {string} name
|
|---|
| 15 | * @return {boolean}
|
|---|
| 16 | */
|
|---|
| 17 | function isOverridable(name) {
|
|---|
| 18 | return OVERRIDABLE_RULES.has(vendorUnprefixed(name.toLowerCase()));
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * @param {string} name
|
|---|
| 23 | * @return {boolean}
|
|---|
| 24 | */
|
|---|
| 25 | function isScope(name) {
|
|---|
| 26 | return SCOPE_RULES.has(vendorUnprefixed(name.toLowerCase()));
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * @param {import('postcss').AtRule} node
|
|---|
| 31 | * @return {string}
|
|---|
| 32 | */
|
|---|
| 33 | function getScope(node) {
|
|---|
| 34 | /** @type {import('postcss').Container<import('postcss').ChildNode> | import('postcss').Document | undefined} */
|
|---|
| 35 | let current = node.parent;
|
|---|
| 36 |
|
|---|
| 37 | const chain = [node.name.toLowerCase(), node.params];
|
|---|
| 38 |
|
|---|
| 39 | while (current) {
|
|---|
| 40 | if (
|
|---|
| 41 | current.type === 'atrule' &&
|
|---|
| 42 | isScope(/** @type import('postcss').AtRule */ (current).name)
|
|---|
| 43 | ) {
|
|---|
| 44 | chain.unshift(
|
|---|
| 45 | /** @type import('postcss').AtRule */ (current).name +
|
|---|
| 46 | ' ' +
|
|---|
| 47 | /** @type import('postcss').AtRule */ (current).params
|
|---|
| 48 | );
|
|---|
| 49 | }
|
|---|
| 50 | current = current.parent;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return chain.join('|');
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * @type {import('postcss').PluginCreator<void>}
|
|---|
| 58 | * @return {import('postcss').Plugin}
|
|---|
| 59 | */
|
|---|
| 60 | function pluginCreator() {
|
|---|
| 61 | return {
|
|---|
| 62 | postcssPlugin: 'postcss-discard-overridden',
|
|---|
| 63 | prepare() {
|
|---|
| 64 | const cache = new Map();
|
|---|
| 65 | /** @type {{node: import('postcss').AtRule, scope: string}[]} */
|
|---|
| 66 | const rules = [];
|
|---|
| 67 |
|
|---|
| 68 | return {
|
|---|
| 69 | OnceExit(css) {
|
|---|
| 70 | css.walkAtRules((node) => {
|
|---|
| 71 | if (isOverridable(node.name)) {
|
|---|
| 72 | const scope = getScope(node);
|
|---|
| 73 |
|
|---|
| 74 | cache.set(scope, node);
|
|---|
| 75 | rules.push({
|
|---|
| 76 | node,
|
|---|
| 77 | scope,
|
|---|
| 78 | });
|
|---|
| 79 | }
|
|---|
| 80 | });
|
|---|
| 81 |
|
|---|
| 82 | rules.forEach((rule) => {
|
|---|
| 83 | if (cache.get(rule.scope) !== rule.node) {
|
|---|
| 84 | rule.node.remove();
|
|---|
| 85 | }
|
|---|
| 86 | });
|
|---|
| 87 | },
|
|---|
| 88 | };
|
|---|
| 89 | },
|
|---|
| 90 | };
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | pluginCreator.postcss = true;
|
|---|
| 94 | module.exports = pluginCreator;
|
|---|