only-dev-server.js 2.5 KB

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