index.js
3.5 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
'use strict';
const browserslist = require('browserslist');
const valueParser = require('postcss-value-parser');
const { getArguments } = require('cssnano-utils');
/**
* Return the greatest common divisor
* of two numbers.
*
* @param {number} a
* @param {number} b
* @return {number}
*/
function gcd(a, b) {
return b ? gcd(b, a % b) : a;
}
/**
* @param {number} a
* @param {number} b
* @return {[number, number]}
*/
function aspectRatio(a, b) {
const divisor = gcd(a, b);
return [a / divisor, b / divisor];
}
/**
* @param {valueParser.Node[]} args
* @return {string}
*/
function split(args) {
return args.map((arg) => valueParser.stringify(arg)).join('');
}
/**
* @param {valueParser.Node} node
* @return {void}
*/
function removeNode(node) {
node.value = '';
node.type = 'word';
}
/**
* @param {unknown[]} items
* @return {string}
*/
function sortAndDedupe(items) {
const a = [...new Set(items)];
a.sort();
return a.join();
}
/**
* @param {boolean} legacy
* @param {import('postcss').AtRule} rule
* @return {void}
*/
function transform(legacy, rule) {
const ruleName = rule.name.toLowerCase();
// We should re-arrange parameters only for `@media` and `@supports` at-rules
if (!rule.params || !['media', 'supports'].includes(ruleName)) {
return;
}
const params = valueParser(rule.params);
params.walk((node, index) => {
if (node.type === 'div') {
node.before = node.after = '';
} else if (node.type === 'function') {
node.before = '';
if (
node.nodes[0] &&
node.nodes[0].type === 'word' &&
node.nodes[0].value.startsWith('--') &&
node.nodes[2] === undefined
) {
node.after = ' ';
} else {
node.after = '';
}
if (
node.nodes[4] &&
node.nodes[0].value.toLowerCase().indexOf('-aspect-ratio') === 3
) {
const [a, b] = aspectRatio(
Number(node.nodes[2].value),
Number(node.nodes[4].value)
);
node.nodes[2].value = a.toString();
node.nodes[4].value = b.toString();
}
} else if (node.type === 'space') {
node.value = ' ';
} else {
const prevWord = params.nodes[index - 2];
if (
node.value.toLowerCase() === 'all' &&
rule.name.toLowerCase() === 'media' &&
!prevWord
) {
const nextWord = params.nodes[index + 2];
if (!legacy || nextWord) {
removeNode(node);
}
if (nextWord && nextWord.value.toLowerCase() === 'and') {
const nextSpace = params.nodes[index + 1];
const secondSpace = params.nodes[index + 3];
removeNode(nextWord);
removeNode(nextSpace);
removeNode(secondSpace);
}
}
}
}, true);
rule.params = sortAndDedupe(getArguments(params).map(split));
if (!rule.params.length) {
rule.raws.afterName = '';
}
}
const allBugBrowers = new Set(['ie 10', 'ie 11']);
/**
* @type {import('postcss').PluginCreator<browserslist.Options>}
* @param {browserslist.Options} options
* @return {import('postcss').Plugin}
*/
function pluginCreator(options = {}) {
const browsers = browserslist(null, {
stats: options.stats,
path: __dirname,
env: options.env,
});
const hasAllBug = browsers.some((browser) => allBugBrowers.has(browser));
return {
postcssPlugin: 'postcss-minify-params',
OnceExit(css) {
css.walkAtRules((rule) => transform(hasAllBug, rule));
},
};
}
pluginCreator.postcss = true;
module.exports = pluginCreator;