This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
68 lines (58 loc) · 1.56 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
58
59
60
61
62
63
64
65
66
67
68
/**
* Copyright (c) 2019-present, ProductBoard, Inc.
* All rights reserved.
*/
const fs = require('fs');
const child = require('child_process');
const gzipSize = require('gzip-size');
/**
* Webpack Plugin for generating info JSON (hash, sha, assetSize), it's consumable by webpack-deploy later on when deploying
*/
class BuildInfo {
constructor({ filename = 'build.log' }) {
this.filename = filename;
}
getSha() {
return child
.execSync('git rev-parse HEAD')
.toString()
.trim();
}
async createBuildInfoReport(compilation) {
const hash = compilation.getStats().toJson().hash;
const assetsSize = await Promise.all(
Object.entries(compilation.assets)
.filter(([asset]) => /.js$/.test(asset))
.map(async ([asset, content]) => {
const gzipedSize = await gzipSize(content.source());
return {
asset,
size: content.size(),
gzipedSize,
};
}),
);
// get full SHA from git
const sha = this.getSha();
const info = JSON.stringify({
sha,
hash,
assetsSize,
});
fs.writeFileSync(this.filename, info);
}
apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
this.createBuildInfoReport(compilation)
.catch(e => {
compilation.errors.push(
new Error(
`Webpack BuildInfo plugin has thrown an error in 'createBuildInfoReport': ${e}`,
),
);
})
.then(callback);
});
}
}
module.exports = { BuildInfo };