process-update.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Based heavily on https://github.com/webpack/webpack/blob/
  3. * c0afdf9c6abc1dd70707c594e473802a566f7b6e/hot/only-dev-server.js
  4. * Original copyright Tobias Koppers @sokra (MIT license)
  5. */
  6. /* global window __webpack_hash__ */
  7. if (!module.hot) {
  8. throw new Error('[HMR] Hot Module Replacement is disabled.');
  9. }
  10. var hmrDocsUrl = 'https://webpack.js.org/concepts/hot-module-replacement/'; // eslint-disable-line max-len
  11. var lastHash;
  12. var failureStatuses = { abort: 1, fail: 1 };
  13. var applyOptions = {
  14. ignoreUnaccepted: true,
  15. ignoreDeclined: true,
  16. ignoreErrored: true,
  17. onUnaccepted: function (data) {
  18. console.warn(
  19. 'Ignored an update to unaccepted module ' + data.chain.join(' -> ')
  20. );
  21. },
  22. onDeclined: function (data) {
  23. console.warn(
  24. 'Ignored an update to declined module ' + data.chain.join(' -> ')
  25. );
  26. },
  27. onErrored: function (data) {
  28. console.error(data.error);
  29. console.warn(
  30. 'Ignored an error while updating module ' +
  31. data.moduleId +
  32. ' (' +
  33. data.type +
  34. ')'
  35. );
  36. },
  37. };
  38. function upToDate(hash) {
  39. if (hash) lastHash = hash;
  40. return lastHash == __webpack_hash__;
  41. }
  42. module.exports = function (hash, moduleMap, options) {
  43. var reload = options.reload;
  44. if (!upToDate(hash) && module.hot.status() == 'idle') {
  45. if (options.log) console.log('[HMR] Checking for updates on the server...');
  46. check();
  47. }
  48. function check() {
  49. var cb = function (err, updatedModules) {
  50. if (err) return handleError(err);
  51. if (!updatedModules) {
  52. if (options.warn) {
  53. console.warn('[HMR] Cannot find update (Full reload needed)');
  54. console.warn('[HMR] (Probably because of restarting the server)');
  55. }
  56. performReload();
  57. return null;
  58. }
  59. var applyCallback = function (applyErr, renewedModules) {
  60. if (applyErr) return handleError(applyErr);
  61. if (!upToDate()) check();
  62. logUpdates(updatedModules, renewedModules);
  63. };
  64. var applyResult = module.hot.apply(applyOptions, applyCallback);
  65. // webpack 2 promise
  66. if (applyResult && applyResult.then) {
  67. // HotModuleReplacement.runtime.js refers to the result as `outdatedModules`
  68. applyResult.then(function (outdatedModules) {
  69. applyCallback(null, outdatedModules);
  70. });
  71. applyResult.catch(applyCallback);
  72. }
  73. };
  74. var result = module.hot.check(false, cb);
  75. // webpack 2 promise
  76. if (result && result.then) {
  77. result.then(function (updatedModules) {
  78. cb(null, updatedModules);
  79. });
  80. result.catch(cb);
  81. }
  82. }
  83. function logUpdates(updatedModules, renewedModules) {
  84. var unacceptedModules = updatedModules.filter(function (moduleId) {
  85. return renewedModules && renewedModules.indexOf(moduleId) < 0;
  86. });
  87. if (unacceptedModules.length > 0) {
  88. if (options.warn) {
  89. console.warn(
  90. "[HMR] The following modules couldn't be hot updated: " +
  91. '(Full reload needed)\n' +
  92. 'This is usually because the modules which have changed ' +
  93. '(and their parents) do not know how to hot reload themselves. ' +
  94. 'See ' +
  95. hmrDocsUrl +
  96. ' for more details.'
  97. );
  98. unacceptedModules.forEach(function (moduleId) {
  99. console.warn('[HMR] - ' + (moduleMap[moduleId] || moduleId));
  100. });
  101. }
  102. performReload();
  103. return;
  104. }
  105. if (options.log) {
  106. if (!renewedModules || renewedModules.length === 0) {
  107. console.log('[HMR] Nothing hot updated.');
  108. } else {
  109. console.log('[HMR] Updated modules:');
  110. renewedModules.forEach(function (moduleId) {
  111. console.log('[HMR] - ' + (moduleMap[moduleId] || moduleId));
  112. });
  113. }
  114. if (upToDate()) {
  115. console.log('[HMR] App is up to date.');
  116. }
  117. }
  118. }
  119. function handleError(err) {
  120. if (module.hot.status() in failureStatuses) {
  121. if (options.warn) {
  122. console.warn('[HMR] Cannot check for update (Full reload needed)');
  123. console.warn('[HMR] ' + (err.stack || err.message));
  124. }
  125. performReload();
  126. return;
  127. }
  128. if (options.warn) {
  129. console.warn('[HMR] Update check failed: ' + (err.stack || err.message));
  130. }
  131. }
  132. function performReload() {
  133. if (reload) {
  134. if (options.warn) console.warn('[HMR] Reloading page');
  135. window.location.reload();
  136. }
  137. }
  138. };