JavascriptHotModuleReplacement.runtime.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. var $installedChunks$ = undefined;
  7. var $loadUpdateChunk$ = undefined;
  8. var $moduleCache$ = undefined;
  9. var $moduleFactories$ = undefined;
  10. var $ensureChunkHandlers$ = undefined;
  11. var $hasOwnProperty$ = undefined;
  12. var $hmrModuleData$ = undefined;
  13. var $hmrDownloadUpdateHandlers$ = undefined;
  14. var $hmrInvalidateModuleHandlers$ = undefined;
  15. var __webpack_require__ = undefined;
  16. module.exports = function () {
  17. var currentUpdateChunks;
  18. var currentUpdate;
  19. var currentUpdateRemovedChunks;
  20. var currentUpdateRuntime;
  21. function applyHandler(options) {
  22. if ($ensureChunkHandlers$) delete $ensureChunkHandlers$.$key$Hmr;
  23. currentUpdateChunks = undefined;
  24. function getAffectedModuleEffects(updateModuleId) {
  25. var outdatedModules = [updateModuleId];
  26. var outdatedDependencies = {};
  27. var queue = outdatedModules.map(function (id) {
  28. return {
  29. chain: [id],
  30. id: id
  31. };
  32. });
  33. while (queue.length > 0) {
  34. var queueItem = queue.pop();
  35. var moduleId = queueItem.id;
  36. var chain = queueItem.chain;
  37. var module = $moduleCache$[moduleId];
  38. if (
  39. !module ||
  40. (module.hot._selfAccepted && !module.hot._selfInvalidated)
  41. )
  42. continue;
  43. if (module.hot._selfDeclined) {
  44. return {
  45. type: "self-declined",
  46. chain: chain,
  47. moduleId: moduleId
  48. };
  49. }
  50. if (module.hot._main) {
  51. return {
  52. type: "unaccepted",
  53. chain: chain,
  54. moduleId: moduleId
  55. };
  56. }
  57. for (var i = 0; i < module.parents.length; i++) {
  58. var parentId = module.parents[i];
  59. var parent = $moduleCache$[parentId];
  60. if (!parent) continue;
  61. if (parent.hot._declinedDependencies[moduleId]) {
  62. return {
  63. type: "declined",
  64. chain: chain.concat([parentId]),
  65. moduleId: moduleId,
  66. parentId: parentId
  67. };
  68. }
  69. if (outdatedModules.indexOf(parentId) !== -1) continue;
  70. if (parent.hot._acceptedDependencies[moduleId]) {
  71. if (!outdatedDependencies[parentId])
  72. outdatedDependencies[parentId] = [];
  73. addAllToSet(outdatedDependencies[parentId], [moduleId]);
  74. continue;
  75. }
  76. delete outdatedDependencies[parentId];
  77. outdatedModules.push(parentId);
  78. queue.push({
  79. chain: chain.concat([parentId]),
  80. id: parentId
  81. });
  82. }
  83. }
  84. return {
  85. type: "accepted",
  86. moduleId: updateModuleId,
  87. outdatedModules: outdatedModules,
  88. outdatedDependencies: outdatedDependencies
  89. };
  90. }
  91. function addAllToSet(a, b) {
  92. for (var i = 0; i < b.length; i++) {
  93. var item = b[i];
  94. if (a.indexOf(item) === -1) a.push(item);
  95. }
  96. }
  97. // at begin all updates modules are outdated
  98. // the "outdated" status can propagate to parents if they don't accept the children
  99. var outdatedDependencies = {};
  100. var outdatedModules = [];
  101. var appliedUpdate = {};
  102. var warnUnexpectedRequire = function warnUnexpectedRequire(module) {
  103. console.warn(
  104. "[HMR] unexpected require(" + module.id + ") to disposed module"
  105. );
  106. };
  107. for (var moduleId in currentUpdate) {
  108. if ($hasOwnProperty$(currentUpdate, moduleId)) {
  109. var newModuleFactory = currentUpdate[moduleId];
  110. /** @type {TODO} */
  111. var result;
  112. if (newModuleFactory) {
  113. result = getAffectedModuleEffects(moduleId);
  114. } else {
  115. result = {
  116. type: "disposed",
  117. moduleId: moduleId
  118. };
  119. }
  120. /** @type {Error|false} */
  121. var abortError = false;
  122. var doApply = false;
  123. var doDispose = false;
  124. var chainInfo = "";
  125. if (result.chain) {
  126. chainInfo = "\nUpdate propagation: " + result.chain.join(" -> ");
  127. }
  128. switch (result.type) {
  129. case "self-declined":
  130. if (options.onDeclined) options.onDeclined(result);
  131. if (!options.ignoreDeclined)
  132. abortError = new Error(
  133. "Aborted because of self decline: " +
  134. result.moduleId +
  135. chainInfo
  136. );
  137. break;
  138. case "declined":
  139. if (options.onDeclined) options.onDeclined(result);
  140. if (!options.ignoreDeclined)
  141. abortError = new Error(
  142. "Aborted because of declined dependency: " +
  143. result.moduleId +
  144. " in " +
  145. result.parentId +
  146. chainInfo
  147. );
  148. break;
  149. case "unaccepted":
  150. if (options.onUnaccepted) options.onUnaccepted(result);
  151. if (!options.ignoreUnaccepted)
  152. abortError = new Error(
  153. "Aborted because " + moduleId + " is not accepted" + chainInfo
  154. );
  155. break;
  156. case "accepted":
  157. if (options.onAccepted) options.onAccepted(result);
  158. doApply = true;
  159. break;
  160. case "disposed":
  161. if (options.onDisposed) options.onDisposed(result);
  162. doDispose = true;
  163. break;
  164. default:
  165. throw new Error("Unexception type " + result.type);
  166. }
  167. if (abortError) {
  168. return {
  169. error: abortError
  170. };
  171. }
  172. if (doApply) {
  173. appliedUpdate[moduleId] = newModuleFactory;
  174. addAllToSet(outdatedModules, result.outdatedModules);
  175. for (moduleId in result.outdatedDependencies) {
  176. if ($hasOwnProperty$(result.outdatedDependencies, moduleId)) {
  177. if (!outdatedDependencies[moduleId])
  178. outdatedDependencies[moduleId] = [];
  179. addAllToSet(
  180. outdatedDependencies[moduleId],
  181. result.outdatedDependencies[moduleId]
  182. );
  183. }
  184. }
  185. }
  186. if (doDispose) {
  187. addAllToSet(outdatedModules, [result.moduleId]);
  188. appliedUpdate[moduleId] = warnUnexpectedRequire;
  189. }
  190. }
  191. }
  192. currentUpdate = undefined;
  193. // Store self accepted outdated modules to require them later by the module system
  194. var outdatedSelfAcceptedModules = [];
  195. for (var j = 0; j < outdatedModules.length; j++) {
  196. var outdatedModuleId = outdatedModules[j];
  197. var module = $moduleCache$[outdatedModuleId];
  198. if (
  199. module &&
  200. (module.hot._selfAccepted || module.hot._main) &&
  201. // removed self-accepted modules should not be required
  202. appliedUpdate[outdatedModuleId] !== warnUnexpectedRequire &&
  203. // when called invalidate self-accepting is not possible
  204. !module.hot._selfInvalidated
  205. ) {
  206. outdatedSelfAcceptedModules.push({
  207. module: outdatedModuleId,
  208. require: module.hot._requireSelf,
  209. errorHandler: module.hot._selfAccepted
  210. });
  211. }
  212. }
  213. var moduleOutdatedDependencies;
  214. return {
  215. dispose: function () {
  216. currentUpdateRemovedChunks.forEach(function (chunkId) {
  217. delete $installedChunks$[chunkId];
  218. });
  219. currentUpdateRemovedChunks = undefined;
  220. var idx;
  221. var queue = outdatedModules.slice();
  222. while (queue.length > 0) {
  223. var moduleId = queue.pop();
  224. var module = $moduleCache$[moduleId];
  225. if (!module) continue;
  226. var data = {};
  227. // Call dispose handlers
  228. var disposeHandlers = module.hot._disposeHandlers;
  229. for (j = 0; j < disposeHandlers.length; j++) {
  230. disposeHandlers[j].call(null, data);
  231. }
  232. $hmrModuleData$[moduleId] = data;
  233. // disable module (this disables requires from this module)
  234. module.hot.active = false;
  235. // remove module from cache
  236. delete $moduleCache$[moduleId];
  237. // when disposing there is no need to call dispose handler
  238. delete outdatedDependencies[moduleId];
  239. // remove "parents" references from all children
  240. for (j = 0; j < module.children.length; j++) {
  241. var child = $moduleCache$[module.children[j]];
  242. if (!child) continue;
  243. idx = child.parents.indexOf(moduleId);
  244. if (idx >= 0) {
  245. child.parents.splice(idx, 1);
  246. }
  247. }
  248. }
  249. // remove outdated dependency from module children
  250. var dependency;
  251. for (var outdatedModuleId in outdatedDependencies) {
  252. if ($hasOwnProperty$(outdatedDependencies, outdatedModuleId)) {
  253. module = $moduleCache$[outdatedModuleId];
  254. if (module) {
  255. moduleOutdatedDependencies =
  256. outdatedDependencies[outdatedModuleId];
  257. for (j = 0; j < moduleOutdatedDependencies.length; j++) {
  258. dependency = moduleOutdatedDependencies[j];
  259. idx = module.children.indexOf(dependency);
  260. if (idx >= 0) module.children.splice(idx, 1);
  261. }
  262. }
  263. }
  264. }
  265. },
  266. apply: function (reportError) {
  267. // insert new code
  268. for (var updateModuleId in appliedUpdate) {
  269. if ($hasOwnProperty$(appliedUpdate, updateModuleId)) {
  270. $moduleFactories$[updateModuleId] = appliedUpdate[updateModuleId];
  271. }
  272. }
  273. // run new runtime modules
  274. for (var i = 0; i < currentUpdateRuntime.length; i++) {
  275. currentUpdateRuntime[i](__webpack_require__);
  276. }
  277. // call accept handlers
  278. for (var outdatedModuleId in outdatedDependencies) {
  279. if ($hasOwnProperty$(outdatedDependencies, outdatedModuleId)) {
  280. var module = $moduleCache$[outdatedModuleId];
  281. if (module) {
  282. moduleOutdatedDependencies =
  283. outdatedDependencies[outdatedModuleId];
  284. var callbacks = [];
  285. var errorHandlers = [];
  286. var dependenciesForCallbacks = [];
  287. for (var j = 0; j < moduleOutdatedDependencies.length; j++) {
  288. var dependency = moduleOutdatedDependencies[j];
  289. var acceptCallback =
  290. module.hot._acceptedDependencies[dependency];
  291. var errorHandler =
  292. module.hot._acceptedErrorHandlers[dependency];
  293. if (acceptCallback) {
  294. if (callbacks.indexOf(acceptCallback) !== -1) continue;
  295. callbacks.push(acceptCallback);
  296. errorHandlers.push(errorHandler);
  297. dependenciesForCallbacks.push(dependency);
  298. }
  299. }
  300. for (var k = 0; k < callbacks.length; k++) {
  301. try {
  302. callbacks[k].call(null, moduleOutdatedDependencies);
  303. } catch (err) {
  304. if (typeof errorHandlers[k] === "function") {
  305. try {
  306. errorHandlers[k](err, {
  307. moduleId: outdatedModuleId,
  308. dependencyId: dependenciesForCallbacks[k]
  309. });
  310. } catch (err2) {
  311. if (options.onErrored) {
  312. options.onErrored({
  313. type: "accept-error-handler-errored",
  314. moduleId: outdatedModuleId,
  315. dependencyId: dependenciesForCallbacks[k],
  316. error: err2,
  317. originalError: err
  318. });
  319. }
  320. if (!options.ignoreErrored) {
  321. reportError(err2);
  322. reportError(err);
  323. }
  324. }
  325. } else {
  326. if (options.onErrored) {
  327. options.onErrored({
  328. type: "accept-errored",
  329. moduleId: outdatedModuleId,
  330. dependencyId: dependenciesForCallbacks[k],
  331. error: err
  332. });
  333. }
  334. if (!options.ignoreErrored) {
  335. reportError(err);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. // Load self accepted modules
  344. for (var o = 0; o < outdatedSelfAcceptedModules.length; o++) {
  345. var item = outdatedSelfAcceptedModules[o];
  346. var moduleId = item.module;
  347. try {
  348. item.require(moduleId);
  349. } catch (err) {
  350. if (typeof item.errorHandler === "function") {
  351. try {
  352. item.errorHandler(err, {
  353. moduleId: moduleId,
  354. module: $moduleCache$[moduleId]
  355. });
  356. } catch (err2) {
  357. if (options.onErrored) {
  358. options.onErrored({
  359. type: "self-accept-error-handler-errored",
  360. moduleId: moduleId,
  361. error: err2,
  362. originalError: err
  363. });
  364. }
  365. if (!options.ignoreErrored) {
  366. reportError(err2);
  367. reportError(err);
  368. }
  369. }
  370. } else {
  371. if (options.onErrored) {
  372. options.onErrored({
  373. type: "self-accept-errored",
  374. moduleId: moduleId,
  375. error: err
  376. });
  377. }
  378. if (!options.ignoreErrored) {
  379. reportError(err);
  380. }
  381. }
  382. }
  383. }
  384. return outdatedModules;
  385. }
  386. };
  387. }
  388. $hmrInvalidateModuleHandlers$.$key$ = function (moduleId, applyHandlers) {
  389. if (!currentUpdate) {
  390. currentUpdate = {};
  391. currentUpdateRuntime = [];
  392. currentUpdateRemovedChunks = [];
  393. applyHandlers.push(applyHandler);
  394. }
  395. if (!$hasOwnProperty$(currentUpdate, moduleId)) {
  396. currentUpdate[moduleId] = $moduleFactories$[moduleId];
  397. }
  398. };
  399. $hmrDownloadUpdateHandlers$.$key$ = function (
  400. chunkIds,
  401. removedChunks,
  402. removedModules,
  403. promises,
  404. applyHandlers,
  405. updatedModulesList
  406. ) {
  407. applyHandlers.push(applyHandler);
  408. currentUpdateChunks = {};
  409. currentUpdateRemovedChunks = removedChunks;
  410. currentUpdate = removedModules.reduce(function (obj, key) {
  411. obj[key] = false;
  412. return obj;
  413. }, {});
  414. currentUpdateRuntime = [];
  415. chunkIds.forEach(function (chunkId) {
  416. if (
  417. $hasOwnProperty$($installedChunks$, chunkId) &&
  418. $installedChunks$[chunkId] !== undefined
  419. ) {
  420. promises.push($loadUpdateChunk$(chunkId, updatedModulesList));
  421. currentUpdateChunks[chunkId] = true;
  422. } else {
  423. currentUpdateChunks[chunkId] = false;
  424. }
  425. });
  426. if ($ensureChunkHandlers$) {
  427. $ensureChunkHandlers$.$key$Hmr = function (chunkId, promises) {
  428. if (
  429. currentUpdateChunks &&
  430. $hasOwnProperty$(currentUpdateChunks, chunkId) &&
  431. !currentUpdateChunks[chunkId]
  432. ) {
  433. promises.push($loadUpdateChunk$(chunkId));
  434. currentUpdateChunks[chunkId] = true;
  435. }
  436. };
  437. }
  438. };
  439. };