-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.41 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
const babar = require('babar');
const fs = require('fs');
const path = require('path');
// Post Build Plugin for analyzing bundle sizes after the assets are emitted.
module.exports = class PostBuildPlugin {
/**
*Constructor to initialize options.
*@param {Object} options.
*/
constructor(options) {
this.options = options || {
height: 10,
width: 40,
color: 'cyan'
};
this.postBuildProcess = this.postBuildProcess.bind(this);
}
/**
* Function Called after build is done and assets are compied to folder.
* @param {Object} stats
*/
postBuildProcess(stats) {
const filePath = stats.compilation.options.output.path;
const { height, width, color } = this.options;
const data = [];
let i = 0;
fs.readdir(filePath, (error, files) => {
if (error) throw error;
files.forEach(file => {
const { size } = fs.statSync(path.resolve(filePath, file));
data.push([++i, size / 1000]);
})
console.log(babar(data, {
color,
width,
height,
yFractions: 1
}));
});
}
/**
*
* @param {Tapable Instance} compiler
*/
apply(compiler) {
compiler.hooks.done.tap("PostBuildPlugin", this.postBuildProcess);
}
};