dev-server.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. /* globals __webpack_hash__ */
  6. // Universal regular-HMR client (web + Node): runs `module.hot.check` whenever a
  7. // `webpackHotUpdate` signal arrives on ./emitter (pushed by the dev-server).
  8. if (module.hot) {
  9. /** @type {undefined|string} */
  10. var lastHash;
  11. var upToDate = function upToDate() {
  12. return /** @type {string} */ (lastHash).indexOf(__webpack_hash__) >= 0;
  13. };
  14. var log = require("./log");
  15. var check = function check() {
  16. module.hot
  17. .check(true)
  18. .then(function (updatedModules) {
  19. if (!updatedModules) {
  20. log(
  21. "warning",
  22. "[HMR] Cannot find update. " +
  23. (typeof window !== "undefined"
  24. ? "Need to do a full reload!"
  25. : "Please reload manually!")
  26. );
  27. log(
  28. "warning",
  29. "[HMR] (Probably because of restarting the webpack-dev-server)"
  30. );
  31. if (typeof window !== "undefined") {
  32. window.location.reload();
  33. }
  34. return;
  35. }
  36. if (!upToDate()) {
  37. check();
  38. }
  39. require("./log-apply-result")(updatedModules, updatedModules);
  40. if (upToDate()) {
  41. log("info", "[HMR] App is up to date.");
  42. }
  43. })
  44. .catch(function (err) {
  45. var status = module.hot.status();
  46. if (["abort", "fail"].indexOf(status) >= 0) {
  47. log(
  48. "warning",
  49. "[HMR] Cannot apply update. " +
  50. (typeof window !== "undefined"
  51. ? "Need to do a full reload!"
  52. : "Please reload manually!")
  53. );
  54. log("warning", "[HMR] " + log.formatError(err));
  55. if (typeof window !== "undefined") {
  56. window.location.reload();
  57. }
  58. } else {
  59. log("warning", "[HMR] Update failed: " + log.formatError(err));
  60. }
  61. });
  62. };
  63. /** @type {EventTarget | NodeJS.EventEmitter} */
  64. var hotEmitter = require("./emitter");
  65. /**
  66. * @param {CustomEvent<{ currentHash: string }>} event event or hash
  67. */
  68. var handler = function (event) {
  69. lastHash = typeof event === "string" ? event : event.detail.currentHash;
  70. if (!upToDate() && module.hot.status() === "idle") {
  71. log("info", "[HMR] Checking for updates on the server...");
  72. check();
  73. }
  74. };
  75. if (typeof EventTarget !== "undefined" && hotEmitter instanceof EventTarget) {
  76. hotEmitter.addEventListener(
  77. "webpackHotUpdate",
  78. /** @type {EventListener} */
  79. (handler)
  80. );
  81. } else {
  82. hotEmitter.on("webpackHotUpdate", handler);
  83. }
  84. log("info", "[HMR] Waiting for update signal from WDS...");
  85. } else {
  86. throw new Error("[HMR] Hot Module Replacement is disabled.");
  87. }