-
Notifications
You must be signed in to change notification settings - Fork 1
/
print-file-size.js
37 lines (35 loc) · 1.18 KB
/
print-file-size.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
const fs = require("fs").promises;
const fileSize = require("file-size");
const gzipSize = require("gzip-size");
const globWithCallbacks = require("glob");
const { promisify } = require("util");
const glob = promisify(globWithCallbacks);
var UglifyJS = require("uglify-es");
const run = async folder => {
const files = await glob(`${folder}/**.js`);
const fileStats = await Promise.all(
files.map(async fileName => {
const fileStats = await fs.stat(fileName);
const fileContent = await fs.readFile(fileName, "utf8");
const minifyResult = UglifyJS.minify(fileContent);
const minifiedContent = minifyResult.code;
return {
fileSize: fileStats.size,
minifiedSize: minifiedContent.length,
gzipSize: await gzipSize(minifiedContent),
};
})
);
const printTotal = async (f, key) =>
(await fileSize(
fileStats.map(stats => stats[key]).reduce((a, b) => a + b)
)).human();
console.log(folder, {
files,
fileSizeTotal: await printTotal(fileStats, "fileSize"),
minifiedSizeTotal: await printTotal(fileStats, "minifiedSize"),
gzipSizeTotal: await printTotal(fileStats, "gzipSize"),
});
};
run("es6");
run("es5");