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
434 lines (394 loc) · 12.1 KB
/
Copy pathModuleGraph.js
File metadata and controls
434 lines (394 loc) · 12.1 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
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 */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @template T @typedef {import("./util/SortableSet")<T>} SortableSet<T> */
/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
class ModuleGraphModule {
constructor() {
/** @type {Set<ModuleGraphConnection>} */
this.incomingConnections = new Set();
/** @type {Set<ModuleGraphConnection>} */
this.outgoingConnections = new Set();
/** @type {Module | null} */
this.issuer = null;
/** @type {(string | OptimizationBailoutFunction)[]} */
this.optimizationBailout = [];
/** @type {false | true | SortableSet<string> | null} */
this.usedExports = null;
/** @type {number} */
this.preOrderIndex = null;
/** @type {number} */
this.postOrderIndex = null;
}
}
class ModuleGraphDependency {
constructor() {
/** @type {ModuleGraphConnection} */
this.connection = undefined;
/** @type {Module} */
this.parentModule = undefined;
/** @type {DependenciesBlock} */
this.parentBlock = undefined;
}
}
class ModuleGraph {
constructor() {
/** @type {Map<Dependency, ModuleGraphDependency>} */
this._dependencyMap = new Map();
/** @type {Map<Module, ModuleGraphModule>} */
this._moduleMap = new Map();
/** @type {Map<Module, Set<ModuleGraphConnection>>} */
this._originMap = new Map();
/** @type {Map<any, Object>} */
this._metaMap = new Map();
}
/**
* @param {Module} module the module
* @returns {ModuleGraphModule} the internal module
*/
_getModuleGraphModule(module) {
let mgm = this._moduleMap.get(module);
if (mgm === undefined) {
mgm = new ModuleGraphModule();
this._moduleMap.set(module, mgm);
}
return mgm;
}
/**
* @param {Dependency} dependency the dependency
* @returns {ModuleGraphDependency} the internal dependency
*/
_getModuleGraphDependency(dependency) {
let mgd = this._dependencyMap.get(dependency);
if (mgd === undefined) {
mgd = new ModuleGraphDependency();
this._dependencyMap.set(dependency, mgd);
}
return mgd;
}
/**
* @param {Dependency} dependency the dependency
* @param {DependenciesBlock} block parent block
* @param {Module} module parent module
* @returns {void}
*/
setParents(dependency, block, module) {
const mgd = this._getModuleGraphDependency(dependency);
mgd.parentBlock = block;
mgd.parentModule = module;
}
/**
* @param {Dependency} dependency the dependency
* @returns {Module} parent module
*/
getParentModule(dependency) {
const mgd = this._getModuleGraphDependency(dependency);
return mgd.parentModule;
}
/**
* @param {Dependency} dependency the dependency
* @returns {DependenciesBlock} parent block
*/
getParentBlock(dependency) {
const mgd = this._getModuleGraphDependency(dependency);
return mgd.parentBlock;
}
/**
* @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
);
const mgd = this._getModuleGraphDependency(dependency);
mgd.connection = connection;
const connections = this._getModuleGraphModule(module).incomingConnections;
connections.add(connection);
const originConnections = this._getModuleGraphModule(originModule)
.outgoingConnections;
originConnections.add(connection);
}
/**
* @param {Dependency} dependency the referencing dependency
* @param {Module} module the referenced module
* @returns {void}
*/
updateModule(dependency, module) {
const { connection } = this._getModuleGraphDependency(dependency);
if (connection.module === module) return;
const oldMgm = this._getModuleGraphModule(connection.module);
oldMgm.incomingConnections.delete(connection);
connection.module = module;
const newMgm = this._getModuleGraphModule(module);
newMgm.incomingConnections.add(connection);
}
/**
* @param {Dependency} dependency the referencing dependency
* @param {string} explanation an explanation
* @returns {void}
*/
addExplanation(dependency, explanation) {
const { connection } = this._getModuleGraphDependency(dependency);
connection.addExplanation(explanation);
}
/**
* @param {Module} oldModule the old module
* @param {Module} newModule the new module
* @returns {void}
*/
moveModuleAttributes(oldModule, newModule) {
const oldMgm = this._getModuleGraphModule(oldModule);
const newMgm = this._getModuleGraphModule(newModule);
newMgm.postOrderIndex = oldMgm.postOrderIndex;
newMgm.preOrderIndex = oldMgm.preOrderIndex;
oldMgm.postOrderIndex = null;
oldMgm.preOrderIndex = null;
}
/**
* @param {Module} module the module
* @returns {void}
*/
removeModuleAttributes(module) {
const mgm = this._getModuleGraphModule(module);
mgm.postOrderIndex = null;
mgm.preOrderIndex = null;
}
/**
* @returns {void}
*/
removeAllModuleAttributes() {
for(const mgm of this._moduleMap.values()) {
mgm.postOrderIndex = null;
mgm.preOrderIndex = null;
}
}
/**
* @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}
*/
moveModuleConnections(oldModule, newModule, filterConnection) {
if (oldModule === newModule) return;
const oldMgm = this._getModuleGraphModule(oldModule);
const newMgm = this._getModuleGraphModule(newModule);
// Outgoing connections
const oldConnections = oldMgm.outgoingConnections;
const newConnections = newMgm.outgoingConnections;
for (const connection of oldConnections) {
if (filterConnection(connection)) {
connection.originModule = newModule;
newConnections.add(connection);
oldConnections.delete(connection);
}
}
// Incoming connections
const oldConnections2 = oldMgm.incomingConnections;
const newConnections2 = newMgm.incomingConnections;
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._getModuleGraphModule(module).incomingConnections;
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._getModuleGraphDependency(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._getModuleGraphDependency(dependency);
return connection;
}
/**
* @param {Dependency} dependency the dependency to look for a referenced module
* @returns {Module} the referenced module
*/
getModule(dependency) {
const { connection } = this._getModuleGraphDependency(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._getModuleGraphDependency(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._getModuleGraphDependency(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._getModuleGraphModule(module).incomingConnections;
return Array.from(connections);
}
/**
* @param {Module} module the module
* @returns {ModuleGraphConnection[]} list of outgoing connections
*/
getOutgoingConnection(module) {
const connections = this._getModuleGraphModule(module).outgoingConnections;
return Array.from(connections);
}
/**
* @param {Module} module the module
* @returns {Module | null} the issuer module
*/
getIssuer(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.issuer;
}
/**
* @param {Module} module the module
* @param {Module | null} issuer the issuer module
* @returns {void}
*/
setIssuer(module, issuer) {
const mgm = this._getModuleGraphModule(module);
mgm.issuer = issuer;
}
/**
* @param {Module} module the module
* @returns {(string | OptimizationBailoutFunction)[]} optimization bailouts
*/
getOptimizationBailout(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.optimizationBailout;
}
/**
* @param {Module} module the module
* @returns {false | true | SortableSet<string> | null} the used exports
* false: module is not used at all.
* true: the module namespace/object export is used.
* SortableSet<string>: these export names are used.
* empty SortableSet<string>: module is used but no export.
* null: unknown, worst case should be assumed.
*/
getUsedExports(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.usedExports;
}
/**
* @param {Module} module the module
* @param {false | true | SortableSet<string>} usedExports the used exports
* @returns {void}
*/
setUsedExports(module, usedExports) {
const mgm = this._getModuleGraphModule(module);
mgm.usedExports = usedExports;
}
/**
* @param {Module} module the module
* @returns {number} the index of the module
*/
getPreOrderIndex(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.preOrderIndex;
}
/**
* @param {Module} module the module
* @returns {number} the index of the module
*/
getPostOrderIndex(module) {
const mgm = this._getModuleGraphModule(module);
return mgm.postOrderIndex;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {void}
*/
setPreOrderIndex(module, index) {
const mgm = this._getModuleGraphModule(module);
mgm.preOrderIndex = index;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {boolean} true, if the index was set
*/
setPreOrderIndexIfUnset(module, index) {
const mgm = this._getModuleGraphModule(module);
if(mgm.preOrderIndex === null) {
mgm.preOrderIndex = index;
return true;
}
return false;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {void}
*/
setPostOrderIndex(module, index) {
const mgm = this._getModuleGraphModule(module);
mgm.postOrderIndex = index;
}
/**
* @param {Module} module the module
* @param {number} index the index of the module
* @returns {boolean} true, if the index was set
*/
setPostOrderIndexIfUnset(module, index) {
const mgm = this._getModuleGraphModule(module);
if(mgm.postOrderIndex === null) {
mgm.postOrderIndex = index;
return true;
}
return false;
}
/**
* @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;