HotModuleReplacement.runtime.js 10.0 KB

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