/
index.js
325 lines (273 loc) · 7.86 KB
/
index.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import {
usePostCSSPlugin,
processCSSFactory,
processCSSWithPresetFactory,
} from '../../../../util/testHelpers';
import plugin from '..';
const { passthroughCSS, processCSS } = processCSSFactory(plugin);
const {
processCSS: withDefaultPreset,
passthroughCSS: passthroughDefault,
} = processCSSWithPresetFactory('default');
test(
'should minify lowercase color values',
withDefaultPreset('h1{color:yellow}', 'h1{color:#ff0}')
);
test(
'should minify uppercase color values',
withDefaultPreset('h1{COLOR:YELLOW}', 'h1{COLOR:#ff0}')
);
test(
'should minify color values (2)',
withDefaultPreset(
'h1{box-shadow:0 1px 3px rgba(255, 230, 220, 0.5)}',
'h1{box-shadow:0 1px 3px rgba(255,230,220,.5)}'
)
);
test(
'should minify color values (3)',
withDefaultPreset(
'h1{background:hsla(134, 50%, 50%, 1)}',
'h1{background:#40bf5e}'
)
);
test(
'should minify color values (4)',
withDefaultPreset(
'h1{text-shadow:1px 1px 2px #000000}',
'h1{text-shadow:1px 1px 2px #000}'
)
);
test(
'should minify color values (5)',
withDefaultPreset(
'h1{text-shadow:1px 1px 2px rgb(255, 255, 255)}',
'h1{text-shadow:1px 1px 2px #fff}'
)
);
test(
'should minify color values (6)',
withDefaultPreset(
'h1{text-shadow:1px 1px 2px hsl(0,0%,100%)}',
'h1{text-shadow:1px 1px 2px #fff}'
)
);
test(
'should minify color values (7)',
withDefaultPreset(
'h1{background:HSLA(134, 50%, 50%, 1)}',
'h1{background:#40bf5e}'
)
);
test(
'should minify color values (8)',
withDefaultPreset('h1{background:#FFFFFF}', 'h1{background:#fff}')
);
test(
'should minify color values (9)',
withDefaultPreset('h1{background:#F0FFFF}', 'h1{background:azure}')
);
test(
'should minify color values (10)',
withDefaultPreset(
'h1{text-shadow: 1px 1px 1px #F0FFFF, 1px 1px 1px #F0FFFF}',
'h1{text-shadow:1px 1px 1px azure,1px 1px 1px azure}'
)
);
test(
'should minify color values (11)',
processCSS(
'h1{background:#F0FFFF;background:#F0FFFF}',
'h1{background:azure;background:azure}'
)
);
test(
'should minify color values in background gradients',
processCSS(
'h1{background:linear-gradient( #ff0000,yellow )}',
'h1{background:linear-gradient( red,#ff0 )}'
)
);
test(
'should minify color values in background gradients (preset)',
withDefaultPreset(
'h1{background:linear-gradient( #ff0000,yellow )}',
'h1{background:linear-gradient(red,#ff0)}'
)
);
test(
'should minify color values in background gradients (2)',
processCSS(
'h1{background:linear-gradient(yellow, orange), linear-gradient(black, rgba(255, 255, 255, 0))}',
'h1{background:linear-gradient(#ff0, orange), linear-gradient(#000, hsla(0, 0%, 100%, 0))}'
)
);
test(
'should minify color values in background gradients (2) (preset)',
withDefaultPreset(
'h1{background:linear-gradient(yellow, orange), linear-gradient(black, rgba(255, 255, 255, 0))}',
'h1{background:linear-gradient(#ff0,orange),linear-gradient(#000,hsla(0,0%,100%,0))}'
)
);
test(
'should minify color values in background gradients (3)',
processCSS(
'h1{background:linear-gradient(0deg, yellow, black 40%, red)}',
'h1{background:linear-gradient(0deg, #ff0, #000 40%, red)}'
)
);
test(
'should minify color values in background gradients (3) (preset)',
withDefaultPreset(
'h1{background:linear-gradient(0deg, yellow, black 40%, red)}',
'h1{background:linear-gradient(0deg,#ff0,#000 40%,red)}'
)
);
test(
'should not minify in font properties',
passthroughCSS('h1{font-family:black}')
);
test(
'should make an exception for webkit tap highlight color (issue 1)',
passthroughCSS('h1{-webkit-tap-highlight-color:rgba(0,0,0,0)}')
);
test(
'should not crash on transparent in webkit tap highlight color',
passthroughCSS('h1{-webkit-tap-highlight-color:transparent}')
);
test(
'should not crash on inherit in webkit tap highlight color',
passthroughCSS('h1{-webkit-tap-highlight-color:inherit}')
);
test(
'should not minify in lowercase filter properties',
passthroughDefault(
'h1{filter:progid:DXImageTransform.Microsoft.gradient(startColorstr= #000000,endColorstr= #ffffff)}'
)
);
test(
'should not minify in uppercase filter properties',
passthroughDefault(
'h1{FILTER:progid:DXImageTransform.Microsoft.gradient(startColorstr= #000000,endColorstr= #ffffff)}'
)
);
test(
'should minify color stops',
processCSS(
'h1{background-image:-webkit-gradient(linear,50% 0%,50% 100%,color-stop(1px, #fbfbfb),color-stop(1px, #ffffff),color-stop(2px, #ffffff),color-stop(2px, #fbfbfb),color-stop(100%, #ececec))}',
'h1{background-image:-webkit-gradient(linear,50% 0%,50% 100%,color-stop(1px, #fbfbfb),color-stop(1px, #fff),color-stop(2px, #fff),color-stop(2px, #fbfbfb),color-stop(100%, #ececec))}'
)
);
test(
'should not minify in lowercase calc values',
passthroughCSS('h1{width:calc(100vw / 2 - 6px + 0)}')
);
test(
'should not minify in uppercase calc values',
passthroughCSS('h1{width:CALC(100vw / 2 - 6px + 0)}')
);
test(
'should minify hex colors without keywords',
processCSS(
'h1{background:linear-gradient(#ffffff,#999999) no-repeat;}',
'h1{background:linear-gradient(#fff,#999) no-repeat;}'
)
);
test(
'should not mangle percentage based rgba values',
processCSS(
'h1{color:rgba(50%,50%,50%,0.5)}',
'h1{color:hsla(0, 0%, 50%, 0.5)}'
)
);
test(
'should convert percentage based rgba values',
processCSS('h1{color:rgb(100%,100%,100%)}', 'h1{color:#fff}')
);
test(
'should handle errored cases',
passthroughCSS('h1{color:rgb(50%, 23, 54)}')
);
test(
'should add extra spaces when converting rgb',
processCSS(
'h1{background:linear-gradient(rgb(50, 50, 50)0%,blue 100%)}',
'h1{background:linear-gradient(#323232 0%,#00f 100%)}'
)
);
test(
'should add extra spaces when converting rgb (2)',
processCSS(
'h1{background:linear-gradient(rgba(0,0,0,0)0%, blue 100%)}',
'h1{background:linear-gradient(transparent 0%, #00f 100%)}'
)
);
test(
'should add extra spaces when converting rgb (3)',
processCSS(
'h1{background:rgb(1,2,3)url(bar.png)}',
'h1{background:#010203 url(bar.png)}'
)
);
test(
'should save extra spaces when converting hex',
withDefaultPreset(
'h1{background:#F0FFFF url(bar.png)}',
'h1{background:azure url(bar.png)}'
)
);
test(
'should bail on the "composes" property',
passthroughDefault('h1{composes:black from "styles"}')
);
test('should not mangle empty strings', passthroughDefault('h1{content:""}'));
test(
'should passthrough css variables',
passthroughDefault('h1{color:var(--foo)}')
);
test(
'should passthrough css variables #2',
passthroughDefault('h1{color:var(--foo) var(--bar)}')
);
test(
'should passthrough css variables #3',
passthroughDefault('h1{color:rgb(var(--foo),255,255)}')
);
test(
'should passthrough css variables #4',
passthroughDefault('h1{color:rgb(255,var(--bar),255)}')
);
test(
'should passthrough css variables #5',
passthroughDefault('h1{color:rgb(255,255,var(--baz))}')
);
test(
'should passthrough css variables #6',
passthroughDefault('h1{color:rgb(var(--foo))}')
);
test(
'should passthrough env function',
passthroughDefault('h1{color:rgb(env(foo))}')
);
test('should passthrough broken syntax', passthroughCSS('h1{color:}'));
test(
'should not convert this specific rgba value to "transparent" (old IE)',
passthroughCSS('h1{color:rgba(0, 0, 0, 0)}', { env: 'ie8' })
);
test('should use the postcss plugin api', usePostCSSPlugin(plugin()));
test(
'should not mangle colours in the content property',
passthroughCSS('h2:before{content:"black"}')
);
test(
'should not attempt to convert z-index',
passthroughCSS('h1{z-index:999}')
);
test(
'should not attempt to convert variables',
passthroughCSS(':root{--some-color: 200 255 0}')
);
test(
'should respect CSS variables',
passthroughCSS('div{background-color:rgba(51,153,255,var(--tw-bg-opacity))}')
);