only-dev-server.js 2.6 KB

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