index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _postcss2 = _interopRequireDefault(_postcss);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. const OVERRIDABLE_RULES = ['keyframes', 'counter-style'];
  9. const SCOPE_RULES = ['media', 'supports'];
  10. function isOverridable(name) {
  11. return ~OVERRIDABLE_RULES.indexOf(_postcss2.default.vendor.unprefixed(name.toLowerCase()));
  12. }
  13. function isScope(name) {
  14. return ~SCOPE_RULES.indexOf(_postcss2.default.vendor.unprefixed(name.toLowerCase()));
  15. }
  16. function getScope(node) {
  17. let current = node.parent;
  18. const chain = [node.name.toLowerCase(), node.params];
  19. do {
  20. if (current.type === 'atrule' && isScope(current.name)) {
  21. chain.unshift(current.name + ' ' + current.params);
  22. }
  23. current = current.parent;
  24. } while (current);
  25. return chain.join('|');
  26. }
  27. exports.default = _postcss2.default.plugin('postcss-discard-overridden', () => {
  28. return css => {
  29. const cache = {};
  30. const rules = [];
  31. css.walkAtRules(node => {
  32. if (isOverridable(node.name)) {
  33. const scope = getScope(node);
  34. cache[scope] = node;
  35. rules.push({
  36. node,
  37. scope
  38. });
  39. }
  40. });
  41. rules.forEach(rule => {
  42. if (cache[rule.scope] !== rule.node) {
  43. rule.node.remove();
  44. }
  45. });
  46. };
  47. });
  48. module.exports = exports['default'];