dev-server.js 1.9 KB

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