index.js
5.49 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _postcss = require('postcss');
var _postcss2 = _interopRequireDefault(_postcss);
var _postcssValueParser = require('postcss-value-parser');
var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
var _isColorStop = require('is-color-stop');
var _isColorStop2 = _interopRequireDefault(_isColorStop);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const angles = {
top: '0deg',
right: '90deg',
bottom: '180deg',
left: '270deg'
};
function isLessThan(a, b) {
return a.unit.toLowerCase() === b.unit.toLowerCase() && parseFloat(a.number) >= parseFloat(b.number);
}
function optimise(decl) {
const value = decl.value;
if (!~value.toLowerCase().indexOf('gradient')) {
return;
}
decl.value = (0, _postcssValueParser2.default)(value).walk(node => {
if (node.type !== 'function' || !node.nodes.length) {
return false;
}
const lowerCasedValue = node.value.toLowerCase();
if (lowerCasedValue === 'linear-gradient' || lowerCasedValue === 'repeating-linear-gradient' || lowerCasedValue === '-webkit-linear-gradient' || lowerCasedValue === '-webkit-repeating-linear-gradient') {
let args = (0, _cssnanoUtilGetArguments2.default)(node);
if (node.nodes[0].value.toLowerCase() === 'to' && args[0].length === 3) {
node.nodes = node.nodes.slice(2);
node.nodes[0].value = angles[node.nodes[0].value.toLowerCase()];
}
let lastStop = null;
args.forEach((arg, index) => {
if (!arg[2]) {
return;
}
let isFinalStop = index === args.length - 1;
let thisStop = (0, _postcssValueParser.unit)(arg[2].value);
if (lastStop === null) {
lastStop = thisStop;
if (!isFinalStop && lastStop && lastStop.number === '0' && lastStop.unit.toLowerCase() !== 'deg') {
arg[1].value = arg[2].value = '';
}
return;
}
if (lastStop && thisStop && isLessThan(lastStop, thisStop)) {
arg[2].value = 0;
}
lastStop = thisStop;
if (isFinalStop && arg[2].value === '100%') {
arg[1].value = arg[2].value = '';
}
});
return false;
}
if (lowerCasedValue === 'radial-gradient' || lowerCasedValue === 'repeating-radial-gradient') {
let args = (0, _cssnanoUtilGetArguments2.default)(node);
let lastStop;
const hasAt = args[0].find(n => n.value.toLowerCase() === 'at');
args.forEach((arg, index) => {
if (!arg[2] || !index && hasAt) {
return;
}
let thisStop = (0, _postcssValueParser.unit)(arg[2].value);
if (!lastStop) {
lastStop = thisStop;
return;
}
if (lastStop && thisStop && isLessThan(lastStop, thisStop)) {
arg[2].value = 0;
}
lastStop = thisStop;
});
return false;
}
if (lowerCasedValue === '-webkit-radial-gradient' || lowerCasedValue === '-webkit-repeating-radial-gradient') {
let args = (0, _cssnanoUtilGetArguments2.default)(node);
let lastStop;
args.forEach(arg => {
let color;
let stop;
if (arg[2] !== undefined) {
if (arg[0].type === 'function') {
color = `${arg[0].value}(${(0, _postcssValueParser.stringify)(arg[0].nodes)})`;
} else {
color = arg[0].value;
}
if (arg[2].type === 'function') {
stop = `${arg[2].value}(${(0, _postcssValueParser.stringify)(arg[2].nodes)})`;
} else {
stop = arg[2].value;
}
} else {
if (arg[0].type === 'function') {
color = `${arg[0].value}(${(0, _postcssValueParser.stringify)(arg[0].nodes)})`;
}
color = arg[0].value;
}
color = color.toLowerCase();
const colorStop = stop || stop === 0 ? (0, _isColorStop2.default)(color, stop.toLowerCase()) : (0, _isColorStop2.default)(color);
if (!colorStop || !arg[2]) {
return;
}
let thisStop = (0, _postcssValueParser.unit)(arg[2].value);
if (!lastStop) {
lastStop = thisStop;
return;
}
if (lastStop && thisStop && isLessThan(lastStop, thisStop)) {
arg[2].value = 0;
}
lastStop = thisStop;
});
return false;
}
}).toString();
}
exports.default = _postcss2.default.plugin('postcss-minify-gradients', () => {
return css => css.walkDecls(optimise);
});
module.exports = exports['default'];