dev-server.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(true)
  16. .then(function (updatedModules) {
  17. if (!updatedModules) {
  18. log(
  19. "warning",
  20. "[HMR] Cannot find update. " +
  21. (typeof window !== "undefined"
  22. ? "Need to do a full reload!"
  23. : "Please reload manually!")
  24. );
  25. log(
  26. "warning",
  27. "[HMR] (Probably because of restarting the webpack-dev-server)"
  28. );
  29. if (typeof window !== "undefined") {
  30. window.location.reload();
  31. }
  32. return;
  33. }
  34. if (!upToDate()) {
  35. check();
  36. }
  37. require("./log-apply-result")(updatedModules, updatedModules);
  38. if (upToDate()) {
  39. log("info", "[HMR] App is up to date.");
  40. }
  41. })
  42. .catch(function (err) {
  43. var status = module.hot.status();
  44. if (["abort", "fail"].indexOf(status) >= 0) {
  45. log(
  46. "warning",
  47. "[HMR] Cannot apply update. " +
  48. (typeof window !== "undefined"
  49. ? "Need to do a full reload!"
  50. : "Please reload manually!")
  51. );
  52. log("warning", "[HMR] " + log.formatError(err));
  53. if (typeof window !== "undefined") {
  54. window.location.reload();
  55. }
  56. } else {
  57. log("warning", "[HMR] Update failed: " + log.formatError(err));
  58. }
  59. });
  60. };
  61. /** @type {EventTarget | NodeJS.EventEmitter} */
  62. var hotEmitter = require("./emitter");
  63. /**
  64. * @param {CustomEvent<{ currentHash: string }>} event event or hash
  65. */
  66. var handler = function (event) {
  67. lastHash = typeof event === "string" ? event : event.detail.currentHash;
  68. if (!upToDate() && module.hot.status() === "idle") {
  69. log("info", "[HMR] Checking for updates on the server...");
  70. check();
  71. }
  72. };
  73. if (typeof EventTarget !== "undefined" && hotEmitter instanceof EventTarget) {
  74. hotEmitter.addEventListener(
  75. "webpackHotUpdate",
  76. /** @type {EventListener} */
  77. (handler)
  78. );
  79. } else {
  80. hotEmitter.on("webpackHotUpdate", handler);
  81. }
  82. log("info", "[HMR] Waiting for update signal from WDS...");
  83. } else {
  84. throw new Error("[HMR] Hot Module Replacement is disabled.");
  85. }