All files / webpack-hot-socketio socket-handler.js

100% Statements 15/15
100% Branches 15/15
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x   1x   7x 7x     7x     14x 14x 10x               10x 10x   10x 9x   10x 11x                     4x            
const { getModuleMap, extractBundles } = require('./helper.js');
 
module.exports = class SocketHandler {
	constructor(io, opts) {
		this.io = io;
		this.opts = opts;
	}
	listen(eventName, fn) {
		return this.io.on(eventName, fn);
	}
	sendStats(action, statsResult) {
		let eventName = this.opts.eventName;
		if (statsResult) {
			let stats = statsResult.toJson({
				all: false,
				cached: true,
				children: true,
				modules: true,
				timings: true,
				hash: true,
			});
			let bundles = extractBundles(stats);
			let 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 || '';
			}
			for (let bundle of bundles) {
				this.io.emit(eventName, {
					name: name,
					action: action,
					time: stats.time,
					hash:  stats.hash,
					warnings: stats.warnings || [],
					errors: stats.errors || [],
					modules: getModuleMap(stats.modules)
				});
			}
		} else {
			this.io.emit(eventName, {
				action: action
			});
		}
	}
};