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