You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

4 years ago
  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.substr(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. }