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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from productboard/feat/bundle-size
Ability to upload Info (along with Index) files
- Loading branch information
Showing
4 changed files
with
134 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1338,6 +1338,11 @@ [email protected]: | |
dependencies: | ||
readable-stream "~1.1.9" | ||
|
||
duplexer@^0.1.1: | ||
version "0.1.1" | ||
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1" | ||
integrity sha1-rOb/gIwc5mtX0ev5eXessCM0z8E= | ||
|
||
duplexify@^3.5.0, duplexify@^3.6.0: | ||
version "3.7.1" | ||
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309" | ||
|
@@ -2327,6 +2332,14 @@ gulplog@^1.0.0: | |
dependencies: | ||
glogg "^1.0.0" | ||
|
||
gzip-size@^5.0.0: | ||
version "5.0.0" | ||
resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-5.0.0.tgz#a55ecd99222f4c48fd8c01c625ce3b349d0a0e80" | ||
integrity sha512-5iI7omclyqrnWw4XbXAmGhPsABkSIDQonv2K0h61lybgofWa6iZyvrI3r2zsJH4P8Nb64fFVzlvfhs0g7BBxAA== | ||
dependencies: | ||
duplexer "^0.1.1" | ||
pify "^3.0.0" | ||
|
||
har-schema@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92" | ||
|
@@ -4121,6 +4134,11 @@ pify@^2.0.0, pify@^2.3.0: | |
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" | ||
integrity sha1-7RQaasBDqEnqWISY59yosVMw6Qw= | ||
|
||
pify@^3.0.0: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" | ||
integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY= | ||
|
||
pinkie-promise@^2.0.0: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa" | ||
|