-
Notifications
You must be signed in to change notification settings - Fork 0
/
purgecss.js
49 lines (41 loc) · 1.39 KB
/
purgecss.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
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const purgePath = './dist/prod';
const safeList = [
'mat-primary',
'mat-accent',
'mat-error',
'mat-warn',
];
// find the styles css file
const files = getCssFiles(purgePath);
let data = [];
if (files.length <= 0) {
console.log(`No StyleSheets, please confirm the path : ${purgePath} Or forgot to run build?`);
return;
}
for (let file of files) {
// get original file size
const originalSize = getFilesizeInKiloBytes(purgePath + '/' + file) + ' KB';
var originalFile = { 'file': file, 'originalSize': originalSize, 'newSize': '' };
data.push(originalFile);
}
console.log('Run PurgeCSS...');
exec(`purgecss -css ${purgePath}/*.css --content ${purgePath}/index.html ${purgePath}/*.js --safelist ${safeList.join(' ')} -o ${purgePath}/`, function () {
for (let file of data) {
// get new file size
const newSize = getFilesizeInKiloBytes(`${purgePath}/` + file.file) + ' KB';
file.newSize = newSize;
}
console.table(data);
});
function getFilesizeInKiloBytes(filename) {
var stats = fs.statSync(filename);
var fileSizeInBytes = stats.size / 1024;
return fileSizeInBytes.toFixed(2);
}
function getCssFiles(dir) {
const files = fs.readdirSync(dir);
return files.filter(file => path.extname(file).toLowerCase() === '.css');
}