forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleGraph.js
More file actions
234 lines (211 loc) · 6.77 KB
/
Copy pathModuleGraph.js
File metadata and controls
234 lines (211 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const ModuleGraphConnection = require("./ModuleGraphConnection");
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./Module")} Module */
class ModuleGraph {
constructor() {
/** @type {Map<Dependency, ModuleGraphConnection>} */
this._dependencyMap = new Map();
/** @type {Map<Module, Set<ModuleGraphConnection>>} */
this._moduleMap = new Map();
/** @type {Map<Module, Set<ModuleGraphConnection>>} */
this._originMap = new Map();
/** @type {Map<any, Object>} */
this._metaMap = new Map();
/** @type {Map<Dependency, {module: Module, block: DependenciesBlock}>} */
this._parentsMap = new Map();
}
_getModuleSet(module) {
let connections = this._moduleMap.get(module);
if (connections === undefined) {
connections = new Set();
this._moduleMap.set(module, connections);
}
return connections;
}
_getOriginSet(module) {
let connections = this._originMap.get(module);
if (connections === undefined) {
connections = new Set();
this._originMap.set(module, connections);
}
return connections;
}
/**
* @param {Dependency} dependency the dependency
* @param {DependenciesBlock} block parent block
* @param {Module} module parent module
* @returns {void}
*/
setParents(dependency, block, module) {
this._parentsMap.set(dependency, { module, block });
}
/**
* @param {Dependency} dependency the dependency
* @returns {Module} parent module
*/
getParentModule(dependency) {
const entry = this._parentsMap.get(dependency);
return entry !== undefined ? entry.module : null;
}
/**
* @param {Dependency} dependency the dependency
* @returns {DependenciesBlock} parent block
*/
getParentBlock(dependency) {
const entry = this._parentsMap.get(dependency);
return entry !== undefined ? entry.block : null;
}
/**
* @param {Module} originModule the referencing module
* @param {Dependency} dependency the referencing dependency
* @param {Module} module the referenced module
* @returns {void}}
*/
setResolvedModule(originModule, dependency, module) {
const connection = new ModuleGraphConnection(
originModule,
dependency,
module
);
this._dependencyMap.set(dependency, connection);
const connections = this._getModuleSet(module);
connections.add(connection);
const originConnections = this._getOriginSet(originModule);
originConnections.add(connection);
}
/**
* @param {Dependency} dependency the referencing dependency
* @param {Module} module the referenced module
* @returns {void}
*/
updateModule(dependency, module) {
const connection = this._dependencyMap.get(dependency);
if (connection.module === module) return;
const oldSet = this._moduleMap.get(connection.module);
oldSet.delete(connection);
connection.module = module;
const newSet = this._moduleMap.get(module);
newSet.add(connection);
}
/**
* @param {Dependency} dependency the referencing dependency
* @param {string} explanation an explanation
* @returns {void}
*/
addExplanation(dependency, explanation) {
const connection = this._dependencyMap.get(dependency);
connection.addExplanation(explanation);
}
/**
* @param {Module} oldModule the old referencing module
* @param {Module} newModule the new referencing module
* @param {function(ModuleGraphConnection): boolean} filterConnection filter predicate for replacement
* @returns {void}
*/
replaceModule(oldModule, newModule, filterConnection) {
if (oldModule === newModule) return;
const oldConnections = this._getOriginSet(oldModule);
const newConnections = this._getOriginSet(newModule);
for (const connection of oldConnections) {
if (filterConnection(connection)) {
connection.originModule = newModule;
newConnections.add(connection);
oldConnections.delete(connection);
}
}
const oldConnections2 = this._getModuleSet(oldModule);
const newConnections2 = this._getModuleSet(newModule);
for (const connection of oldConnections2) {
if (filterConnection(connection)) {
connection.module = newModule;
newConnections2.add(connection);
oldConnections2.delete(connection);
}
}
}
/**
* @param {Module} module the referenced module
* @param {string} explanation an explanation why it's referenced
* @returns {void}
*/
addExtraReason(module, explanation) {
const connections = this._getModuleSet(module);
connections.add(new ModuleGraphConnection(null, null, module, explanation));
}
/**
* @param {Dependency} dependency the dependency to look for a referenced module
* @returns {Module} the referenced module
*/
getResolvedModule(dependency) {
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.resolvedModule : null;
}
/**
* @param {Dependency} dependency the dependency to look for a referenced module
* @returns {ModuleGraphConnection | undefined} the connection
*/
getConnection(dependency) {
const connection = this._dependencyMap.get(dependency);
return connection;
}
/**
* @param {Dependency} dependency the dependency to look for a referenced module
* @returns {Module} the referenced module
*/
getModule(dependency) {
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.module : null;
}
/**
* @param {Dependency} dependency the dependency to look for a referencing module
* @returns {Module} the referencing module
*/
getOrigin(dependency) {
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.originModule : null;
}
/**
* @param {Dependency} dependency the dependency to look for a referencing module
* @returns {Module} the original referencing module
*/
getResolvedOrigin(dependency) {
const connection = this._dependencyMap.get(dependency);
return connection !== undefined ? connection.resolvedOriginModule : null;
}
/**
* @param {Module} module the module
* @returns {ModuleGraphConnection[]} reasons why a module is included
*/
getIncomingConnections(module) {
const connections = this._getModuleSet(module);
return Array.from(connections);
}
/**
* @param {Module} module the module
* @returns {ModuleGraphConnection[]} list of outgoing connections
*/
getOutgoingConnection(module) {
const connections = this._getOriginSet(module);
return Array.from(connections);
}
/**
* @param {any} thing any thing
* @returns {Object} metadata
*/
getMeta(thing) {
let meta = this._metaMap.get(thing);
if (meta === undefined) {
meta = Object.create(null);
this._metaMap.set(thing, meta);
}
return meta;
}
}
module.exports = ModuleGraph;
module.exports.ModuleGraphConnection = ModuleGraphConnection;