webpack/hot/signal.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-03-12 06:48:00 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2015-05-22 04:58:22 +08:00
/*globals __resourceQuery */
2015-03-12 06:48:00 +08:00
if(module.hot) {
var log = require("./log");
2016-10-29 21:57:58 +08:00
var checkForUpdate = function checkForUpdate(fromUpdate) {
module.hot.check().then(function(updatedModules) {
2015-03-12 06:48:00 +08:00
if(!updatedModules) {
if(fromUpdate)
log("info", "[HMR] Update applied.");
2015-03-12 06:48:00 +08:00
else
log("warning", "[HMR] Cannot find update.");
2015-03-12 06:48:00 +08:00
return;
}
2016-08-17 18:52:02 +08:00
return module.hot.apply({
ignoreUnaccepted: true,
onUnaccepted: function(data) {
log("warning", "Ignored an update to unaccepted module " + data.chain.join(" -> "));
2016-08-17 18:52:02 +08:00
},
}).then(function(renewedModules) {
2015-03-12 06:48:00 +08:00
require("./log-apply-result")(updatedModules, renewedModules);
2015-05-22 04:58:22 +08:00
2015-03-12 06:48:00 +08:00
checkForUpdate(true);
2017-12-29 22:13:45 +08:00
return null;
2015-03-12 06:48:00 +08:00
});
}).catch(function(err) {
var status = module.hot.status();
if(["abort", "fail"].indexOf(status) >= 0) {
log("warning", "[HMR] Cannot apply update.");
log("warning", "[HMR] " + err.stack || err.message);
log("warning", "[HMR] You need to restart the application!");
} else {
log("warning", "[HMR] Update failed: " + err.stack || err.message);
}
2015-03-12 06:48:00 +08:00
});
2017-01-11 17:51:58 +08:00
};
2015-03-12 06:48:00 +08:00
process.on(__resourceQuery.substr(1) || "SIGUSR2", function() {
if(module.hot.status() !== "idle") {
log("warning", "[HMR] Got signal but currently in " + module.hot.status() + " state.");
log("warning", "[HMR] Need to be in idle state to start hot update.");
2015-03-12 06:48:00 +08:00
return;
}
checkForUpdate();
});
} else {
throw new Error("[HMR] Hot Module Replacement is disabled.");
}