Skip to content

Commit

Permalink
feat: improve logic for handling multicomplier stats result to accomo…
Browse files Browse the repository at this point in the history
…date webpack5
  • Loading branch information
eokoneyo committed Oct 28, 2022
1 parent 2fbab92 commit 35b6140
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,35 @@ function createEventStream(heartbeat) {
}

function publishStats(action, statsResult, eventStream, log) {
var stats = statsResult.toJson({
var statsOptions = {
all: false,
cached: true,
children: true,
modules: true,
timings: true,
hash: true,
});
// For multi-compiler, stats will be an object with a 'children' array of stats
var bundles = extractBundles(stats);
};

var bundles = [];

// multi-compiler stats have stats for each child compiler
// see https://github.com/webpack/webpack/blob/main/lib/MultiCompiler.js#L97
if (statsResult.stats) {
var processed = statsResult.stats.map(function (stats) {
return extractBundles(normalizeStats(stats, statsOptions));
});

bundles = processed.flat();
} else {
bundles = extractBundles(normalizeStats(statsResult, statsOptions));
}

bundles.forEach(function (stats) {
var name = stats.name || '';

// Fallback to compilation name in case of 1 bundle (if it exists)
if (bundles.length === 1 && !name && statsResult.compilation) {
name = statsResult.compilation.name || '';
if (!name && stats.compilation) {
name = stats.compilation.name || '';
}

if (log) {
Expand All @@ -155,6 +168,19 @@ function publishStats(action, statsResult, eventStream, log) {
});
}

function normalizeStats(stats, statsOptions) {
var statsJson = stats.toJson(statsOptions);

if (stats.compilation) {
// webpack 5 has the compilation property directly on stats object
Object.assign(statsJson, {
compilation: stats.compilation,
});
}

return statsJson;
}

function extractBundles(stats) {
// Stats has modules, single bundle
if (stats.modules) return [stats];
Expand Down

0 comments on commit 35b6140

Please sign in to comment.