metro 0.70.2 → 0.71.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +22 -21
- package/src/Assets.js.flow +4 -4
- package/src/Bundler/util.js +1 -1
- package/src/Bundler/util.js.flow +2 -2
- package/src/Bundler.js +17 -10
- package/src/Bundler.js.flow +19 -14
- package/src/DeltaBundler/DeltaCalculator.js +13 -17
- package/src/DeltaBundler/DeltaCalculator.js.flow +15 -20
- package/src/DeltaBundler/Serializers/getAllFiles.js.flow +2 -2
- package/src/DeltaBundler/Serializers/getAssets.js.flow +2 -2
- package/src/DeltaBundler/Serializers/getExplodedSourceMap.js.flow +4 -4
- package/src/DeltaBundler/Serializers/getRamBundleInfo.js.flow +6 -6
- package/src/DeltaBundler/Serializers/helpers/getSourceMapInfo.js.flow +4 -4
- package/src/DeltaBundler/Serializers/helpers/processBytecodeModules.js.flow +2 -2
- package/src/DeltaBundler/Serializers/helpers/processModules.js.flow +2 -2
- package/src/DeltaBundler/Serializers/hmrJSBundle.js.flow +2 -2
- package/src/DeltaBundler/Serializers/sourceMapGenerator.js.flow +6 -6
- package/src/DeltaBundler/Serializers/sourceMapObject.js.flow +4 -4
- package/src/DeltaBundler/Serializers/sourceMapString.js.flow +2 -2
- package/src/DeltaBundler/Worker.flow.js +78 -0
- package/src/DeltaBundler/Worker.flow.js.flow +121 -0
- package/src/DeltaBundler/Worker.js +8 -66
- package/src/DeltaBundler/Worker.js.flow +8 -107
- package/src/DeltaBundler/WorkerFarm.js.flow +4 -4
- package/src/DeltaBundler/__fixtures__/hasteImpl.js +4 -0
- package/src/DeltaBundler/getTransformCacheKey.js.flow +2 -2
- package/src/DeltaBundler/graphOperations.js +634 -0
- package/src/DeltaBundler/graphOperations.js.flow +749 -0
- package/src/DeltaBundler/types.flow.js.flow +36 -30
- package/src/DeltaBundler.js +14 -6
- package/src/DeltaBundler.js.flow +14 -10
- package/src/HmrServer.js.flow +6 -6
- package/src/IncrementalBundler.js +1 -1
- package/src/IncrementalBundler.js.flow +8 -8
- package/src/ModuleGraph/node-haste/ModuleCache.js +1 -1
- package/src/ModuleGraph/node-haste/ModuleCache.js.flow +1 -1
- package/src/ModuleGraph/node-haste/node-haste.flow.js.flow +2 -2
- package/src/ModuleGraph/node-haste/node-haste.js +4 -4
- package/src/ModuleGraph/node-haste/node-haste.js.flow +13 -7
- package/src/ModuleGraph/output/indexed-ram-bundle.js.flow +2 -2
- package/src/ModuleGraph/output/plain-bundle.js.flow +2 -2
- package/src/ModuleGraph/output/reverse-dependency-map-references.js.flow +8 -8
- package/src/ModuleGraph/output/util.js.flow +2 -2
- package/src/ModuleGraph/types.flow.js.flow +37 -37
- package/src/ModuleGraph/worker/collectDependencies.js.flow +2 -2
- package/src/Server/symbolicate.js.flow +1 -1
- package/src/Server.js.flow +18 -18
- package/src/cli.js +5 -0
- package/src/cli.js.flow +5 -0
- package/src/commands/build.js +4 -3
- package/src/commands/build.js.flow +5 -3
- package/src/commands/serve.js +3 -3
- package/src/commands/serve.js.flow +5 -3
- package/src/index.flow.js +392 -0
- package/src/index.flow.js.flow +480 -0
- package/src/index.js +8 -366
- package/src/index.js.flow +8 -456
- package/src/lib/bundleToBytecode.js.flow +2 -2
- package/src/lib/bundleToString.js.flow +2 -2
- package/src/lib/getPreludeCode.js.flow +2 -2
- package/src/lib/transformHelpers.js.flow +2 -2
- package/src/node-haste/DependencyGraph/ModuleResolution.js +17 -4
- package/src/node-haste/DependencyGraph/ModuleResolution.js.flow +20 -12
- package/src/node-haste/DependencyGraph/createHasteMap.js +79 -19
- package/src/node-haste/DependencyGraph/createHasteMap.js.flow +15 -14
- package/src/node-haste/DependencyGraph.js +31 -27
- package/src/node-haste/DependencyGraph.js.flow +43 -37
- package/src/node-haste/ModuleCache.js.flow +1 -1
- package/src/node-haste/lib/AssetPaths.js.flow +2 -2
- package/src/node-haste/lib/parsePlatformFilePath.js.flow +2 -2
- package/src/shared/output/RamBundle/as-indexed-file.js.flow +1 -1
- package/src/shared/output/RamBundle/buildSourcemapWithMetadata.js.flow +2 -2
- package/src/shared/types.flow.js.flow +14 -14
- package/src/DeltaBundler/computeDelta.js +0 -42
- package/src/DeltaBundler/computeDelta.js.flow +0 -47
- package/src/DeltaBundler/traverseDependencies.js +0 -470
- package/src/DeltaBundler/traverseDependencies.js.flow +0 -565
- package/src/node-haste/DependencyGraph/types.js +0 -10
- package/src/node-haste/DependencyGraph/types.js.flow +0 -88
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Portions of this code are based on the Synchronous Cycle Collection
|
|
13
|
+
* algorithm described in:
|
|
14
|
+
*
|
|
15
|
+
* David F. Bacon and V. T. Rajan. 2001. Concurrent Cycle Collection in
|
|
16
|
+
* Reference Counted Systems. In Proceedings of the 15th European Conference on
|
|
17
|
+
* Object-Oriented Programming (ECOOP '01). Springer-Verlag, Berlin,
|
|
18
|
+
* Heidelberg, 207–235.
|
|
19
|
+
*
|
|
20
|
+
* Notable differences from the algorithm in the paper:
|
|
21
|
+
* 1. Our implementation uses the inverseDependencies set (which we already
|
|
22
|
+
* have to maintain) instead of a separate refcount variable. A module's
|
|
23
|
+
* reference count is equal to the size of its inverseDependencies set, plus
|
|
24
|
+
* 1 if it's an entry point of the graph.
|
|
25
|
+
* 2. We keep the "root buffer" (possibleCycleRoots) free of duplicates by
|
|
26
|
+
* making it a Set, instead of storing a "buffered" flag on each node.
|
|
27
|
+
* 3. On top of tracking edges between nodes, we also count references between
|
|
28
|
+
* nodes and entries in the importBundleNames set.
|
|
29
|
+
*/
|
|
30
|
+
"use strict";
|
|
31
|
+
|
|
32
|
+
const invariant = require("invariant");
|
|
33
|
+
|
|
34
|
+
const nullthrows = require("nullthrows"); // TODO: Convert to a Flow enum
|
|
35
|
+
|
|
36
|
+
function createGraph(options) {
|
|
37
|
+
return {
|
|
38
|
+
...options,
|
|
39
|
+
dependencies: new Map(),
|
|
40
|
+
importBundleNames: new Set(),
|
|
41
|
+
privateState: {
|
|
42
|
+
gc: {
|
|
43
|
+
color: new Map(),
|
|
44
|
+
possibleCycleRoots: new Set(),
|
|
45
|
+
importBundleRefs: new Map(),
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getInternalOptions({
|
|
52
|
+
transform,
|
|
53
|
+
resolve,
|
|
54
|
+
onProgress,
|
|
55
|
+
experimentalImportBundleSupport,
|
|
56
|
+
shallow,
|
|
57
|
+
}) {
|
|
58
|
+
let numProcessed = 0;
|
|
59
|
+
let total = 0;
|
|
60
|
+
return {
|
|
61
|
+
experimentalImportBundleSupport,
|
|
62
|
+
transform,
|
|
63
|
+
resolve,
|
|
64
|
+
onDependencyAdd: () => onProgress && onProgress(numProcessed, ++total),
|
|
65
|
+
onDependencyAdded: () => onProgress && onProgress(++numProcessed, total),
|
|
66
|
+
shallow,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Dependency Traversal logic for the Delta Bundler. This method calculates
|
|
71
|
+
* the modules that should be included in the bundle by traversing the
|
|
72
|
+
* dependency graph.
|
|
73
|
+
* Instead of traversing the whole graph each time, it just calculates the
|
|
74
|
+
* difference between runs by only traversing the added/removed dependencies.
|
|
75
|
+
* To do so, it uses the passed passed graph dependencies and it mutates it.
|
|
76
|
+
* The paths parameter contains the absolute paths of the root files that the
|
|
77
|
+
* method should traverse. Normally, these paths should be the modified files
|
|
78
|
+
* since the last traversal.
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
async function traverseDependencies(paths, graph, options) {
|
|
82
|
+
const delta = {
|
|
83
|
+
added: new Set(),
|
|
84
|
+
modified: new Set(),
|
|
85
|
+
deleted: new Set(),
|
|
86
|
+
earlyInverseDependencies: new Map(),
|
|
87
|
+
};
|
|
88
|
+
const internalOptions = getInternalOptions(options);
|
|
89
|
+
|
|
90
|
+
for (const path of paths) {
|
|
91
|
+
// Start traversing from modules that are already part of the dependency graph.
|
|
92
|
+
if (graph.dependencies.get(path)) {
|
|
93
|
+
delta.modified.add(path);
|
|
94
|
+
await traverseDependenciesForSingleFile(
|
|
95
|
+
path,
|
|
96
|
+
graph,
|
|
97
|
+
delta,
|
|
98
|
+
internalOptions
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
collectCycles(graph, delta);
|
|
104
|
+
const added = new Map();
|
|
105
|
+
|
|
106
|
+
for (const path of delta.added) {
|
|
107
|
+
added.set(path, nullthrows(graph.dependencies.get(path)));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const modified = new Map();
|
|
111
|
+
|
|
112
|
+
for (const path of delta.modified) {
|
|
113
|
+
// Only report a module as modified if we're not already reporting it as added.
|
|
114
|
+
if (!delta.added.has(path)) {
|
|
115
|
+
modified.set(path, nullthrows(graph.dependencies.get(path)));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
added,
|
|
121
|
+
modified,
|
|
122
|
+
deleted: delta.deleted,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function initialTraverseDependencies(graph, options) {
|
|
127
|
+
const delta = {
|
|
128
|
+
added: new Set(),
|
|
129
|
+
modified: new Set(),
|
|
130
|
+
deleted: new Set(),
|
|
131
|
+
earlyInverseDependencies: new Map(),
|
|
132
|
+
};
|
|
133
|
+
const internalOptions = getInternalOptions(options);
|
|
134
|
+
invariant(
|
|
135
|
+
graph.dependencies.size === 0,
|
|
136
|
+
"initialTraverseDependencies called on nonempty graph"
|
|
137
|
+
);
|
|
138
|
+
invariant(
|
|
139
|
+
graph.importBundleNames.size === 0,
|
|
140
|
+
"initialTraverseDependencies called on nonempty graph"
|
|
141
|
+
);
|
|
142
|
+
graph.privateState.gc.color.clear();
|
|
143
|
+
graph.privateState.gc.possibleCycleRoots.clear();
|
|
144
|
+
graph.privateState.gc.importBundleRefs.clear();
|
|
145
|
+
|
|
146
|
+
for (const path of graph.entryPoints) {
|
|
147
|
+
// Each entry point implicitly has a refcount of 1, so mark them all black.
|
|
148
|
+
graph.privateState.gc.color.set(path, "black");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
await Promise.all(
|
|
152
|
+
[...graph.entryPoints].map((path) =>
|
|
153
|
+
traverseDependenciesForSingleFile(path, graph, delta, internalOptions)
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
reorderGraph(graph, {
|
|
157
|
+
shallow: options.shallow,
|
|
158
|
+
});
|
|
159
|
+
return {
|
|
160
|
+
added: graph.dependencies,
|
|
161
|
+
modified: new Map(),
|
|
162
|
+
deleted: new Set(),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function traverseDependenciesForSingleFile(path, graph, delta, options) {
|
|
167
|
+
options.onDependencyAdd();
|
|
168
|
+
await processModule(path, graph, delta, options);
|
|
169
|
+
options.onDependencyAdded();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function processModule(path, graph, delta, options) {
|
|
173
|
+
// Transform the file via the given option.
|
|
174
|
+
// TODO: Unbind the transform method from options
|
|
175
|
+
const result = await options.transform(path); // Get the absolute path of all sub-dependencies (some of them could have been
|
|
176
|
+
// moved but maintain the same relative path).
|
|
177
|
+
|
|
178
|
+
const currentDependencies = resolveDependencies(
|
|
179
|
+
path,
|
|
180
|
+
result.dependencies,
|
|
181
|
+
options
|
|
182
|
+
);
|
|
183
|
+
const previousModule = graph.dependencies.get(path) || {
|
|
184
|
+
inverseDependencies: delta.earlyInverseDependencies.get(path) || new Set(),
|
|
185
|
+
path,
|
|
186
|
+
};
|
|
187
|
+
const previousDependencies = previousModule.dependencies || new Map(); // Update the module information.
|
|
188
|
+
|
|
189
|
+
const module = {
|
|
190
|
+
...previousModule,
|
|
191
|
+
dependencies: new Map(previousDependencies),
|
|
192
|
+
getSource: result.getSource,
|
|
193
|
+
output: result.output,
|
|
194
|
+
};
|
|
195
|
+
graph.dependencies.set(module.path, module); // Diff dependencies (1/2): remove dependencies that have changed or been removed.
|
|
196
|
+
|
|
197
|
+
for (const [relativePath, prevDependency] of previousDependencies) {
|
|
198
|
+
const curDependency = currentDependencies.get(relativePath);
|
|
199
|
+
|
|
200
|
+
if (
|
|
201
|
+
!curDependency ||
|
|
202
|
+
curDependency.absolutePath !== prevDependency.absolutePath ||
|
|
203
|
+
(options.experimentalImportBundleSupport &&
|
|
204
|
+
curDependency.data.data.asyncType !==
|
|
205
|
+
prevDependency.data.data.asyncType)
|
|
206
|
+
) {
|
|
207
|
+
removeDependency(
|
|
208
|
+
module,
|
|
209
|
+
relativePath,
|
|
210
|
+
prevDependency,
|
|
211
|
+
graph,
|
|
212
|
+
delta,
|
|
213
|
+
options
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
} // Diff dependencies (2/2): add dependencies that have changed or been added.
|
|
217
|
+
|
|
218
|
+
const promises = [];
|
|
219
|
+
|
|
220
|
+
for (const [relativePath, curDependency] of currentDependencies) {
|
|
221
|
+
const prevDependency = previousDependencies.get(relativePath);
|
|
222
|
+
|
|
223
|
+
if (
|
|
224
|
+
!prevDependency ||
|
|
225
|
+
prevDependency.absolutePath !== curDependency.absolutePath ||
|
|
226
|
+
(options.experimentalImportBundleSupport &&
|
|
227
|
+
prevDependency.data.data.asyncType !==
|
|
228
|
+
curDependency.data.data.asyncType)
|
|
229
|
+
) {
|
|
230
|
+
promises.push(
|
|
231
|
+
addDependency(
|
|
232
|
+
module,
|
|
233
|
+
relativePath,
|
|
234
|
+
curDependency,
|
|
235
|
+
graph,
|
|
236
|
+
delta,
|
|
237
|
+
options
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
await Promise.all(promises); // Replace dependencies with the correctly-ordered version. As long as all
|
|
244
|
+
// the above promises have resolved, this will be the same map but without
|
|
245
|
+
// the added nondeterminism of promise resolution order. Because this
|
|
246
|
+
// assignment does not add or remove edges, it does NOT invalidate any of the
|
|
247
|
+
// garbage collection state.
|
|
248
|
+
// Catch obvious errors with a cheap assertion.
|
|
249
|
+
|
|
250
|
+
invariant(
|
|
251
|
+
module.dependencies.size === currentDependencies.size,
|
|
252
|
+
"Failed to add the correct dependencies"
|
|
253
|
+
); // $FlowFixMe[cannot-write]
|
|
254
|
+
|
|
255
|
+
module.dependencies = currentDependencies;
|
|
256
|
+
return module;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function addDependency(
|
|
260
|
+
parentModule,
|
|
261
|
+
relativePath,
|
|
262
|
+
dependency,
|
|
263
|
+
graph,
|
|
264
|
+
delta,
|
|
265
|
+
options
|
|
266
|
+
) {
|
|
267
|
+
const path = dependency.absolutePath; // The module may already exist, in which case we just need to update some
|
|
268
|
+
// bookkeeping instead of adding a new node to the graph.
|
|
269
|
+
|
|
270
|
+
let module = graph.dependencies.get(path);
|
|
271
|
+
|
|
272
|
+
if (options.shallow) {
|
|
273
|
+
// Don't add a node for the module if the graph is shallow (single-module).
|
|
274
|
+
} else if (
|
|
275
|
+
options.experimentalImportBundleSupport &&
|
|
276
|
+
dependency.data.data.asyncType != null
|
|
277
|
+
) {
|
|
278
|
+
// Don't add a node for the module if we are traversing async dependencies
|
|
279
|
+
// lazily (and this is an async dependency). Instead, record it in
|
|
280
|
+
// importBundleNames.
|
|
281
|
+
incrementImportBundleReference(dependency, graph);
|
|
282
|
+
} else {
|
|
283
|
+
if (!module) {
|
|
284
|
+
// Add a new node to the graph.
|
|
285
|
+
const earlyInverseDependencies = delta.earlyInverseDependencies.get(path);
|
|
286
|
+
|
|
287
|
+
if (earlyInverseDependencies) {
|
|
288
|
+
// This module is being transformed at the moment in parallel, so we
|
|
289
|
+
// should only mark its parent as an inverse dependency.
|
|
290
|
+
earlyInverseDependencies.add(parentModule.path);
|
|
291
|
+
} else {
|
|
292
|
+
if (delta.deleted.has(path)) {
|
|
293
|
+
// Mark the addition by clearing a prior deletion.
|
|
294
|
+
delta.deleted.delete(path);
|
|
295
|
+
} else {
|
|
296
|
+
// Mark the addition in the added set.
|
|
297
|
+
delta.added.add(path);
|
|
298
|
+
delta.modified.delete(path);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
delta.earlyInverseDependencies.set(path, new Set([parentModule.path]));
|
|
302
|
+
options.onDependencyAdd();
|
|
303
|
+
module = await processModule(path, graph, delta, options);
|
|
304
|
+
options.onDependencyAdded();
|
|
305
|
+
graph.dependencies.set(module.path, module);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (module) {
|
|
310
|
+
// We either added a new node to the graph, or we're updating an existing one.
|
|
311
|
+
module.inverseDependencies.add(parentModule.path);
|
|
312
|
+
markModuleInUse(module, graph);
|
|
313
|
+
}
|
|
314
|
+
} // Always update the parent's dependency map.
|
|
315
|
+
// This means the parent's dependencies can get desynced from
|
|
316
|
+
// inverseDependencies and the other fields in the case of lazy edges.
|
|
317
|
+
// Not an optimal representation :(
|
|
318
|
+
|
|
319
|
+
parentModule.dependencies.set(relativePath, dependency);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function removeDependency(
|
|
323
|
+
parentModule,
|
|
324
|
+
relativePath,
|
|
325
|
+
dependency,
|
|
326
|
+
graph,
|
|
327
|
+
delta,
|
|
328
|
+
options
|
|
329
|
+
) {
|
|
330
|
+
parentModule.dependencies.delete(relativePath);
|
|
331
|
+
const { absolutePath } = dependency;
|
|
332
|
+
|
|
333
|
+
if (
|
|
334
|
+
options.experimentalImportBundleSupport &&
|
|
335
|
+
dependency.data.data.asyncType != null
|
|
336
|
+
) {
|
|
337
|
+
decrementImportBundleReference(dependency, graph);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const module = graph.dependencies.get(absolutePath);
|
|
341
|
+
|
|
342
|
+
if (!module) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
module.inverseDependencies.delete(parentModule.path);
|
|
347
|
+
|
|
348
|
+
if (
|
|
349
|
+
module.inverseDependencies.size > 0 ||
|
|
350
|
+
graph.entryPoints.has(absolutePath)
|
|
351
|
+
) {
|
|
352
|
+
// The reference count has decreased, but not to zero.
|
|
353
|
+
// NOTE: Each entry point implicitly has a refcount of 1.
|
|
354
|
+
markAsPossibleCycleRoot(module, graph);
|
|
355
|
+
} else {
|
|
356
|
+
// The reference count has decreased to zero.
|
|
357
|
+
releaseModule(module, graph, delta, options);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function resolveDependencies(parentPath, dependencies, options) {
|
|
362
|
+
const resolve = (parentPath, result) => {
|
|
363
|
+
const relativePath = result.name;
|
|
364
|
+
|
|
365
|
+
try {
|
|
366
|
+
return [
|
|
367
|
+
relativePath,
|
|
368
|
+
{
|
|
369
|
+
absolutePath: options.resolve(parentPath, relativePath),
|
|
370
|
+
data: result,
|
|
371
|
+
},
|
|
372
|
+
];
|
|
373
|
+
} catch (error) {
|
|
374
|
+
// Ignore unavailable optional dependencies. They are guarded
|
|
375
|
+
// with a try-catch block and will be handled during runtime.
|
|
376
|
+
if (result.data.isOptional !== true) {
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return undefined;
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
const resolved = dependencies.reduce((list, result) => {
|
|
385
|
+
const resolvedPath = resolve(parentPath, result);
|
|
386
|
+
|
|
387
|
+
if (resolvedPath) {
|
|
388
|
+
list.push(resolvedPath);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return list;
|
|
392
|
+
}, []);
|
|
393
|
+
return new Map(resolved);
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Re-traverse the dependency graph in DFS order to reorder the modules and
|
|
397
|
+
* guarantee the same order between runs. This method mutates the passed graph.
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
function reorderGraph(graph, options) {
|
|
401
|
+
const orderedDependencies = new Map();
|
|
402
|
+
graph.entryPoints.forEach((entryPoint) => {
|
|
403
|
+
const mainModule = graph.dependencies.get(entryPoint);
|
|
404
|
+
|
|
405
|
+
if (!mainModule) {
|
|
406
|
+
throw new ReferenceError("Module not registered in graph: " + entryPoint);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
reorderDependencies(graph, mainModule, orderedDependencies, options);
|
|
410
|
+
});
|
|
411
|
+
graph.dependencies = orderedDependencies;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function reorderDependencies(graph, module, orderedDependencies, options) {
|
|
415
|
+
if (module.path) {
|
|
416
|
+
if (orderedDependencies.has(module.path)) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
orderedDependencies.set(module.path, module);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
module.dependencies.forEach((dependency) => {
|
|
424
|
+
const path = dependency.absolutePath;
|
|
425
|
+
const childModule = graph.dependencies.get(path);
|
|
426
|
+
|
|
427
|
+
if (!childModule) {
|
|
428
|
+
if (dependency.data.data.asyncType != null || options.shallow) {
|
|
429
|
+
return;
|
|
430
|
+
} else {
|
|
431
|
+
throw new ReferenceError("Module not registered in graph: " + path);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
reorderDependencies(graph, childModule, orderedDependencies, options);
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
/** Garbage collection functions */
|
|
439
|
+
// Add an entry to importBundleNames (or increase the reference count of an existing one)
|
|
440
|
+
|
|
441
|
+
function incrementImportBundleReference(dependency, graph) {
|
|
442
|
+
var _graph$privateState$g;
|
|
443
|
+
|
|
444
|
+
const { absolutePath } = dependency;
|
|
445
|
+
graph.privateState.gc.importBundleRefs.set(
|
|
446
|
+
absolutePath,
|
|
447
|
+
((_graph$privateState$g =
|
|
448
|
+
graph.privateState.gc.importBundleRefs.get(absolutePath)) !== null &&
|
|
449
|
+
_graph$privateState$g !== void 0
|
|
450
|
+
? _graph$privateState$g
|
|
451
|
+
: 0) + 1
|
|
452
|
+
);
|
|
453
|
+
graph.importBundleNames.add(absolutePath);
|
|
454
|
+
} // Decrease the reference count of an entry in importBundleNames (and delete it if necessary)
|
|
455
|
+
|
|
456
|
+
function decrementImportBundleReference(dependency, graph) {
|
|
457
|
+
const { absolutePath } = dependency;
|
|
458
|
+
const prevRefCount = nullthrows(
|
|
459
|
+
graph.privateState.gc.importBundleRefs.get(absolutePath)
|
|
460
|
+
);
|
|
461
|
+
invariant(
|
|
462
|
+
prevRefCount > 0,
|
|
463
|
+
"experimentalImportBundleSupport: import bundle refcount not valid"
|
|
464
|
+
);
|
|
465
|
+
graph.privateState.gc.importBundleRefs.set(absolutePath, prevRefCount - 1);
|
|
466
|
+
|
|
467
|
+
if (prevRefCount === 1) {
|
|
468
|
+
graph.privateState.gc.importBundleRefs.delete(absolutePath);
|
|
469
|
+
graph.importBundleNames.delete(absolutePath);
|
|
470
|
+
}
|
|
471
|
+
} // Mark a module as in use (ref count >= 1)
|
|
472
|
+
|
|
473
|
+
function markModuleInUse(module, graph) {
|
|
474
|
+
graph.privateState.gc.color.set(module.path, "black");
|
|
475
|
+
} // Delete an unreachable module from the graph immediately, unless it's queued
|
|
476
|
+
// for later deletion as a potential cycle root. Delete the module's outbound
|
|
477
|
+
// edges.
|
|
478
|
+
// Called when the reference count of a module has reached 0.
|
|
479
|
+
|
|
480
|
+
function releaseModule(module, graph, delta, options) {
|
|
481
|
+
for (const [relativePath, dependency] of module.dependencies) {
|
|
482
|
+
removeDependency(module, relativePath, dependency, graph, delta, options);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
graph.privateState.gc.color.set(module.path, "black");
|
|
486
|
+
|
|
487
|
+
if (!graph.privateState.gc.possibleCycleRoots.has(module.path)) {
|
|
488
|
+
freeModule(module, graph, delta);
|
|
489
|
+
}
|
|
490
|
+
} // Delete an unreachable module from the graph.
|
|
491
|
+
|
|
492
|
+
function freeModule(module, graph, delta) {
|
|
493
|
+
if (delta.added.has(module.path)) {
|
|
494
|
+
// Mark the deletion by clearing a prior addition.
|
|
495
|
+
delta.added.delete(module.path);
|
|
496
|
+
} else {
|
|
497
|
+
// Mark the deletion in the deleted set.
|
|
498
|
+
delta.deleted.add(module.path);
|
|
499
|
+
delta.modified.delete(module.path);
|
|
500
|
+
} // This module is not used anywhere else! We can clear it from the bundle.
|
|
501
|
+
// Clean up all the state associated with this module in order to correctly
|
|
502
|
+
// re-add it if we encounter it again.
|
|
503
|
+
|
|
504
|
+
graph.dependencies.delete(module.path);
|
|
505
|
+
delta.earlyInverseDependencies.delete(module.path);
|
|
506
|
+
graph.privateState.gc.possibleCycleRoots.delete(module.path);
|
|
507
|
+
graph.privateState.gc.color.delete(module.path);
|
|
508
|
+
} // Mark a module as a possible cycle root
|
|
509
|
+
|
|
510
|
+
function markAsPossibleCycleRoot(module, graph) {
|
|
511
|
+
if (nullthrows(graph.privateState.gc.color.get(module.path)) !== "purple") {
|
|
512
|
+
graph.privateState.gc.color.set(module.path, "purple");
|
|
513
|
+
graph.privateState.gc.possibleCycleRoots.add(module.path);
|
|
514
|
+
}
|
|
515
|
+
} // Collect any unreachable cycles in the graph.
|
|
516
|
+
|
|
517
|
+
function collectCycles(graph, delta) {
|
|
518
|
+
// Mark recursively from roots (trial deletion)
|
|
519
|
+
for (const path of graph.privateState.gc.possibleCycleRoots) {
|
|
520
|
+
const module = nullthrows(graph.dependencies.get(path));
|
|
521
|
+
const color = nullthrows(graph.privateState.gc.color.get(path));
|
|
522
|
+
|
|
523
|
+
if (color === "purple") {
|
|
524
|
+
markGray(module, graph);
|
|
525
|
+
} else {
|
|
526
|
+
graph.privateState.gc.possibleCycleRoots.delete(path);
|
|
527
|
+
|
|
528
|
+
if (
|
|
529
|
+
color === "black" &&
|
|
530
|
+
module.inverseDependencies.size === 0 &&
|
|
531
|
+
!graph.entryPoints.has(path)
|
|
532
|
+
) {
|
|
533
|
+
freeModule(module, graph, delta);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
} // Scan recursively from roots (undo unsuccessful trial deletions)
|
|
537
|
+
|
|
538
|
+
for (const path of graph.privateState.gc.possibleCycleRoots) {
|
|
539
|
+
const module = nullthrows(graph.dependencies.get(path));
|
|
540
|
+
scan(module, graph);
|
|
541
|
+
} // Collect recursively from roots (free unreachable cycles)
|
|
542
|
+
|
|
543
|
+
for (const path of graph.privateState.gc.possibleCycleRoots) {
|
|
544
|
+
graph.privateState.gc.possibleCycleRoots.delete(path);
|
|
545
|
+
const module = nullthrows(graph.dependencies.get(path));
|
|
546
|
+
collectWhite(module, graph, delta);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function markGray(module, graph) {
|
|
551
|
+
const color = nullthrows(graph.privateState.gc.color.get(module.path));
|
|
552
|
+
|
|
553
|
+
if (color !== "gray") {
|
|
554
|
+
graph.privateState.gc.color.set(module.path, "gray");
|
|
555
|
+
|
|
556
|
+
for (const dependency of module.dependencies.values()) {
|
|
557
|
+
const childModule = nullthrows(
|
|
558
|
+
graph.dependencies.get(dependency.absolutePath)
|
|
559
|
+
); // The inverse dependency will be restored during the scan phase if this module remains live.
|
|
560
|
+
|
|
561
|
+
childModule.inverseDependencies.delete(module.path);
|
|
562
|
+
markGray(childModule, graph);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
function scan(module, graph) {
|
|
568
|
+
const color = nullthrows(graph.privateState.gc.color.get(module.path));
|
|
569
|
+
|
|
570
|
+
if (color === "gray") {
|
|
571
|
+
if (
|
|
572
|
+
module.inverseDependencies.size > 0 ||
|
|
573
|
+
graph.entryPoints.has(module.path)
|
|
574
|
+
) {
|
|
575
|
+
scanBlack(module, graph);
|
|
576
|
+
} else {
|
|
577
|
+
graph.privateState.gc.color.set(module.path, "white");
|
|
578
|
+
|
|
579
|
+
for (const dependency of module.dependencies.values()) {
|
|
580
|
+
const childModule = nullthrows(
|
|
581
|
+
graph.dependencies.get(dependency.absolutePath)
|
|
582
|
+
);
|
|
583
|
+
scan(childModule, graph);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function scanBlack(module, graph) {
|
|
590
|
+
graph.privateState.gc.color.set(module.path, "black");
|
|
591
|
+
|
|
592
|
+
for (const dependency of module.dependencies.values()) {
|
|
593
|
+
const childModule = nullthrows(
|
|
594
|
+
graph.dependencies.get(dependency.absolutePath)
|
|
595
|
+
); // The inverse dependency must have been deleted during the mark phase.
|
|
596
|
+
|
|
597
|
+
childModule.inverseDependencies.add(module.path);
|
|
598
|
+
const childColor = nullthrows(
|
|
599
|
+
graph.privateState.gc.color.get(childModule.path)
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
if (childColor !== "black") {
|
|
603
|
+
scanBlack(childModule, graph);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function collectWhite(module, graph, delta) {
|
|
609
|
+
const color = nullthrows(graph.privateState.gc.color.get(module.path));
|
|
610
|
+
|
|
611
|
+
if (
|
|
612
|
+
color === "white" &&
|
|
613
|
+
!graph.privateState.gc.possibleCycleRoots.has(module.path)
|
|
614
|
+
) {
|
|
615
|
+
graph.privateState.gc.color.set(module.path, "black");
|
|
616
|
+
|
|
617
|
+
for (const dependency of module.dependencies.values()) {
|
|
618
|
+
const childModule = nullthrows(
|
|
619
|
+
graph.dependencies.get(dependency.absolutePath)
|
|
620
|
+
);
|
|
621
|
+
collectWhite(childModule, graph, delta);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
freeModule(module, graph, delta);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
/** End of garbage collection functions */
|
|
628
|
+
|
|
629
|
+
module.exports = {
|
|
630
|
+
createGraph,
|
|
631
|
+
initialTraverseDependencies,
|
|
632
|
+
traverseDependencies,
|
|
633
|
+
reorderGraph,
|
|
634
|
+
};
|