Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve logic for handling multicomplier stats result to accomodate webpack5 #429

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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