Skip to content

Commit

Permalink
Feature: Updated plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
VasylLymych committed Aug 27, 2023
1 parent 99f1a40 commit 0da30fe
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions plugins/GetLargestBundleSizePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ class GetLargestBundleSizePlugin {
constructor(buildOptions) {
this.buildOptions = buildOptions
}

getLargestBundleSize(assets) {
return Math.max(...Object.values(assets).map(asset => asset.size()))
}

getLargestBundleName({ assets, largestBundleSize }) {
return Object.keys(assets).find(assetName => assets[assetName].size() === largestBundleSize)
}

apply(compiler) {
compiler.hooks.emit.tapAsync('GetLargestBundleSizePlugin', (compilation, cb) => {
const largestBundleSize = Math.max(
...Object.values(compilation.assets).map(asset => asset.size())
)
const largestBundle = Object.keys(compilation.assets).find(
assetName => compilation.assets[assetName].size() === largestBundleSize
)
this.buildOptions.largestBundleSize = largestBundleSize;
compiler.hooks.emit.tapAsync('GetLargestBundleSizePlugin', ({ assets }, cb) => {
const largestBundleSize = this.getLargestBundleSize(assets)
const largestBundleName = this.getLargestBundleName({ assets, largestBundleSize })
this.buildOptions.largestBundleSize = largestBundleSize

console.log('\x1b[33m', `The largest bundle is ${largestBundle} with size: ${largestBundleSize} bytes`)
console.log('\x1b[33m', `The largest bundle is ${largestBundleSize} with size: ${largestBundleName} bytes`)

cb()
})
Expand Down

0 comments on commit 0da30fe

Please sign in to comment.