var client = null;
var socket = function initSocket(url, handlers, reconnect) {
client = new WebSocketClient(url);
client.onOpen(function () {
retries = 0;
if (typeof reconnect !== "undefined") {
maxRetries = reconnect;
}
});
client.onClose(function () {
if (retries === 0) {
handlers.close();
}
client = null;
if (retries < maxRetries) {
var retryInMs = 1000 * Math.pow(2, retries) + Math.random() * 100;
retries += 1;
setTimeout(function () {
socket(url, handlers, reconnect);
}, retryInMs);
}
});
client.onMessage(function (data) {
var message = JSON.parse(data);
if (handlers[message.type]) {
handlers[message.type](message.data, message.params);
}
});
};
var status = {
isUnloading: false,
currentHash: typeof __webpack_hash__ !== "undefined" ? __webpack_hash__ : ""
};
var onSocketMessage = {
ok: function ok() {
sendMessage("Ok");
reloadApp(options, status);
},
hash: function hash(_hash) {
status.previousHash = status.currentHash;
status.currentHash = _hash;
},
// ...
};
var socketURL = createSocketURL(parsedResourceQuery);
socket(socketURL, onSocketMessage, options.reconnect);
在Server.js中sendStats有一行代码 <math xmlns="http://www.w3.org/1998/Math/MathML"> t h i s . s e n d M e s s a g e ( c l i e n t s , " o k " ) ; this.sendMessage(clients, "ok"); </math>this.sendMessage(clients,"ok");这样会触发onSocketMessage的ok方法,由此触发reloadApp方法
当每次构建完触发done钩子函数,服务端向客户端发送hash事件
简略版reloadApp
js复制代码
function reloadApp(_ref, status) {
var hot = _ref.hot, liveReload = _ref.liveReload;
function applyReload(rootWindow, intervalId) {
clearInterval(intervalId);
rootWindow.location.reload();
}
var search = self.location.search.toLowerCase();
var allowToHot = search.indexOf("webpack-dev-server-hot=false") === -1;
var allowToLiveReload = search.indexOf("webpack-dev-server-live-reload=false") === -1;
if (hot && allowToHot) {
hotEmitter.emit("webpackHotUpdate", status.currentHash);
} else if (liveReload && allowToLiveReload) {
var rootWindow = self;
var intervalId = self.setInterval(function () {
if (rootWindow.location.protocol !== "about:") {
applyReload(rootWindow, intervalId);
} else {
rootWindow = rootWindow.parent;
if (rootWindow.parent === rootWindow) {
applyReload(rootWindow, intervalId);
}
}
});
}
}
允许热更新则触发webpackHotUpdate事件,该事件在 <math xmlns="http://www.w3.org/1998/Math/MathML"> a d d i t i o n a l E n t r i e s . p u s h ( r e q u i r e . r e s o l v e ( " w e b p a c k / h o t / d e v − s e r v e r " ) ) additionalEntries.push(require.resolve("webpack/hot/dev-server")) </math>additionalEntries.push(require.resolve("webpack/hot/dev−server"))引入的模块中注册过
webpack的hot文件下dev-server
js复制代码
if (module.hot) {
var lastHash;
var upToDate = function upToDate() {
return lastHash.indexOf(__webpack_hash__) >= 0;
};
var check = function check() {
module.hot
.check(true)
.then(function (updatedModules) {
if (!updateModules) {
log("warning","[HMR] Cannot find update. " + (typeof window !== "undefined" ? "Need to do a full reload!" : "Please reload manually!"));
return;
}
if (!upToDate()) {
check();
}
})
};
var hotEmitter = require("./emitter");
hotEmitter.on("webpackHotUpdate", function (currentHash) {
lastHash = currentHash;
if (!upToDate() && module.hot.status() === "idle") check();
});
}
// RuntimeGlobals.js
// function downloading the update manifest
exports.hmrDownloadManifest = "__webpack_require__.hmrM";
// array with handler functions to download chunk updates
exports.hmrDownloadUpdateHandlers = "__webpack_require__.hmrC";
// object with all hmr module data for all modules
exports.hmrModuleData = "__webpack_require__.hmrD";
这些变量会在其他地方赋值为函数
js复制代码
Template.asString([`${RuntimeGlobals.hmrDownloadManifest} = ${runtimeTemplate.basicFunction("", [
'if (typeof fetch === "undefined") throw new Error("No browser support: need fetch API");',
`return fetch(${RuntimeGlobals.publicPath} + ${
RuntimeGlobals.getUpdateManifestFilename
}()).then(${runtimeTemplate.basicFunction("response", [
"if(response.status === 404) return; // no update available",
'if(!response.ok) throw new Error("Failed to fetch update manifest " + response.statusText);',
"return response.json();"
])});`
])};`
])