You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HotModuleReplacement.runtime.js 17KB

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