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