signal.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 log = require("./log");
  8. var checkForUpdate = function checkForUpdate(fromUpdate) {
  9. module.hot
  10. .check()
  11. .then(function (updatedModules) {
  12. if (!updatedModules) {
  13. if (fromUpdate) log("info", "[HMR] Update applied.");
  14. else log("warning", "[HMR] Cannot find update.");
  15. return;
  16. }
  17. return module.hot
  18. .apply({
  19. ignoreUnaccepted: true,
  20. onUnaccepted: function (data) {
  21. log(
  22. "warning",
  23. "Ignored an update to unaccepted module " +
  24. data.chain.join(" -> ")
  25. );
  26. }
  27. })
  28. .then(function (renewedModules) {
  29. require("./log-apply-result")(updatedModules, renewedModules);
  30. checkForUpdate(true);
  31. return null;
  32. });
  33. })
  34. .catch(function (err) {
  35. var status = module.hot.status();
  36. if (["abort", "fail"].indexOf(status) >= 0) {
  37. log("warning", "[HMR] Cannot apply update.");
  38. log("warning", "[HMR] " + log.formatError(err));
  39. log("warning", "[HMR] You need to restart the application!");
  40. } else {
  41. log("warning", "[HMR] Update failed: " + (err.stack || err.message));
  42. }
  43. });
  44. };
  45. process.on(__resourceQuery.slice(1) || "SIGUSR2", function () {
  46. if (module.hot.status() !== "idle") {
  47. log(
  48. "warning",
  49. "[HMR] Got signal but currently in " + module.hot.status() + " state."
  50. );
  51. log("warning", "[HMR] Need to be in idle state to start hot update.");
  52. return;
  53. }
  54. checkForUpdate();
  55. });
  56. } else {
  57. throw new Error("[HMR] Hot Module Replacement is disabled.");
  58. }