forked from ben-eb/css-minifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
276 lines (263 loc) · 6.7 KB
/
test.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict';
const test = require('tape');
const minifiers = require('./');
const charset = '@charset "utf-8";';
const utf16Encoding = '\ufeff';
const h1Long = 'h1::before,';
const h1Bad = 'h1:before,';
const h1 = 'h1:before{';
const marginOriginal = 'margin: 10px 20px 10px 20px;';
const margin = 'margin:10px 20px;';
const gradientOriginal = 'background: linear-gradient(to bottom, #ffe500 0%, #ffe500 50%, #121 50%, #121 100%);';
const gradient = 'background:linear-gradient(to bottom,#ffe500 0%,#ffe500 50%,#121 50%,#121 100%);';
const gradient0 = 'background:linear-gradient(to bottom,#ffe500 0,#ffe500 50%,#121 50%,#121 100%);';
const gradientDeg = 'background:linear-gradient(180deg,#ffe500,#ffe500 50%,#121 0,#121);';
const gradientHex = 'background:linear-gradient(#ffe500 0% 50%,#121 50% 100%);';
const bgPostionPercent = 'background-position:100% 100%;';
const bgPositionWords = 'background-position:bottom right;';
const colorOriginal = 'color: #ff0000;';
const colorRed = 'color:red;';
const colorHex = 'color:#f00;';
const originalFontWeight = 'font-weight: 400;';
const fontWeight = 'font-weight:400;';
const originalQuotes = 'quotes: \'«\' "»";';
const quotesNoSpace = 'quotes:\'«\'"»";';
const quotes = 'quotes:"«" "»";';
const minWidth0 = 'min-width:0;';
const minWidthInit = 'min-width:initial;';
const closeQuote = '}';
const cssInput = [
'/* normalize selectors */',
h1Long + ' ' + h1.replace('{', ' {'),
' /* reduce shorthand even further */',
' ' + marginOriginal,
' /* reduce color values */',
' ' + colorOriginal,
' /* remove duplicated properties */',
' ' + originalFontWeight,
' ' + originalFontWeight,
' /* reduce position values */',
' ' + bgPositionWords.replace(':', ': '),
' /* normalize wrapping quotes */',
' ' + originalQuotes,
' /* reduce gradient parameters */',
' ' + gradientOriginal,
' /* replace initial values */',
' ' + minWidthInit.replace(':', ': '),
'}',
'/* correct invalid placement */',
charset
].join('\n');
const smallestPossibleWhileLossless = [
charset,
h1,
margin,
colorRed,
fontWeight,
bgPostionPercent,
quotesNoSpace,
gradientHex
].join('');
const outputMap = {
// 186
crass: [
h1,
gradientDeg,
bgPositionWords,
colorRed,
fontWeight,
margin,
minWidthInit,
quotes.replace(';', ''),
closeQuote
],
// 194
'css-nano': [
charset,
h1,
margin,
colorRed,
fontWeight,
bgPostionPercent,
quotes,
gradientDeg,
minWidth0.replace(';', ''),
closeQuote
],
// 209
csso: [
h1Long,
h1,
margin,
colorRed,
fontWeight,
bgPositionWords,
originalQuotes.replace(': ', ':'),
gradient0,
minWidthInit.replace(';', ''),
closeQuote
],
// 211
sqwish: [
h1Long,
h1,
minWidthInit,
gradient,
originalQuotes.replace(': ', ':'),
bgPositionWords,
fontWeight,
colorHex,
margin.replace(';', ''),
closeQuote
],
// 214
csswring: [
h1,
margin,
colorRed,
fontWeight,
bgPositionWords,
originalQuotes.replace(': ', ':'),
gradient0,
minWidthInit.replace(';', ''),
closeQuote,
charset.replace(';', '')
],
// 153
lightningcss: [
h1Bad,
h1,
colorRed,
quotes,
minWidthInit,
gradientHex,
margin,
fontWeight.replace(';', ''),
closeQuote
],
// 241
sass: [
utf16Encoding,
h1Long,
h1,
marginOriginal.replace(': ', ':'),
colorRed,
fontWeight,
fontWeight,
bgPositionWords,
quotes,
gradientOriginal.replace(': ', ':'),
minWidthInit.replace(';', ''),
closeQuote
],
// 244
'css-condense': [
charset,
h1Long,
h1,
gradient,
bgPositionWords,
colorHex,
fontWeight,
fontWeight,
margin,
minWidthInit,
originalQuotes.replace(': ', ':').replace(';', ''),
closeQuote
],
// 252
'clean-css': [
charset,
h1Long,
h1,
marginOriginal.replace(': ', ':'),
colorRed,
fontWeight,
fontWeight,
bgPositionWords,
originalQuotes.replace(': ', ':'),
gradient0,
minWidthInit.replace(';', ''),
closeQuote
],
// 252
ncss: [
h1Long,
h1,
marginOriginal.replace(': ', ':'),
colorHex,
fontWeight,
fontWeight,
bgPositionWords,
quotesNoSpace,
gradient0,
minWidthInit.replace(';', ''),
closeQuote,
charset
],
// 253
ycssmin: [
charset,
h1Long,
h1,
marginOriginal.replace(': ', ':'),
colorHex,
fontWeight,
fontWeight,
bgPositionWords,
originalQuotes.replace(': ', ':'),
gradient0,
minWidthInit.replace(';', ''),
closeQuote
]
};
const longestNameSize = Object.keys(outputMap).sort(function (a, b) {
return b.length - a.length;
})[0].length;
// Check how close they come to perfection
console.log('The smallest possible lossless size is ' + smallestPossibleWhileLossless.length + '.');
Object.keys(outputMap).forEach(function (key) {
const outputSize = outputMap[key].join('').length;
const smallest = smallestPossibleWhileLossless.length;
const message = [
key.padEnd(longestNameSize, ' ') + ' has a size of (' + outputSize + ') which is '
];
if (outputSize < smallest) {
message.push('smaller than is possible losslessly');
} else if (outputSize > smallest) {
const percent = ((((smallest / outputSize) * 100) - 100) * -1).toFixed(2) + '%';
message.push(percent + ' larger than perfection');
} else {
message.push('is the same as the perfect output (though it may not have produced the perfect output, just a matching size)');
}
console.log(message.join(''));
});
test('integration tests', function (t) {
t.plan(Object.keys(minifiers).length);
Object.keys(minifiers).forEach(function (minifier) {
Promise.resolve(minifiers[minifier](cssInput)).then(function (css) {
t.equal(css, outputMap[minifier].join(''), 'Output length is ' + css.length + ' when using ' + minifier);
}, function (err) {
t.fail('Failure from engine "' + minifier + '": ' + String(err));
});
});
});
test('benchmark tests', function (t) {
t.plan(Object.keys(minifiers).length);
Object.keys(minifiers).forEach(function (m) {
let minifier = minifiers[m];
Promise.resolve(minifier.bench(cssInput)).then(function (time) {
t.ok(time, minifier + ' took ' + time);
}, function (err) {
t.fail('Failure from engine "' + minifier + '": ' + String(err));
});
});
});
test('metadata tests', function (t) {
t.plan(Object.keys(minifiers).length * 2);
Object.keys(minifiers).forEach(function (m) {
let minifier = minifiers[m];
t.ok(minifier.version, minifier + ' version ' + minifier.version);
t.ok('' + minifier, minifier + ' should be cast to string');
});
});