source: trip-planner-front/node_modules/webpack/lib/ModuleGraph.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 21.1 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9const ExportsInfo = require("./ExportsInfo");
10const ModuleGraphConnection = require("./ModuleGraphConnection");
11const SortableSet = require("./util/SortableSet");
12const WeakTupleMap = require("./util/WeakTupleMap");
13
14/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
15/** @typedef {import("./Dependency")} Dependency */
16/** @typedef {import("./ExportsInfo").ExportInfo} ExportInfo */
17/** @typedef {import("./Module")} Module */
18/** @typedef {import("./ModuleProfile")} ModuleProfile */
19/** @typedef {import("./RequestShortener")} RequestShortener */
20/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
21
22/**
23 * @callback OptimizationBailoutFunction
24 * @param {RequestShortener} requestShortener
25 * @returns {string}
26 */
27
28const EMPTY_SET = new Set();
29
30/**
31 * @param {SortableSet<ModuleGraphConnection>} set input
32 * @returns {readonly Map<Module, readonly ModuleGraphConnection[]>} mapped by origin module
33 */
34const getConnectionsByOriginModule = set => {
35 const map = new Map();
36 /** @type {Module | 0} */
37 let lastModule = 0;
38 /** @type {ModuleGraphConnection[]} */
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
60class ModuleGraphModule {
61 constructor() {
62 /** @type {SortableSet<ModuleGraphConnection>} */
63 this.incomingConnections = new SortableSet();
64 /** @type {Set<ModuleGraphConnection> | undefined} */
65 this.outgoingConnections = undefined;
66 /** @type {Module | null} */
67 this.issuer = undefined;
68 /** @type {(string | OptimizationBailoutFunction)[]} */
69 this.optimizationBailout = [];
70 /** @type {ExportsInfo} */
71 this.exports = new ExportsInfo();
72 /** @type {number} */
73 this.preOrderIndex = null;
74 /** @type {number} */
75 this.postOrderIndex = null;
76 /** @type {number} */
77 this.depth = null;
78 /** @type {ModuleProfile} */
79 this.profile = undefined;
80 /** @type {boolean} */
81 this.async = false;
82 }
83}
84
85class ModuleGraph {
86 constructor() {
87 /** @type {Map<Dependency, ModuleGraphConnection>} */
88 this._dependencyMap = new Map();
89 /** @type {Map<Module, ModuleGraphModule>} */
90 this._moduleMap = new Map();
91 /** @type {Map<Module, Set<ModuleGraphConnection>>} */
92 this._originMap = new Map();
93 /** @type {Map<any, Object>} */
94 this._metaMap = new Map();
95
96 // Caching
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 /** @type {WeakTupleMap<any[], any>} */
105 this._cache = undefined;
106 }
107
108 /**
109 * @param {Module} module the module
110 * @returns {ModuleGraphModule} the internal module
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 * @param {Dependency} dependency the dependency
131 * @param {DependenciesBlock} block parent block
132 * @param {Module} module parent module
133 * @returns {void}
134 */
135 setParents(dependency, block, module) {
136 dependency._parentDependenciesBlock = block;
137 dependency._parentModule = module;
138 }
139
140 /**
141 * @param {Dependency} dependency the dependency
142 * @returns {Module} parent module
143 */
144 getParentModule(dependency) {
145 return dependency._parentModule;
146 }
147
148 /**
149 * @param {Dependency} dependency the dependency
150 * @returns {DependenciesBlock} parent block
151 */
152 getParentBlock(dependency) {
153 return dependency._parentDependenciesBlock;
154 }
155
156 /**
157 * @param {Module} originModule the referencing module
158 * @param {Dependency} dependency the referencing dependency
159 * @param {Module} module the referenced module
160 * @returns {void}
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 * @param {Dependency} dependency the referencing dependency
183 * @param {Module} module the referenced module
184 * @returns {void}
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 * @param {Dependency} dependency the referencing dependency
201 * @returns {void}
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 * @param {Dependency} dependency the referencing dependency
214 * @param {string} explanation an explanation
215 * @returns {void}
216 */
217 addExplanation(dependency, explanation) {
218 const connection = this._dependencyMap.get(dependency);
219 connection.addExplanation(explanation);
220 }
221
222 /**
223 * @param {Module} sourceModule the source module
224 * @param {Module} targetModule the target module
225 * @returns {void}
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 * @param {Module} module the module
239 * @returns {void}
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 * @returns {void}
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 * @param {Module} oldModule the old referencing module
263 * @param {Module} newModule the new referencing module
264 * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
265 * @returns {void}
266 */
267 moveModuleConnections(oldModule, newModule, filterConnection) {
268 if (oldModule === newModule) return;
269 const oldMgm = this._getModuleGraphModule(oldModule);
270 const newMgm = this._getModuleGraphModule(newModule);
271 // Outgoing connections
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 // Incoming connections
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 * @param {Module} oldModule the old referencing module
300 * @param {Module} newModule the new referencing module
301 * @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
302 * @returns {void}
303 */
304 copyOutgoingModuleConnections(oldModule, newModule, filterConnection) {
305 if (oldModule === newModule) return;
306 const oldMgm = this._getModuleGraphModule(oldModule);
307 const newMgm = this._getModuleGraphModule(newModule);
308 // Outgoing connections
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 * @param {Module} module the referenced module
331 * @param {string} explanation an explanation why it's referenced
332 * @returns {void}
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 * @param {Dependency} dependency the dependency to look for a referenced module
341 * @returns {Module} the referenced module
342 */
343 getResolvedModule(dependency) {
344 const connection = this._dependencyMap.get(dependency);
345 return connection !== undefined ? connection.resolvedModule : null;
346 }
347
348 /**
349 * @param {Dependency} dependency the dependency to look for a referenced module
350 * @returns {ModuleGraphConnection | undefined} the connection
351 */
352 getConnection(dependency) {
353 const connection = this._dependencyMap.get(dependency);
354 return connection;
355 }
356
357 /**
358 * @param {Dependency} dependency the dependency to look for a referenced module
359 * @returns {Module} the referenced module
360 */
361 getModule(dependency) {
362 const connection = this._dependencyMap.get(dependency);
363 return connection !== undefined ? connection.module : null;
364 }
365
366 /**
367 * @param {Dependency} dependency the dependency to look for a referencing module
368 * @returns {Module} the referencing module
369 */
370 getOrigin(dependency) {
371 const connection = this._dependencyMap.get(dependency);
372 return connection !== undefined ? connection.originModule : null;
373 }
374
375 /**
376 * @param {Dependency} dependency the dependency to look for a referencing module
377 * @returns {Module} the original referencing module
378 */
379 getResolvedOrigin(dependency) {
380 const connection = this._dependencyMap.get(dependency);
381 return connection !== undefined ? connection.resolvedOriginModule : null;
382 }
383
384 /**
385 * @param {Module} module the module
386 * @returns {Iterable<ModuleGraphConnection>} reasons why a module is included
387 */
388 getIncomingConnections(module) {
389 const connections = this._getModuleGraphModule(module).incomingConnections;
390 return connections;
391 }
392
393 /**
394 * @param {Module} module the module
395 * @returns {Iterable<ModuleGraphConnection>} list of outgoing connections
396 */
397 getOutgoingConnections(module) {
398 const connections = this._getModuleGraphModule(module).outgoingConnections;
399 return connections === undefined ? EMPTY_SET : connections;
400 }
401
402 /**
403 * @param {Module} module the module
404 * @returns {readonly Map<Module, readonly ModuleGraphConnection[]>} reasons why a module is included, in a map by source module
405 */
406 getIncomingConnectionsByOriginModule(module) {
407 const connections = this._getModuleGraphModule(module).incomingConnections;
408 return connections.getFromUnorderedCache(getConnectionsByOriginModule);
409 }
410
411 /**
412 * @param {Module} module the module
413 * @returns {ModuleProfile | null} the module profile
414 */
415 getProfile(module) {
416 const mgm = this._getModuleGraphModule(module);
417 return mgm.profile;
418 }
419
420 /**
421 * @param {Module} module the module
422 * @param {ModuleProfile | null} profile the module profile
423 * @returns {void}
424 */
425 setProfile(module, profile) {
426 const mgm = this._getModuleGraphModule(module);
427 mgm.profile = profile;
428 }
429
430 /**
431 * @param {Module} module the module
432 * @returns {Module | null} the issuer module
433 */
434 getIssuer(module) {
435 const mgm = this._getModuleGraphModule(module);
436 return mgm.issuer;
437 }
438
439 /**
440 * @param {Module} module the module
441 * @param {Module | null} issuer the issuer module
442 * @returns {void}
443 */
444 setIssuer(module, issuer) {
445 const mgm = this._getModuleGraphModule(module);
446 mgm.issuer = issuer;
447 }
448
449 /**
450 * @param {Module} module the module
451 * @param {Module | null} issuer the issuer module
452 * @returns {void}
453 */
454 setIssuerIfUnset(module, issuer) {
455 const mgm = this._getModuleGraphModule(module);
456 if (mgm.issuer === undefined) mgm.issuer = issuer;
457 }
458
459 /**
460 * @param {Module} module the module
461 * @returns {(string | OptimizationBailoutFunction)[]} optimization bailouts
462 */
463 getOptimizationBailout(module) {
464 const mgm = this._getModuleGraphModule(module);
465 return mgm.optimizationBailout;
466 }
467
468 /**
469 * @param {Module} module the module
470 * @returns {true | string[] | null} the provided exports
471 */
472 getProvidedExports(module) {
473 const mgm = this._getModuleGraphModule(module);
474 return mgm.exports.getProvidedExports();
475 }
476
477 /**
478 * @param {Module} module the module
479 * @param {string | string[]} exportName a name of an export
480 * @returns {boolean | null} true, if the export is provided by the module.
481 * null, if it's unknown.
482 * false, if it's not provided.
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 * @param {Module} module the module
492 * @returns {ExportsInfo} info about the exports
493 */
494 getExportsInfo(module) {
495 const mgm = this._getModuleGraphModule(module);
496 return mgm.exports;
497 }
498
499 /**
500 * @param {Module} module the module
501 * @param {string} exportName the export
502 * @returns {ExportInfo} info about the export
503 */
504 getExportInfo(module, exportName) {
505 const mgm = this._getModuleGraphModule(module);
506 return mgm.exports.getExportInfo(exportName);
507 }
508
509 /**
510 * @param {Module} module the module
511 * @param {string} exportName the export
512 * @returns {ExportInfo} info about the export (do not modify)
513 */
514 getReadOnlyExportInfo(module, exportName) {
515 const mgm = this._getModuleGraphModule(module);
516 return mgm.exports.getReadOnlyExportInfo(exportName);
517 }
518
519 /**
520 * @param {Module} module the module
521 * @param {RuntimeSpec} runtime the runtime
522 * @returns {false | true | SortableSet<string> | null} the used exports
523 * false: module is not used at all.
524 * true: the module namespace/object export is used.
525 * SortableSet<string>: these export names are used.
526 * empty SortableSet<string>: module is used but no export.
527 * null: unknown, worst case should be assumed.
528 */
529 getUsedExports(module, runtime) {
530 const mgm = this._getModuleGraphModule(module);
531 return mgm.exports.getUsedExports(runtime);
532 }
533
534 /**
535 * @param {Module} module the module
536 * @returns {number} the index of the module
537 */
538 getPreOrderIndex(module) {
539 const mgm = this._getModuleGraphModule(module);
540 return mgm.preOrderIndex;
541 }
542
543 /**
544 * @param {Module} module the module
545 * @returns {number} the index of the module
546 */
547 getPostOrderIndex(module) {
548 const mgm = this._getModuleGraphModule(module);
549 return mgm.postOrderIndex;
550 }
551
552 /**
553 * @param {Module} module the module
554 * @param {number} index the index of the module
555 * @returns {void}
556 */
557 setPreOrderIndex(module, index) {
558 const mgm = this._getModuleGraphModule(module);
559 mgm.preOrderIndex = index;
560 }
561
562 /**
563 * @param {Module} module the module
564 * @param {number} index the index of the module
565 * @returns {boolean} true, if the index was set
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 * @param {Module} module the module
578 * @param {number} index the index of the module
579 * @returns {void}
580 */
581 setPostOrderIndex(module, index) {
582 const mgm = this._getModuleGraphModule(module);
583 mgm.postOrderIndex = index;
584 }
585
586 /**
587 * @param {Module} module the module
588 * @param {number} index the index of the module
589 * @returns {boolean} true, if the index was set
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 * @param {Module} module the module
602 * @returns {number} the depth of the module
603 */
604 getDepth(module) {
605 const mgm = this._getModuleGraphModule(module);
606 return mgm.depth;
607 }
608
609 /**
610 * @param {Module} module the module
611 * @param {number} depth the depth of the module
612 * @returns {void}
613 */
614 setDepth(module, depth) {
615 const mgm = this._getModuleGraphModule(module);
616 mgm.depth = depth;
617 }
618
619 /**
620 * @param {Module} module the module
621 * @param {number} depth the depth of the module
622 * @returns {boolean} true, if the depth was set
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 * @param {Module} module the module
635 * @returns {boolean} true, if the module is async
636 */
637 isAsync(module) {
638 const mgm = this._getModuleGraphModule(module);
639 return mgm.async;
640 }
641
642 /**
643 * @param {Module} module the module
644 * @returns {void}
645 */
646 setAsync(module) {
647 const mgm = this._getModuleGraphModule(module);
648 mgm.async = true;
649 }
650
651 /**
652 * @param {any} thing any thing
653 * @returns {Object} metadata
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 * @param {any} thing any thing
666 * @returns {Object} metadata
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 * @template {any[]} T
682 * @template V
683 * @param {(moduleGraph: ModuleGraph, ...args: T) => V} fn computer
684 * @param {T} args arguments
685 * @returns {V} computed value or cached
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 // TODO remove in webpack 6
693 /**
694 * @param {Module} module the module
695 * @param {string} deprecateMessage message for the deprecation message
696 * @param {string} deprecationCode code for the deprecation
697 * @returns {ModuleGraph} the module graph
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 * @param {Module} module the module
705 * @returns {ModuleGraph} the module graph
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 // TODO remove in webpack 6
724 /**
725 * @param {Module} module the module
726 * @param {ModuleGraph} moduleGraph the module graph
727 * @returns {void}
728 */
729 static setModuleGraphForModule(module, moduleGraph) {
730 moduleGraphForModuleMap.set(module, moduleGraph);
731 }
732
733 // TODO remove in webpack 6
734 /**
735 * @param {Module} module the module
736 * @returns {void}
737 */
738 static clearModuleGraphForModule(module) {
739 moduleGraphForModuleMap.delete(module);
740 }
741}
742
743// TODO remove in webpack 6
744/** @type {WeakMap<Module, ModuleGraph>} */
745const moduleGraphForModuleMap = new WeakMap();
746
747// TODO remove in webpack 6
748/** @type {Map<string, (module: Module) => ModuleGraph>} */
749const deprecateMap = new Map();
750
751module.exports = ModuleGraph;
752module.exports.ModuleGraphConnection = ModuleGraphConnection;
Note: See TracBrowser for help on using the repository browser.