-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
389 lines (327 loc) Β· 12.5 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
/**
* Simple remove unused and duplicated css
* Works with @ queries and normal styles
*/
const glob = require('glob');
const fs = require('fs');
module.exports = (cssFilesOrContent, options) => {
return new Promise((resolve) => {
let defaults = {
path: '**/+(*.html|*.htm|*.js)',
content: null,
override: false,
whitelistPatterns: [],
blacklistPatterns: [],
exclude: [],
globOptions: {
ignore: 'node_modules/**/*'
}
};
options = Object.assign({}, defaults, options);
let isString = false;
let allfilesContent = '';
if (options.path) {
let files = glob.sync(options.path, options.globOptions);
for (let i in files) {
let file = files[i];
if (options.exclude.indexOf(file) >= 0) {
continue;
}
allfilesContent += fs.readFileSync(file).toString();
}
} else if (options.content) {
allfilesContent = options.content;
}
if (typeof cssFilesOrContent === 'string') {
let cssData = '';
if (fs.existsSync(cssFilesOrContent)) {
cssData = fs.readFileSync(cssFilesOrContent).toString();
} else {
cssData = cssFilesOrContent;
isString = true;
}
if (cssData.length) {
let newContent = cleanCssFile(cssData).replace(/\n|\r\n/ig, '');
if (isString === false && options.override) {
fs.writeFileSync(cssFilesOrContent, newContent);
}
resolve({file: isString ? null : cssFilesOrContent, newContent: newContent});
} else {
resolve({file: isString ? null : cssFilesOrContent, newContent: cssData});
}
} else {
cssFilesOrContent.forEach((file) => {
let cssData = fs.readFileSync(file).toString();
if (cssData.length) {
let newContent = cleanCssFile(cssData).replace(/\n|\r\n/ig, '');
if (options.override) {
fs.writeFileSync(file, newContent);
}
}
});
resolve({file: cssFilesOrContent});
}
/**
* Remove CSS comments
* @param css
* @returns {string}
*/
function removeComments(css) {
let commentFound = false;
let chars = css.split('');
let cleaned = '';
for (let i = 0; i < chars.length; i++) {
if (commentFound === false) {
if (chars[i] === '/' && chars[i + 1] === '*') {
commentFound = true;
i++;
} else {
cleaned += chars[i];
}
} else {
if (chars[i] === '*' && chars[i + 1] === '/') {
commentFound = false;
i++;
}
}
}
return cleaned;
}
/**
* Parse CSS and return all styles uncleaned
* @param css
*/
function tokenizer(css) {
css = removeComments(css);
let chars = css.split('');
let atStyles = [];
let styles = {};
let count = 0;
let selector = '';
let readSelector = true;
let selectorContent = '';
for (let i in chars) {
if (chars[i] === '{') {
readSelector = false;
count++;
}
if (chars[i] === '}') {
count--;
}
if (readSelector) {
selector += chars[i];
} else {
selectorContent += chars[i];
}
if (readSelector === false && count === 0) {
readSelector = true;
selector = selector.trim();
selectorContent = selectorContent.trim().substr(1).slice(0, -1).replace(/\n|\r\n|\s{2,}/ig, '');
if (selector.indexOf('@') >= 0 && selector.indexOf('@-moz-document') >= 0) {
// skip moz because it is not supported anymore
} else if (selector.indexOf('@') >= 0 && selector.indexOf('@font-face') < 0 && selector.indexOf('@page') < 0) {
let [atContent] = tokenizer(selectorContent);
if (typeof styles[selector] === 'undefined') {
styles[selector] = atContent;
} else {
for (let i in atContent) {
if(typeof atContent[i] === 'string') {
if (typeof styles[selector][i] !== 'undefined') {
styles[selector][i] += atContent[i] + (atContent[i].slice(-1) === ';' ? '' : ';');
} else {
styles[selector][i] = atContent[i] + (atContent[i].slice(-1) === ';' ? '' : ';');
}
} else {
console.error('Selector not supported: ' + i);
}
}
}
} else if (selector.indexOf('@') >= 0) { // @font-face etc.
atStyles.push({
selector: selector,
content: selectorContent
})
} else {
if (typeof styles[selector] === 'undefined') {
styles[selector] = '';
}
styles[selector] += selectorContent + (selectorContent.slice(-1) === ';' ? '' : ';');
}
selectorContent = '';
selector = '';
}
}
return [styles, atStyles];
}
/**
* Returns all selectors with cleaned content
* @param styles
*/
function processStyles(styles) {
let rules = {};
for (let i in styles) {
let selector = i;
let selectorContent = styles[i];
if (typeof styles[i] === 'object') {
continue;
}
let properties = selectorContent.split(/;|\n/).map((o) => {
return o.trim();
}).filter((o) => {
return o.toString().trim() !== ''
});
let selectors = selector.split(',');
selectors = selectors.map((o) => {
return o.toString().trim();
});
let newSel = [];
for (let i in selectors) {
let sel = selectors[i];
if (selectorExists(sel) !== false) {
newSel.push(sel);
}
}
if (newSel.length === 0) {
continue;
} else {
newSel.sort();
selector = newSel.join(',');
}
if (!rules[selector]) {
rules[selector] = [];
}
rules[selector] = rules[selector].concat(properties);
rules[selector] = uniqueProps(rules[selector]);
}
return rules;
}
/**
* Cleanup CSS
* @param css
* @returns {string}
*/
function cleanCssFile(css) {
let cssToClean = css.replace(/@charset[^;]+;?/ig, '');
let [styles, styleBlocks] = tokenizer(cssToClean);
let rules = processStyles(styles);
let newCss = '';
styleBlocks.forEach(sb => {
newCss += sb.selector + '{' + sb.content + '}';
});
Object.keys(rules).forEach(k => {
newCss += k + '{' + rules[k].join(';') + '}';
});
let objSelectors = {};
for (let i in styles) {
if (typeof styles[i] === 'object') {
objSelectors[i] = styles[i];
}
}
objSelectors = sortMediaQueries(objSelectors);
for (let i in objSelectors) {
let rules = processStyles(objSelectors[i]);
let newCssInner = '';
Object.keys(rules).forEach(k => {
newCssInner += k + '{' + rules[k].join(';') + '}';
});
if (newCssInner.length) {
newCss += i + '{' + newCssInner + '}';
}
}
return newCss;
}
/**
* Sort media queries
*/
function sortMediaQueries(objSelectors) {
const ordered = {};
const nonOrdered = {};
for (let i in objSelectors) {
if(i.indexOf('@media') >= 0) {
nonOrdered[i] = objSelectors[i];
} else {
ordered[i] = objSelectors[i];
}
}
Object.keys(nonOrdered).sort((a, b) => {
let aVal = parseInt(a.replace(/[^0-9]+/, ''));
let bVal = parseInt(b.replace(/[^0-9]+/, ''));
aVal = isNaN(aVal) ? 0 : aVal;
bVal = isNaN(bVal) ? 0 : bVal;
return aVal - bVal;
}).forEach((key) => {
ordered[key] = nonOrdered[key];
});
return ordered;
}
/**
* Make the css props unique
* @param data
* @returns {*}
*/
function uniqueProps(data) {
let uniqueProps = {}, selectorData, prop, val, prefix, prefixMatch;
for (let i in data) {
selectorData = data[i];
prop = selectorData.substr(0, selectorData.indexOf(':')).trim();
val = selectorData.substr(selectorData.indexOf(':') + 1).trim();
prefix = '';
prefixMatch = val.match(/^\-(moz|o|webkit|ms|khtml)\-/, 'i');
if (prefixMatch && prefixMatch.length > 1) {
prefix = prefixMatch[1];
}
uniqueProps[prop + '--' + prefix] = {val, prop, prefix};
}
data = [];
for (let i in uniqueProps) {
data.push(uniqueProps[i].prop + ':' + uniqueProps[i].val);
}
return data;
}
/**
* Check if the selector is used in html or js
* @param selector
* @returns {boolean}
*/
function selectorExists(selector) {
// remove escape /
selector = selector.replace(/\\\//g, '/');
// remove css escape :
selector = selector.replace(/\\:/g, '\\');
// remove pseudo
selector = selector.replace(/(:{1,2}([^\s]+))/g, '');
// restore escapes :
selector = selector.replace(/\\/g, ':');
if ((!!options.path || !!options.content) && selector.length) {
for(let i in options.whitelistPatterns) {
if(options.whitelistPatterns[i].test(selector)) {
return true;
}
}
for(let i in options.blacklistPatterns) {
if(options.blacklistPatterns[i].test(selector)) {
return false;
}
}
// detect class names
let regex = /(?=\S*[-*]?)([a-zA-Z-*_\/\\0-9:]+)/g;
let m = null;
while ((m = regex.exec(selector)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
let val = m[0];
if (val === '*') {
return true;
}
let wordExp = new RegExp(val, 'g');
if (wordExp.test(allfilesContent)) {
return true;
}
}
return false;
}
return true;
}
})
};