only-dev-server.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. /*globals __webpack_hash__ */
  6. if (module.hot) {
  7. var lastHash;
  8. var upToDate = function upToDate() {
  9. return lastHash.indexOf(__webpack_hash__) >= 0;
  10. };
  11. var log = require("./log");
  12. var check = function check() {
  13. module.hot
  14. .check()
  15. .then(function(updatedModules) {
  16. if (!updatedModules) {
  17. log("warning", "[HMR] Cannot find update. Need to do a full reload!");
  18. log(
  19. "warning",
  20. "[HMR] (Probably because of restarting the webpack-dev-server)"
  21. );
  22. return;
  23. }
  24. return module.hot
  25. .apply({
  26. ignoreUnaccepted: true,
  27. ignoreDeclined: true,
  28. ignoreErrored: true,
  29. onUnaccepted: function(data) {
  30. log(
  31. "warning",
  32. "Ignored an update to unaccepted module " +
  33. data.chain.join(" -> ")
  34. );
  35. },
  36. onDeclined: function(data) {
  37. log(
  38. "warning",
  39. "Ignored an update to declined module " +
  40. data.chain.join(" -> ")
  41. );
  42. },
  43. onErrored: function(data) {
  44. log("error", data.error);
  45. log(
  46. "warning",
  47. "Ignored an error while updating module " +
  48. data.moduleId +
  49. " (" +
  50. data.type +
  51. ")"
  52. );
  53. }
  54. })
  55. .then(function(renewedModules) {
  56. if (!upToDate()) {
  57. check();
  58. }
  59. require("./log-apply-result")(updatedModules, renewedModules);
  60. if (upToDate()) {
  61. log("info", "[HMR] App is up to date.");
  62. }
  63. });
  64. })
  65. .catch(function(err) {
  66. var status = module.hot.status();
  67. if (["abort", "fail"].indexOf(status) >= 0) {
  68. log(
  69. "warning",
  70. "[HMR] Cannot check for update. Need to do a full reload!"
  71. );
  72. log("warning", "[HMR] " + (err.stack || err.message));
  73. } else {
  74. log(
  75. "warning",
  76. "[HMR] Update check failed: " + (err.stack || err.message)
  77. );
  78. }
  79. });
  80. };
  81. var hotEmitter = require("./emitter");
  82. hotEmitter.on("webpackHotUpdate", function(currentHash) {
  83. lastHash = currentHash;
  84. if (!upToDate()) {
  85. var status = module.hot.status();
  86. if (status === "idle") {
  87. log("info", "[HMR] Checking for updates on the server...");
  88. check();
  89. } else if (["abort", "fail"].indexOf(status) >= 0) {
  90. log(
  91. "warning",
  92. "[HMR] Cannot apply update as a previous update " +
  93. status +
  94. "ed. Need to do a full reload!"
  95. );
  96. }
  97. }
  98. });
  99. log("info", "[HMR] Waiting for update signal from WDS...");
  100. } else {
  101. throw new Error("[HMR] Hot Module Replacement is disabled.");
  102. }