1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
|
9 |
|
10 | var _cssnanoUtils = require("cssnano-utils");
|
11 |
|
12 | var _isColorStop = _interopRequireDefault(require("./isColorStop.js"));
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
17 |
|
18 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
19 |
|
20 | const angles = {
|
21 | top: '0deg',
|
22 | right: '90deg',
|
23 | bottom: '180deg',
|
24 | left: '270deg'
|
25 | };
|
26 |
|
27 | function isLessThan(a, b) {
|
28 | return a.unit.toLowerCase() === b.unit.toLowerCase() && parseFloat(a.number) >= parseFloat(b.number);
|
29 | }
|
30 |
|
31 | function optimise(decl) {
|
32 | const value = decl.value;
|
33 |
|
34 | if (!value) {
|
35 | return;
|
36 | }
|
37 |
|
38 | const normalizedValue = value.toLowerCase();
|
39 |
|
40 | if (normalizedValue.includes('var(') || normalizedValue.includes('env(')) {
|
41 | return;
|
42 | }
|
43 |
|
44 | if (!normalizedValue.includes('gradient')) {
|
45 | return;
|
46 | }
|
47 |
|
48 | decl.value = (0, _postcssValueParser.default)(value).walk(node => {
|
49 | if (node.type !== 'function' || !node.nodes.length) {
|
50 | return false;
|
51 | }
|
52 |
|
53 | const lowerCasedValue = node.value.toLowerCase();
|
54 |
|
55 | if (lowerCasedValue === 'linear-gradient' || lowerCasedValue === 'repeating-linear-gradient' || lowerCasedValue === '-webkit-linear-gradient' || lowerCasedValue === '-webkit-repeating-linear-gradient') {
|
56 | let args = (0, _cssnanoUtils.getArguments)(node);
|
57 |
|
58 | if (node.nodes[0].value.toLowerCase() === 'to' && args[0].length === 3) {
|
59 | node.nodes = node.nodes.slice(2);
|
60 | node.nodes[0].value = angles[node.nodes[0].value.toLowerCase()];
|
61 | }
|
62 |
|
63 | let lastStop = null;
|
64 | args.forEach((arg, index) => {
|
65 | if (!arg[2]) {
|
66 | return;
|
67 | }
|
68 |
|
69 | let isFinalStop = index === args.length - 1;
|
70 | let thisStop = (0, _postcssValueParser.unit)(arg[2].value);
|
71 |
|
72 | if (lastStop === null) {
|
73 | lastStop = thisStop;
|
74 |
|
75 | if (!isFinalStop && lastStop && lastStop.number === '0' && lastStop.unit.toLowerCase() !== 'deg') {
|
76 | arg[1].value = arg[2].value = '';
|
77 | }
|
78 |
|
79 | return;
|
80 | }
|
81 |
|
82 | if (lastStop && thisStop && isLessThan(lastStop, thisStop)) {
|
83 | arg[2].value = 0;
|
84 | }
|
85 |
|
86 | lastStop = thisStop;
|
87 |
|
88 | if (isFinalStop && arg[2].value === '100%') {
|
89 | arg[1].value = arg[2].value = '';
|
90 | }
|
91 | });
|
92 | return false;
|
93 | }
|
94 |
|
95 | if (lowerCasedValue === 'radial-gradient' || lowerCasedValue === 'repeating-radial-gradient') {
|
96 | let args = (0, _cssnanoUtils.getArguments)(node);
|
97 | let lastStop;
|
98 | const hasAt = args[0].find(n => n.value.toLowerCase() === 'at');
|
99 | args.forEach((arg, index) => {
|
100 | if (!arg[2] || !index && hasAt) {
|
101 | return;
|
102 | }
|
103 |
|
104 | let thisStop = (0, _postcssValueParser.unit)(arg[2].value);
|
105 |
|
106 | if (!lastStop) {
|
107 | lastStop = thisStop;
|
108 | return;
|
109 | }
|
110 |
|
111 | if (lastStop && thisStop && isLessThan(lastStop, thisStop)) {
|
112 | arg[2].value = 0;
|
113 | }
|
114 |
|
115 | lastStop = thisStop;
|
116 | });
|
117 | return false;
|
118 | }
|
119 |
|
120 | if (lowerCasedValue === '-webkit-radial-gradient' || lowerCasedValue === '-webkit-repeating-radial-gradient') {
|
121 | let args = (0, _cssnanoUtils.getArguments)(node);
|
122 | let lastStop;
|
123 | args.forEach(arg => {
|
124 | let color;
|
125 | let stop;
|
126 |
|
127 | if (arg[2] !== undefined) {
|
128 | if (arg[0].type === 'function') {
|
129 | color = `${arg[0].value}(${(0, _postcssValueParser.stringify)(arg[0].nodes)})`;
|
130 | } else {
|
131 | color = arg[0].value;
|
132 | }
|
133 |
|
134 | if (arg[2].type === 'function') {
|
135 | stop = `${arg[2].value}(${(0, _postcssValueParser.stringify)(arg[2].nodes)})`;
|
136 | } else {
|
137 | stop = arg[2].value;
|
138 | }
|
139 | } else {
|
140 | if (arg[0].type === 'function') {
|
141 | color = `${arg[0].value}(${(0, _postcssValueParser.stringify)(arg[0].nodes)})`;
|
142 | }
|
143 |
|
144 | color = arg[0].value;
|
145 | }
|
146 |
|
147 | color = color.toLowerCase();
|
148 | const colorStop = stop || stop === 0 ? (0, _isColorStop.default)(color, stop.toLowerCase()) : (0, _isColorStop.default)(color);
|
149 |
|
150 | if (!colorStop || !arg[2]) {
|
151 | return;
|
152 | }
|
153 |
|
154 | let thisStop = (0, _postcssValueParser.unit)(arg[2].value);
|
155 |
|
156 | if (!lastStop) {
|
157 | lastStop = thisStop;
|
158 | return;
|
159 | }
|
160 |
|
161 | if (lastStop && thisStop && isLessThan(lastStop, thisStop)) {
|
162 | arg[2].value = 0;
|
163 | }
|
164 |
|
165 | lastStop = thisStop;
|
166 | });
|
167 | return false;
|
168 | }
|
169 | }).toString();
|
170 | }
|
171 |
|
172 | function pluginCreator() {
|
173 | return {
|
174 | postcssPlugin: 'postcss-minify-gradients',
|
175 |
|
176 | OnceExit(css) {
|
177 | css.walkDecls(optimise);
|
178 | }
|
179 |
|
180 | };
|
181 | }
|
182 |
|
183 | pluginCreator.postcss = true;
|
184 | var _default = pluginCreator;
|
185 | exports.default = _default;
|
186 | module.exports = exports.default; |
\ | No newline at end of file |