Skip to content

Commit

Permalink
fix: properly handle webpack compilation stats
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmaciejewski committed Apr 14, 2022
1 parent 834ef5b commit 7eca29e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
17 changes: 7 additions & 10 deletions src/BuildTimeReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ export class BuildTimeReport {
this.hash = hash;
}

public addAssets(assets: Record<string, webpack.compilation.Asset & { emitted: boolean }>): void {
this.assets = [];
Object.entries(assets).forEach(([assetName, asset]) => {
if (asset.emitted) {
this.assets.push({
name: assetName,
size: asset.info.size!,
});
}
});
public addAssets(assets: Array<AssetStats & { emitted?: boolean }>): void {
this.assets = assets
.filter((asset) => asset.emitted)
.map((asset) => ({
name: asset.name,
size: asset.size,
}));
}

public addChunks(chunks: webpack.compilation.Chunk[]): void {
Expand Down
6 changes: 3 additions & 3 deletions src/BuildTimeReporterWebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import webpack from 'webpack';
import { JSONSchema4 } from 'schema-utils/declarations/validate';
import validateOptions from 'schema-utils';
import { BuildTimeReport } from './BuildTimeReport';
import { BuildTimeReporterWebpackPluginOptions } from './types';
import { AssetStats, BuildTimeReporterWebpackPluginOptions } from './types';
import { getLogger, tapInto, tapPromiseInto } from './utils';

const schema: JSONSchema4 = {
Expand Down Expand Up @@ -64,11 +64,11 @@ export class BuildTimeReporterWebpackPlugin {
report.track('emit');
}
});
tapInto(this.compiler, 'afterEmit', (compilation) => {
tapInto(this.compiler, 'afterEmit', (compilation: webpack.compilation.Compilation) => {
const report = this.reportsByHash.get(compilation.hash as string);
if (report) {
report.track('emit');
report.addAssets(compilation.assets);
report.addAssets(compilation.getStats().toJson().assets as AssetStats[]);
report.addChunks(compilation.chunks);
report.addModules(compilation.modules);
} else {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { BuildTimeReporterWebpackPlugin } from './BuildTimeReporterWebpackPlugin';

export = BuildTimeReporterWebpackPlugin;
import { BuildTimeReporterWebpackPluginOptions, StepStats, AssetStats, Step, BuildTimeReporterStats } from './types';

export type { BuildTimeReporterWebpackPluginOptions, StepStats, AssetStats, Step, BuildTimeReporterStats };

export default BuildTimeReporterWebpackPlugin;

export { BuildTimeReporterWebpackPlugin };

0 comments on commit 7eca29e

Please sign in to comment.