@atlaspack/bundler-default 2.12.1-dev.3478 → 2.12.1-dev.3520

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.
@@ -0,0 +1,1382 @@
1
+ // @flow strict-local
2
+
3
+ import path from 'path';
4
+
5
+ import {getFeatureFlag} from '@atlaspack/feature-flags';
6
+ import {
7
+ ALL_EDGE_TYPES,
8
+ BitSet,
9
+ ContentGraph,
10
+ Graph,
11
+ type NodeId,
12
+ } from '@atlaspack/graph';
13
+ import type {
14
+ Asset,
15
+ BundleBehavior,
16
+ Dependency,
17
+ Environment,
18
+ MutableBundleGraph,
19
+ Target,
20
+ PluginLogger,
21
+ } from '@atlaspack/types';
22
+ import {DefaultMap, globToRegex} from '@atlaspack/utils';
23
+ import invariant from 'assert';
24
+ import nullthrows from 'nullthrows';
25
+
26
+ import type {ResolvedBundlerConfig} from './bundlerConfig';
27
+
28
+ /* BundleRoot - An asset that is the main entry of a Bundle. */
29
+ type BundleRoot = Asset;
30
+
31
+ export type Bundle = {|
32
+ uniqueKey: ?string,
33
+ assets: Set<Asset>,
34
+ internalizedAssets?: BitSet,
35
+ bundleBehavior?: ?BundleBehavior,
36
+ needsStableName: boolean,
37
+ mainEntryAsset: ?Asset,
38
+ size: number,
39
+ sourceBundles: Set<NodeId>,
40
+ target: Target,
41
+ env: Environment,
42
+ type: string,
43
+ manualSharedBundle: ?string, // for naming purposes
44
+ |};
45
+
46
+ export type DependencyBundleGraph = ContentGraph<
47
+ | {|
48
+ value: Bundle,
49
+ type: 'bundle',
50
+ |}
51
+ | {|
52
+ value: Dependency,
53
+ type: 'dependency',
54
+ |},
55
+ number,
56
+ >;
57
+
58
+ // IdealGraph is the structure we will pass to decorate,
59
+ // which mutates the assetGraph into the bundleGraph we would
60
+ // expect from default bundler
61
+ export type IdealGraph = {|
62
+ assetReference: DefaultMap<Asset, Array<[Dependency, Bundle]>>,
63
+ assets: Array<Asset>,
64
+ bundleGraph: Graph<Bundle | 'root'>,
65
+ bundleGroupBundleIds: Set<NodeId>,
66
+ dependencyBundleGraph: DependencyBundleGraph,
67
+ manualAssetToBundle: Map<Asset, NodeId>,
68
+ |};
69
+
70
+ const dependencyPriorityEdges = {
71
+ sync: 1,
72
+ parallel: 2,
73
+ lazy: 3,
74
+ conditional: 4,
75
+ };
76
+
77
+ export function createIdealGraph(
78
+ assetGraph: MutableBundleGraph,
79
+ config: ResolvedBundlerConfig,
80
+ entries: Map<Asset, Dependency>,
81
+ logger: PluginLogger,
82
+ ): IdealGraph {
83
+ // Asset to the bundle and group it's an entry of
84
+ let bundleRoots: Map<BundleRoot, [NodeId, NodeId]> = new Map();
85
+ let bundles: Map<string, NodeId> = new Map();
86
+ let dependencyBundleGraph: DependencyBundleGraph = new ContentGraph();
87
+ let assetReference: DefaultMap<
88
+ Asset,
89
+ Array<[Dependency, Bundle]>,
90
+ > = new DefaultMap(() => []);
91
+
92
+ // A Graph of Bundles and a root node (dummy string), which models only Bundles, and connections to their
93
+ // referencing Bundle. There are no actual BundleGroup nodes, just bundles that take on that role.
94
+ let bundleGraph: Graph<Bundle | 'root'> = new Graph();
95
+ let stack: Array<[BundleRoot, NodeId]> = [];
96
+
97
+ let bundleRootEdgeTypes = {
98
+ parallel: 1,
99
+ lazy: 2,
100
+ };
101
+ // Graph that models bundleRoots, with parallel & async deps only to inform reachability
102
+ let bundleRootGraph: Graph<
103
+ number, // asset index
104
+ $Values<typeof bundleRootEdgeTypes>,
105
+ > = new Graph();
106
+ let assetToBundleRootNodeId = new Map<BundleRoot, number>();
107
+
108
+ let bundleGroupBundleIds: Set<NodeId> = new Set();
109
+
110
+ let bundleGraphRootNodeId = nullthrows(bundleGraph.addNode('root'));
111
+ bundleGraph.setRootNodeId(bundleGraphRootNodeId);
112
+ // Step Create Entry Bundles
113
+ for (let [asset, dependency] of entries) {
114
+ let bundle = createBundle({
115
+ asset,
116
+ target: nullthrows(dependency.target),
117
+ needsStableName: dependency.isEntry,
118
+ });
119
+ let nodeId = bundleGraph.addNode(bundle);
120
+ bundles.set(asset.id, nodeId);
121
+ bundleRoots.set(asset, [nodeId, nodeId]);
122
+ bundleGraph.addEdge(bundleGraphRootNodeId, nodeId);
123
+
124
+ dependencyBundleGraph.addEdge(
125
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(dependency.id, {
126
+ value: dependency,
127
+ type: 'dependency',
128
+ }),
129
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(String(nodeId), {
130
+ value: bundle,
131
+ type: 'bundle',
132
+ }),
133
+ dependencyPriorityEdges[dependency.priority],
134
+ );
135
+ bundleGroupBundleIds.add(nodeId);
136
+ }
137
+
138
+ let assets = [];
139
+ let assetToIndex = new Map<Asset, number>();
140
+
141
+ function makeManualAssetToConfigLookup() {
142
+ let manualAssetToConfig = new Map();
143
+ let constantModuleToMSB = new DefaultMap(() => []);
144
+
145
+ if (config.manualSharedBundles.length === 0) {
146
+ return {manualAssetToConfig, constantModuleToMSB};
147
+ }
148
+
149
+ let parentsToConfig = new DefaultMap(() => []);
150
+
151
+ for (let c of config.manualSharedBundles) {
152
+ if (c.root != null) {
153
+ parentsToConfig.get(path.join(config.projectRoot, c.root)).push(c);
154
+ }
155
+ }
156
+ let numParentsToFind = parentsToConfig.size;
157
+ let configToParentAsset = new Map();
158
+
159
+ assetGraph.traverse((node, _, actions) => {
160
+ if (node.type === 'asset' && parentsToConfig.has(node.value.filePath)) {
161
+ for (let c of parentsToConfig.get(node.value.filePath)) {
162
+ configToParentAsset.set(c, node.value);
163
+ }
164
+
165
+ numParentsToFind--;
166
+
167
+ if (numParentsToFind === 0) {
168
+ // If we've found all parents we can stop traversal
169
+ actions.stop();
170
+ }
171
+ }
172
+ });
173
+
174
+ // Process in reverse order so earlier configs take precedence
175
+ for (let c of config.manualSharedBundles.reverse()) {
176
+ if (c.root != null && !configToParentAsset.has(c)) {
177
+ logger.warn({
178
+ origin: '@atlaspack/bundler-default',
179
+ message: `Manual shared bundle "${c.name}" skipped, no root asset found`,
180
+ });
181
+ continue;
182
+ }
183
+
184
+ let parentAsset = configToParentAsset.get(c);
185
+ let assetRegexes = c.assets.map((glob) => globToRegex(glob));
186
+
187
+ assetGraph.traverse((node, _, actions) => {
188
+ if (
189
+ node.type === 'asset' &&
190
+ (!Array.isArray(c.types) || c.types.includes(node.value.type))
191
+ ) {
192
+ let projectRelativePath = path.relative(
193
+ config.projectRoot,
194
+ node.value.filePath,
195
+ );
196
+ if (!assetRegexes.some((regex) => regex.test(projectRelativePath))) {
197
+ return;
198
+ }
199
+
200
+ // We track all matching MSB's for constant modules as they are never duplicated
201
+ // and need to be assigned to all matching bundles
202
+ if (node.value.meta.isConstantModule === true) {
203
+ constantModuleToMSB.get(node.value).push(c);
204
+ }
205
+ manualAssetToConfig.set(node.value, c);
206
+ return;
207
+ }
208
+
209
+ if (
210
+ node.type === 'dependency' &&
211
+ (node.value.priority === 'lazy' ||
212
+ (getFeatureFlag('conditionalBundlingApi') &&
213
+ node.value.priority === 'conditional')) &&
214
+ parentAsset
215
+ ) {
216
+ // Don't walk past the bundle group assets
217
+ actions.skipChildren();
218
+ }
219
+ }, parentAsset);
220
+ }
221
+
222
+ return {manualAssetToConfig, constantModuleToMSB};
223
+ }
224
+
225
+ //Manual is a map of the user-given name to the bundle node Id that corresponds to ALL the assets that match any glob in that user-specified array
226
+ let manualSharedMap: Map<string, NodeId> = new Map();
227
+ // May need a map to be able to look up NON- bundle root assets which need special case instructions
228
+ // Use this when placing assets into bundles, to avoid duplication
229
+ let manualAssetToBundle: Map<Asset, NodeId> = new Map();
230
+ let {manualAssetToConfig, constantModuleToMSB} =
231
+ makeManualAssetToConfigLookup();
232
+ let manualBundleToInternalizedAsset: DefaultMap<
233
+ NodeId,
234
+ Array<Asset>,
235
+ > = new DefaultMap(() => []);
236
+
237
+ /**
238
+ * Step Create Bundles: Traverse the assetGraph (aka MutableBundleGraph) and create bundles
239
+ * for asset type changes, parallel, inline, and async or lazy dependencies,
240
+ * adding only that asset to each bundle, not its entire subgraph.
241
+ */
242
+ assetGraph.traverse(
243
+ {
244
+ enter(node, context, actions) {
245
+ if (node.type === 'asset') {
246
+ if (
247
+ context?.type === 'dependency' &&
248
+ context?.value.isEntry &&
249
+ !entries.has(node.value)
250
+ ) {
251
+ // Skip whole subtrees of other targets by skipping those entries
252
+ actions.skipChildren();
253
+ return node;
254
+ }
255
+ assetToIndex.set(node.value, assets.length);
256
+ assets.push(node.value);
257
+
258
+ let bundleIdTuple = bundleRoots.get(node.value);
259
+ if (bundleIdTuple && bundleIdTuple[0] === bundleIdTuple[1]) {
260
+ // Push to the stack (only) when a new bundle is created
261
+ stack.push([node.value, bundleIdTuple[0]]);
262
+ } else if (bundleIdTuple) {
263
+ // Otherwise, push on the last bundle that marks the start of a BundleGroup
264
+ stack.push([node.value, stack[stack.length - 1][1]]);
265
+ }
266
+ } else if (node.type === 'dependency') {
267
+ if (context == null) {
268
+ return node;
269
+ }
270
+ let dependency = node.value;
271
+
272
+ invariant(context?.type === 'asset');
273
+
274
+ let assets = assetGraph.getDependencyAssets(dependency);
275
+ if (assets.length === 0) {
276
+ return node;
277
+ }
278
+
279
+ for (let childAsset of assets) {
280
+ let bundleId = bundles.get(childAsset.id);
281
+ let bundle;
282
+
283
+ // MSB Step 1: Match glob on filepath and type for any asset
284
+ let manualSharedBundleKey;
285
+ let manualSharedObject = manualAssetToConfig.get(childAsset);
286
+
287
+ if (manualSharedObject) {
288
+ // MSB Step 2: Generate a key for which to look up this manual bundle with
289
+ manualSharedBundleKey =
290
+ manualSharedObject.name + ',' + childAsset.type;
291
+ }
292
+
293
+ if (
294
+ // MSB Step 3: If a bundle for these globs already exists, use it
295
+ manualSharedBundleKey != null &&
296
+ manualSharedMap.has(manualSharedBundleKey)
297
+ ) {
298
+ bundleId = nullthrows(manualSharedMap.get(manualSharedBundleKey));
299
+ }
300
+ if (
301
+ dependency.priority === 'lazy' ||
302
+ (getFeatureFlag('conditionalBundlingApi') &&
303
+ node.value.priority === 'conditional') ||
304
+ childAsset.bundleBehavior === 'isolated' // An isolated Dependency, or Bundle must contain all assets it needs to load.
305
+ ) {
306
+ if (bundleId == null) {
307
+ let firstBundleGroup = nullthrows(
308
+ bundleGraph.getNode(stack[0][1]),
309
+ );
310
+ invariant(firstBundleGroup !== 'root');
311
+ bundle = createBundle({
312
+ asset: childAsset,
313
+ bundleBehavior:
314
+ dependency.bundleBehavior ?? childAsset.bundleBehavior,
315
+ needsStableName:
316
+ dependency.bundleBehavior === 'inline' ||
317
+ childAsset.bundleBehavior === 'inline'
318
+ ? false
319
+ : dependency.isEntry || dependency.needsStableName,
320
+ target: firstBundleGroup.target,
321
+ });
322
+ bundleId = bundleGraph.addNode(bundle);
323
+ bundles.set(childAsset.id, bundleId);
324
+ bundleRoots.set(childAsset, [bundleId, bundleId]);
325
+ bundleGroupBundleIds.add(bundleId);
326
+ bundleGraph.addEdge(bundleGraphRootNodeId, bundleId);
327
+ if (manualSharedObject) {
328
+ // MSB Step 4: If this was the first instance of a match, mark mainAsset for internalization
329
+ // since MSBs should not have main entry assets
330
+ manualBundleToInternalizedAsset
331
+ .get(bundleId)
332
+ .push(childAsset);
333
+ }
334
+ } else {
335
+ bundle = nullthrows(bundleGraph.getNode(bundleId));
336
+ invariant(bundle !== 'root');
337
+
338
+ if (
339
+ // If this dependency requests isolated, but the bundle is not,
340
+ // make the bundle isolated for all uses.
341
+ dependency.bundleBehavior === 'isolated' &&
342
+ bundle.bundleBehavior == null
343
+ ) {
344
+ bundle.bundleBehavior = dependency.bundleBehavior;
345
+ }
346
+ }
347
+
348
+ dependencyBundleGraph.addEdge(
349
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(
350
+ dependency.id,
351
+ {
352
+ value: dependency,
353
+ type: 'dependency',
354
+ },
355
+ ),
356
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(
357
+ String(bundleId),
358
+ {
359
+ value: bundle,
360
+ type: 'bundle',
361
+ },
362
+ ),
363
+ dependencyPriorityEdges[dependency.priority],
364
+ );
365
+
366
+ if (
367
+ (config.loadConditionalBundlesInParallel ??
368
+ !bundle.env.shouldScopeHoist) &&
369
+ dependency.priority === 'conditional'
370
+ ) {
371
+ // When configured (or serving code in development), serve conditional bundles in parallel so we don't get module not found errors
372
+ let [referencingBundleRoot, bundleGroupNodeId] = nullthrows(
373
+ stack[stack.length - 1],
374
+ );
375
+
376
+ let referencingBundleId = nullthrows(
377
+ bundleRoots.get(referencingBundleRoot),
378
+ )[0];
379
+
380
+ bundleRoots.set(childAsset, [bundleId, bundleGroupNodeId]);
381
+ bundleGraph.addEdge(referencingBundleId, bundleId);
382
+ }
383
+ } else if (
384
+ dependency.priority === 'parallel' ||
385
+ childAsset.bundleBehavior === 'inline'
386
+ ) {
387
+ // The referencing bundleRoot is the root of a Bundle that first brings in another bundle (essentially the FIRST parent of a bundle, this may or may not be a bundleGroup)
388
+ let [referencingBundleRoot, bundleGroupNodeId] = nullthrows(
389
+ stack[stack.length - 1],
390
+ );
391
+ let bundleGroup = nullthrows(
392
+ bundleGraph.getNode(bundleGroupNodeId),
393
+ );
394
+ invariant(bundleGroup !== 'root');
395
+
396
+ let referencingBundleId = nullthrows(
397
+ bundleRoots.get(referencingBundleRoot),
398
+ )[0];
399
+ let referencingBundle = nullthrows(
400
+ bundleGraph.getNode(referencingBundleId),
401
+ );
402
+ invariant(referencingBundle !== 'root');
403
+
404
+ if (bundleId == null) {
405
+ bundle = createBundle({
406
+ // Bundles created from type changes shouldn't have an entry asset.
407
+ asset: childAsset,
408
+ type: childAsset.type,
409
+ env: childAsset.env,
410
+ bundleBehavior:
411
+ dependency.bundleBehavior ?? childAsset.bundleBehavior,
412
+ target: referencingBundle.target,
413
+ needsStableName:
414
+ childAsset.bundleBehavior === 'inline' ||
415
+ dependency.bundleBehavior === 'inline' ||
416
+ (dependency.priority === 'parallel' &&
417
+ !dependency.needsStableName)
418
+ ? false
419
+ : referencingBundle.needsStableName,
420
+ });
421
+ bundleId = bundleGraph.addNode(bundle);
422
+ } else {
423
+ bundle = bundleGraph.getNode(bundleId);
424
+ invariant(bundle != null && bundle !== 'root');
425
+
426
+ if (
427
+ // If this dependency requests isolated, but the bundle is not,
428
+ // make the bundle isolated for all uses.
429
+ dependency.bundleBehavior === 'isolated' &&
430
+ bundle.bundleBehavior == null
431
+ ) {
432
+ bundle.bundleBehavior = dependency.bundleBehavior;
433
+ }
434
+ }
435
+
436
+ bundles.set(childAsset.id, bundleId);
437
+
438
+ // A bundle can belong to multiple bundlegroups, all the bundle groups of it's
439
+ // ancestors, and all async and entry bundles before it are "bundle groups"
440
+ // TODO: We may need to track bundles to all bundleGroups it belongs to in the future.
441
+ bundleRoots.set(childAsset, [bundleId, bundleGroupNodeId]);
442
+ bundleGraph.addEdge(referencingBundleId, bundleId);
443
+
444
+ if (bundleId != bundleGroupNodeId) {
445
+ dependencyBundleGraph.addEdge(
446
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(
447
+ dependency.id,
448
+ {
449
+ value: dependency,
450
+ type: 'dependency',
451
+ },
452
+ ),
453
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(
454
+ String(bundleId),
455
+ {
456
+ value: bundle,
457
+ type: 'bundle',
458
+ },
459
+ ),
460
+ dependencyPriorityEdges.parallel,
461
+ );
462
+ }
463
+
464
+ assetReference.get(childAsset).push([dependency, bundle]);
465
+ } else {
466
+ bundleId = null;
467
+ }
468
+ if (manualSharedObject && bundleId != null) {
469
+ // MSB Step 5: At this point we've either created or found an existing MSB bundle
470
+ // add the asset if it doesn't already have it and set key
471
+
472
+ invariant(
473
+ bundle !== 'root' && bundle != null && bundleId != null,
474
+ );
475
+
476
+ manualAssetToBundle.set(childAsset, bundleId);
477
+
478
+ if (!bundle.assets.has(childAsset)) {
479
+ // Add asset to bundle
480
+ bundle.assets.add(childAsset);
481
+ bundle.size += childAsset.stats.size;
482
+ }
483
+
484
+ bundles.set(childAsset.id, bundleId);
485
+ bundleRoots.set(childAsset, [bundleId, bundleId]);
486
+
487
+ invariant(manualSharedBundleKey != null);
488
+ // Ensure we set key to BundleId so the next glob match uses the appropriate bundle
489
+ if (!manualSharedMap.has(manualSharedBundleKey)) {
490
+ manualSharedMap.set(manualSharedBundleKey, bundleId);
491
+ }
492
+ bundle.manualSharedBundle = manualSharedObject.name;
493
+ bundle.uniqueKey = manualSharedObject.name + childAsset.type;
494
+ }
495
+ }
496
+ }
497
+ return node;
498
+ },
499
+ exit(node) {
500
+ if (stack[stack.length - 1]?.[0] === node.value) {
501
+ stack.pop();
502
+ }
503
+ },
504
+ },
505
+ null,
506
+ {skipUnusedDependencies: true},
507
+ );
508
+
509
+ // Strip MSBs of entries
510
+ for (let [
511
+ nodeId,
512
+ internalizedAssets,
513
+ ] of manualBundleToInternalizedAsset.entries()) {
514
+ let bundle = bundleGraph.getNode(nodeId);
515
+ invariant(bundle != null && bundle !== 'root');
516
+
517
+ if (!bundle.internalizedAssets) {
518
+ bundle.internalizedAssets = new BitSet(assets.length);
519
+ }
520
+ for (let asset of internalizedAssets) {
521
+ bundle.internalizedAssets.add(nullthrows(assetToIndex.get(asset)));
522
+ }
523
+ bundle.mainEntryAsset = null;
524
+ bundleGroupBundleIds.delete(nodeId); // manual bundles can now act as shared, non-bundle group, should they be non-bundleRoots as well?
525
+ }
526
+
527
+ /**
528
+ * Step Determine Reachability: Determine reachability for every asset from each bundleRoot.
529
+ * This is later used to determine which bundles to place each asset in. We build up two
530
+ * structures, one traversal each. ReachableRoots to store sync relationships,
531
+ * and bundleRootGraph to store the minimal availability through `parallel` and `async` relationships.
532
+ * The two graphs, are used to build up ancestorAssets, a structure which holds all availability by
533
+ * all means for each asset.
534
+ */
535
+ let rootNodeId = bundleRootGraph.addNode(-1);
536
+ bundleRootGraph.setRootNodeId(rootNodeId);
537
+
538
+ for (let [root] of bundleRoots) {
539
+ let nodeId = bundleRootGraph.addNode(nullthrows(assetToIndex.get(root)));
540
+ assetToBundleRootNodeId.set(root, nodeId);
541
+ if (entries.has(root)) {
542
+ bundleRootGraph.addEdge(rootNodeId, nodeId);
543
+ }
544
+ }
545
+
546
+ // reachableRoots is an array of bit sets for each asset. Each bit set
547
+ // indicates which bundle roots are reachable from that asset synchronously.
548
+ let reachableRoots = [];
549
+ for (let i = 0; i < assets.length; i++) {
550
+ reachableRoots.push(new BitSet(bundleRootGraph.nodes.length));
551
+ }
552
+
553
+ // reachableAssets is the inverse mapping of reachableRoots. For each bundle root,
554
+ // it contains a bit set that indicates which assets are reachable from it.
555
+ let reachableAssets = [];
556
+
557
+ // ancestorAssets maps bundle roots to the set of all assets available to it at runtime,
558
+ // including in earlier parallel bundles. These are intersected through all paths to
559
+ // the bundle to ensure that the available assets are always present no matter in which
560
+ // order the bundles are loaded.
561
+ let ancestorAssets = [];
562
+
563
+ let inlineConstantDeps = new DefaultMap(() => new Set());
564
+
565
+ for (let [bundleRootId, assetId] of bundleRootGraph.nodes.entries()) {
566
+ let reachable = new BitSet(assets.length);
567
+ reachableAssets.push(reachable);
568
+ ancestorAssets.push(null);
569
+
570
+ if (bundleRootId == rootNodeId || assetId == null) continue;
571
+ // Add sync relationships to ReachableRoots
572
+ let root = assets[assetId];
573
+ assetGraph.traverse(
574
+ (node, _, actions) => {
575
+ if (node.value === root) {
576
+ return;
577
+ }
578
+ if (node.type === 'dependency') {
579
+ let dependency = node.value;
580
+
581
+ if (
582
+ dependency.priority !== 'sync' &&
583
+ dependencyBundleGraph.hasContentKey(dependency.id)
584
+ ) {
585
+ let assets = assetGraph.getDependencyAssets(dependency);
586
+ if (assets.length === 0) {
587
+ return;
588
+ }
589
+ invariant(assets.length === 1);
590
+ let bundleRoot = assets[0];
591
+ let bundle = nullthrows(
592
+ bundleGraph.getNode(nullthrows(bundles.get(bundleRoot.id))),
593
+ );
594
+ if (
595
+ bundle !== 'root' &&
596
+ bundle.bundleBehavior == null &&
597
+ !bundle.env.isIsolated() &&
598
+ bundle.env.context === root.env.context
599
+ ) {
600
+ bundleRootGraph.addEdge(
601
+ bundleRootId,
602
+ nullthrows(assetToBundleRootNodeId.get(bundleRoot)),
603
+ dependency.priority === 'parallel' ||
604
+ ((config.loadConditionalBundlesInParallel ??
605
+ !bundle.env.shouldScopeHoist) &&
606
+ dependency.priority === 'conditional')
607
+ ? bundleRootEdgeTypes.parallel
608
+ : bundleRootEdgeTypes.lazy,
609
+ );
610
+ }
611
+ }
612
+
613
+ if (dependency.priority !== 'sync') {
614
+ actions.skipChildren();
615
+ }
616
+ return;
617
+ }
618
+ //asset node type
619
+ let asset = node.value;
620
+ if (asset.bundleBehavior != null) {
621
+ actions.skipChildren();
622
+ return;
623
+ }
624
+ let assetIndex = nullthrows(assetToIndex.get(node.value));
625
+ reachable.add(assetIndex);
626
+ reachableRoots[assetIndex].add(bundleRootId);
627
+
628
+ if (asset.meta.isConstantModule === true) {
629
+ let parents = assetGraph
630
+ .getIncomingDependencies(asset)
631
+ .map((dep) => nullthrows(assetGraph.getAssetWithDependency(dep)));
632
+
633
+ for (let parent of parents) {
634
+ inlineConstantDeps.get(parent).add(asset);
635
+ }
636
+ }
637
+
638
+ return;
639
+ },
640
+ root,
641
+ {skipUnusedDependencies: true},
642
+ );
643
+ }
644
+
645
+ for (let entry of entries.keys()) {
646
+ // Initialize an empty set of ancestors available to entries
647
+ let entryId = nullthrows(assetToBundleRootNodeId.get(entry));
648
+ ancestorAssets[entryId] = new BitSet(assets.length);
649
+ }
650
+
651
+ // Step Determine Availability
652
+ // Visit nodes in a topological order, visiting parent nodes before child nodes.
653
+
654
+ // This allows us to construct an understanding of which assets will already be
655
+ // loaded and available when a bundle runs, by pushing available assets downwards and
656
+ // computing the intersection of assets available through all possible paths to a bundle.
657
+ // We call this structure ancestorAssets, a Map that tracks a bundleRoot,
658
+ // to all assets available to it (meaning they will exist guaranteed when the bundleRoot is loaded)
659
+ // The topological sort ensures all parents are visited before the node we want to process.
660
+ for (let nodeId of bundleRootGraph.topoSort(ALL_EDGE_TYPES)) {
661
+ if (nodeId === rootNodeId) continue;
662
+ const bundleRoot = assets[nullthrows(bundleRootGraph.getNode(nodeId))];
663
+ let bundleGroupId = nullthrows(bundleRoots.get(bundleRoot))[1];
664
+
665
+ // At a BundleRoot, we access it's available assets (via ancestorAssets),
666
+ // and add to that all assets within the bundles in that BundleGroup.
667
+
668
+ // This set is available to all bundles in a particular bundleGroup because
669
+ // bundleGroups are just bundles loaded at the same time. However it is
670
+ // not true that a bundle's available assets = all assets of all the bundleGroups
671
+ // it belongs to. It's the intersection of those sets.
672
+ let available;
673
+ if (bundleRoot.bundleBehavior === 'isolated') {
674
+ available = new BitSet(assets.length);
675
+ } else {
676
+ available = nullthrows(ancestorAssets[nodeId]).clone();
677
+ for (let bundleIdInGroup of [
678
+ bundleGroupId,
679
+ ...bundleGraph.getNodeIdsConnectedFrom(bundleGroupId),
680
+ ]) {
681
+ let bundleInGroup = nullthrows(bundleGraph.getNode(bundleIdInGroup));
682
+ invariant(bundleInGroup !== 'root');
683
+ if (bundleInGroup.bundleBehavior != null) {
684
+ continue;
685
+ }
686
+
687
+ for (let bundleRoot of bundleInGroup.assets) {
688
+ // Assets directly connected to current bundleRoot
689
+ available.add(nullthrows(assetToIndex.get(bundleRoot)));
690
+ available.union(
691
+ reachableAssets[
692
+ nullthrows(assetToBundleRootNodeId.get(bundleRoot))
693
+ ],
694
+ );
695
+ }
696
+ }
697
+ }
698
+
699
+ // Now that we have bundleGroup availability, we will propagate that down to all the children
700
+ // of this bundleGroup. For a child, we also must maintain parallel availability. If it has
701
+ // parallel siblings that come before it, those, too, are available to it. Add those parallel
702
+ // available assets to the set of available assets for this child as well.
703
+ let children = bundleRootGraph.getNodeIdsConnectedFrom(
704
+ nodeId,
705
+ ALL_EDGE_TYPES,
706
+ );
707
+ let parallelAvailability = new BitSet(assets.length);
708
+
709
+ for (let childId of children) {
710
+ let assetId = nullthrows(bundleRootGraph.getNode(childId));
711
+ let child = assets[assetId];
712
+ let bundleBehavior = getBundleFromBundleRoot(child).bundleBehavior;
713
+ if (bundleBehavior != null) {
714
+ continue;
715
+ }
716
+ let isParallel = bundleRootGraph.hasEdge(
717
+ nodeId,
718
+ childId,
719
+ bundleRootEdgeTypes.parallel,
720
+ );
721
+
722
+ // Most of the time, a child will have many parent bundleGroups,
723
+ // so the next time we peek at a child from another parent, we will
724
+ // intersect the availability built there with the previously computed
725
+ // availability. this ensures no matter which bundleGroup loads a particular bundle,
726
+ // it will only assume availability of assets it has under any circumstance
727
+ const childAvailableAssets = ancestorAssets[childId];
728
+ let currentChildAvailable = isParallel
729
+ ? BitSet.union(parallelAvailability, available)
730
+ : available;
731
+ if (childAvailableAssets != null) {
732
+ childAvailableAssets.intersect(currentChildAvailable);
733
+ } else {
734
+ ancestorAssets[childId] = currentChildAvailable.clone();
735
+ }
736
+ if (isParallel) {
737
+ parallelAvailability.union(reachableAssets[childId]);
738
+ parallelAvailability.add(assetId); //The next sibling should have older sibling available via parallel
739
+ }
740
+ }
741
+ }
742
+ // Step Internalize async bundles - internalize Async bundles if and only if,
743
+ // the bundle is synchronously available elsewhere.
744
+ // We can query sync assets available via reachableRoots. If the parent has
745
+ // the bundleRoot by reachableRoots AND ancestorAssets, internalize it.
746
+ for (let [id, bundleRootId] of bundleRootGraph.nodes.entries()) {
747
+ if (bundleRootId == null || id === rootNodeId) continue;
748
+ let bundleRoot = assets[bundleRootId];
749
+
750
+ if (manualAssetToConfig.has(bundleRoot)) {
751
+ // We internalize for MSBs later, we should never delete MSBs
752
+ continue;
753
+ }
754
+
755
+ let parentRoots = bundleRootGraph.getNodeIdsConnectedTo(id, ALL_EDGE_TYPES);
756
+ let canDelete =
757
+ getBundleFromBundleRoot(bundleRoot).bundleBehavior !== 'isolated';
758
+ if (parentRoots.length === 0) continue;
759
+ for (let parentId of parentRoots) {
760
+ if (parentId === rootNodeId) {
761
+ // connected to root.
762
+ canDelete = false;
763
+ continue;
764
+ }
765
+ if (
766
+ reachableAssets[parentId].has(bundleRootId) ||
767
+ ancestorAssets[parentId]?.has(bundleRootId)
768
+ ) {
769
+ let parentAssetId = nullthrows(bundleRootGraph.getNode(parentId));
770
+ let parent = assets[parentAssetId];
771
+ let parentBundle = bundleGraph.getNode(
772
+ nullthrows(bundles.get(parent.id)),
773
+ );
774
+ invariant(parentBundle != null && parentBundle !== 'root');
775
+ if (!parentBundle.internalizedAssets) {
776
+ parentBundle.internalizedAssets = new BitSet(assets.length);
777
+ }
778
+
779
+ parentBundle.internalizedAssets.add(bundleRootId);
780
+ } else {
781
+ canDelete = false;
782
+ }
783
+ }
784
+ if (canDelete) {
785
+ deleteBundle(bundleRoot);
786
+ }
787
+ }
788
+
789
+ function assignInlineConstants(parentAsset: Asset, bundle: Bundle) {
790
+ for (let inlineConstant of inlineConstantDeps.get(parentAsset)) {
791
+ if (!bundle.assets.has(inlineConstant)) {
792
+ bundle.assets.add(inlineConstant);
793
+ bundle.size += inlineConstant.stats.size;
794
+ }
795
+ }
796
+ }
797
+
798
+ // Step Insert Or Share: Place all assets into bundles or create shared bundles. Each asset
799
+ // is placed into a single bundle based on the bundle entries it is reachable from.
800
+ // This creates a maximally code split bundle graph with no duplication.
801
+ let reachable = new BitSet(assets.length);
802
+ let reachableNonEntries = new BitSet(assets.length);
803
+ let reachableIntersection = new BitSet(assets.length);
804
+ for (let i = 0; i < assets.length; i++) {
805
+ let asset = assets[i];
806
+ let manualSharedObject = manualAssetToConfig.get(asset);
807
+
808
+ if (bundleRoots.has(asset) && inlineConstantDeps.get(asset).size > 0) {
809
+ let entryBundleId = nullthrows(bundleRoots.get(asset))[0];
810
+ let entryBundle = nullthrows(bundleGraph.getNode(entryBundleId));
811
+ invariant(entryBundle !== 'root');
812
+ assignInlineConstants(asset, entryBundle);
813
+ }
814
+
815
+ if (asset.meta.isConstantModule === true) {
816
+ // Ignore constant modules as they are placed with their direct parents
817
+ continue;
818
+ }
819
+
820
+ // Unreliable bundleRoot assets which need to pulled in by shared bundles or other means.
821
+ // Filter out entries, since they can't have shared bundles.
822
+ // Neither can non-splittable, isolated, or needing of stable name bundles.
823
+ // Reserve those filtered out bundles since we add the asset back into them.
824
+ reachableNonEntries.clear();
825
+ reachableRoots[i].forEach((nodeId) => {
826
+ let assetId = bundleRootGraph.getNode(nodeId);
827
+ if (assetId == null) return; // deleted
828
+ let a = assets[assetId];
829
+ if (
830
+ entries.has(a) ||
831
+ !a.isBundleSplittable ||
832
+ (bundleRoots.get(a) &&
833
+ (getBundleFromBundleRoot(a).needsStableName ||
834
+ getBundleFromBundleRoot(a).bundleBehavior === 'isolated'))
835
+ ) {
836
+ // Add asset to non-splittable bundles.
837
+ addAssetToBundleRoot(asset, a);
838
+ } else if (!ancestorAssets[nodeId]?.has(i)) {
839
+ // Filter out bundles from this asset's reachable array if
840
+ // bundle does not contain the asset in its ancestry
841
+ reachableNonEntries.add(assetId);
842
+ }
843
+ });
844
+
845
+ reachable.bits.set(reachableNonEntries.bits);
846
+
847
+ // If we encounter a "manual" asset, draw an edge from reachable to its MSB
848
+ if (manualSharedObject && !reachable.empty()) {
849
+ let bundle;
850
+ let bundleId;
851
+ let manualSharedBundleKey = manualSharedObject.name + ',' + asset.type;
852
+ let sourceBundles = [];
853
+ reachable.forEach((id) => {
854
+ sourceBundles.push(nullthrows(bundleRoots.get(assets[id]))[0]);
855
+ });
856
+
857
+ if (!manualSharedMap.has(manualSharedBundleKey)) {
858
+ let firstSourceBundle = nullthrows(
859
+ bundleGraph.getNode(sourceBundles[0]),
860
+ );
861
+ invariant(firstSourceBundle !== 'root');
862
+
863
+ bundle = createBundle({
864
+ env: firstSourceBundle.env,
865
+ manualSharedBundle: manualSharedObject?.name,
866
+ sourceBundles: new Set(sourceBundles),
867
+ target: firstSourceBundle.target,
868
+ type: asset.type,
869
+ uniqueKey: manualSharedBundleKey,
870
+ });
871
+ bundle.assets.add(asset);
872
+ bundleId = bundleGraph.addNode(bundle);
873
+ manualSharedMap.set(manualSharedBundleKey, bundleId);
874
+ } else {
875
+ bundleId = nullthrows(manualSharedMap.get(manualSharedBundleKey));
876
+ bundle = nullthrows(bundleGraph.getNode(bundleId));
877
+ invariant(
878
+ bundle != null && bundle !== 'root',
879
+ 'We tried to use the root incorrectly',
880
+ );
881
+
882
+ if (!bundle.assets.has(asset)) {
883
+ bundle.assets.add(asset);
884
+ bundle.size += asset.stats.size;
885
+ }
886
+
887
+ for (let s of sourceBundles) {
888
+ if (s != bundleId) {
889
+ bundle.sourceBundles.add(s);
890
+ }
891
+ }
892
+ }
893
+
894
+ for (let sourceBundleId of sourceBundles) {
895
+ if (bundleId !== sourceBundleId) {
896
+ bundleGraph.addEdge(sourceBundleId, bundleId);
897
+ }
898
+ }
899
+
900
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(String(bundleId), {
901
+ value: bundle,
902
+ type: 'bundle',
903
+ });
904
+ continue;
905
+ }
906
+
907
+ // Finally, filter out bundleRoots (bundles) from this assets
908
+ // reachable if they are subgraphs, and reuse that subgraph bundle
909
+ // by drawing an edge. Essentially, if two bundles within an asset's
910
+ // reachable array, have an ancestor-subgraph relationship, draw that edge.
911
+ // This allows for us to reuse a bundle instead of making a shared bundle if
912
+ // a bundle represents the exact set of assets a set of bundles would share
913
+
914
+ // if a bundle b is a subgraph of another bundle f, reuse it, drawing an edge between the two
915
+ if (config.disableSharedBundles === false) {
916
+ reachableNonEntries.forEach((candidateId) => {
917
+ let candidateSourceBundleRoot = assets[candidateId];
918
+ let candidateSourceBundleId = nullthrows(
919
+ bundleRoots.get(candidateSourceBundleRoot),
920
+ )[0];
921
+ if (candidateSourceBundleRoot.env.isIsolated()) {
922
+ return;
923
+ }
924
+ let reuseableBundleId = bundles.get(asset.id);
925
+ if (reuseableBundleId != null) {
926
+ reachable.delete(candidateId);
927
+ bundleGraph.addEdge(candidateSourceBundleId, reuseableBundleId);
928
+
929
+ let reusableBundle = bundleGraph.getNode(reuseableBundleId);
930
+ invariant(reusableBundle !== 'root' && reusableBundle != null);
931
+ reusableBundle.sourceBundles.add(candidateSourceBundleId);
932
+ } else {
933
+ // Asset is not a bundleRoot, but if its ancestor bundle (in the asset's reachable) can be
934
+ // reused as a subgraph of another bundleRoot in its reachable, reuse it
935
+ reachableIntersection.bits.set(reachableNonEntries.bits);
936
+ reachableIntersection.intersect(
937
+ reachableAssets[
938
+ nullthrows(assetToBundleRootNodeId.get(candidateSourceBundleRoot))
939
+ ],
940
+ );
941
+
942
+ reachableIntersection.forEach((otherCandidateId) => {
943
+ let otherReuseCandidate = assets[otherCandidateId];
944
+ if (candidateSourceBundleRoot === otherReuseCandidate) return;
945
+ let reusableBundleId = nullthrows(
946
+ bundles.get(otherReuseCandidate.id),
947
+ );
948
+ reachable.delete(candidateId);
949
+ bundleGraph.addEdge(
950
+ nullthrows(bundles.get(candidateSourceBundleRoot.id)),
951
+ reusableBundleId,
952
+ );
953
+ let reusableBundle = bundleGraph.getNode(reusableBundleId);
954
+ invariant(reusableBundle !== 'root' && reusableBundle != null);
955
+ reusableBundle.sourceBundles.add(candidateSourceBundleId);
956
+ });
957
+ }
958
+ });
959
+ }
960
+
961
+ let reachableArray = [];
962
+ reachable.forEach((id) => {
963
+ reachableArray.push(assets[id]);
964
+ });
965
+
966
+ // Create shared bundles for splittable bundles.
967
+ if (
968
+ config.disableSharedBundles === false &&
969
+ reachableArray.length > config.minBundles
970
+ ) {
971
+ let sourceBundles = reachableArray.map(
972
+ (a) => nullthrows(bundleRoots.get(a))[0],
973
+ );
974
+ let key = reachableArray.map((a) => a.id).join(',') + '.' + asset.type;
975
+ let bundleId = bundles.get(key);
976
+ let bundle;
977
+ if (bundleId == null) {
978
+ let firstSourceBundle = nullthrows(
979
+ bundleGraph.getNode(sourceBundles[0]),
980
+ );
981
+ invariant(firstSourceBundle !== 'root');
982
+ bundle = createBundle({
983
+ env: firstSourceBundle.env,
984
+ sourceBundles: new Set(sourceBundles),
985
+ target: firstSourceBundle.target,
986
+ type: asset.type,
987
+ });
988
+ let sharedInternalizedAssets = firstSourceBundle.internalizedAssets
989
+ ? firstSourceBundle.internalizedAssets.clone()
990
+ : new BitSet(assets.length);
991
+
992
+ for (let p of sourceBundles) {
993
+ let parentBundle = nullthrows(bundleGraph.getNode(p));
994
+ invariant(parentBundle !== 'root');
995
+ if (parentBundle === firstSourceBundle) continue;
996
+
997
+ if (parentBundle.internalizedAssets) {
998
+ sharedInternalizedAssets.intersect(parentBundle.internalizedAssets);
999
+ } else {
1000
+ sharedInternalizedAssets.clear();
1001
+ }
1002
+ }
1003
+ bundle.internalizedAssets = sharedInternalizedAssets;
1004
+ bundleId = bundleGraph.addNode(bundle);
1005
+ bundles.set(key, bundleId);
1006
+ } else {
1007
+ bundle = nullthrows(bundleGraph.getNode(bundleId));
1008
+ invariant(bundle !== 'root');
1009
+ }
1010
+ bundle.assets.add(asset);
1011
+ bundle.size += asset.stats.size;
1012
+
1013
+ assignInlineConstants(asset, bundle);
1014
+
1015
+ for (let sourceBundleId of sourceBundles) {
1016
+ if (bundleId !== sourceBundleId) {
1017
+ bundleGraph.addEdge(sourceBundleId, bundleId);
1018
+ }
1019
+ }
1020
+
1021
+ dependencyBundleGraph.addNodeByContentKeyIfNeeded(String(bundleId), {
1022
+ value: bundle,
1023
+ type: 'bundle',
1024
+ });
1025
+ } else if (
1026
+ config.disableSharedBundles === true ||
1027
+ reachableArray.length <= config.minBundles
1028
+ ) {
1029
+ for (let root of reachableArray) {
1030
+ addAssetToBundleRoot(asset, root);
1031
+ }
1032
+ }
1033
+ }
1034
+
1035
+ let manualSharedBundleIds = new Set([...manualSharedMap.values()]);
1036
+ // Step split manual shared bundles for those that have the "split" property set
1037
+ let remainderMap = new DefaultMap(() => []);
1038
+ for (let id of manualSharedMap.values()) {
1039
+ let manualBundle = bundleGraph.getNode(id);
1040
+ invariant(manualBundle !== 'root' && manualBundle != null);
1041
+
1042
+ if (manualBundle.sourceBundles.size > 0) {
1043
+ let firstSourceBundle = nullthrows(
1044
+ bundleGraph.getNode([...manualBundle.sourceBundles][0]),
1045
+ );
1046
+ invariant(firstSourceBundle !== 'root');
1047
+ let firstAsset = [...manualBundle.assets][0];
1048
+ let manualSharedObject = manualAssetToConfig.get(firstAsset);
1049
+ invariant(manualSharedObject != null);
1050
+ let modNum = manualAssetToConfig.get(firstAsset)?.split;
1051
+ if (modNum != null) {
1052
+ for (let a of [...manualBundle.assets]) {
1053
+ let numRep = getBigIntFromContentKey(a.id);
1054
+ // $FlowFixMe Flow doesn't know about BigInt
1055
+ let r = Number(numRep % BigInt(modNum));
1056
+
1057
+ remainderMap.get(r).push(a);
1058
+ }
1059
+
1060
+ for (let i = 1; i < [...remainderMap.keys()].length; i++) {
1061
+ let bundle = createBundle({
1062
+ env: firstSourceBundle.env,
1063
+ manualSharedBundle: manualSharedObject.name,
1064
+ sourceBundles: manualBundle.sourceBundles,
1065
+ target: firstSourceBundle.target,
1066
+ type: firstSourceBundle.type,
1067
+ uniqueKey: manualSharedObject.name + firstSourceBundle.type + i,
1068
+ });
1069
+ bundle.internalizedAssets = manualBundle.internalizedAssets;
1070
+ let bundleId = bundleGraph.addNode(bundle);
1071
+ manualSharedBundleIds.add(bundleId);
1072
+ for (let sourceBundleId of manualBundle.sourceBundles) {
1073
+ if (bundleId !== sourceBundleId) {
1074
+ bundleGraph.addEdge(sourceBundleId, bundleId);
1075
+ }
1076
+ }
1077
+ for (let sp of remainderMap.get(i)) {
1078
+ bundle.assets.add(sp);
1079
+ bundle.size += sp.stats.size;
1080
+ manualBundle.assets.delete(sp);
1081
+ manualBundle.size -= sp.stats.size;
1082
+ }
1083
+ }
1084
+ }
1085
+ }
1086
+ }
1087
+
1088
+ // Step insert constant modules into manual shared bundles.
1089
+ // We have to do this separately as they're the only case where a single asset can
1090
+ // match multiple MSB's
1091
+ for (let [asset, msbs] of constantModuleToMSB.entries()) {
1092
+ for (let manualSharedObject of msbs) {
1093
+ let bundleId = manualSharedMap.get(manualSharedObject.name + ',js');
1094
+ if (bundleId == null) continue;
1095
+ let bundle = nullthrows(bundleGraph.getNode(bundleId));
1096
+ invariant(
1097
+ bundle != null && bundle !== 'root',
1098
+ 'We tried to use the root incorrectly',
1099
+ );
1100
+
1101
+ if (!bundle.assets.has(asset)) {
1102
+ bundle.assets.add(asset);
1103
+ bundle.size += asset.stats.size;
1104
+ }
1105
+ }
1106
+ }
1107
+
1108
+ // Step Merge Share Bundles: Merge any shared bundles under the minimum bundle size back into
1109
+ // their source bundles, and remove the bundle.
1110
+ // We should include "bundle reuse" as shared bundles that may be removed but the bundle itself would have to be retained
1111
+ for (let [bundleNodeId, bundle] of bundleGraph.nodes.entries()) {
1112
+ if (!bundle || bundle === 'root') continue;
1113
+ if (
1114
+ bundle.sourceBundles.size > 0 &&
1115
+ bundle.mainEntryAsset == null &&
1116
+ bundle.size < config.minBundleSize &&
1117
+ !manualSharedBundleIds.has(bundleNodeId)
1118
+ ) {
1119
+ removeBundle(bundleGraph, bundleNodeId, assetReference);
1120
+ }
1121
+ }
1122
+
1123
+ let modifiedSourceBundles = new Set();
1124
+
1125
+ // Step Remove Shared Bundles: Remove shared bundles from bundle groups that hit the parallel request limit.
1126
+ if (config.disableSharedBundles === false) {
1127
+ for (let bundleGroupId of bundleGraph.getNodeIdsConnectedFrom(rootNodeId)) {
1128
+ // Find shared bundles in this bundle group.
1129
+ let bundleId = bundleGroupId;
1130
+
1131
+ // We should include "bundle reuse" as shared bundles that may be removed but the bundle itself would have to be retained
1132
+ let bundleIdsInGroup = getBundlesForBundleGroup(bundleId); //get all bundlegrups this bundle is an ancestor of
1133
+
1134
+ // Filter out inline assests as they should not contribute to PRL
1135
+ let numBundlesContributingToPRL = bundleIdsInGroup.reduce((count, b) => {
1136
+ let bundle = nullthrows(bundleGraph.getNode(b));
1137
+ invariant(bundle !== 'root');
1138
+ return count + (bundle.bundleBehavior !== 'inline');
1139
+ }, 0);
1140
+
1141
+ if (numBundlesContributingToPRL > config.maxParallelRequests) {
1142
+ let sharedBundleIdsInBundleGroup = bundleIdsInGroup.filter((b) => {
1143
+ let bundle = nullthrows(bundleGraph.getNode(b));
1144
+ // shared bundles must have source bundles, we could have a bundle
1145
+ // connected to another bundle that isnt a shared bundle, so check
1146
+ return (
1147
+ bundle !== 'root' &&
1148
+ bundle.sourceBundles.size > 0 &&
1149
+ bundleId != b &&
1150
+ !manualSharedBundleIds.has(b)
1151
+ );
1152
+ });
1153
+
1154
+ // Sort the bundles so the smallest ones are removed first.
1155
+ let sharedBundlesInGroup = sharedBundleIdsInBundleGroup
1156
+ .map((id) => ({
1157
+ id,
1158
+ bundle: nullthrows(bundleGraph.getNode(id)),
1159
+ }))
1160
+ .map(({id, bundle}) => {
1161
+ // For Flow
1162
+ invariant(bundle !== 'root');
1163
+ return {id, bundle};
1164
+ })
1165
+ .sort((a, b) => b.bundle.size - a.bundle.size);
1166
+
1167
+ // Remove bundles until the bundle group is within the parallel request limit.
1168
+ while (
1169
+ sharedBundlesInGroup.length > 0 &&
1170
+ numBundlesContributingToPRL > config.maxParallelRequests
1171
+ ) {
1172
+ let bundleTuple = sharedBundlesInGroup.pop();
1173
+ let bundleToRemove = bundleTuple.bundle;
1174
+ let bundleIdToRemove = bundleTuple.id;
1175
+ //TODO add integration test where bundles in bunlde group > max parallel request limit & only remove a couple shared bundles
1176
+ // but total # bundles still exceeds limit due to non shared bundles
1177
+
1178
+ // Add all assets in the shared bundle into the source bundles that are within this bundle group.
1179
+ let sourceBundles = [...bundleToRemove.sourceBundles].filter((b) =>
1180
+ bundleIdsInGroup.includes(b),
1181
+ );
1182
+
1183
+ for (let sourceBundleId of sourceBundles) {
1184
+ let sourceBundle = nullthrows(bundleGraph.getNode(sourceBundleId));
1185
+ invariant(sourceBundle !== 'root');
1186
+ modifiedSourceBundles.add(sourceBundle);
1187
+ bundleToRemove.sourceBundles.delete(sourceBundleId);
1188
+ for (let asset of bundleToRemove.assets) {
1189
+ addAssetToBundleRoot(
1190
+ asset,
1191
+ nullthrows(sourceBundle.mainEntryAsset),
1192
+ );
1193
+ }
1194
+ //This case is specific to reused bundles, which can have shared bundles attached to it
1195
+ for (let childId of bundleGraph.getNodeIdsConnectedFrom(
1196
+ bundleIdToRemove,
1197
+ )) {
1198
+ let child = bundleGraph.getNode(childId);
1199
+ invariant(child !== 'root' && child != null);
1200
+ child.sourceBundles.add(sourceBundleId);
1201
+ bundleGraph.addEdge(sourceBundleId, childId);
1202
+ }
1203
+ // needs to add test case where shared bundle is removed from ONE bundlegroup but not from the whole graph!
1204
+ // Remove the edge from this bundle group to the shared bundle.
1205
+ // If there is now only a single bundle group that contains this bundle,
1206
+ // merge it into the remaining source bundles. If it is orphaned entirely, remove it.
1207
+ let incomingNodeCount =
1208
+ bundleGraph.getNodeIdsConnectedTo(bundleIdToRemove).length;
1209
+
1210
+ if (
1211
+ incomingNodeCount <= 2 &&
1212
+ //Never fully remove reused bundles
1213
+ bundleToRemove.mainEntryAsset == null
1214
+ ) {
1215
+ // If one bundle group removes a shared bundle, but the other *can* keep it, still remove because that shared bundle is pointless (only one source bundle)
1216
+ removeBundle(bundleGraph, bundleIdToRemove, assetReference);
1217
+ // Stop iterating through bundleToRemove's sourceBundles as the bundle has been removed.
1218
+ break;
1219
+ } else {
1220
+ bundleGraph.removeEdge(sourceBundleId, bundleIdToRemove);
1221
+ }
1222
+ }
1223
+ numBundlesContributingToPRL--;
1224
+ }
1225
+ }
1226
+ }
1227
+ }
1228
+
1229
+ function getBigIntFromContentKey(contentKey) {
1230
+ let b = Buffer.alloc(64);
1231
+ b.write(contentKey);
1232
+ // $FlowFixMe Flow doesn't have BigInt types in this version
1233
+ return b.readBigInt64BE();
1234
+ }
1235
+ // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
1236
+ if (modifiedSourceBundles.size > 0) {
1237
+ let assetOrderMap = new Map(assets.map((a, index) => [a, index]));
1238
+
1239
+ for (let bundle of modifiedSourceBundles) {
1240
+ bundle.assets = new Set(
1241
+ [...bundle.assets].sort((a, b) => {
1242
+ let aIndex = nullthrows(assetOrderMap.get(a));
1243
+ let bIndex = nullthrows(assetOrderMap.get(b));
1244
+
1245
+ return aIndex - bIndex;
1246
+ }),
1247
+ );
1248
+ }
1249
+ }
1250
+ function deleteBundle(bundleRoot: BundleRoot) {
1251
+ bundleGraph.removeNode(nullthrows(bundles.get(bundleRoot.id)));
1252
+ bundleRoots.delete(bundleRoot);
1253
+ bundles.delete(bundleRoot.id);
1254
+ let bundleRootId = assetToBundleRootNodeId.get(bundleRoot);
1255
+ if (bundleRootId != null && bundleRootGraph.hasNode(bundleRootId)) {
1256
+ bundleRootGraph.removeNode(bundleRootId);
1257
+ }
1258
+ }
1259
+ function getBundlesForBundleGroup(bundleGroupId) {
1260
+ let bundlesInABundleGroup = [];
1261
+ bundleGraph.traverse((nodeId) => {
1262
+ bundlesInABundleGroup.push(nodeId);
1263
+ }, bundleGroupId);
1264
+ return bundlesInABundleGroup;
1265
+ }
1266
+
1267
+ function getBundleFromBundleRoot(bundleRoot: BundleRoot): Bundle {
1268
+ let bundle = bundleGraph.getNode(
1269
+ nullthrows(bundleRoots.get(bundleRoot))[0],
1270
+ );
1271
+ invariant(bundle !== 'root' && bundle != null);
1272
+ return bundle;
1273
+ }
1274
+
1275
+ function addAssetToBundleRoot(asset: Asset, bundleRoot: Asset) {
1276
+ let [bundleId, bundleGroupId] = nullthrows(bundleRoots.get(bundleRoot));
1277
+ let bundle = nullthrows(bundleGraph.getNode(bundleId));
1278
+ invariant(bundle !== 'root');
1279
+
1280
+ if (asset.type !== bundle.type) {
1281
+ let bundleGroup = nullthrows(bundleGraph.getNode(bundleGroupId));
1282
+ invariant(bundleGroup !== 'root');
1283
+ let key = nullthrows(bundleGroup.mainEntryAsset).id + '.' + asset.type;
1284
+ let typeChangeBundleId = bundles.get(key);
1285
+ if (typeChangeBundleId == null) {
1286
+ let typeChangeBundle = createBundle({
1287
+ uniqueKey: key,
1288
+ needsStableName: bundle.needsStableName,
1289
+ bundleBehavior: bundle.bundleBehavior,
1290
+ type: asset.type,
1291
+ target: bundle.target,
1292
+ env: bundle.env,
1293
+ });
1294
+ typeChangeBundleId = bundleGraph.addNode(typeChangeBundle);
1295
+ bundleGraph.addEdge(bundleId, typeChangeBundleId);
1296
+ bundles.set(key, typeChangeBundleId);
1297
+ bundle = typeChangeBundle;
1298
+ } else {
1299
+ bundle = nullthrows(bundleGraph.getNode(typeChangeBundleId));
1300
+ invariant(bundle !== 'root');
1301
+ }
1302
+ }
1303
+
1304
+ bundle.assets.add(asset);
1305
+ bundle.size += asset.stats.size;
1306
+ assignInlineConstants(asset, bundle);
1307
+ }
1308
+
1309
+ function removeBundle(
1310
+ bundleGraph: Graph<Bundle | 'root'>,
1311
+ bundleId: NodeId,
1312
+ assetReference: DefaultMap<Asset, Array<[Dependency, Bundle]>>,
1313
+ ) {
1314
+ let bundle = nullthrows(bundleGraph.getNode(bundleId));
1315
+ invariant(bundle !== 'root');
1316
+ for (let asset of bundle.assets) {
1317
+ assetReference.set(
1318
+ asset,
1319
+ assetReference.get(asset).filter((t) => !t.includes(bundle)),
1320
+ );
1321
+ for (let sourceBundleId of bundle.sourceBundles) {
1322
+ let sourceBundle = nullthrows(bundleGraph.getNode(sourceBundleId));
1323
+ invariant(sourceBundle !== 'root');
1324
+ addAssetToBundleRoot(asset, nullthrows(sourceBundle.mainEntryAsset));
1325
+ }
1326
+ }
1327
+
1328
+ bundleGraph.removeNode(bundleId);
1329
+ }
1330
+
1331
+ return {
1332
+ assets,
1333
+ bundleGraph,
1334
+ dependencyBundleGraph,
1335
+ bundleGroupBundleIds,
1336
+ assetReference,
1337
+ manualAssetToBundle,
1338
+ };
1339
+ }
1340
+
1341
+ function createBundle(opts: {|
1342
+ asset?: Asset,
1343
+ bundleBehavior?: ?BundleBehavior,
1344
+ env?: Environment,
1345
+ manualSharedBundle?: ?string,
1346
+ needsStableName?: boolean,
1347
+ sourceBundles?: Set<NodeId>,
1348
+ target: Target,
1349
+ type?: string,
1350
+ uniqueKey?: string,
1351
+ |}): Bundle {
1352
+ if (opts.asset == null) {
1353
+ return {
1354
+ assets: new Set(),
1355
+ bundleBehavior: opts.bundleBehavior,
1356
+ env: nullthrows(opts.env),
1357
+ mainEntryAsset: null,
1358
+ manualSharedBundle: opts.manualSharedBundle,
1359
+ needsStableName: Boolean(opts.needsStableName),
1360
+ size: 0,
1361
+ sourceBundles: opts.sourceBundles ?? new Set(),
1362
+ target: opts.target,
1363
+ type: nullthrows(opts.type),
1364
+ uniqueKey: opts.uniqueKey,
1365
+ };
1366
+ }
1367
+
1368
+ let asset = nullthrows(opts.asset);
1369
+ return {
1370
+ assets: new Set([asset]),
1371
+ bundleBehavior: opts.bundleBehavior ?? asset.bundleBehavior,
1372
+ env: opts.env ?? asset.env,
1373
+ mainEntryAsset: asset,
1374
+ manualSharedBundle: opts.manualSharedBundle,
1375
+ needsStableName: Boolean(opts.needsStableName),
1376
+ size: asset.stats.size,
1377
+ sourceBundles: opts.sourceBundles ?? new Set(),
1378
+ target: opts.target,
1379
+ type: opts.type ?? asset.type,
1380
+ uniqueKey: opts.uniqueKey,
1381
+ };
1382
+ }