/*eslint-env browser*/
/*global __resourceQuery*/
var options = {
path: "/__webpack_hmr",
timeout: 20 * 1000,
overlay: true,
reload: false
};
Iif (__resourceQuery) {
var pathMatch = /path=(.*?)(\&|$)/.exec(__resourceQuery);
if (pathMatch) {
options.path = pathMatch[1];
}
var timeoutMatch = /timeout=(.*?)(\&|$)/.exec(__resourceQuery);
if (timeoutMatch) {
options.timeout = parseFloat(timeoutMatch[1]);
}
var overlayMatch = /overlay=(.*?)(\&|$)/.exec(__resourceQuery);
if (overlayMatch) {
options.overlay = overlayMatch[1] !== 'false';
}
var reloadMatch = /reload=(.*?)(\&|$)/.exec(__resourceQuery);
if (reloadMatch) {
options.reload = reloadMatch[1] !== 'false';
}
}
Iif (typeof window.EventSource !== 'function') {
console.warn(
"webpack-hot-middleware's client requires EventSource to work. " +
"You should include a polyfill if you want to support this browser: " +
"https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events#Tools"
);
} else {
connect();
}
function connect() {
var source = new window.EventSource(options.path);
var lastActivity = new Date();
source.onopen = handleOnline;
source.onmessage = handleMessage;
source.onerror = handleDisconnect;
var timer = setInterval(function() {
if ((new Date() - lastActivity) > options.timeout) {
handleDisconnect();
}
}, options.timeout / 2);
function handleOnline() {
console.log("[HMR] connected");
lastActivity = new Date();
}
function handleMessage(event) {
lastActivity = new Date();
Iif (event.data == "\uD83D\uDC93") {
return;
}
try {
processMessage(JSON.parse(event.data));
} catch (ex) {
console.warn("Invalid HMR message: " + event.data + "\n" + ex);
}
}
function handleDisconnect() {
clearInterval(timer);
source.close();
setTimeout(connect, options.timeout);
}
}
var strip = require('strip-ansi');
var overlay;
Eif (options.overlay) {
overlay = require('./client-overlay');
}
function problems(type, obj) {
console.warn("[HMR] bundle has " + type + ":");
var list = [];
obj[type].forEach(function(msg) {
var clean = strip(msg);
console.warn("[HMR] " + clean);
list.push(clean);
});
if (overlay && type !== 'warnings') overlay.showProblems(list);
}
function success() {
Eif (overlay) overlay.clear();
}
var processUpdate = require('./process-update');
var customHandler;
function processMessage(obj) {
Iif (obj.action == "building") {
console.log("[HMR] bundle rebuilding");
} else if (obj.action == "built") {
console.log("[HMR] bundle rebuilt in " + obj.time + "ms");
Iif (obj.errors.length > 0) {
problems('errors', obj);
} else {
Iif (obj.warnings.length > 0) {
problems('warnings', obj);
} else {
success();
}
processUpdate(obj.hash, obj.modules, options.reload);
}
} else Eif (customHandler) {
customHandler(obj);
}
}
Eif (module) {
module.exports = {
subscribe: function subscribe(handler) {
customHandler = handler;
}
};
}
|