-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (29 loc) · 1.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
const {readFileSync, writeFileSync, statSync} = require('fs');
function toString(size) {
return `${size === 0 ? 0 : (size/1024).toFixed(3)} kB`;
}
module.exports = (props = {}) => {
const outputFile = props.output || './.filestats.json';
return {
name: 'size-diff',
writeBundle: ({file}) => {
let {size} = statSync(file);
let prevStats, diff = '';
try {
prevStats = JSON.parse(readFileSync(outputFile).toString());
}
catch(e) {}
if (prevStats !== undefined && typeof prevStats[file] === 'number') {
let delta = size - prevStats[file];
diff = ` (${delta < 0 ? '' : '+'}${toString(delta)})`;
}
try {
if (!prevStats) prevStats = {};
prevStats[file] = size;
writeFileSync(outputFile, JSON.stringify(prevStats, null, 2));
}
catch(e) {}
console.log(`${toString(size)}${diff}`);
},
};
};