HotModuleReplacement.runtime.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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.slice().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. )
  446. outdatedSelfAcceptedModules.push({
  447. module: moduleId,
  448. errorHandler: installedModules[moduleId].hot._selfAccepted
  449. });
  450. }
  451. // Now in "dispose" phase
  452. hotSetStatus("dispose");
  453. Object.keys(hotAvailableFilesMap).forEach(function(chunkId) {
  454. if (hotAvailableFilesMap[chunkId] === false) {
  455. hotDisposeChunk(chunkId);
  456. }
  457. });
  458. var idx;
  459. var queue = outdatedModules.slice();
  460. while (queue.length > 0) {
  461. moduleId = queue.pop();
  462. module = installedModules[moduleId];
  463. if (!module) continue;
  464. var data = {};
  465. // Call dispose handlers
  466. var disposeHandlers = module.hot._disposeHandlers;
  467. for (j = 0; j < disposeHandlers.length; j++) {
  468. cb = disposeHandlers[j];
  469. cb(data);
  470. }
  471. hotCurrentModuleData[moduleId] = data;
  472. // disable module (this disables requires from this module)
  473. module.hot.active = false;
  474. // remove module from cache
  475. delete installedModules[moduleId];
  476. // when disposing there is no need to call dispose handler
  477. delete outdatedDependencies[moduleId];
  478. // remove "parents" references from all children
  479. for (j = 0; j < module.children.length; j++) {
  480. var child = installedModules[module.children[j]];
  481. if (!child) continue;
  482. idx = child.parents.indexOf(moduleId);
  483. if (idx >= 0) {
  484. child.parents.splice(idx, 1);
  485. }
  486. }
  487. }
  488. // remove outdated dependency from module children
  489. var dependency;
  490. var moduleOutdatedDependencies;
  491. for (moduleId in outdatedDependencies) {
  492. if (
  493. Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)
  494. ) {
  495. module = installedModules[moduleId];
  496. if (module) {
  497. moduleOutdatedDependencies = outdatedDependencies[moduleId];
  498. for (j = 0; j < moduleOutdatedDependencies.length; j++) {
  499. dependency = moduleOutdatedDependencies[j];
  500. idx = module.children.indexOf(dependency);
  501. if (idx >= 0) module.children.splice(idx, 1);
  502. }
  503. }
  504. }
  505. }
  506. // Not in "apply" phase
  507. hotSetStatus("apply");
  508. hotCurrentHash = hotUpdateNewHash;
  509. // insert new code
  510. for (moduleId in appliedUpdate) {
  511. if (Object.prototype.hasOwnProperty.call(appliedUpdate, moduleId)) {
  512. modules[moduleId] = appliedUpdate[moduleId];
  513. }
  514. }
  515. // call accept handlers
  516. var error = null;
  517. for (moduleId in outdatedDependencies) {
  518. if (
  519. Object.prototype.hasOwnProperty.call(outdatedDependencies, moduleId)
  520. ) {
  521. module = installedModules[moduleId];
  522. if (module) {
  523. moduleOutdatedDependencies = outdatedDependencies[moduleId];
  524. var callbacks = [];
  525. for (i = 0; i < moduleOutdatedDependencies.length; i++) {
  526. dependency = moduleOutdatedDependencies[i];
  527. cb = module.hot._acceptedDependencies[dependency];
  528. if (cb) {
  529. if (callbacks.indexOf(cb) !== -1) continue;
  530. callbacks.push(cb);
  531. }
  532. }
  533. for (i = 0; i < callbacks.length; i++) {
  534. cb = callbacks[i];
  535. try {
  536. cb(moduleOutdatedDependencies);
  537. } catch (err) {
  538. if (options.onErrored) {
  539. options.onErrored({
  540. type: "accept-errored",
  541. moduleId: moduleId,
  542. dependencyId: moduleOutdatedDependencies[i],
  543. error: err
  544. });
  545. }
  546. if (!options.ignoreErrored) {
  547. if (!error) error = err;
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. // Load self accepted modules
  555. for (i = 0; i < outdatedSelfAcceptedModules.length; i++) {
  556. var item = outdatedSelfAcceptedModules[i];
  557. moduleId = item.module;
  558. hotCurrentParents = [moduleId];
  559. try {
  560. $require$(moduleId);
  561. } catch (err) {
  562. if (typeof item.errorHandler === "function") {
  563. try {
  564. item.errorHandler(err);
  565. } catch (err2) {
  566. if (options.onErrored) {
  567. options.onErrored({
  568. type: "self-accept-error-handler-errored",
  569. moduleId: moduleId,
  570. error: err2,
  571. originalError: err
  572. });
  573. }
  574. if (!options.ignoreErrored) {
  575. if (!error) error = err2;
  576. }
  577. if (!error) error = err;
  578. }
  579. } else {
  580. if (options.onErrored) {
  581. options.onErrored({
  582. type: "self-accept-errored",
  583. moduleId: moduleId,
  584. error: err
  585. });
  586. }
  587. if (!options.ignoreErrored) {
  588. if (!error) error = err;
  589. }
  590. }
  591. }
  592. }
  593. // handle errors in accept handlers and self accepted module load
  594. if (error) {
  595. hotSetStatus("fail");
  596. return Promise.reject(error);
  597. }
  598. hotSetStatus("idle");
  599. return new Promise(function(resolve) {
  600. resolve(outdatedModules);
  601. });
  602. }
  603. };