index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
  4. var _postcss = require('postcss');
  5. var _postcss2 = _interopRequireDefault(_postcss);
  6. var _postcssValueParser = require('postcss-value-parser');
  7. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var angles = {
  10. top: '0deg',
  11. right: '90deg',
  12. bottom: '180deg',
  13. left: '270deg'
  14. };
  15. function getArguments(node) {
  16. return node.nodes.reduce(function (list, child) {
  17. if (child.type !== 'div') {
  18. list[list.length - 1].push(child);
  19. } else {
  20. list.push([]);
  21. }
  22. return list;
  23. }, [[]]);
  24. }
  25. function isLessThan(a, b) {
  26. return a.unit === b.unit && parseFloat(a.number) >= parseFloat(b.number);
  27. }
  28. function optimise(decl) {
  29. if (!~decl.value.indexOf('gradient')) {
  30. return;
  31. }
  32. decl.value = (0, _postcssValueParser2.default)(decl.value).walk(function (node) {
  33. if (node.type !== 'function') {
  34. return false;
  35. }
  36. if (node.value === 'linear-gradient' || node.value === 'repeating-linear-gradient' || node.value === '-webkit-linear-gradient' || node.value === '-webkit-repeating-linear-gradient') {
  37. var _ret = function () {
  38. if (!node.nodes.length) {
  39. return {
  40. v: false
  41. };
  42. }
  43. var args = getArguments(node);
  44. if (node.nodes[0].value === 'to' && args[0].length === 3) {
  45. node.nodes = node.nodes.slice(2);
  46. node.nodes[0].value = angles[node.nodes[0].value];
  47. }
  48. var lastStop = null;
  49. args.forEach(function (arg, index) {
  50. if (!arg[2]) {
  51. return;
  52. }
  53. var isFinalStop = index === args.length - 1;
  54. var thisStop = (0, _postcssValueParser.unit)(arg[2].value);
  55. if (lastStop === null) {
  56. lastStop = thisStop;
  57. if (!isFinalStop && lastStop && lastStop.number === '0' && lastStop.unit !== 'deg') {
  58. arg[1].value = arg[2].value = '';
  59. }
  60. return;
  61. }
  62. if (isLessThan(lastStop, thisStop)) {
  63. arg[2].value = 0;
  64. }
  65. lastStop = thisStop;
  66. if (isFinalStop && arg[2].value === '100%') {
  67. arg[1].value = arg[2].value = '';
  68. }
  69. });
  70. return {
  71. v: false
  72. };
  73. }();
  74. if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
  75. }
  76. if (node.value === 'radial-gradient' || node.value === 'repeating-radial-gradient' || node.value === '-webkit-radial-gradient' || node.value === '-webkit-repeating-radial-gradient') {
  77. var _ret2 = function () {
  78. if (!node.nodes.length) {
  79. return {
  80. v: false
  81. };
  82. }
  83. var args = getArguments(node);
  84. var lastStop = void 0;
  85. args.forEach(function (arg) {
  86. if (!arg[2]) {
  87. return;
  88. }
  89. var thisStop = (0, _postcssValueParser.unit)(arg[2].value);
  90. if (!lastStop) {
  91. lastStop = thisStop;
  92. return;
  93. }
  94. if (isLessThan(lastStop, thisStop)) {
  95. arg[2].value = 0;
  96. }
  97. lastStop = thisStop;
  98. });
  99. return {
  100. v: false
  101. };
  102. }();
  103. if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v;
  104. }
  105. }).toString();
  106. }
  107. exports.default = _postcss2.default.plugin('postcss-minify-gradients', function () {
  108. return function (css) {
  109. return css.walkDecls(optimise);
  110. };
  111. });
  112. module.exports = exports['default'];