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) {
|
2017-05-31 17:00:41 +08:00
|
|
|
var log = require("./log");
|
2016-10-29 21:57:58 +08:00
|
|
|
var checkForUpdate = function checkForUpdate(fromUpdate) {
|
2015-06-13 17:45:28 +08:00
|
|
|
module.hot.check().then(function(updatedModules) {
|
2015-03-12 06:48:00 +08:00
|
|
|
if(!updatedModules) {
|
|
|
|
if(fromUpdate)
|
2017-05-31 17:00:41 +08:00
|
|
|
log("info", "[HMR] Update applied.");
|
2015-03-12 06:48:00 +08:00
|
|
|
else
|
2017-05-31 17:00:41 +08:00
|
|
|
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) {
|
2017-05-31 17:00:41 +08:00
|
|
|
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
|
|
|
});
|
2015-06-13 17:45:28 +08:00
|
|
|
}).catch(function(err) {
|
2016-06-21 03:46:27 +08:00
|
|
|
var status = module.hot.status();
|
|
|
|
if(["abort", "fail"].indexOf(status) >= 0) {
|
2017-05-31 17:00:41 +08:00
|
|
|
log("warning", "[HMR] Cannot apply update.");
|
|
|
|
log("warning", "[HMR] " + err.stack || err.message);
|
|
|
|
log("warning", "[HMR] You need to restart the application!");
|
2015-06-13 17:45:28 +08:00
|
|
|
} else {
|
2017-05-31 17:00:41 +08:00
|
|
|
log("warning", "[HMR] Update failed: " + err.stack || err.message);
|
2015-06-13 17:45:28 +08:00
|
|
|
}
|
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") {
|
2017-05-31 17:00:41 +08:00
|
|
|
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.");
|
|
|
|
}
|