dev-server.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. var hotEmitter = require("./emitter");
  62. hotEmitter.on("webpackHotUpdate", function (currentHash) {
  63. lastHash = currentHash;
  64. if (!upToDate() && module.hot.status() === "idle") {
  65. log("info", "[HMR] Checking for updates on the server...");
  66. check();
  67. }
  68. });
  69. log("info", "[HMR] Waiting for update signal from WDS...");
  70. } else {
  71. throw new Error("[HMR] Hot Module Replacement is disabled.");
  72. }