Blame view

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