signal.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. /*globals __resourceQuery */
  6. if(module.hot) {
  7. var checkForUpdate = function checkForUpdate(fromUpdate) {
  8. module.hot.check().then(function(updatedModules) {
  9. if(!updatedModules) {
  10. if(fromUpdate)
  11. console.log("[HMR] Update applied.");
  12. else
  13. console.warn("[HMR] Cannot find update.");
  14. return;
  15. }
  16. return module.hot.apply({
  17. ignoreUnaccepted: true,
  18. onUnaccepted: function(data) {
  19. console.warn("Ignored an update to unaccepted module " + data.chain.join(" -> "));
  20. },
  21. }).then(function(renewedModules) {
  22. require("./log-apply-result")(updatedModules, renewedModules);
  23. checkForUpdate(true);
  24. });
  25. }).catch(function(err) {
  26. var status = module.hot.status();
  27. if(["abort", "fail"].indexOf(status) >= 0) {
  28. console.warn("[HMR] Cannot apply update.");
  29. console.warn("[HMR] " + err.stack || err.message);
  30. console.warn("[HMR] You need to restart the application!");
  31. } else {
  32. console.warn("[HMR] Update failed: " + err.stack || err.message);
  33. }
  34. });
  35. };
  36. process.on(__resourceQuery.substr(1) || "SIGUSR2", function() {
  37. if(module.hot.status() !== "idle") {
  38. console.warn("[HMR] Got signal but currently in " + module.hot.status() + " state.");
  39. console.warn("[HMR] Need to be in idle state to start hot update.");
  40. return;
  41. }
  42. checkForUpdate();
  43. });
  44. } else {
  45. throw new Error("[HMR] Hot Module Replacement is disabled.");
  46. }