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