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