-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpurgecss.js
49 lines (40 loc) · 1.28 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 DIST_PATH = "./dist/nano-casa/";
// find the styles css file
const files = getFilesFromPath(DIST_PATH, ".css");
let data = [];
if (!files && files.length <= 0) {
console.log("cannot find style files to purge");
return;
}
for (let f of files) {
// get original file size
const originalSize = getFilesizeInKiloBytes(DIST_PATH + f) + "kb";
var o = { file: f, originalSize: originalSize, newSize: "" };
data.push(o);
}
console.log("Run PurgeCSS...");
exec(
`purgecss -css ${DIST_PATH}*.css --content ${DIST_PATH}index.html ${DIST_PATH}*.js -o ${DIST_PATH} --safelist glass funding-wrap`,
function (error, stdout, stderr) {
console.log("PurgeCSS done:");
console.log();
for (let d of data) {
// get new file size
const newSize = getFilesizeInKiloBytes(DIST_PATH + d.file) + "kb";
d.newSize = newSize;
}
console.table(data);
}
);
function getFilesizeInKiloBytes(filename) {
var stats = fs.statSync(filename);
var fileSizeInBytes = stats.size / 1024;
return fileSizeInBytes.toFixed(2);
}
function getFilesFromPath(dir, extension) {
let files = fs.readdirSync(dir);
return files.filter((e) => path.extname(e).toLowerCase() === extension);
}