1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const util = require("util");
|
9 | const ExportsInfo = require("./ExportsInfo");
|
10 | const ModuleGraphConnection = require("./ModuleGraphConnection");
|
11 | const SortableSet = require("./util/SortableSet");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const EMPTY_ARRAY = [];
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const getConnectionsByOriginModule = set => {
|
34 | const map = new Map();
|
35 |
|
36 | let lastModule = 0;
|
37 |
|
38 | let lastList = undefined;
|
39 | for (const connection of set) {
|
40 | const { originModule } = connection;
|
41 | if (lastModule === originModule) {
|
42 | lastList.push(connection);
|
43 | } else {
|
44 | lastModule = originModule;
|
45 | const list = map.get(originModule);
|
46 | if (list !== undefined) {
|
47 | lastList = list;
|
48 | list.push(connection);
|
49 | } else {
|
50 | const list = [connection];
|
51 | lastList = list;
|
52 | map.set(originModule, list);
|
53 | }
|
54 | }
|
55 | }
|
56 | return map;
|
57 | };
|
58 |
|
59 | class ModuleGraphModule {
|
60 | constructor() {
|
61 |
|
62 | this.incomingConnections = new SortableSet();
|
63 |
|
64 | this.outgoingConnections = undefined;
|
65 |
|
66 | this.issuer = undefined;
|
67 |
|
68 | this.optimizationBailout = [];
|
69 |
|
70 | this.exports = new ExportsInfo();
|
71 |
|
72 | this.preOrderIndex = null;
|
73 |
|
74 | this.postOrderIndex = null;
|
75 |
|
76 | this.depth = null;
|
77 |
|
78 | this.profile = undefined;
|
79 |
|
80 | this.async = false;
|
81 | }
|
82 | }
|
83 |
|
84 | class ModuleGraphDependency {
|
85 | constructor() {
|
86 |
|
87 | this.connection = undefined;
|
88 |
|
89 | this.parentModule = undefined;
|
90 |
|
91 | this.parentBlock = undefined;
|
92 | }
|
93 | }
|
94 |
|
95 | class ModuleGraph {
|
96 | constructor() {
|
97 |
|
98 | this._dependencyMap = new Map();
|
99 |
|
100 | this._moduleMap = new Map();
|
101 |
|
102 | this._originMap = new Map();
|
103 |
|
104 | this._metaMap = new Map();
|
105 |
|
106 |
|
107 | this._cacheModuleGraphModuleKey1 = undefined;
|
108 | this._cacheModuleGraphModuleValue1 = undefined;
|
109 | this._cacheModuleGraphModuleKey2 = undefined;
|
110 | this._cacheModuleGraphModuleValue2 = undefined;
|
111 | this._cacheModuleGraphDependencyKey = undefined;
|
112 | this._cacheModuleGraphDependencyValue = undefined;
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 | _getModuleGraphModule(module) {
|
120 | if (this._cacheModuleGraphModuleKey1 === module)
|
121 | return this._cacheModuleGraphModuleValue1;
|
122 | if (this._cacheModuleGraphModuleKey2 === module)
|
123 | return this._cacheModuleGraphModuleValue2;
|
124 | let mgm = this._moduleMap.get(module);
|
125 | if (mgm === undefined) {
|
126 | mgm = new ModuleGraphModule();
|
127 | this._moduleMap.set(module, mgm);
|
128 | }
|
129 | this._cacheModuleGraphModuleKey2 = this._cacheModuleGraphModuleKey1;
|
130 | this._cacheModuleGraphModuleValue2 = this._cacheModuleGraphModuleValue1;
|
131 | this._cacheModuleGraphModuleKey1 = module;
|
132 | this._cacheModuleGraphModuleValue1 = mgm;
|
133 | return mgm;
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 | _getModuleGraphDependency(dependency) {
|
141 | if (this._cacheModuleGraphDependencyKey === dependency)
|
142 | return this._cacheModuleGraphDependencyValue;
|
143 | let mgd = this._dependencyMap.get(dependency);
|
144 | if (mgd === undefined) {
|
145 | mgd = new ModuleGraphDependency();
|
146 | this._dependencyMap.set(dependency, mgd);
|
147 | }
|
148 | this._cacheModuleGraphDependencyKey = dependency;
|
149 | this._cacheModuleGraphDependencyValue = mgd;
|
150 | return mgd;
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | setParents(dependency, block, module) {
|
160 | const mgd = this._getModuleGraphDependency(dependency);
|
161 | mgd.parentBlock = block;
|
162 | mgd.parentModule = module;
|
163 | }
|
164 |
|
165 | |
166 |
|
167 |
|
168 |
|
169 | getParentModule(dependency) {
|
170 | const mgd = this._getModuleGraphDependency(dependency);
|
171 | return mgd.parentModule;
|
172 | }
|
173 |
|
174 | |
175 |
|
176 |
|
177 |
|
178 | getParentBlock(dependency) {
|
179 | const mgd = this._getModuleGraphDependency(dependency);
|
180 | return mgd.parentBlock;
|
181 | }
|
182 |
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | setResolvedModule(originModule, dependency, module) {
|
190 | const connection = new ModuleGraphConnection(
|
191 | originModule,
|
192 | dependency,
|
193 | module,
|
194 | undefined,
|
195 | dependency.weak,
|
196 | dependency.getCondition(this)
|
197 | );
|
198 | const mgd = this._getModuleGraphDependency(dependency);
|
199 | mgd.connection = connection;
|
200 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
201 | connections.add(connection);
|
202 | const mgm = this._getModuleGraphModule(originModule);
|
203 | if (mgm.outgoingConnections === undefined) {
|
204 | mgm.outgoingConnections = new Set();
|
205 | }
|
206 | mgm.outgoingConnections.add(connection);
|
207 | }
|
208 |
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 | updateModule(dependency, module) {
|
215 | const mgd = this._getModuleGraphDependency(dependency);
|
216 | if (mgd.connection.module === module) return;
|
217 | const { connection } = mgd;
|
218 | const newConnection = connection.clone();
|
219 | newConnection.module = module;
|
220 | mgd.connection = newConnection;
|
221 | connection.setActive(false);
|
222 | const originMgm = this._getModuleGraphModule(connection.originModule);
|
223 | originMgm.outgoingConnections.add(newConnection);
|
224 | const targetMgm = this._getModuleGraphModule(module);
|
225 | targetMgm.incomingConnections.add(newConnection);
|
226 | }
|
227 |
|
228 | |
229 |
|
230 |
|
231 |
|
232 | removeConnection(dependency) {
|
233 | const mgd = this._getModuleGraphDependency(dependency);
|
234 | const { connection } = mgd;
|
235 | const targetMgm = this._getModuleGraphModule(connection.module);
|
236 | targetMgm.incomingConnections.delete(connection);
|
237 | const originMgm = this._getModuleGraphModule(connection.originModule);
|
238 | originMgm.outgoingConnections.delete(connection);
|
239 | mgd.connection = undefined;
|
240 | }
|
241 |
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 | addExplanation(dependency, explanation) {
|
248 | const { connection } = this._getModuleGraphDependency(dependency);
|
249 | connection.addExplanation(explanation);
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 | cloneModuleAttributes(sourceModule, targetModule) {
|
258 | const oldMgm = this._getModuleGraphModule(sourceModule);
|
259 | const newMgm = this._getModuleGraphModule(targetModule);
|
260 | newMgm.postOrderIndex = oldMgm.postOrderIndex;
|
261 | newMgm.preOrderIndex = oldMgm.preOrderIndex;
|
262 | newMgm.depth = oldMgm.depth;
|
263 | newMgm.exports = oldMgm.exports;
|
264 | newMgm.async = oldMgm.async;
|
265 | }
|
266 |
|
267 | |
268 |
|
269 |
|
270 |
|
271 | removeModuleAttributes(module) {
|
272 | const mgm = this._getModuleGraphModule(module);
|
273 | mgm.postOrderIndex = null;
|
274 | mgm.preOrderIndex = null;
|
275 | mgm.depth = null;
|
276 | mgm.async = false;
|
277 | }
|
278 |
|
279 | |
280 |
|
281 |
|
282 | removeAllModuleAttributes() {
|
283 | for (const mgm of this._moduleMap.values()) {
|
284 | mgm.postOrderIndex = null;
|
285 | mgm.preOrderIndex = null;
|
286 | mgm.depth = null;
|
287 | mgm.async = false;
|
288 | }
|
289 | }
|
290 |
|
291 | |
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | moveModuleConnections(oldModule, newModule, filterConnection) {
|
298 | if (oldModule === newModule) return;
|
299 | const oldMgm = this._getModuleGraphModule(oldModule);
|
300 | const newMgm = this._getModuleGraphModule(newModule);
|
301 |
|
302 | const oldConnections = oldMgm.outgoingConnections;
|
303 | if (oldConnections !== undefined) {
|
304 | if (newMgm.outgoingConnections === undefined) {
|
305 | newMgm.outgoingConnections = new Set();
|
306 | }
|
307 | const newConnections = newMgm.outgoingConnections;
|
308 | for (const connection of oldConnections) {
|
309 | if (filterConnection(connection)) {
|
310 | connection.originModule = newModule;
|
311 | newConnections.add(connection);
|
312 | oldConnections.delete(connection);
|
313 | }
|
314 | }
|
315 | }
|
316 |
|
317 | const oldConnections2 = oldMgm.incomingConnections;
|
318 | const newConnections2 = newMgm.incomingConnections;
|
319 | for (const connection of oldConnections2) {
|
320 | if (filterConnection(connection)) {
|
321 | connection.module = newModule;
|
322 | newConnections2.add(connection);
|
323 | oldConnections2.delete(connection);
|
324 | }
|
325 | }
|
326 | }
|
327 |
|
328 | |
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | copyOutgoingModuleConnections(oldModule, newModule, filterConnection) {
|
335 | if (oldModule === newModule) return;
|
336 | const oldMgm = this._getModuleGraphModule(oldModule);
|
337 | const newMgm = this._getModuleGraphModule(newModule);
|
338 |
|
339 | const oldConnections = oldMgm.outgoingConnections;
|
340 | if (oldConnections !== undefined) {
|
341 | if (newMgm.outgoingConnections === undefined) {
|
342 | newMgm.outgoingConnections = new Set();
|
343 | }
|
344 | const newConnections = newMgm.outgoingConnections;
|
345 | for (const connection of oldConnections) {
|
346 | if (filterConnection(connection)) {
|
347 | const newConnection = connection.clone();
|
348 | newConnection.originModule = newModule;
|
349 | newConnections.add(newConnection);
|
350 | if (newConnection.module !== undefined) {
|
351 | const otherMgm = this._getModuleGraphModule(newConnection.module);
|
352 | otherMgm.incomingConnections.add(newConnection);
|
353 | }
|
354 | }
|
355 | }
|
356 | }
|
357 | }
|
358 |
|
359 | |
360 |
|
361 |
|
362 |
|
363 |
|
364 | addExtraReason(module, explanation) {
|
365 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
366 | connections.add(new ModuleGraphConnection(null, null, module, explanation));
|
367 | }
|
368 |
|
369 | |
370 |
|
371 |
|
372 |
|
373 | getResolvedModule(dependency) {
|
374 | const { connection } = this._getModuleGraphDependency(dependency);
|
375 | return connection !== undefined ? connection.resolvedModule : null;
|
376 | }
|
377 |
|
378 | |
379 |
|
380 |
|
381 |
|
382 | getConnection(dependency) {
|
383 | const { connection } = this._getModuleGraphDependency(dependency);
|
384 | return connection;
|
385 | }
|
386 |
|
387 | |
388 |
|
389 |
|
390 |
|
391 | getModule(dependency) {
|
392 | const { connection } = this._getModuleGraphDependency(dependency);
|
393 | return connection !== undefined ? connection.module : null;
|
394 | }
|
395 |
|
396 | |
397 |
|
398 |
|
399 |
|
400 | getOrigin(dependency) {
|
401 | const { connection } = this._getModuleGraphDependency(dependency);
|
402 | return connection !== undefined ? connection.originModule : null;
|
403 | }
|
404 |
|
405 | |
406 |
|
407 |
|
408 |
|
409 | getResolvedOrigin(dependency) {
|
410 | const { connection } = this._getModuleGraphDependency(dependency);
|
411 | return connection !== undefined ? connection.resolvedOriginModule : null;
|
412 | }
|
413 |
|
414 | |
415 |
|
416 |
|
417 |
|
418 | getIncomingConnections(module) {
|
419 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
420 | return connections;
|
421 | }
|
422 |
|
423 | |
424 |
|
425 |
|
426 |
|
427 | getOutgoingConnections(module) {
|
428 | const connections = this._getModuleGraphModule(module).outgoingConnections;
|
429 | return connections === undefined ? EMPTY_ARRAY : connections;
|
430 | }
|
431 |
|
432 | |
433 |
|
434 |
|
435 |
|
436 | getIncomingConnectionsByOriginModule(module) {
|
437 | const connections = this._getModuleGraphModule(module).incomingConnections;
|
438 | return connections.getFromUnorderedCache(getConnectionsByOriginModule);
|
439 | }
|
440 |
|
441 | |
442 |
|
443 |
|
444 |
|
445 | getProfile(module) {
|
446 | const mgm = this._getModuleGraphModule(module);
|
447 | return mgm.profile;
|
448 | }
|
449 |
|
450 | |
451 |
|
452 |
|
453 |
|
454 |
|
455 | setProfile(module, profile) {
|
456 | const mgm = this._getModuleGraphModule(module);
|
457 | mgm.profile = profile;
|
458 | }
|
459 |
|
460 | |
461 |
|
462 |
|
463 |
|
464 | getIssuer(module) {
|
465 | const mgm = this._getModuleGraphModule(module);
|
466 | return mgm.issuer;
|
467 | }
|
468 |
|
469 | |
470 |
|
471 |
|
472 |
|
473 |
|
474 | setIssuer(module, issuer) {
|
475 | const mgm = this._getModuleGraphModule(module);
|
476 | mgm.issuer = issuer;
|
477 | }
|
478 |
|
479 | |
480 |
|
481 |
|
482 |
|
483 |
|
484 | setIssuerIfUnset(module, issuer) {
|
485 | const mgm = this._getModuleGraphModule(module);
|
486 | if (mgm.issuer === undefined) mgm.issuer = issuer;
|
487 | }
|
488 |
|
489 | |
490 |
|
491 |
|
492 |
|
493 | getOptimizationBailout(module) {
|
494 | const mgm = this._getModuleGraphModule(module);
|
495 | return mgm.optimizationBailout;
|
496 | }
|
497 |
|
498 | |
499 |
|
500 |
|
501 |
|
502 | getProvidedExports(module) {
|
503 | const mgm = this._getModuleGraphModule(module);
|
504 | return mgm.exports.getProvidedExports();
|
505 | }
|
506 |
|
507 | |
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | isExportProvided(module, exportName) {
|
515 | const mgm = this._getModuleGraphModule(module);
|
516 | const result = mgm.exports.isExportProvided(exportName);
|
517 | return result === undefined ? null : result;
|
518 | }
|
519 |
|
520 | |
521 |
|
522 |
|
523 |
|
524 | getExportsInfo(module) {
|
525 | const mgm = this._getModuleGraphModule(module);
|
526 | return mgm.exports;
|
527 | }
|
528 |
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 | getExportInfo(module, exportName) {
|
535 | const mgm = this._getModuleGraphModule(module);
|
536 | return mgm.exports.getExportInfo(exportName);
|
537 | }
|
538 |
|
539 | |
540 |
|
541 |
|
542 |
|
543 |
|
544 | getReadOnlyExportInfo(module, exportName) {
|
545 | const mgm = this._getModuleGraphModule(module);
|
546 | return mgm.exports.getReadOnlyExportInfo(exportName);
|
547 | }
|
548 |
|
549 | |
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 | getUsedExports(module, runtime) {
|
560 | const mgm = this._getModuleGraphModule(module);
|
561 | return mgm.exports.getUsedExports(runtime);
|
562 | }
|
563 |
|
564 | |
565 |
|
566 |
|
567 |
|
568 | getPreOrderIndex(module) {
|
569 | const mgm = this._getModuleGraphModule(module);
|
570 | return mgm.preOrderIndex;
|
571 | }
|
572 |
|
573 | |
574 |
|
575 |
|
576 |
|
577 | getPostOrderIndex(module) {
|
578 | const mgm = this._getModuleGraphModule(module);
|
579 | return mgm.postOrderIndex;
|
580 | }
|
581 |
|
582 | |
583 |
|
584 |
|
585 |
|
586 |
|
587 | setPreOrderIndex(module, index) {
|
588 | const mgm = this._getModuleGraphModule(module);
|
589 | mgm.preOrderIndex = index;
|
590 | }
|
591 |
|
592 | |
593 |
|
594 |
|
595 |
|
596 |
|
597 | setPreOrderIndexIfUnset(module, index) {
|
598 | const mgm = this._getModuleGraphModule(module);
|
599 | if (mgm.preOrderIndex === null) {
|
600 | mgm.preOrderIndex = index;
|
601 | return true;
|
602 | }
|
603 | return false;
|
604 | }
|
605 |
|
606 | |
607 |
|
608 |
|
609 |
|
610 |
|
611 | setPostOrderIndex(module, index) {
|
612 | const mgm = this._getModuleGraphModule(module);
|
613 | mgm.postOrderIndex = index;
|
614 | }
|
615 |
|
616 | |
617 |
|
618 |
|
619 |
|
620 |
|
621 | setPostOrderIndexIfUnset(module, index) {
|
622 | const mgm = this._getModuleGraphModule(module);
|
623 | if (mgm.postOrderIndex === null) {
|
624 | mgm.postOrderIndex = index;
|
625 | return true;
|
626 | }
|
627 | return false;
|
628 | }
|
629 |
|
630 | |
631 |
|
632 |
|
633 |
|
634 | getDepth(module) {
|
635 | const mgm = this._getModuleGraphModule(module);
|
636 | return mgm.depth;
|
637 | }
|
638 |
|
639 | |
640 |
|
641 |
|
642 |
|
643 |
|
644 | setDepth(module, depth) {
|
645 | const mgm = this._getModuleGraphModule(module);
|
646 | mgm.depth = depth;
|
647 | }
|
648 |
|
649 | |
650 |
|
651 |
|
652 |
|
653 |
|
654 | setDepthIfLower(module, depth) {
|
655 | const mgm = this._getModuleGraphModule(module);
|
656 | if (mgm.depth === null || mgm.depth > depth) {
|
657 | mgm.depth = depth;
|
658 | return true;
|
659 | }
|
660 | return false;
|
661 | }
|
662 |
|
663 | |
664 |
|
665 |
|
666 |
|
667 | isAsync(module) {
|
668 | const mgm = this._getModuleGraphModule(module);
|
669 | return mgm.async;
|
670 | }
|
671 |
|
672 | |
673 |
|
674 |
|
675 |
|
676 | setAsync(module) {
|
677 | const mgm = this._getModuleGraphModule(module);
|
678 | mgm.async = true;
|
679 | }
|
680 |
|
681 | |
682 |
|
683 |
|
684 |
|
685 | getMeta(thing) {
|
686 | let meta = this._metaMap.get(thing);
|
687 | if (meta === undefined) {
|
688 | meta = Object.create(null);
|
689 | this._metaMap.set(thing, meta);
|
690 | }
|
691 | return meta;
|
692 | }
|
693 |
|
694 | |
695 |
|
696 |
|
697 |
|
698 | getMetaIfExisting(thing) {
|
699 | return this._metaMap.get(thing);
|
700 | }
|
701 |
|
702 |
|
703 | |
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 | static getModuleGraphForModule(module, deprecateMessage, deprecationCode) {
|
710 | const fn = deprecateMap.get(deprecateMessage);
|
711 | if (fn) return fn(module);
|
712 | const newFn = util.deprecate(
|
713 | |
714 |
|
715 |
|
716 |
|
717 | module => {
|
718 | const moduleGraph = moduleGraphForModuleMap.get(module);
|
719 | if (!moduleGraph)
|
720 | throw new Error(
|
721 | deprecateMessage +
|
722 | "There was no ModuleGraph assigned to the Module for backward-compat (Use the new API)"
|
723 | );
|
724 | return moduleGraph;
|
725 | },
|
726 | deprecateMessage + ": Use new ModuleGraph API",
|
727 | deprecationCode
|
728 | );
|
729 | deprecateMap.set(deprecateMessage, newFn);
|
730 | return newFn(module);
|
731 | }
|
732 |
|
733 |
|
734 | |
735 |
|
736 |
|
737 |
|
738 |
|
739 | static setModuleGraphForModule(module, moduleGraph) {
|
740 | moduleGraphForModuleMap.set(module, moduleGraph);
|
741 | }
|
742 |
|
743 |
|
744 | |
745 |
|
746 |
|
747 |
|
748 | static clearModuleGraphForModule(module) {
|
749 | moduleGraphForModuleMap.delete(module);
|
750 | }
|
751 | }
|
752 |
|
753 |
|
754 |
|
755 | const moduleGraphForModuleMap = new WeakMap();
|
756 |
|
757 |
|
758 |
|
759 | const deprecateMap = new Map();
|
760 |
|
761 | module.exports = ModuleGraph;
|
762 | module.exports.ModuleGraphConnection = ModuleGraphConnection;
|