All files / webpack-hot-socketio client.js

87.23% Statements 41/47
77.5% Branches 31/40
75% Functions 6/8
87.23% Lines 41/47

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107    5x   5x                           5x 5x 5x 5x 5x 5x 5x       5x 5x 5x       5x     5x 5x 5x 5x 5x 5x     5x 5x 5x         5x 5x 5x         5x     5x 5x 5x             5x     5x               2x                 5x 2x   3x 3x 1x   3x 2x   3x       5x  
/* global __resourceQuery __webpack_public_path__ */
 
var context = {};
 
var options = {
	path: '',
	timeout: 20 * 1000,
	reload: false,
	log: true,
	warn: true,
	name: '',
	reconnect: true,
	eventName: '__webpack_hot_socketio__',
	port: 80
};
 
var log, warn;
 
Eif (__resourceQuery) {
	var queryString = require('query-string');
	var overrides = queryString.parse(__resourceQuery);
	setOverrides(overrides);
	log = options.log ? console.log.bind(console) : function() {};
	warn = options.warn ? console.warn.bind(console) : function() {};
	setContext();
}
 
function setContext() {
	context.opts = options;
	context.log = log;
	context.warn = warn;
}
 
function setOverrides(overrides) {
	Iif (overrides.reconnect) {
		options.reconnect = overrides.autoConnect === 'true';
	}
	options.path = overrides.path || options.path;
	options.timeout = overrides.timeout || options.timeout;
	options.port = overrides.port || options.port;
	options.eventName = overrides.eventName || options.eventName;
	options.name = overrides.name || options.name;
	Iif (overrides.reload) {
		options.reload = overrides.reload !== 'false';
	}
	Eif (overrides.quiet && overrides.quiet !== 'false') {
		options.log = false;
		options.warn = false;
	}
}
 
function connect() {
	var io = require('socket.io-client');
	var serverPath = window.location.protocol + '//' + window.location.hostname + ':' + options.port;
	var socket = io.connect(serverPath, {
		timeout: options.timeout,
		path: options.path,
		reconnection: options.reconnect
	});
	socket.on('connect', function() {
		log('[HMR] connected');
	});
	socket.on(options.eventName, function(data) {
		try {
			processWebpackMessage(data);
		} catch (e) {
			warn('Invalid HMR info: ' + data + '\n' + e);
		}
	});
}
 
var processUpdate = require('./hot-client/process-update');
 
function processWebpackMessage(obj) {
	switch (obj.action) {
		case 'building':
			log('[HMR] bundle ' +
				(obj.name ? "'" + obj.name + "' " : '') +
				'rebuilding'
			);
			break;
		case 'built':
			log(
				'[HMR] bundle ' +
				(obj.name ? "'" + obj.name + "' " : '') +
				'rebuilt in ' +
				obj.time +
				'ms'
			);
			// fail through
		case 'sync':
			if (obj.name && options.name && obj.name !== options.name) {
				return;
			}
			var applyUpdate = true;
			if (obj.errors.length > 0) {
				applyUpdate = false;
			}
			if (applyUpdate) {
				processUpdate(obj.hash, obj.modules, context);
			}
			break;
	}
}
 
connect();