Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): add debugging and timing informat…
Browse files Browse the repository at this point in the history
…ion in JavaScript and CSS optimization plugins

This can be useful to debug slow builds.

Example of output
```
LOG from build-angular.JavaScriptOptimizerPlugin
<t> optimize asset: runtime.ad5c30339e926c89.js: 221.959564 ms
<t> optimize asset: polyfills.ec3ffae5bac27204.js: 1071.080092 ms
<t> optimize asset: main.aa8a15155ca2133f.js: 3391.588635 ms
<t> optimize js assets: 3483.799739 ms

LOG from build-angular.CssOptimizerPlugin
<t> optimize asset: styles.d251c5bf54715558.css: 26.569907 ms
<t> optimize css assets: 34.441737 ms
```

```
LOG from build-angular.JavaScriptOptimizerPlugin
<i> polyfills.ec3ffae5bac27204.js restored from cache.
<i> runtime.ad5c30339e926c89.js restored from cache.
<t> optimize asset: main.69fb55a243b46bfa.js: 2618.5191210000003 ms
<t> optimize js assets: 2721.226144 ms

LOG from build-angular.CssOptimizerPlugin
<i> styles.d251c5bf54715558.css restored from cache.
<t> optimize css assets: 12.149169 ms
```
  • Loading branch information
alan-agius4 authored and dgp1130 committed May 20, 2022
1 parent 1140d73 commit 6cbb941
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class CssOptimizerPlugin {
const { OriginalSource, SourceMapSource } = compiler.webpack.sources;

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const logger = compilation.getLogger('build-angular.CssOptimizerPlugin');

compilation.hooks.processAssets.tapPromise(
{
name: PLUGIN_NAME,
Expand All @@ -48,6 +50,7 @@ export class CssOptimizerPlugin {
async (compilationAssets) => {
const cache = compilation.options.cache && compilation.getCache(PLUGIN_NAME);

logger.time('optimize css assets');
for (const assetName of Object.keys(compilationAssets)) {
if (!/\.(?:css|scss|sass|less|styl)$/.test(assetName)) {
continue;
Expand All @@ -70,6 +73,7 @@ export class CssOptimizerPlugin {
>();

if (cachedOutput) {
logger.debug(`${name} restored from cache`);
await this.addWarnings(compilation, cachedOutput.warnings);
compilation.updateAsset(name, cachedOutput.source, (assetInfo) => ({
...assetInfo,
Expand All @@ -82,12 +86,15 @@ export class CssOptimizerPlugin {
const { source, map: inputMap } = styleAssetSource.sourceAndMap();
const input = typeof source === 'string' ? source : source.toString();

const optimizeAssetLabel = `optimize asset: ${asset.name}`;
logger.time(optimizeAssetLabel);
const { code, warnings, map } = await this.optimize(
input,
asset.name,
inputMap,
this.targets,
);
logger.timeEnd(optimizeAssetLabel);

await this.addWarnings(compilation, warnings);

Expand All @@ -104,6 +111,7 @@ export class CssOptimizerPlugin {
warnings,
});
}
logger.timeEnd('optimize css assets');
},
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ export class JavaScriptOptimizerPlugin {
const { OriginalSource, SourceMapSource } = compiler.webpack.sources;

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
const logger = compilation.getLogger('build-angular.JavaScriptOptimizerPlugin');

compilation.hooks.processAssets.tapPromise(
{
name: PLUGIN_NAME,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
},
async (compilationAssets) => {
logger.time('optimize js assets');
const scriptsToOptimize = [];
const cache =
compilation.options.cache && compilation.getCache('JavaScriptOptimizerPlugin');
Expand Down Expand Up @@ -123,6 +126,7 @@ export class JavaScriptOptimizerPlugin {
>();

if (cachedOutput) {
logger.debug(`${name} restored from cache`);
compilation.updateAsset(name, cachedOutput.source, (assetInfo) => ({
...assetInfo,
minimized: true,
Expand Down Expand Up @@ -195,6 +199,8 @@ export class JavaScriptOptimizerPlugin {
try {
const tasks = [];
for (const { name, code, map, cacheItem } of scriptsToOptimize) {
logger.time(`optimize asset: ${name}`);

tasks.push(
workerPool
.run({
Expand All @@ -215,6 +221,8 @@ export class JavaScriptOptimizerPlugin {
minimized: true,
}));

logger.timeEnd(`optimize asset: ${name}`);

return cacheItem?.storePromise({
source: optimizedAsset,
});
Expand All @@ -233,6 +241,8 @@ export class JavaScriptOptimizerPlugin {
} finally {
void workerPool.destroy();
}

logger.timeEnd('optimize js assets');
},
);
});
Expand Down

0 comments on commit 6cbb941

Please sign in to comment.