Skip to content

Commit

Permalink
feat: added the statsOptions parameter (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperOleg39 authored Dec 25, 2023
1 parent 1bd43d5 commit f63a39f
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function webpackHotMiddleware(compiler, opts) {
typeof opts.log == 'undefined' ? console.log.bind(console) : opts.log;
opts.path = opts.path || '/__webpack_hmr';
opts.heartbeat = opts.heartbeat || 10 * 1000;
opts.statsCached =
typeof opts.statsCached == 'undefined' ? true : opts.statsCached;
opts.statsOptions =
typeof opts.statsOptions == 'undefined' ? {} : opts.statsOptions;

var eventStream = createEventStream(opts.heartbeat);
var latestStats = null;
Expand All @@ -33,7 +33,13 @@ function webpackHotMiddleware(compiler, opts) {
if (closed) return;
// Keep hold of latest stats so they can be propagated to new clients
latestStats = statsResult;
publishStats('built', latestStats, eventStream, opts.log, opts.statsCached);
publishStats(
'built',
latestStats,
eventStream,
opts.log,
opts.statsOptions
);
}
var middleware = function (req, res, next) {
if (closed) return next();
Expand All @@ -42,7 +48,7 @@ function webpackHotMiddleware(compiler, opts) {
if (latestStats) {
// Explicitly not passing in `log` fn as we don't want to log again on
// the server
publishStats('sync', latestStats, eventStream, false, opts.statsCached);
publishStats('sync', latestStats, eventStream, false, opts.statsOptions);
}
};
middleware.publish = function (payload) {
Expand Down Expand Up @@ -116,30 +122,33 @@ function createEventStream(heartbeat) {
};
}

function publishStats(action, statsResult, eventStream, log, statsCached) {
var statsOptions = {
all: false,
cached: statsCached,
children: true,
modules: true,
timings: true,
hash: true,
errors: true,
warnings: true,
};
function publishStats(action, statsResult, eventStream, log, statsOptions) {
var resultStatsOptions = Object.assign(
{
all: false,
cached: true,
children: true,
modules: true,
timings: true,
hash: true,
errors: true,
warnings: true,
},
statsOptions
);

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));
return extractBundles(normalizeStats(stats, resultStatsOptions));
});

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

bundles.forEach(function (stats) {
Expand Down

0 comments on commit f63a39f

Please sign in to comment.