signal.js 1.7 KB

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