-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
73 lines (59 loc) · 2.03 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
import isRegExp from 'is-regexp';
export default function stripCssComments(cssString, {preserve = true, whitespace = true, all} = {}) {
if (all) {
throw new Error('The `all` option is no longer supported. Use the `preserve` option instead.');
}
let preserveImportant = preserve;
let preserveFilter;
if (typeof preserve === 'function') {
preserveImportant = false;
preserveFilter = preserve;
} else if (isRegExp(preserve)) {
preserveImportant = false;
preserveFilter = comment => preserve.test(comment);
}
let isInsideString = false;
let currentCharacter = '';
let comment = '';
let returnValue = '';
for (let index = 0; index < cssString.length; index++) {
currentCharacter = cssString[index];
if (cssString[index - 1] !== '\\' && (currentCharacter === '"' || currentCharacter === '\'')) {
if (isInsideString === currentCharacter) {
isInsideString = false;
} else if (!isInsideString) {
isInsideString = currentCharacter;
}
}
// Find beginning of `/*` type comment
if (!isInsideString && currentCharacter === '/' && cssString[index + 1] === '*') {
// Ignore important comment when configured to preserve comments using important syntax: /*!
const isImportantComment = cssString[index + 2] === '!';
let index2 = index + 2;
// Iterate over comment
for (; index2 < cssString.length; index2++) {
// Find end of comment
if (cssString[index2] === '*' && cssString[index2 + 1] === '/') {
if ((preserveImportant && isImportantComment) || (preserveFilter && preserveFilter(comment))) {
returnValue += `/*${comment}*/`;
} else if (!whitespace) {
if (cssString[index2 + 2] === '\n') {
index2++;
} else if (cssString[index2 + 2] + cssString[index2 + 3] === '\r\n') {
index2 += 2;
}
}
comment = '';
break;
}
// Store comment text
comment += cssString[index2];
}
// Resume iteration over CSS string from the end of the comment
index = index2 + 1;
continue;
}
returnValue += currentCharacter;
}
return returnValue;
}