HotModuleReplacement.runtime.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. // eslint-disable no-unused-vars
  6. var $hash$ = undefined;
  7. var $requestTimeout$ = undefined;
  8. var installedModules = undefined;
  9. var $require$ = undefined;
  10. var hotDownloadManifest = undefined;
  11. var hotDownloadUpdateChunk = undefined;
  12. var hotDisposeChunk = undefined;
  13. var modules = undefined;
  14. var chunkId = undefined;
  15. module.exports = function() {
  16. var hotApplyOnUpdate = true;
  17. // eslint-disable-next-line no-unused-vars
  18. var hotCurrentHash = $hash$;
  19. var hotRequestTimeout = $requestTimeout$;
  20. var hotCurrentModuleData = {};
  21. var hotCurrentChildModule;
  22. // eslint-disable-next-line no-unused-vars
  23. var hotCurrentParents = [];
  24. // eslint-disable-next-line no-unused-vars
  25. var hotCurrentParentsTemp = [];
  26. // eslint-disable-next-line no-unused-vars
  27. function hotCreateRequire(moduleId) {
  28. var me = installedModules[moduleId];
  29. if (!me) return $require$;
  30. var fn = function(request) {
  31. if (me.hot.active) {
  32. if (installedModules[request]) {
  33. if (installedModules[request].parents.indexOf(moduleId) === -1) {
  34. installedModules[request].parents.push(moduleId);
  35. }
  36. } else {
  37. hotCurrentParents = [moduleId];
  38. hotCurrentChildModule = request;
  39. }
  40. if (me.children.indexOf(request) === -1) {
  41. me.children.push(request);
  42. }
  43. } else {
  44. console.warn(
  45. "[HMR] unexpected require(" +
  46. request +
  47. ") from disposed module " +
  48. moduleId
  49. );
  50. hotCurrentParents = [];
  51. }
  52. return $require$(request);
  53. };
  54. var ObjectFactory = function ObjectFactory(name) {
  55. return {
  56. configurable: true,
  57. enumerable: true,
  58. get: function() {
  59. return $require$[name];
  60. },
  61. set: function(value) {
  62. $require$[name] = value;
  63. }
  64. };
  65. };
  66. for (var name in $require$) {
  67. if (
  68. Object.prototype.hasOwnProperty.call($require$, name) &&
  69. name !== "e" &&
  70. name !== "t"
  71. ) {
  72. Object.defineProperty(fn, name, ObjectFactory(name));
  73. }
  74. }
  75. fn.e = function(chunkId) {
  76. if (hotStatus === "ready") hotSetStatus("prepare");
  77. hotChunksLoading++;
  78. return $require$.e(chunkId).then(finishChunkLoading, function(err) {
  79. finishChunkLoading();
  80. throw err;
  81. });
  82. function finishChunkLoading() {
  83. hotChunksLoading--;
  84. if (hotStatus === "prepare") {
  85. if (!hotWaitingFilesMap[chunkId]) {
  86. hotEnsureUpdateChunk(chunkId);
  87. }
  88. if (hotChunksLoading === 0 && hotWaitingFiles === 0) {
  89. hotUpdateDownloaded();
  90. }
  91. }
  92. }
  93. };
  94. fn.t = function(value, mode) {
  95. if (mode & 1) value = fn(value);
  96. return $require$.t(value, mode & ~1);
  97. };
  98. return fn;
  99. }
  100. // eslint-disable-next-line no-unused-vars
  101. function hotCreateModule(moduleId) {
  102. var hot = {
  103. // private stuff
  104. _acceptedDependencies: {},
  105. _declinedDependencies: {},
  106. _selfAccepted: false,
  107. _selfDeclined: false,
  108. _disposeHandlers: [],
  109. _main: hotCurrentChildModule !== moduleId,
  110. // Module API
  111. active: true,
  112. accept: function(dep, callback) {
  113. if (dep === undefined) hot._selfAccepted = true;
  114. else if (typeof dep === "function") hot._selfAccepted = dep;
  115. else if (typeof dep === "object")
  116. for (var i = 0; i < dep.length; i++)
  117. hot._acceptedDependencies[dep[i]] = callback || function() {};
  118. else hot._acceptedDependencies[dep] = callback || function() {};
  119. },
  120. decline: function(dep) {
  121. if (dep === undefined) hot._selfDeclined = true;
  122. else if (typeof dep === "object")
  123. for (var i = 0; i < dep.length; i++)
  124. hot._declinedDependencies[dep[i]] = true;
  125. else hot._declinedDependencies[dep] = true;
  126. },
  127. dispose: function(callback) {
  128. hot._disposeHandlers.push(callback);
  129. },
  130. addDisposeHandler: function(callback) {
  131. hot._disposeHandlers.push(callback);
  132. },
  133. removeDisposeHandler: function(callback) {
  134. var idx = hot._disposeHandlers.indexOf(callback);
  135. if (idx >= 0) hot._disposeHandlers.splice(idx, 1);
  136. },
  137. // Management API
  138. check: hotCheck,
  139. apply: hotApply,
  140. status: function(l) {
  141. if (!l) return hotStatus;
  142. hotStatusHandlers.push(l);
  143. },
  144. addStatusHandler: function(l) {
  145. hotStatusHandlers.push(l);
  146. },
  147. removeStatusHandler: function(l) {
  148. var idx = hotStatusHandlers.indexOf(l);
  149. if (idx >= 0) hotStatusHandlers.splice(idx, 1);
  150. },
  151. //inherit from previous dispose call
  152. data: hotCurrentModuleData[moduleId]
  153. };
  154. hotCurrentChildModule = undefined;
  155. return hot;
  156. }
  157. var hotStatusHandlers = [];
  158. var hotStatus = "idle";
  159. function hotSetStatus(newStatus) {
  160. hotStatus = newStatus;
  161. for (var i = 0; i < hotStatusHandlers.length; i++)
  162. hotStatusHandlers[i].call(null, newStatus);
  163. }
  164. // while downloading
  165. var hotWaitingFiles = 0;
  166. var hotChunksLoading = 0;
  167. var hotWaitingFilesMap = {};
  168. var hotRequestedFilesMap = {};
  169. var hotAvailableFilesMap = {};
  170. var hotDeferred;
  171. // The update info
  172. var hotUpdate, hotUpdateNewHash;
  173. function toModuleId(id) {
  174. var isNumber = +id + "" === id;
  175. return isNumber ? +id : id;
  176. }
  177. function hotCheck(apply) {
  178. if (hotStatus !== "idle") {
  179. throw new Error("check() is only allowed in idle status");
  180. }
  181. hotApplyOnUpdate = apply;
  182. hotSetStatus("check");
  183. return hotDownloadManifest(hotRequestTimeout).then(function(update) {
  184. if (!update) {
  185. hotSetStatus("idle");
  186. return null;
  187. }
  188. hotRequestedFilesMap = {};
  189. hotWaitingFilesMap = {};
  190. hotAvailableFilesMap = update.c;
  191. hotUpdateNewHash = update.h;
  192. hotSetStatus("prepare");
  193. var promise = new Promise(function(resolve, reject) {
  194. hotDeferred = {
  195. resolve: resolve,
  196. reject: reject
  197. };
  198. });
  199. hotUpdate = {};
  200. /*foreachInstalledChunks*/
  201. // eslint-disable-next-line no-lone-blocks
  202. {
  203. hotEnsureUpdateChunk(chunkId);
  204. }
  205. if (
  206. hotStatus === "prepare" &&
  207. hotChunksLoading === 0 &&
  208. hotWaitingFiles === 0
  209. ) {
  210. hotUpdateDownloaded();
  211. }
  212. return promise;
  213. });
  214. }
  215. // eslint-disable-next-line no-unused-vars
  216. function hotAddUpdateChunk(chunkId, moreModules) {
  217. if (!hotAvailableFilesMap[chunkId] || !hotRequestedFilesMap[chunkId])
  218. return;
  219. hotRequestedFilesMap[chunkId] = false;
  220. for (var moduleId in moreModules) {
  221. if (Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
  222. hotUpdate[moduleId] = moreModules[moduleId];
  223. }
  224. }
  225. if (--hotWaitingFiles === 0 && hotChunksLoading === 0) {
  226. hotUpdateDownloaded();
  227. }
  228. }
  229. function hotEnsureUpdateChunk(chunkId) {
  230. if (!hotAvailableFilesMap[chunkId]) {
  231. hotWaitingFilesMap[chunkId] = true;
  232. } else {
  233. hotRequestedFilesMap[chunkId] = true;
  234. hotWaitingFiles++;
  235. hotDownloadUpdateChunk(chunkId);
  236. }
  237. }
  238. function hotUpdateDownloaded() {
  239. hotSetStatus("ready");
  240. var deferred = hotDeferred;
  241. hotDeferred = null;
  242. if (!deferred) return;
  243. if (hotApplyOnUpdate) {
  244. // Wrap deferred object in Promise to mark it as a well-handled Promise to
  245. // avoid triggering uncaught exception warning in Chrome.
  246. // See https://bugs.chromium.org/p/chromium/issues/detail?id=465666
  247. Promise.resolve()
  248. .then(function() {
  249. return hotApply(hotApplyOnUpdate);
  250. })
  251. .then(
  252. function(result) {
  253. deferred.resolve(result);
  254. },
  255. function(err) {
  256. deferred.reject(err);
  257. }
  258. );
  259. } else {
  260. var outdatedModules = [];
  261. for (var id in hotUpdate) {
  262. if (Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
  263. outdatedModules.push(toModuleId(id));
  264. }
  265. }
  266. deferred.resolve(outdatedModules);
  267. }
  268. }
  269. function hotApply(options) {
  270. if (hotStatus !== "ready")
  271. throw new Error("apply() is only allowed in ready status");
  272. options = options || {};
  273. var cb;
  274. var i;
  275. var j;
  276. var module;
  277. var moduleId;
  278. function getAffectedStuff(updateModuleId) {
  279. var outdatedModules = [updateModuleId];
  280. var outdatedDependencies = {};
  281. var queue = outdatedModules.map(function(id) {
  282. return {
  283. chain: [id],
  284. id: id
  285. };
  286. });
  287. while (queue.length > 0) {
  288. var queueItem = queue.pop();
  289. var moduleId = queueItem.id;
  290. var chain = queueItem.chain;
  291. module = installedModules[moduleId];
  292. if (!module || module.hot._selfAccepted) continue;
  293. if (module.hot._selfDeclined) {
  294. return {
  295. type: "self-declined",
  296. chain: chain,
  297. moduleId: moduleId
  298. };
  299. }
  300. if (module.hot._main) {
  301. return {
  302. type: "unaccepted",
  303. chain: chain,
  304. moduleId: moduleId
  305. };
  306. }
  307. for (var i = 0; i < module.parents.length; i++) {
  308. var parentId = module.parents[i];
  309. var parent = installedModules[parentId];
  310. if (!parent) continue;
  311. if (parent.hot._declinedDependencies[moduleId]) {
  312. return {
  313. type: "declined",
  314. chain: chain.concat([parentId]),
  315. moduleId: moduleId,
  316. parentId: parentId
  317. };
  318. }
  319. if (outdatedModules.indexOf(parentId) !== -1) continue;
  320. if (parent.hot._acceptedDependencies[moduleId]) {
  321. if (!outdatedDependencies[parentId])
  322. outdatedDependencies[parentId] = [];
  323. addAllToSet(outdatedDependencies[parentId], [moduleId]);
  324. continue;
  325. }
  326. delete outdatedDependencies[parentId];
  327. outdatedModules.push(parentId);
  328. queue.push({
  329. chain: chain.concat([parentId]),
  330. id: parentId
  331. });
  332. }
  333. }
  334. return {
  335. type: "accepted",
  336. moduleId: updateModuleId,
  337. outdatedModules: outdatedModules,
  338. outdatedDependencies: outdatedDependencies
  339. };
  340. }
  341. function addAllToSet(a, b) {
  342. for (var i = 0; i < b.length; i++) {
  343. var item = b[i];
  344. if (a.indexOf(item) === -1) a.push(item);
  345. }
  346. }
  347. // at begin all updates modules are outdated
  348. // the "outdated" status can propagate to parents if they don't accept the children
  349. var outdatedDependencies = {};
  350. var outdatedModules = [];
  351. var appliedUpdate = {};
  352. var warnUnexpectedRequire = function warnUnexpectedRequire() {
  353. console.warn(
  354. "[HMR] unexpected require(" + result.moduleId + ") to disposed module"
  355. );
  356. };
  357. for (var id in hotUpdate) {
  358. if (Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
  359. moduleId = toModuleId(id);
  360. /** @type {TODO} */
  361. var result;
  362. if (hotUpdate[id]) {
  363. result = getAffectedStuff(moduleId);
  364. } else {
  365. result = {
  366. type: "disposed",
  367. moduleId: id
  368. };
  369. }
  370. /** @type {Error|false} */
  371. var abortError = false;
  372. var doApply = false;
  373. var doDispose = false;
  374. var chainInfo = "";
  375. if (result.chain) {
  376. chainInfo = "\nUpdate propagation: " + result.chain.join(" -> ");
  377. }
  378. switch (result.type) {
  379. case "self-declined":
  380. if (options.onDeclined) options.onDeclined(result);
  381. if (!options.ignoreDeclined)
  382. abortError = new Error(
  383. "Aborted because of self decline: " +
  384. result.moduleId +
  385. chainInfo
  386. );
  387. break;
  388. case "declined":
  389. if (options.onDeclined) options.onDeclined(result);
  390. if (!options.ignoreDeclined)
  391. abortError = new Error(
  392. "Aborted because of declined dependency: " +
  393. result.moduleId +
  394. " in " +
  395. result.parentId +
  396. chainInfo
  397. );
  398. break;
  399. case "unaccepted":
  400. if (options.onUnaccepted) options.onUnaccepted(result);
  401. if (!options.ignoreUnaccepted)
  402. abortError = new Error(
  403. "Aborted because " + moduleId + " is not accepted" + chainInfo
  404. );
  405. break;
  406. case "accepted":
  407. if (options.onAccepted) options.onAccepted(result);
  408. doApply = true;
  409. break;
  410. case "disposed":
  411. if (options.onDisposed) options.onDisposed(result);
  412. doDispose = true;
  413. break;
  414. default:
  415. throw new Error("Unexception type " + result.type);
  416. }
  417. if (abortError) {
  418. hotSetStatus("abort");
  419. return Promise.reject(abortError);
  420. }
  421. if (doApply) {
  422. appliedUpdate[moduleId] = hotUpdate[moduleId];
  423. addAllToSet(outdatedModules, result.outdatedModules);
  424. for (moduleId in result.outdatedDependencies) {
  425. if (
  426. Object.prototype.hasOwnProperty.call(
  427. result.outdatedDependencies,
  428. moduleId
  429. )
  430. ) {
  431. if (!outdatedDependencies[moduleId])
  432. outdatedDependencies[moduleId] = [];
  433. addAllToSet(
  434. outdatedDependencies[moduleId],
  435. result.outdatedDependencies[moduleId]
  436. );
  437. }
  438. }
  439. }
  440. if (doDispose) {
  441. addAllToSet(outdatedModules, [result.moduleId]);
  442. appliedUpdate[moduleId] = warnUnexpectedRequire;
  443. }
  444. }
  445. }
  446. // Store self accepted outdated modules to require them later by the module system
  447. var outdatedSelfAcceptedModules = [];
  448. for (i = 0; i < outdatedModules.length; i++) {
  449. moduleId = outdatedModules[i];
  450. if (
  451. installedModules[moduleId] &&
  452. installedModules[moduleId].hot._selfAccepted &&
  453. // removed self-accepted modules should not be required
  454. appliedUpdate[moduleId] !== warnUnexpectedRequire
  455. ) {
  456. outdatedSelfAcceptedModules.push({
  457. module: moduleId,
  458. errorHandler: installedModules[moduleId].hot._selfAccepted
  459. });
  460. }
  461. }
  462. // Now in "dispose" phase
  463. hotSetStatus("dispose");
  464. Object.keys(hotAvailableFilesMap).forEach(function(chunkId) {
  465. if (hotAvailableFilesMap[chunkId] === false) {
  466. hotDisposeChunk(chunkId);
  467. }
  468. });
  469. var idx;
  470. var queue = outdatedModules.slice();
  471. while (queue.length > 0) {
  472. moduleId = queue.pop();
  473. module = installedModules[moduleId];
  474. if (!module) continue;
  475. var data = {};
  476. // Call dispose handlers
  477. var disposeHandlers = module.hot._disposeHandlers;
  478. for (j = 0; j < disposeHandlers.length; j++) {
  479. cb = disposeHandlers[j];
  480. cb(data);
  481. }
  482. hotCurrentModuleData[moduleId] = data;
  483. // disable module (this disables requires from this module)
  484. module.hot.active = false;
  485. // remove module from cache
  486. delete installedModules[moduleId];
  487. // when disposing there is no need to call dispose handler
  488. delete outdatedDependencies[moduleId];
  489. // remove "parents" references from all children
  490. for (j = 0; j < module.children.length; j++) {
  491. var child = installedModules[module.children[j]];
  492. if (!child) continue;
  493. idx = child.parents.indexOf(moduleId);
  494. if (idx >= 0) {
  495. child.parents.splice(idx, 1);
  496. }
  497. }
  498. }
  499. // remove outdated dependency from module children
  500. var dependency;
  501. var moduleOutdatedDependencies;
  502. for (moduleId in outdatedDependencies) {
  503. if (
  504. Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)
  505. ) {
  506. module = installedModules[moduleId];
  507. if (module) {
  508. moduleOutdatedDependencies = outdatedDependencies[moduleId];
  509. for (j = 0; j < moduleOutdatedDependencies.length; j++) {
  510. dependency = moduleOutdatedDependencies[j];
  511. idx = module.children.indexOf(dependency);
  512. if (idx >= 0) module.children.splice(idx, 1);
  513. }
  514. }
  515. }
  516. }
  517. // Now in "apply" phase
  518. hotSetStatus("apply");
  519. hotCurrentHash = hotUpdateNewHash;
  520. // insert new code
  521. for (moduleId in appliedUpdate) {
  522. if (Object.prototype.hasOwnProperty.call(appliedUpdate, moduleId)) {
  523. modules[moduleId] = appliedUpdate[moduleId];
  524. }
  525. }
  526. // call accept handlers
  527. var error = null;
  528. for (moduleId in outdatedDependencies) {
  529. if (
  530. Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)
  531. ) {
  532. module = installedModules[moduleId];
  533. if (module) {
  534. moduleOutdatedDependencies = outdatedDependencies[moduleId];
  535. var callbacks = [];
  536. for (i = 0; i < moduleOutdatedDependencies.length; i++) {
  537. dependency = moduleOutdatedDependencies[i];
  538. cb = module.hot._acceptedDependencies[dependency];
  539. if (cb) {
  540. if (callbacks.indexOf(cb) !== -1) continue;
  541. callbacks.push(cb);
  542. }
  543. }
  544. for (i = 0; i < callbacks.length; i++) {
  545. cb = callbacks[i];
  546. try {
  547. cb(moduleOutdatedDependencies);
  548. } catch (err) {
  549. if (options.onErrored) {
  550. options.onErrored({
  551. type: "accept-errored",
  552. moduleId: moduleId,
  553. dependencyId: moduleOutdatedDependencies[i],
  554. error: err
  555. });
  556. }
  557. if (!options.ignoreErrored) {
  558. if (!error) error = err;
  559. }
  560. }
  561. }
  562. }
  563. }
  564. }
  565. // Load self accepted modules
  566. for (i = 0; i < outdatedSelfAcceptedModules.length; i++) {
  567. var item = outdatedSelfAcceptedModules[i];
  568. moduleId = item.module;
  569. hotCurrentParents = [moduleId];
  570. try {
  571. $require$(moduleId);
  572. } catch (err) {
  573. if (typeof item.errorHandler === "function") {
  574. try {
  575. item.errorHandler(err);
  576. } catch (err2) {
  577. if (options.onErrored) {
  578. options.onErrored({
  579. type: "self-accept-error-handler-errored",
  580. moduleId: moduleId,
  581. error: err2,
  582. originalError: err
  583. });
  584. }
  585. if (!options.ignoreErrored) {
  586. if (!error) error = err2;
  587. }
  588. if (!error) error = err;
  589. }
  590. } else {
  591. if (options.onErrored) {
  592. options.onErrored({
  593. type: "self-accept-errored",
  594. moduleId: moduleId,
  595. error: err
  596. });
  597. }
  598. if (!options.ignoreErrored) {
  599. if (!error) error = err;
  600. }
  601. }
  602. }
  603. }
  604. // handle errors in accept handlers and self accepted module load
  605. if (error) {
  606. hotSetStatus("fail");
  607. return Promise.reject(error);
  608. }
  609. hotSetStatus("idle");
  610. return new Promise(function(resolve) {
  611. resolve(outdatedModules);
  612. });
  613. }
  614. };