This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
591 lines (459 loc) · 12.8 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
const color = require("./lib/color");
const list = require("postcss/lib/list");
const onecolor = require("onecolor");
const pkg = require("./package.json");
const postcss = require("postcss");
const re = require("./lib/regexp");
const unit = require("./lib/unit");
// Check comment is a source map annotation or not
const isSourceMapAnnotation = comment => {
if (
comment.parent.type === "root" &&
comment.parent.last === comment &&
comment.text.toLowerCase().indexOf("# sourcemappingurl=") === 0
) {
return true;
}
return false;
};
// Set quotation mark
const setQuote = quote => {
if (!quote) {
return '"';
}
return quote;
};
// Check string can unquote or not
const canUnquote = str => {
const firstChar = str.slice(0, 1);
if (re.number.test(firstChar)) {
return false;
}
const secondChar = str.slice(1, 2);
if (
firstChar === "-" &&
(secondChar === "-" || secondChar === "" || re.number.test(secondChar))
) {
return false;
}
if (re.sequenceOfIdentifiers.test(str)) {
return true;
}
return false;
};
// Unquote font family name if possible
const unquoteFontFamily = family => {
if (family.match(re.varFunction)) {
return family;
}
let newFamily = family.replace(re.quotedString, "$2");
const quote = setQuote(RegExp.$1);
if (!list.space(newFamily).every(canUnquote)) {
newFamily = `${quote}${newFamily}${quote}`;
}
return newFamily;
};
// Convert colors to HEX or `rgba()` notation
const toRGBColor = (m, leading, c) => {
const co = onecolor(c);
// Return unmodified value when `one.color` failed to parse `c`
if (!co) {
return m;
}
if (co.alpha() < 1) {
return `${leading}${co.cssa()}`;
}
return `${leading}${co.hex()} `;
};
// Convert to shortest color
const toShortestColor = (m, leading, r1, r2, g1, g2, b1, b2) => {
let c = `#${r1}${r2}${g1}${g2}${b1}${b2}`;
if (r1 === r2 && g1 === g2 && b1 === b2) {
c = `#${r1}${g1}${b1}`;
}
if (color.shortest.hasOwnProperty(c)) {
c = color.shortest[c];
}
return `${leading}${c.toLowerCase()}`;
};
// Remove unit from 0 length and 0 percentage if possible
const removeUnitOfZero = (
preserveHacks,
prop,
m,
leading,
num,
u,
position,
value
) => {
if (
prop === "flex" ||
prop === "-ms-flex" ||
prop === "-webkit-flex" ||
prop === "flex-basis" ||
prop === "-webkit-flex-basis" ||
value.indexOf("calc(") !== -1 ||
prop.startsWith("--") ||
(preserveHacks && prop === "min-width" && u === "%")
) {
return m;
}
if (unit.forZero.hasOwnProperty(u)) {
return `${leading}${num}${unit.forZero[u]}`;
}
return `${leading}${num}`;
};
// Convert to shortest time
const toShortestTime = (m, leading, n) =>
`${leading}${(parseInt(n, 10) / 100).toString().replace(/^0+/, "")}s`;
// Convert to shortest angle
const toShortestAngle = (m, leading, n, u) => {
const num = parseInt(n, 10);
if (Number.isInteger(num / 10)) {
return `${leading}${num * (360 / 400)}deg`;
}
return `${leading}${num}${u}`;
};
// Unquote inside `url()` notation if possible
const unquoteURL = (m, leading, u) => {
let url = u.replace(re.quotedString, "$2");
const quote = setQuote(RegExp.$1);
url = url.replace(re.escapedBraces, "$1");
if (re.urlNeedQuote.test(url)) {
url = `${quote}${url}${quote}`;
}
return `${leading}url(${url})`;
};
// Remove white spaces inside `calc()` notation
const removeCalcWhiteSpaces = (m, leading, calc) =>
`${leading}calc(${calc.replace(re.whiteSpacesBothEndsOfSymbol, "$1")})`;
// Wring value of declaration
const wringValue = (preserveHacks, prop, value) =>
value
.replace(re.colorFunction, toRGBColor)
.replace(re.colorHex, toShortestColor)
.replace(re.colorTransparent, "$1transparent ")
.trim()
.replace(re.whiteSpaces, " ")
.replace(re.whiteSpacesAfterSymbol, "$1")
.replace(re.whiteSpacesBeforeSymbol, "$1")
.replace(re.numberLeadingZeros, "$1$2")
.replace(re.zeroValueUnit, removeUnitOfZero.bind(null, preserveHacks, prop))
.replace(re.decimalWithZeros, "$1$2$3.$4")
.replace(re.timeEndsWithZero, toShortestTime)
.replace(re.angle, toShortestAngle)
.replace(re.freqEndsWithThreeZeros, "$1$2kHz")
.replace(re.urlFunction, unquoteURL)
.replace(re.calcFunction, removeCalcWhiteSpaces);
// Unquote attribute selector if possible
const unquoteAttributeSelector = (m, att, con, val, oq, f) => {
if (!con || !val) {
return `[${att}]`;
}
let value = val.trim().replace(re.quotedString, "$2");
const quote = setQuote(RegExp.$1);
if (!canUnquote(value)) {
value = `${quote}${value}${quote}`;
}
let flag = f;
if (!flag) {
flag = "";
}
if (flag && !value.startsWith(quote)) {
flag = ` ${flag}`;
}
return `[${att}${con}${value}${flag}]`;
};
// Remove white spaces from string
const removeWhiteSpaces = string => string.replace(re.whiteSpaces, "");
// Remove white spaces from both ends of `:not()`
const trimNegationFunction = (m, not) => `:not(${not.trim()})`;
// Remove white spaces around `>`, `+`, and `~`, but not `\>`, `\+`, and `\~`
const trimSelectorCombinator = (m, combinator, backslash) => {
if (backslash) {
return ` ${combinator} `;
}
return combinator;
};
// Wring selector of ruleset
const wringSelector = selector =>
selector
.replace(re.whiteSpaces, " ")
.replace(re.selectorAtt, unquoteAttributeSelector)
.replace(re.selectorFunctions, removeWhiteSpaces)
.replace(re.selectorNegationFunction, trimNegationFunction)
.replace(re.selectorCombinators, trimSelectorCombinator)
.replace(re.selectorPseudoElements, "$1")
.replace(re.selectorVerboseUniversal, "$1");
// Check keyframe is valid or not
const isValidKeyframe = k => {
if (k === "from" || k === "to") {
return true;
}
const keyframe = parseFloat(k);
if (!isNaN(keyframe) && keyframe >= 0 && keyframe <= 100) {
return true;
}
return false;
};
// Unique array element
const uniqueArray = array => {
const l = array.length;
const result = [];
for (let i = 0; i < l; i = i + 1) {
const value = array[i];
if (result.indexOf(value) < 0) {
result.push(value);
}
}
return result;
};
// Remove duplicate declaration
const removeDuplicateDeclaration = (decls, decl) => {
const d = `${decl.raws.before}${decl.prop}${decl.raws.between}${decl.value}`;
if (decls.hasOwnProperty(d)) {
decls[d].remove();
}
decls[d] = decl;
};
// Check required `@font-face` descriptor or not
const isRequiredFontFaceDescriptor = decl => {
const { prop } = decl;
return prop === "src" || prop === "font-family";
};
// Remove `@font-face` descriptor with default value
const removeDefaultFontFaceDescriptor = decl => {
const { prop, value } = decl;
if (
(re.descriptorFontFace.test(prop) && value === "normal") ||
(prop === "unicode-range" && re.unicodeRangeDefault.test(value)) ||
`${prop}${value}` === "font-weight400"
) {
decl.remove();
}
};
// Quote `@import` URL
const quoteImportURL = (m, quote, url) => {
const newQuote = setQuote(quote);
return `${newQuote}${url}${newQuote}`;
};
// Quote `@namespace` URL
const quoteNamespaceURL = (param, index, params) => {
if (param !== params[params.length - 1]) {
return param;
}
const newParam = param.replace(re.quotedString, "$2");
const quote = setQuote(RegExp.$1);
return `${quote}${newParam}${quote}`;
};
// Wring comment
const wringComment = (removeAllComments, comment) => {
if (
(removeAllComments || comment.text.indexOf("!") !== 0) &&
!isSourceMapAnnotation(comment)
) {
comment.remove();
return;
}
comment.raws.before = "";
};
// Wring declaration
const wringDecl = (preserveHacks, decl) => {
const { prop } = decl;
let { before, between } = decl.raws;
let { value } = decl;
if (!prop.match(re.validProp)) {
decl.remove();
return;
}
if (
!preserveHacks &&
((before && before.match(re.hackSignProp) !== null) ||
(between && between.match(re.hackPropComment) !== null))
) {
decl.remove();
return;
}
if (preserveHacks && before) {
before = before.replace(re.semicolons, "").replace(re.whiteSpaces, "");
} else {
before = "";
}
decl.raws.before = before;
if (preserveHacks && between) {
between = between.replace(re.whiteSpaces, "");
} else {
between = ":";
}
decl.raws.between = between;
if (decl.important) {
decl.raws.important = "!important";
}
if (decl.raws.value) {
decl.raws.value = decl.raws.value.raw.trim();
}
if (prop === "content") {
return;
}
if (prop === "font-family") {
decl.value = list
.comma(value)
.map(unquoteFontFamily)
.join(",");
return;
}
let values = list.comma(value);
value = values.map(wringValue.bind(null, preserveHacks, prop)).join(",");
if (re.propertyMultipleValues.test(prop)) {
values = list.space(value);
if (values.length === 4 && values[1] === values[3]) {
values.splice(3, 1);
}
if (values.length === 3 && values[0] === values[2]) {
values.splice(2, 1);
}
if (values.length === 2 && values[0] === values[1]) {
values.splice(1, 1);
}
value = values.join(" ");
}
if (prop === "font-weight") {
if (value === "normal") {
value = "400";
} else if (value === "bold") {
value = "700";
}
}
decl.value = value;
};
// Wring declaration like string
const wringDeclLike = (m, prop, value) => {
const decl = postcss.decl({
prop: prop,
value: value
});
wringDecl.call(null, false, decl);
return `(${decl.toString()})`;
};
// Wring ruleset
const wringRule = rule => {
rule.raws.before = "";
rule.raws.between = "";
rule.raws.semicolon = false;
rule.raws.after = "";
if (rule.nodes.length === 0 || rule.selector === "") {
rule.remove();
return;
}
const { parent } = rule;
let selectors = rule.selectors.map(wringSelector);
if (parent.type === "atrule" && parent.name === "keyframes") {
selectors = selectors.filter(isValidKeyframe);
if (selectors.length === 0) {
rule.remove();
return;
}
}
rule.selector = uniqueArray(selectors).join(",");
const decls = {};
rule.each(removeDuplicateDeclaration.bind(null, decls));
};
// Filter at-rule
const filterAtRule = (flag, rule) => {
const { name, type } = rule;
if (type === "comment") {
return;
}
if (type !== "atrule" || (name !== "charset" && name !== "import")) {
flag.filter = true;
return;
}
if (name === "charset" && !flag.charset) {
flag.charset = true;
return;
}
if (flag.filter || (name === "charset" && flag.charset)) {
rule.remove();
}
};
// Wring at-rule
const wringAtRule = atRule => {
atRule.raws.before = "";
atRule.raws.afterName = " ";
atRule.raws.between = "";
atRule.raws.semicolon = false;
atRule.raws.after = "";
if (!atRule.params) {
atRule.params = "";
}
if (atRule.name === "charset") {
return;
}
if (atRule.name === "font-face") {
if (atRule.nodes.filter(isRequiredFontFaceDescriptor).length < 2) {
atRule.remove();
return;
}
atRule.each(removeDefaultFontFaceDescriptor);
}
if (atRule.nodes && atRule.nodes.length === 0) {
atRule.remove();
return;
}
let params = atRule.params
.replace(re.whiteSpaces, " ")
.replace(re.whiteSpacesAfterSymbol, "$1")
.replace(re.whiteSpacesBeforeSymbol, "$1");
if (atRule.name === "import") {
params = params
.replace(re.urlFunction, "$1$2")
.replace(re.quotedString, quoteImportURL);
}
if (atRule.name === "namespace") {
params = list
.space(params.replace(re.urlFunction, "$1$2"))
.map(quoteNamespaceURL)
.join("");
}
if (atRule.name === "keyframes") {
params = params.replace(re.quotedString, "$2");
}
if (atRule.name === "supports") {
params = params.replace(re.declInParentheses, wringDeclLike);
}
atRule.params = params;
if (
atRule.params === "" ||
params.indexOf("(") === 0 ||
params.indexOf('"') === 0 ||
params.indexOf("'") === 0
) {
atRule.raws.afterName = "";
}
};
module.exports = postcss.plugin(pkg.name, options => {
const opts = {
preserveHacks: false,
removeAllComments: false,
...options
};
return css => {
css.raws.semicolon = false;
css.raws.after = "";
css.walkComments(wringComment.bind(null, opts.removeAllComments));
css.walkDecls(wringDecl.bind(null, opts.preserveHacks));
css.walkRules(wringRule);
css.each(
filterAtRule.bind(null, {
charset: false,
filter: false
})
);
css.walkAtRules(wringAtRule);
};
});
module.exports.wring = function(css, opts) {
return postcss([this(opts)]).process(css, opts);
};
/* eslint no-param-reassign: [ "error", { "props": true, "ignorePropertyModificationsFor": [ "decls", "comment", "decl", "rule", "flag", "atRule", "css" ] } ] */