HotModuleReplacement.runtime.js 16 KB

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