You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

4 years ago
  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'];