HotModuleReplacement.runtime.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. var $interceptModuleExecution$ = undefined;
  7. var $moduleCache$ = undefined;
  8. // eslint-disable-next-line no-unused-vars
  9. var $hmrModuleData$ = undefined;
  10. /** @type {() => Promise} */
  11. var $hmrDownloadManifest$ = undefined;
  12. var $hmrDownloadUpdateHandlers$ = undefined;
  13. var $hmrInvalidateModuleHandlers$ = undefined;
  14. var __webpack_require__ = undefined;
  15. module.exports = function () {
  16. var currentModuleData = {};
  17. var installedModules = $moduleCache$;
  18. // module and require creation
  19. var currentChildModule;
  20. var currentParents = [];
  21. // status
  22. var registeredStatusHandlers = [];
  23. var currentStatus = "idle";
  24. // while downloading
  25. var blockingPromises = 0;
  26. var blockingPromisesWaiting = [];
  27. // The update info
  28. var currentUpdateApplyHandlers;
  29. var queuedInvalidatedModules;
  30. $hmrModuleData$ = currentModuleData;
  31. $interceptModuleExecution$.push(function (options) {
  32. var module = options.module;
  33. var require = createRequire(options.require, options.id);
  34. module.hot = createModuleHotObject(options.id, module);
  35. module.parents = currentParents;
  36. module.children = [];
  37. currentParents = [];
  38. options.require = require;
  39. });
  40. $hmrDownloadUpdateHandlers$ = {};
  41. $hmrInvalidateModuleHandlers$ = {};
  42. function createRequire(require, moduleId) {
  43. var me = installedModules[moduleId];
  44. if (!me) return require;
  45. var fn = function (request) {
  46. if (me.hot.active) {
  47. if (installedModules[request]) {
  48. var parents = installedModules[request].parents;
  49. if (parents.indexOf(moduleId) === -1) {
  50. parents.push(moduleId);
  51. }
  52. } else {
  53. currentParents = [moduleId];
  54. currentChildModule = request;
  55. }
  56. if (me.children.indexOf(request) === -1) {
  57. me.children.push(request);
  58. }
  59. } else {
  60. console.warn(
  61. "[HMR] unexpected require(" +
  62. request +
  63. ") from disposed module " +
  64. moduleId
  65. );
  66. currentParents = [];
  67. }
  68. return require(request);
  69. };
  70. var createPropertyDescriptor = function (name) {
  71. return {
  72. configurable: true,
  73. enumerable: true,
  74. get: function () {
  75. return require[name];
  76. },
  77. set: function (value) {
  78. require[name] = value;
  79. }
  80. };
  81. };
  82. for (var name in require) {
  83. if (Object.prototype.hasOwnProperty.call(require, name) && name !== "e") {
  84. Object.defineProperty(fn, name, createPropertyDescriptor(name));
  85. }
  86. }
  87. fn.e = function (chunkId, fetchPriority) {
  88. return trackBlockingPromise(require.e(chunkId, fetchPriority));
  89. };
  90. return fn;
  91. }
  92. function createModuleHotObject(moduleId, me) {
  93. var _main = currentChildModule !== moduleId;
  94. var hot = {
  95. // private stuff
  96. _acceptedDependencies: {},
  97. _acceptedErrorHandlers: {},
  98. _declinedDependencies: {},
  99. _selfAccepted: false,
  100. _selfDeclined: false,
  101. _selfInvalidated: false,
  102. _disposeHandlers: [],
  103. _main: _main,
  104. _requireSelf: function () {
  105. currentParents = me.parents.slice();
  106. currentChildModule = _main ? undefined : moduleId;
  107. __webpack_require__(moduleId);
  108. },
  109. // Module API
  110. active: true,
  111. accept: function (dep, callback, errorHandler) {
  112. if (dep === undefined) hot._selfAccepted = true;
  113. else if (typeof dep === "function") hot._selfAccepted = dep;
  114. else if (typeof dep === "object" && dep !== null) {
  115. for (var i = 0; i < dep.length; i++) {
  116. hot._acceptedDependencies[dep[i]] = callback || function () {};
  117. hot._acceptedErrorHandlers[dep[i]] = errorHandler;
  118. }
  119. } else {
  120. hot._acceptedDependencies[dep] = callback || function () {};
  121. hot._acceptedErrorHandlers[dep] = errorHandler;
  122. }
  123. },
  124. decline: function (dep) {
  125. if (dep === undefined) hot._selfDeclined = true;
  126. else if (typeof dep === "object" && dep !== null)
  127. for (var i = 0; i < dep.length; i++)
  128. hot._declinedDependencies[dep[i]] = true;
  129. else hot._declinedDependencies[dep] = true;
  130. },
  131. dispose: function (callback) {
  132. hot._disposeHandlers.push(callback);
  133. },
  134. addDisposeHandler: function (callback) {
  135. hot._disposeHandlers.push(callback);
  136. },
  137. removeDisposeHandler: function (callback) {
  138. var idx = hot._disposeHandlers.indexOf(callback);
  139. if (idx >= 0) hot._disposeHandlers.splice(idx, 1);
  140. },
  141. invalidate: function () {
  142. this._selfInvalidated = true;
  143. switch (currentStatus) {
  144. case "idle":
  145. currentUpdateApplyHandlers = [];
  146. Object.keys($hmrInvalidateModuleHandlers$).forEach(function (key) {
  147. $hmrInvalidateModuleHandlers$[key](
  148. moduleId,
  149. currentUpdateApplyHandlers
  150. );
  151. });
  152. setStatus("ready");
  153. break;
  154. case "ready":
  155. Object.keys($hmrInvalidateModuleHandlers$).forEach(function (key) {
  156. $hmrInvalidateModuleHandlers$[key](
  157. moduleId,
  158. currentUpdateApplyHandlers
  159. );
  160. });
  161. break;
  162. case "prepare":
  163. case "check":
  164. case "dispose":
  165. case "apply":
  166. (queuedInvalidatedModules = queuedInvalidatedModules || []).push(
  167. moduleId
  168. );
  169. break;
  170. default:
  171. // ignore requests in error states
  172. break;
  173. }
  174. },
  175. // Management API
  176. check: hotCheck,
  177. apply: hotApply,
  178. status: function (l) {
  179. if (!l) return currentStatus;
  180. registeredStatusHandlers.push(l);
  181. },
  182. addStatusHandler: function (l) {
  183. registeredStatusHandlers.push(l);
  184. },
  185. removeStatusHandler: function (l) {
  186. var idx = registeredStatusHandlers.indexOf(l);
  187. if (idx >= 0) registeredStatusHandlers.splice(idx, 1);
  188. },
  189. //inherit from previous dispose call
  190. data: currentModuleData[moduleId]
  191. };
  192. currentChildModule = undefined;
  193. return hot;
  194. }
  195. function setStatus(newStatus) {
  196. currentStatus = newStatus;
  197. var results = [];
  198. for (var i = 0; i < registeredStatusHandlers.length; i++)
  199. results[i] = registeredStatusHandlers[i].call(null, newStatus);
  200. return Promise.all(results).then(function () {});
  201. }
  202. function unblock() {
  203. if (--blockingPromises === 0) {
  204. setStatus("ready").then(function () {
  205. if (blockingPromises === 0) {
  206. var list = blockingPromisesWaiting;
  207. blockingPromisesWaiting = [];
  208. for (var i = 0; i < list.length; i++) {
  209. list[i]();
  210. }
  211. }
  212. });
  213. }
  214. }
  215. function trackBlockingPromise(promise) {
  216. switch (currentStatus) {
  217. case "ready":
  218. setStatus("prepare");
  219. /* fallthrough */
  220. case "prepare":
  221. blockingPromises++;
  222. promise.then(unblock, unblock);
  223. return promise;
  224. default:
  225. return promise;
  226. }
  227. }
  228. function waitForBlockingPromises(fn) {
  229. if (blockingPromises === 0) return fn();
  230. return new Promise(function (resolve) {
  231. blockingPromisesWaiting.push(function () {
  232. resolve(fn());
  233. });
  234. });
  235. }
  236. function hotCheck(applyOnUpdate) {
  237. if (currentStatus !== "idle") {
  238. throw new Error("check() is only allowed in idle status");
  239. }
  240. return setStatus("check")
  241. .then($hmrDownloadManifest$)
  242. .then(function (update) {
  243. if (!update) {
  244. return setStatus(applyInvalidatedModules() ? "ready" : "idle").then(
  245. function () {
  246. return null;
  247. }
  248. );
  249. }
  250. return setStatus("prepare").then(function () {
  251. var updatedModules = [];
  252. currentUpdateApplyHandlers = [];
  253. return Promise.all(
  254. Object.keys($hmrDownloadUpdateHandlers$).reduce(function (
  255. promises,
  256. key
  257. ) {
  258. $hmrDownloadUpdateHandlers$[key](
  259. update.c,
  260. update.r,
  261. update.m,
  262. promises,
  263. currentUpdateApplyHandlers,
  264. updatedModules
  265. );
  266. return promises;
  267. }, [])
  268. ).then(function () {
  269. return waitForBlockingPromises(function () {
  270. if (applyOnUpdate) {
  271. return internalApply(applyOnUpdate);
  272. } else {
  273. return setStatus("ready").then(function () {
  274. return updatedModules;
  275. });
  276. }
  277. });
  278. });
  279. });
  280. });
  281. }
  282. function hotApply(options) {
  283. if (currentStatus !== "ready") {
  284. return Promise.resolve().then(function () {
  285. throw new Error(
  286. "apply() is only allowed in ready status (state: " +
  287. currentStatus +
  288. ")"
  289. );
  290. });
  291. }
  292. return internalApply(options);
  293. }
  294. function internalApply(options) {
  295. options = options || {};
  296. applyInvalidatedModules();
  297. var results = currentUpdateApplyHandlers.map(function (handler) {
  298. return handler(options);
  299. });
  300. currentUpdateApplyHandlers = undefined;
  301. var errors = results
  302. .map(function (r) {
  303. return r.error;
  304. })
  305. .filter(Boolean);
  306. if (errors.length > 0) {
  307. return setStatus("abort").then(function () {
  308. throw errors[0];
  309. });
  310. }
  311. // Now in "dispose" phase
  312. var disposePromise = setStatus("dispose");
  313. results.forEach(function (result) {
  314. if (result.dispose) result.dispose();
  315. });
  316. // Now in "apply" phase
  317. var applyPromise = setStatus("apply");
  318. var error;
  319. var reportError = function (err) {
  320. if (!error) error = err;
  321. };
  322. var outdatedModules = [];
  323. results.forEach(function (result) {
  324. if (result.apply) {
  325. var modules = result.apply(reportError);
  326. if (modules) {
  327. for (var i = 0; i < modules.length; i++) {
  328. outdatedModules.push(modules[i]);
  329. }
  330. }
  331. }
  332. });
  333. return Promise.all([disposePromise, applyPromise]).then(function () {
  334. // handle errors in accept handlers and self accepted module load
  335. if (error) {
  336. return setStatus("fail").then(function () {
  337. throw error;
  338. });
  339. }
  340. if (queuedInvalidatedModules) {
  341. return internalApply(options).then(function (list) {
  342. outdatedModules.forEach(function (moduleId) {
  343. if (list.indexOf(moduleId) < 0) list.push(moduleId);
  344. });
  345. return list;
  346. });
  347. }
  348. return setStatus("idle").then(function () {
  349. return outdatedModules;
  350. });
  351. });
  352. }
  353. function applyInvalidatedModules() {
  354. if (queuedInvalidatedModules) {
  355. if (!currentUpdateApplyHandlers) currentUpdateApplyHandlers = [];
  356. Object.keys($hmrInvalidateModuleHandlers$).forEach(function (key) {
  357. queuedInvalidatedModules.forEach(function (moduleId) {
  358. $hmrInvalidateModuleHandlers$[key](
  359. moduleId,
  360. currentUpdateApplyHandlers
  361. );
  362. });
  363. });
  364. queuedInvalidatedModules = undefined;
  365. return true;
  366. }
  367. }
  368. };