@atlaspack/bundler-default 3.1.1 → 3.1.3-typescript-08dcc1c9b.0

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.
@@ -1,5 +1,3 @@
1
- // @flow strict-local
2
-
3
1
  import path from 'path';
4
2
 
5
3
  import {getFeatureFlag} from '@atlaspack/feature-flags';
@@ -8,7 +6,7 @@ import {
8
6
  BitSet,
9
7
  ContentGraph,
10
8
  Graph,
11
- type NodeId,
9
+ NodeId,
12
10
  } from '@atlaspack/graph';
13
11
  import type {
14
12
  Asset,
@@ -23,37 +21,37 @@ import {DefaultMap, globToRegex} from '@atlaspack/utils';
23
21
  import invariant from 'assert';
24
22
  import nullthrows from 'nullthrows';
25
23
 
26
- import {findMergeCandidates, type MergeGroup} from './bundleMerge';
24
+ import {findMergeCandidates, MergeGroup} from './bundleMerge';
27
25
  import type {ResolvedBundlerConfig, MergeCandidates} from './bundlerConfig';
28
26
 
29
27
  /* BundleRoot - An asset that is the main entry of a Bundle. */
30
28
  type BundleRoot = Asset;
31
29
 
32
- export type Bundle = {|
33
- uniqueKey: ?string,
34
- assets: Set<Asset>,
35
- internalizedAssets?: BitSet,
36
- bundleBehavior?: ?BundleBehavior,
37
- needsStableName: boolean,
38
- mainEntryAsset: ?Asset,
39
- size: number,
40
- sourceBundles: Set<NodeId>,
41
- target: Target,
42
- env: Environment,
43
- type: string,
44
- manualSharedBundle: ?string, // for naming purposes
45
- |};
30
+ export type Bundle = {
31
+ uniqueKey: string | null | undefined;
32
+ assets: Set<Asset>;
33
+ internalizedAssets?: BitSet;
34
+ bundleBehavior?: BundleBehavior | null | undefined;
35
+ needsStableName: boolean;
36
+ mainEntryAsset: Asset | null | undefined;
37
+ size: number;
38
+ sourceBundles: Set<NodeId>;
39
+ target: Target;
40
+ env: Environment;
41
+ type: string;
42
+ manualSharedBundle: string | null | undefined; // for naming purposes;
43
+ };
46
44
 
47
45
  export type DependencyBundleGraph = ContentGraph<
48
- | {|
49
- value: Bundle,
50
- type: 'bundle',
51
- |}
52
- | {|
53
- value: Dependency,
54
- type: 'dependency',
55
- |},
56
- number,
46
+ | {
47
+ value: Bundle;
48
+ type: 'bundle';
49
+ }
50
+ | {
51
+ value: Dependency;
52
+ type: 'dependency';
53
+ },
54
+ number
57
55
  >;
58
56
 
59
57
  const dependencyPriorityEdges = {
@@ -61,7 +59,7 @@ const dependencyPriorityEdges = {
61
59
  parallel: 2,
62
60
  lazy: 3,
63
61
  conditional: 4,
64
- };
62
+ } as const;
65
63
 
66
64
  export const idealBundleGraphEdges = Object.freeze({
67
65
  default: 1,
@@ -70,20 +68,20 @@ export const idealBundleGraphEdges = Object.freeze({
70
68
 
71
69
  export type IdealBundleGraph = Graph<
72
70
  Bundle | 'root',
73
- $Values<typeof idealBundleGraphEdges>,
71
+ (typeof idealBundleGraphEdges)[keyof typeof idealBundleGraphEdges]
74
72
  >;
75
73
 
76
74
  // IdealGraph is the structure we will pass to decorate,
77
75
  // which mutates the assetGraph into the bundleGraph we would
78
76
  // expect from default bundler
79
- export type IdealGraph = {|
80
- assetReference: DefaultMap<Asset, Array<[Dependency, Bundle]>>,
81
- assets: Array<Asset>,
82
- bundleGraph: IdealBundleGraph,
83
- bundleGroupBundleIds: Set<NodeId>,
84
- dependencyBundleGraph: DependencyBundleGraph,
85
- manualAssetToBundle: Map<Asset, NodeId>,
86
- |};
77
+ export type IdealGraph = {
78
+ assetReference: DefaultMap<Asset, Array<[Dependency, Bundle]>>;
79
+ assets: Array<Asset>;
80
+ bundleGraph: IdealBundleGraph;
81
+ bundleGroupBundleIds: Set<NodeId>;
82
+ dependencyBundleGraph: DependencyBundleGraph;
83
+ manualAssetToBundle: Map<Asset, NodeId>;
84
+ };
87
85
 
88
86
  export function createIdealGraph(
89
87
  assetGraph: MutableBundleGraph,
@@ -97,7 +95,7 @@ export function createIdealGraph(
97
95
  let dependencyBundleGraph: DependencyBundleGraph = new ContentGraph();
98
96
  let assetReference: DefaultMap<
99
97
  Asset,
100
- Array<[Dependency, Bundle]>,
98
+ Array<[Dependency, Bundle]>
101
99
  > = new DefaultMap(() => []);
102
100
 
103
101
  // A Graph of Bundles and a root node (dummy string), which models only Bundles, and connections to their
@@ -111,8 +109,9 @@ export function createIdealGraph(
111
109
  };
112
110
  // Graph that models bundleRoots, with parallel & async deps only to inform reachability
113
111
  let bundleRootGraph: Graph<
114
- number, // asset index
115
- $Values<typeof bundleRootEdgeTypes>,
112
+ // asset index
113
+ number,
114
+ (typeof bundleRootEdgeTypes)[keyof typeof bundleRootEdgeTypes]
116
115
  > = new Graph();
117
116
  let assetToBundleRootNodeId = new Map<BundleRoot, number>();
118
117
 
@@ -146,7 +145,7 @@ export function createIdealGraph(
146
145
  bundleGroupBundleIds.add(nodeId);
147
146
  }
148
147
 
149
- let assets = [];
148
+ let assets: Array<Asset> = [];
150
149
  let assetToIndex = new Map<Asset, number>();
151
150
 
152
151
  function makeManualAssetToConfigLookup() {
@@ -161,6 +160,7 @@ export function createIdealGraph(
161
160
 
162
161
  for (let c of config.manualSharedBundles) {
163
162
  if (c.root != null) {
163
+ // @ts-expect-error TS2345
164
164
  parentsToConfig.get(path.join(config.projectRoot, c.root)).push(c);
165
165
  }
166
166
  }
@@ -208,6 +208,7 @@ export function createIdealGraph(
208
208
  // We track all matching MSB's for constant modules as they are never duplicated
209
209
  // and need to be assigned to all matching bundles
210
210
  if (node.value.meta.isConstantModule === true) {
211
+ // @ts-expect-error TS2345
211
212
  constantModuleToMSB.get(node.value).push(c);
212
213
  }
213
214
 
@@ -241,14 +242,14 @@ export function createIdealGraph(
241
242
  makeManualAssetToConfigLookup();
242
243
  let manualBundleToInternalizedAsset: DefaultMap<
243
244
  NodeId,
244
- Array<Asset>,
245
+ Array<Asset>
245
246
  > = new DefaultMap(() => []);
246
247
 
247
248
  let mergeSourceBundleLookup = new Map<string, NodeId>();
248
249
  let mergeSourceBundleAssets = new Set(
249
250
  config.sharedBundleMerge?.flatMap(
250
251
  (c) =>
251
- c.sourceBundles?.map((assetMatch) =>
252
+ c.sourceBundles?.map((assetMatch: string) =>
252
253
  path.join(config.projectRoot, assetMatch),
253
254
  ) ?? [],
254
255
  ),
@@ -261,7 +262,27 @@ export function createIdealGraph(
261
262
  */
262
263
  assetGraph.traverse(
263
264
  {
264
- enter(node, context, actions) {
265
+ enter(
266
+ node: // @ts-expect-error TS2304
267
+ | BundleGraphTraversable
268
+ | {
269
+ readonly type: 'dependency';
270
+ value: Dependency;
271
+ },
272
+ context:
273
+ | {
274
+ readonly type: 'asset';
275
+ value: Asset;
276
+ }
277
+ | null
278
+ | undefined
279
+ | {
280
+ readonly type: 'dependency';
281
+ value: Dependency;
282
+ },
283
+ // @ts-expect-error TS2304
284
+ actions: TraversalActions,
285
+ ) {
265
286
  if (node.type === 'asset') {
266
287
  if (
267
288
  context?.type === 'dependency' &&
@@ -388,6 +409,7 @@ export function createIdealGraph(
388
409
  type: 'bundle',
389
410
  },
390
411
  ),
412
+ // @ts-expect-error TS7053
391
413
  dependencyPriorityEdges[dependency.priority],
392
414
  );
393
415
 
@@ -499,6 +521,7 @@ export function createIdealGraph(
499
521
 
500
522
  assetReference.get(childAsset).push([dependency, bundle]);
501
523
  } else {
524
+ // @ts-expect-error TS2322
502
525
  bundleId = null;
503
526
  }
504
527
  if (manualSharedObject && bundleId != null) {
@@ -506,6 +529,7 @@ export function createIdealGraph(
506
529
  // add the asset if it doesn't already have it and set key
507
530
 
508
531
  invariant(
532
+ // @ts-expect-error TS2367
509
533
  bundle !== 'root' && bundle != null && bundleId != null,
510
534
  );
511
535
 
@@ -532,7 +556,8 @@ export function createIdealGraph(
532
556
  }
533
557
  return node;
534
558
  },
535
- exit(node) {
559
+ // @ts-expect-error TS2322
560
+ exit(node: BundleGraphTraversable) {
536
561
  if (stack[stack.length - 1]?.[0] === node.value) {
537
562
  stack.pop();
538
563
  }
@@ -581,20 +606,20 @@ export function createIdealGraph(
581
606
 
582
607
  // reachableRoots is an array of bit sets for each asset. Each bit set
583
608
  // indicates which bundle roots are reachable from that asset synchronously.
584
- let reachableRoots = [];
609
+ let reachableRoots: Array<BitSet> = [];
585
610
  for (let i = 0; i < assets.length; i++) {
586
611
  reachableRoots.push(new BitSet(bundleRootGraph.nodes.length));
587
612
  }
588
613
 
589
614
  // reachableAssets is the inverse mapping of reachableRoots. For each bundle root,
590
615
  // it contains a bit set that indicates which assets are reachable from it.
591
- let reachableAssets = [];
616
+ let reachableAssets: Array<BitSet> = [];
592
617
 
593
618
  // ancestorAssets maps bundle roots to the set of all assets available to it at runtime,
594
619
  // including in earlier parallel bundles. These are intersected through all paths to
595
620
  // the bundle to ensure that the available assets are always present no matter in which
596
621
  // order the bundles are loaded.
597
- let ancestorAssets = [];
622
+ let ancestorAssets: Array<null | BitSet> = [];
598
623
 
599
624
  let inlineConstantDeps = new DefaultMap(() => new Set());
600
625
 
@@ -823,8 +848,11 @@ export function createIdealGraph(
823
848
 
824
849
  function assignInlineConstants(parentAsset: Asset, bundle: Bundle) {
825
850
  for (let inlineConstant of inlineConstantDeps.get(parentAsset)) {
851
+ // @ts-expect-error TS2345
826
852
  if (!bundle.assets.has(inlineConstant)) {
853
+ // @ts-expect-error TS2345
827
854
  bundle.assets.add(inlineConstant);
855
+ // @ts-expect-error TS18046
828
856
  bundle.size += inlineConstant.stats.size;
829
857
  }
830
858
  }
@@ -884,7 +912,7 @@ export function createIdealGraph(
884
912
  let bundle;
885
913
  let bundleId;
886
914
  let manualSharedBundleKey = manualSharedObject.name + ',' + asset.type;
887
- let sourceBundles = [];
915
+ let sourceBundles: Array<NodeId> = [];
888
916
  reachable.forEach((id) => {
889
917
  sourceBundles.push(nullthrows(bundleRoots.get(assets[id]))[0]);
890
918
  });
@@ -999,7 +1027,7 @@ export function createIdealGraph(
999
1027
  });
1000
1028
  }
1001
1029
 
1002
- let reachableArray = [];
1030
+ let reachableArray: Array<Asset> = [];
1003
1031
  reachable.forEach((id) => {
1004
1032
  reachableArray.push(assets[id]);
1005
1033
  });
@@ -1092,9 +1120,9 @@ export function createIdealGraph(
1092
1120
  if (modNum != null) {
1093
1121
  for (let a of [...manualBundle.assets]) {
1094
1122
  let numRep = getBigIntFromContentKey(a.id);
1095
- // $FlowFixMe Flow doesn't know about BigInt
1096
1123
  let r = Number(numRep % BigInt(modNum));
1097
1124
 
1125
+ // @ts-expect-error TS2345
1098
1126
  remainderMap.get(r).push(a);
1099
1127
  }
1100
1128
 
@@ -1117,8 +1145,10 @@ export function createIdealGraph(
1117
1145
  }
1118
1146
  for (let sp of remainderMap.get(i)) {
1119
1147
  bundle.assets.add(sp);
1148
+ // @ts-expect-error TS2339
1120
1149
  bundle.size += sp.stats.size;
1121
1150
  manualBundle.assets.delete(sp);
1151
+ // @ts-expect-error TS2339
1122
1152
  manualBundle.size -= sp.stats.size;
1123
1153
  }
1124
1154
  }
@@ -1131,6 +1161,7 @@ export function createIdealGraph(
1131
1161
  // match multiple MSB's
1132
1162
  for (let [asset, msbs] of constantModuleToMSB.entries()) {
1133
1163
  for (let manualSharedObject of msbs) {
1164
+ // @ts-expect-error TS2339
1134
1165
  let bundleId = manualSharedMap.get(manualSharedObject.name + ',js');
1135
1166
  if (bundleId == null) continue;
1136
1167
  let bundle = nullthrows(bundleGraph.getNode(bundleId));
@@ -1139,8 +1170,11 @@ export function createIdealGraph(
1139
1170
  'We tried to use the root incorrectly',
1140
1171
  );
1141
1172
 
1173
+ // @ts-expect-error TS2345
1142
1174
  if (!bundle.assets.has(asset)) {
1175
+ // @ts-expect-error TS2345
1143
1176
  bundle.assets.add(asset);
1177
+ // @ts-expect-error TS18046
1144
1178
  bundle.size += asset.stats.size;
1145
1179
  }
1146
1180
  }
@@ -1182,6 +1216,7 @@ export function createIdealGraph(
1182
1216
  let numBundlesContributingToPRL = bundleIdsInGroup.reduce((count, b) => {
1183
1217
  let bundle = nullthrows(bundleGraph.getNode(b));
1184
1218
  invariant(bundle !== 'root');
1219
+ // @ts-expect-error TS2365
1185
1220
  return count + (bundle.bundleBehavior !== 'inline');
1186
1221
  }, 0);
1187
1222
 
@@ -1217,7 +1252,9 @@ export function createIdealGraph(
1217
1252
  numBundlesContributingToPRL > config.maxParallelRequests
1218
1253
  ) {
1219
1254
  let bundleTuple = sharedBundlesInGroup.pop();
1255
+ // @ts-expect-error TS18048
1220
1256
  let bundleToRemove = bundleTuple.bundle;
1257
+ // @ts-expect-error TS18048
1221
1258
  let bundleIdToRemove = bundleTuple.id;
1222
1259
  //TODO add integration test where bundles in bunlde group > max parallel request limit & only remove a couple shared bundles
1223
1260
  // but total # bundles still exceeds limit due to non shared bundles
@@ -1288,10 +1325,11 @@ export function createIdealGraph(
1288
1325
 
1289
1326
  let newAssetReference = assetReference
1290
1327
  .get(asset)
1291
- .map(([dep, bundle]) =>
1328
+ .map(([dep, bundle]: [any, any]) =>
1292
1329
  bundle === bundleToRemove ? [dep, bundleToKeep] : [dep, bundle],
1293
1330
  );
1294
1331
 
1332
+ // @ts-expect-error TS2345
1295
1333
  assetReference.set(asset, newAssetReference);
1296
1334
  }
1297
1335
 
@@ -1344,20 +1382,22 @@ export function createIdealGraph(
1344
1382
  let clusters = findMergeCandidates(
1345
1383
  bundleGraph,
1346
1384
  Array.from(sharedBundles),
1347
- mergeConfig.map((config): MergeGroup => ({
1348
- ...config,
1349
- sourceBundles: config.sourceBundles?.map((assetMatch) => {
1350
- let sourceBundleNodeId = mergeSourceBundleLookup.get(assetMatch);
1351
-
1352
- if (sourceBundleNodeId == null) {
1353
- throw new Error(
1354
- `Source bundle ${assetMatch} not found in merge source bundle lookup`,
1355
- );
1356
- }
1385
+ mergeConfig.map(
1386
+ (config): MergeGroup => ({
1387
+ ...config,
1388
+ sourceBundles: config.sourceBundles?.map((assetMatch: string) => {
1389
+ let sourceBundleNodeId = mergeSourceBundleLookup.get(assetMatch);
1390
+
1391
+ if (sourceBundleNodeId == null) {
1392
+ throw new Error(
1393
+ `Source bundle ${assetMatch} not found in merge source bundle lookup`,
1394
+ );
1395
+ }
1357
1396
 
1358
- return sourceBundleNodeId;
1397
+ return sourceBundleNodeId;
1398
+ }),
1359
1399
  }),
1360
- })),
1400
+ ),
1361
1401
  );
1362
1402
 
1363
1403
  for (let cluster of clusters) {
@@ -1369,10 +1409,9 @@ export function createIdealGraph(
1369
1409
  }
1370
1410
  }
1371
1411
 
1372
- function getBigIntFromContentKey(contentKey) {
1412
+ function getBigIntFromContentKey(contentKey: string) {
1373
1413
  let b = Buffer.alloc(64);
1374
1414
  b.write(contentKey);
1375
- // $FlowFixMe Flow doesn't have BigInt types in this version
1376
1415
  return b.readBigInt64BE();
1377
1416
  }
1378
1417
  // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
@@ -1380,7 +1419,9 @@ export function createIdealGraph(
1380
1419
  let assetOrderMap = new Map(assets.map((a, index) => [a, index]));
1381
1420
 
1382
1421
  for (let bundle of modifiedSourceBundles) {
1422
+ // @ts-expect-error TS18046
1383
1423
  bundle.assets = new Set(
1424
+ // @ts-expect-error TS18046
1384
1425
  [...bundle.assets].sort((a, b) => {
1385
1426
  let aIndex = nullthrows(assetOrderMap.get(a));
1386
1427
  let bIndex = nullthrows(assetOrderMap.get(b));
@@ -1399,8 +1440,8 @@ export function createIdealGraph(
1399
1440
  bundleRootGraph.removeNode(bundleRootId);
1400
1441
  }
1401
1442
  }
1402
- function getBundlesForBundleGroup(bundleGroupId) {
1403
- let bundlesInABundleGroup = [];
1443
+ function getBundlesForBundleGroup(bundleGroupId: NodeId) {
1444
+ let bundlesInABundleGroup: Array<NodeId> = [];
1404
1445
  bundleGraph.traverse((nodeId) => {
1405
1446
  bundlesInABundleGroup.push(nodeId);
1406
1447
  }, bundleGroupId);
@@ -1481,17 +1522,17 @@ export function createIdealGraph(
1481
1522
  };
1482
1523
  }
1483
1524
 
1484
- function createBundle(opts: {|
1485
- asset?: Asset,
1486
- bundleBehavior?: ?BundleBehavior,
1487
- env?: Environment,
1488
- manualSharedBundle?: ?string,
1489
- needsStableName?: boolean,
1490
- sourceBundles?: Set<NodeId>,
1491
- target: Target,
1492
- type?: string,
1493
- uniqueKey?: string,
1494
- |}): Bundle {
1525
+ function createBundle(opts: {
1526
+ asset?: Asset;
1527
+ bundleBehavior?: BundleBehavior | null | undefined;
1528
+ env?: Environment;
1529
+ manualSharedBundle?: string | null | undefined;
1530
+ needsStableName?: boolean;
1531
+ sourceBundles?: Set<NodeId>;
1532
+ target: Target;
1533
+ type?: string;
1534
+ uniqueKey?: string;
1535
+ }): Bundle {
1495
1536
  if (opts.asset == null) {
1496
1537
  return {
1497
1538
  assets: new Set(),
@@ -1,9 +1,6 @@
1
- // @flow strict
2
-
3
- // $FlowFixMe
4
1
  import ManyKeysMap from 'many-keys-map';
5
2
 
6
- let caches = [];
3
+ let caches: Array<any> = [];
7
4
 
8
5
  export function clearCaches() {
9
6
  for (let cache of caches) {
@@ -11,7 +8,7 @@ export function clearCaches() {
11
8
  }
12
9
  }
13
10
 
14
- export function memoize<Args: Array<mixed>, Return>(
11
+ export function memoize<Args extends Array<unknown>, Return>(
15
12
  fn: (...args: Args) => Return,
16
13
  ): (...args: Args) => Return {
17
14
  let cache = new ManyKeysMap();
@@ -26,8 +23,8 @@ export function memoize<Args: Array<mixed>, Return>(
26
23
  }
27
24
 
28
25
  // Calculate the result and cache it
26
+ // @ts-expect-error TS2683
29
27
  const result = fn.apply(this, args);
30
- // $FlowFixMe
31
28
  cache.set(args, result);
32
29
 
33
30
  return result;
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "include": ["src"]
4
+ }