metro 0.70.3 → 0.71.2

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