-
Notifications
You must be signed in to change notification settings - Fork 11
/
webpack.fileSize.js
49 lines (38 loc) · 1.47 KB
/
webpack.fileSize.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 fs = require('fs');
const path = require('path');
const packageJson = require('./package.json');
class PluginGetFileSize {
constructor(file) {
// receive file to get size
this.file = file;
}
apply(compiler) {
const { ENV } = process.env;
const map = {
triliumTest: 'Test',
prod: 'Prod',
};
const fileName = map[ENV];
if (!fileName) return;
compiler.hooks.done.tap('Get File Size', (stats) => {
// Get output file
const file = stats.compilation.assetsInfo.get(this.file);
// Verify if file exists
if (!file) return console.log('File not exists');
// Get file size
const fileSizeInBytes = file.size;
// only used to convert
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
// Get size type
const sizeType = parseInt(Math.floor(Math.log(fileSizeInBytes) / Math.log(1024)).toString(), 10);
// Get size of file using size type
const size = Math.round(fileSizeInBytes / 1024 ** sizeType);
const logItem = `${packageJson.version} | ${new Date().toLocaleString()} | ${size} ${
sizes[sizeType]
} | ${fileSizeInBytes}\n`;
const logFile = path.join(__dirname, `./logs/fileSize${fileName}.log`);
fs.appendFileSync(logFile, logItem);
});
}
}
module.exports = { PluginGetFileSize };