-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-angular): add estimated transfer size to b…
…uild output report When optimizations are enabled (either scripts or styles), an additional column will now be present in the output report shown in the console for an application build. This additonal column will display the estimated transfer size for each file as well as the total initial estimated transfer size for the initial files. The estimated transfer size is determined by calculating the compressed size of the file using brotli's default settings. In a development configuration (a configuration with optimizations disabled), the calculations are not performed to avoid any potential increase in rebuild speed due to the large size of unoptimized files. Closes: #21394
- Loading branch information
Showing
4 changed files
with
161 additions
and
23 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
61 changes: 61 additions & 0 deletions
61
packages/angular_devkit/build_angular/src/webpack/plugins/transfer-size-plugin.ts
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,61 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { promisify } from 'util'; | ||
import { Compiler } from 'webpack'; | ||
import { brotliCompress } from 'zlib'; | ||
|
||
const brotliCompressAsync = promisify(brotliCompress); | ||
|
||
const PLUGIN_NAME = 'angular-transfer-size-estimator'; | ||
|
||
export class TransferSizePlugin { | ||
constructor() {} | ||
|
||
apply(compiler: Compiler) { | ||
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { | ||
compilation.hooks.processAssets.tapPromise( | ||
{ | ||
name: PLUGIN_NAME, | ||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE, | ||
}, | ||
async (compilationAssets) => { | ||
const actions = []; | ||
for (const assetName of Object.keys(compilationAssets)) { | ||
if (!assetName.endsWith('.js') && !assetName.endsWith('.css')) { | ||
continue; | ||
} | ||
|
||
const scriptAsset = compilation.getAsset(assetName); | ||
if (!scriptAsset || scriptAsset.source.size() <= 0) { | ||
continue; | ||
} | ||
|
||
actions.push( | ||
brotliCompressAsync(scriptAsset.source.source()) | ||
.then((result) => { | ||
compilation.updateAsset(assetName, (s) => s, { | ||
estimatedTransferSize: result.length, | ||
}); | ||
}) | ||
.catch((error) => { | ||
compilation.warnings.push( | ||
new compilation.compiler.webpack.WebpackError( | ||
`Unable to calculate estimated transfer size for '${assetName}'. Reason: ${error.message}`, | ||
), | ||
); | ||
}), | ||
); | ||
} | ||
|
||
await Promise.all(actions); | ||
}, | ||
); | ||
}); | ||
} | ||
} |
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