only-dev-server.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 check = function check() {
  12. module.hot.check().then(function(updatedModules) {
  13. if(!updatedModules) {
  14. console.warn("[HMR] Cannot find update. Need to do a full reload!");
  15. console.warn("[HMR] (Probably because of restarting the webpack-dev-server)");
  16. return;
  17. }
  18. return module.hot.apply({
  19. ignoreUnaccepted: true,
  20. ignoreDeclined: true,
  21. ignoreErrored: true,
  22. onUnaccepted: function(data) {
  23. console.warn("Ignored an update to unaccepted module " + data.chain.join(" -> "));
  24. },
  25. onDeclined: function(data) {
  26. console.warn("Ignored an update to declined module " + data.chain.join(" -> "));
  27. },
  28. onErrored: function(data) {
  29. console.warn("Ignored an error while updating module " + data.moduleId + " (" + data.type + ")");
  30. }
  31. }).then(function(renewedModules) {
  32. if(!upToDate()) {
  33. check();
  34. }
  35. require("./log-apply-result")(updatedModules, renewedModules);
  36. if(upToDate()) {
  37. console.log("[HMR] App is up to date.");
  38. }
  39. });
  40. }).catch(function(err) {
  41. var status = module.hot.status();
  42. if(["abort", "fail"].indexOf(status) >= 0) {
  43. console.warn("[HMR] Cannot check for update. Need to do a full reload!");
  44. console.warn("[HMR] " + err.stack || err.message);
  45. } else {
  46. console.warn("[HMR] Update check failed: " + err.stack || err.message);
  47. }
  48. });
  49. };
  50. var hotEmitter = require("./emitter");
  51. hotEmitter.on("webpackHotUpdate", function(currentHash) {
  52. lastHash = currentHash;
  53. if(!upToDate()) {
  54. var status = module.hot.status();
  55. if(status === "idle") {
  56. console.log("[HMR] Checking for updates on the server...");
  57. check();
  58. } else if(["abort", "fail"].indexOf(status) >= 0) {
  59. console.warn("[HMR] Cannot apply update as a previous update " + status + "ed. Need to do a full reload!");
  60. }
  61. }
  62. });
  63. console.log("[HMR] Waiting for update signal from WDS...");
  64. } else {
  65. throw new Error("[HMR] Hot Module Replacement is disabled.");
  66. }