[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 | const OVERRIDABLE_RULES = ['keyframes', 'counter-style'];
|
---|
| 8 | const SCOPE_RULES = ['media', 'supports'];
|
---|
| 9 |
|
---|
| 10 | function vendorUnprefixed(prop) {
|
---|
| 11 | return prop.replace(/^-\w+-/, '');
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | function isOverridable(name) {
|
---|
| 15 | return ~OVERRIDABLE_RULES.indexOf(vendorUnprefixed(name.toLowerCase()));
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | function isScope(name) {
|
---|
| 19 | return ~SCOPE_RULES.indexOf(vendorUnprefixed(name.toLowerCase()));
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | function getScope(node) {
|
---|
| 23 | let current = node.parent;
|
---|
| 24 | const chain = [node.name.toLowerCase(), node.params];
|
---|
| 25 |
|
---|
| 26 | do {
|
---|
| 27 | if (current.type === 'atrule' && isScope(current.name)) {
|
---|
| 28 | chain.unshift(current.name + ' ' + current.params);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | current = current.parent;
|
---|
| 32 | } while (current);
|
---|
| 33 |
|
---|
| 34 | return chain.join('|');
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function pluginCreator() {
|
---|
| 38 | return {
|
---|
| 39 | postcssPlugin: 'postcss-discard-overridden',
|
---|
| 40 |
|
---|
| 41 | prepare() {
|
---|
| 42 | const cache = {};
|
---|
| 43 | const rules = [];
|
---|
| 44 | return {
|
---|
| 45 | OnceExit(css) {
|
---|
| 46 | css.walkAtRules(node => {
|
---|
| 47 | if (isOverridable(node.name)) {
|
---|
| 48 | const scope = getScope(node);
|
---|
| 49 | cache[scope] = node;
|
---|
| 50 | rules.push({
|
---|
| 51 | node,
|
---|
| 52 | scope
|
---|
| 53 | });
|
---|
| 54 | }
|
---|
| 55 | });
|
---|
| 56 | rules.forEach(rule => {
|
---|
| 57 | if (cache[rule.scope] !== rule.node) {
|
---|
| 58 | rule.node.remove();
|
---|
| 59 | }
|
---|
| 60 | });
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | };
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | };
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | pluginCreator.postcss = true;
|
---|
| 70 | var _default = pluginCreator;
|
---|
| 71 | exports.default = _default;
|
---|
| 72 | module.exports = exports.default; |
---|