@atlaspack/bundler-default 3.2.0 → 3.2.1-typescript-17c3d1dec.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,38 +21,38 @@ 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
- bundleRoots: Set<Asset>,
40
- size: number,
41
- sourceBundles: Set<NodeId>,
42
- target: Target,
43
- env: Environment,
44
- type: string,
45
- manualSharedBundle: ?string, // for naming purposes
46
- |};
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
+ bundleRoots: Set<Asset>;
38
+ size: number;
39
+ sourceBundles: Set<NodeId>;
40
+ target: Target;
41
+ env: Environment;
42
+ type: string;
43
+ manualSharedBundle: string | null | undefined; // for naming purposes;
44
+ };
47
45
 
48
46
  export type DependencyBundleGraph = ContentGraph<
49
- | {|
50
- value: Bundle,
51
- type: 'bundle',
52
- |}
53
- | {|
54
- value: Dependency,
55
- type: 'dependency',
56
- |},
57
- number,
47
+ | {
48
+ value: Bundle;
49
+ type: 'bundle';
50
+ }
51
+ | {
52
+ value: Dependency;
53
+ type: 'dependency';
54
+ },
55
+ number
58
56
  >;
59
57
 
60
58
  const dependencyPriorityEdges = {
@@ -62,7 +60,7 @@ const dependencyPriorityEdges = {
62
60
  parallel: 2,
63
61
  lazy: 3,
64
62
  conditional: 4,
65
- };
63
+ } as const;
66
64
 
67
65
  export const idealBundleGraphEdges = Object.freeze({
68
66
  default: 1,
@@ -71,20 +69,20 @@ export const idealBundleGraphEdges = Object.freeze({
71
69
 
72
70
  export type IdealBundleGraph = Graph<
73
71
  Bundle | 'root',
74
- $Values<typeof idealBundleGraphEdges>,
72
+ (typeof idealBundleGraphEdges)[keyof typeof idealBundleGraphEdges]
75
73
  >;
76
74
 
77
75
  // IdealGraph is the structure we will pass to decorate,
78
76
  // which mutates the assetGraph into the bundleGraph we would
79
77
  // expect from default bundler
80
- export type IdealGraph = {|
81
- assetReference: DefaultMap<Asset, Array<[Dependency, Bundle]>>,
82
- assets: Array<Asset>,
83
- bundleGraph: IdealBundleGraph,
84
- bundleGroupBundleIds: Set<NodeId>,
85
- dependencyBundleGraph: DependencyBundleGraph,
86
- manualAssetToBundle: Map<Asset, NodeId>,
87
- |};
78
+ export type IdealGraph = {
79
+ assetReference: DefaultMap<Asset, Array<[Dependency, Bundle]>>;
80
+ assets: Array<Asset>;
81
+ bundleGraph: IdealBundleGraph;
82
+ bundleGroupBundleIds: Set<NodeId>;
83
+ dependencyBundleGraph: DependencyBundleGraph;
84
+ manualAssetToBundle: Map<Asset, NodeId>;
85
+ };
88
86
 
89
87
  function isNonRootBundle(
90
88
  bundle?: Bundle | 'root' | null,
@@ -108,7 +106,7 @@ export function createIdealGraph(
108
106
  let dependencyBundleGraph: DependencyBundleGraph = new ContentGraph();
109
107
  let assetReference: DefaultMap<
110
108
  Asset,
111
- Array<[Dependency, Bundle]>,
109
+ Array<[Dependency, Bundle]>
112
110
  > = new DefaultMap(() => []);
113
111
 
114
112
  // A Graph of Bundles and a root node (dummy string), which models only Bundles, and connections to their
@@ -122,8 +120,9 @@ export function createIdealGraph(
122
120
  };
123
121
  // Graph that models bundleRoots, with parallel & async deps only to inform reachability
124
122
  let bundleRootGraph: Graph<
125
- number, // asset index
126
- $Values<typeof bundleRootEdgeTypes>,
123
+ // asset index
124
+ number,
125
+ (typeof bundleRootEdgeTypes)[keyof typeof bundleRootEdgeTypes]
127
126
  > = new Graph();
128
127
  let assetToBundleRootNodeId = new Map<BundleRoot, number>();
129
128
 
@@ -157,7 +156,7 @@ export function createIdealGraph(
157
156
  bundleGroupBundleIds.add(nodeId);
158
157
  }
159
158
 
160
- let assets = [];
159
+ let assets: Array<Asset> = [];
161
160
  let assetToIndex = new Map<Asset, number>();
162
161
 
163
162
  function makeManualAssetToConfigLookup() {
@@ -172,6 +171,7 @@ export function createIdealGraph(
172
171
 
173
172
  for (let c of config.manualSharedBundles) {
174
173
  if (c.root != null) {
174
+ // @ts-expect-error TS2345
175
175
  parentsToConfig.get(path.join(config.projectRoot, c.root)).push(c);
176
176
  }
177
177
  }
@@ -219,6 +219,7 @@ export function createIdealGraph(
219
219
  // We track all matching MSB's for constant modules as they are never duplicated
220
220
  // and need to be assigned to all matching bundles
221
221
  if (node.value.meta.isConstantModule === true) {
222
+ // @ts-expect-error TS2345
222
223
  constantModuleToMSB.get(node.value).push(c);
223
224
  }
224
225
 
@@ -252,14 +253,14 @@ export function createIdealGraph(
252
253
  makeManualAssetToConfigLookup();
253
254
  let manualBundleToInternalizedAsset: DefaultMap<
254
255
  NodeId,
255
- Array<Asset>,
256
+ Array<Asset>
256
257
  > = new DefaultMap(() => []);
257
258
 
258
259
  let mergeSourceBundleLookup = new Map<string, NodeId>();
259
260
  let mergeSourceBundleAssets = new Set(
260
261
  config.sharedBundleMerge?.flatMap(
261
262
  (c) =>
262
- c.sourceBundles?.map((assetMatch) =>
263
+ c.sourceBundles?.map((assetMatch: string) =>
263
264
  path.join(config.projectRoot, assetMatch),
264
265
  ) ?? [],
265
266
  ),
@@ -272,7 +273,27 @@ export function createIdealGraph(
272
273
  */
273
274
  assetGraph.traverse(
274
275
  {
275
- enter(node, context, actions) {
276
+ enter(
277
+ node: // @ts-expect-error TS2304
278
+ | BundleGraphTraversable
279
+ | {
280
+ readonly type: 'dependency';
281
+ value: Dependency;
282
+ },
283
+ context:
284
+ | {
285
+ readonly type: 'asset';
286
+ value: Asset;
287
+ }
288
+ | null
289
+ | undefined
290
+ | {
291
+ readonly type: 'dependency';
292
+ value: Dependency;
293
+ },
294
+ // @ts-expect-error TS2304
295
+ actions: TraversalActions,
296
+ ) {
276
297
  if (node.type === 'asset') {
277
298
  if (
278
299
  context?.type === 'dependency' &&
@@ -399,6 +420,7 @@ export function createIdealGraph(
399
420
  type: 'bundle',
400
421
  },
401
422
  ),
423
+ // @ts-expect-error TS7053
402
424
  dependencyPriorityEdges[dependency.priority],
403
425
  );
404
426
 
@@ -510,6 +532,7 @@ export function createIdealGraph(
510
532
 
511
533
  assetReference.get(childAsset).push([dependency, bundle]);
512
534
  } else {
535
+ // @ts-expect-error TS2322
513
536
  bundleId = null;
514
537
  }
515
538
  if (manualSharedObject && bundleId != null) {
@@ -517,6 +540,7 @@ export function createIdealGraph(
517
540
  // add the asset if it doesn't already have it and set key
518
541
 
519
542
  invariant(
543
+ // @ts-expect-error TS2367
520
544
  bundle !== 'root' && bundle != null && bundleId != null,
521
545
  );
522
546
 
@@ -543,7 +567,8 @@ export function createIdealGraph(
543
567
  }
544
568
  return node;
545
569
  },
546
- exit(node) {
570
+ // @ts-expect-error TS2322
571
+ exit(node: BundleGraphTraversable) {
547
572
  if (stack[stack.length - 1]?.[0] === node.value) {
548
573
  stack.pop();
549
574
  }
@@ -592,20 +617,20 @@ export function createIdealGraph(
592
617
 
593
618
  // reachableRoots is an array of bit sets for each asset. Each bit set
594
619
  // indicates which bundle roots are reachable from that asset synchronously.
595
- let reachableRoots = [];
620
+ let reachableRoots: Array<BitSet> = [];
596
621
  for (let i = 0; i < assets.length; i++) {
597
622
  reachableRoots.push(new BitSet(bundleRootGraph.nodes.length));
598
623
  }
599
624
 
600
625
  // reachableAssets is the inverse mapping of reachableRoots. For each bundle root,
601
626
  // it contains a bit set that indicates which assets are reachable from it.
602
- let reachableAssets = [];
627
+ let reachableAssets: Array<BitSet> = [];
603
628
 
604
629
  // ancestorAssets maps bundle roots to the set of all assets available to it at runtime,
605
630
  // including in earlier parallel bundles. These are intersected through all paths to
606
631
  // the bundle to ensure that the available assets are always present no matter in which
607
632
  // order the bundles are loaded.
608
- let ancestorAssets = [];
633
+ let ancestorAssets: Array<null | BitSet> = [];
609
634
 
610
635
  let inlineConstantDeps = new DefaultMap(() => new Set());
611
636
 
@@ -834,8 +859,11 @@ export function createIdealGraph(
834
859
 
835
860
  function assignInlineConstants(parentAsset: Asset, bundle: Bundle) {
836
861
  for (let inlineConstant of inlineConstantDeps.get(parentAsset)) {
862
+ // @ts-expect-error TS2345
837
863
  if (!bundle.assets.has(inlineConstant)) {
864
+ // @ts-expect-error TS2345
838
865
  bundle.assets.add(inlineConstant);
866
+ // @ts-expect-error TS18046
839
867
  bundle.size += inlineConstant.stats.size;
840
868
  }
841
869
  }
@@ -895,7 +923,7 @@ export function createIdealGraph(
895
923
  let bundle;
896
924
  let bundleId;
897
925
  let manualSharedBundleKey = manualSharedObject.name + ',' + asset.type;
898
- let sourceBundles = [];
926
+ let sourceBundles: Array<NodeId> = [];
899
927
  reachable.forEach((id) => {
900
928
  sourceBundles.push(nullthrows(bundleRoots.get(assets[id]))[0]);
901
929
  });
@@ -1010,7 +1038,7 @@ export function createIdealGraph(
1010
1038
  });
1011
1039
  }
1012
1040
 
1013
- let reachableArray = [];
1041
+ let reachableArray: Array<Asset> = [];
1014
1042
  reachable.forEach((id) => {
1015
1043
  reachableArray.push(assets[id]);
1016
1044
  });
@@ -1105,9 +1133,9 @@ export function createIdealGraph(
1105
1133
  if (modNum != null) {
1106
1134
  for (let a of [...manualBundle.assets]) {
1107
1135
  let numRep = getBigIntFromContentKey(a.id);
1108
- // $FlowFixMe Flow doesn't know about BigInt
1109
1136
  let r = Number(numRep % BigInt(modNum));
1110
1137
 
1138
+ // @ts-expect-error TS2345
1111
1139
  remainderMap.get(r).push(a);
1112
1140
  }
1113
1141
 
@@ -1130,8 +1158,10 @@ export function createIdealGraph(
1130
1158
  }
1131
1159
  for (let sp of remainderMap.get(i)) {
1132
1160
  bundle.assets.add(sp);
1161
+ // @ts-expect-error TS2339
1133
1162
  bundle.size += sp.stats.size;
1134
1163
  manualBundle.assets.delete(sp);
1164
+ // @ts-expect-error TS2339
1135
1165
  manualBundle.size -= sp.stats.size;
1136
1166
  }
1137
1167
  }
@@ -1144,6 +1174,7 @@ export function createIdealGraph(
1144
1174
  // match multiple MSB's
1145
1175
  for (let [asset, msbs] of constantModuleToMSB.entries()) {
1146
1176
  for (let manualSharedObject of msbs) {
1177
+ // @ts-expect-error TS2339
1147
1178
  let bundleId = manualSharedMap.get(manualSharedObject.name + ',js');
1148
1179
  if (bundleId == null) continue;
1149
1180
  let bundle = nullthrows(bundleGraph.getNode(bundleId));
@@ -1152,8 +1183,11 @@ export function createIdealGraph(
1152
1183
  'We tried to use the root incorrectly',
1153
1184
  );
1154
1185
 
1186
+ // @ts-expect-error TS2345
1155
1187
  if (!bundle.assets.has(asset)) {
1188
+ // @ts-expect-error TS2345
1156
1189
  bundle.assets.add(asset);
1190
+ // @ts-expect-error TS18046
1157
1191
  bundle.size += asset.stats.size;
1158
1192
  }
1159
1193
  }
@@ -1227,6 +1261,7 @@ export function createIdealGraph(
1227
1261
  // Merge all bundles with the same chunk name into the first one.
1228
1262
  let [firstBundleId, ...rest] = Array.from(bundleIds);
1229
1263
  for (let bundleId of rest) {
1264
+ // @ts-expect-error TS2345
1230
1265
  mergeBundles(firstBundleId, bundleId);
1231
1266
  }
1232
1267
  }
@@ -1267,6 +1302,7 @@ export function createIdealGraph(
1267
1302
  let numBundlesContributingToPRL = bundleIdsInGroup.reduce((count, b) => {
1268
1303
  let bundle = nullthrows(bundleGraph.getNode(b));
1269
1304
  invariant(bundle !== 'root');
1305
+ // @ts-expect-error TS2365
1270
1306
  return count + (bundle.bundleBehavior !== 'inline');
1271
1307
  }, 0);
1272
1308
 
@@ -1302,7 +1338,9 @@ export function createIdealGraph(
1302
1338
  numBundlesContributingToPRL > config.maxParallelRequests
1303
1339
  ) {
1304
1340
  let bundleTuple = sharedBundlesInGroup.pop();
1341
+ // @ts-expect-error TS18048
1305
1342
  let bundleToRemove = bundleTuple.bundle;
1343
+ // @ts-expect-error TS18048
1306
1344
  let bundleIdToRemove = bundleTuple.id;
1307
1345
  //TODO add integration test where bundles in bunlde group > max parallel request limit & only remove a couple shared bundles
1308
1346
  // but total # bundles still exceeds limit due to non shared bundles
@@ -1374,10 +1412,11 @@ export function createIdealGraph(
1374
1412
 
1375
1413
  let newAssetReference = assetReference
1376
1414
  .get(asset)
1377
- .map(([dep, bundle]) =>
1415
+ .map(([dep, bundle]: [any, any]) =>
1378
1416
  bundle === bundleToRemove ? [dep, bundleToKeep] : [dep, bundle],
1379
1417
  );
1380
1418
 
1419
+ // @ts-expect-error TS2345
1381
1420
  assetReference.set(asset, newAssetReference);
1382
1421
  }
1383
1422
 
@@ -1541,20 +1580,22 @@ export function createIdealGraph(
1541
1580
  let clusters = findMergeCandidates(
1542
1581
  bundleGraph,
1543
1582
  Array.from(sharedBundles),
1544
- mergeConfig.map((config): MergeGroup => ({
1545
- ...config,
1546
- sourceBundles: config.sourceBundles?.map((assetMatch) => {
1547
- let sourceBundleNodeId = mergeSourceBundleLookup.get(assetMatch);
1548
-
1549
- if (sourceBundleNodeId == null) {
1550
- throw new Error(
1551
- `Source bundle ${assetMatch} not found in merge source bundle lookup`,
1552
- );
1553
- }
1583
+ mergeConfig.map(
1584
+ (config): MergeGroup => ({
1585
+ ...config,
1586
+ sourceBundles: config.sourceBundles?.map((assetMatch: string) => {
1587
+ let sourceBundleNodeId = mergeSourceBundleLookup.get(assetMatch);
1588
+
1589
+ if (sourceBundleNodeId == null) {
1590
+ throw new Error(
1591
+ `Source bundle ${assetMatch} not found in merge source bundle lookup`,
1592
+ );
1593
+ }
1554
1594
 
1555
- return sourceBundleNodeId;
1595
+ return sourceBundleNodeId;
1596
+ }),
1556
1597
  }),
1557
- })),
1598
+ ),
1558
1599
  );
1559
1600
 
1560
1601
  let mergedBundles = new Set();
@@ -1574,10 +1615,9 @@ export function createIdealGraph(
1574
1615
  }
1575
1616
  }
1576
1617
 
1577
- function getBigIntFromContentKey(contentKey) {
1618
+ function getBigIntFromContentKey(contentKey: string) {
1578
1619
  let b = Buffer.alloc(64);
1579
1620
  b.write(contentKey);
1580
- // $FlowFixMe Flow doesn't have BigInt types in this version
1581
1621
  return b.readBigInt64BE();
1582
1622
  }
1583
1623
  // Fix asset order in source bundles as they are likely now incorrect after shared bundle deletion
@@ -1604,8 +1644,8 @@ export function createIdealGraph(
1604
1644
  bundleRootGraph.removeNode(bundleRootId);
1605
1645
  }
1606
1646
  }
1607
- function getBundlesForBundleGroup(bundleGroupId) {
1608
- let bundlesInABundleGroup = [];
1647
+ function getBundlesForBundleGroup(bundleGroupId: NodeId) {
1648
+ let bundlesInABundleGroup: Array<NodeId> = [];
1609
1649
  bundleGraph.traverse((nodeId) => {
1610
1650
  bundlesInABundleGroup.push(nodeId);
1611
1651
  }, bundleGroupId);
@@ -1686,17 +1726,17 @@ export function createIdealGraph(
1686
1726
  };
1687
1727
  }
1688
1728
 
1689
- function createBundle(opts: {|
1690
- asset?: Asset,
1691
- bundleBehavior?: ?BundleBehavior,
1692
- env?: Environment,
1693
- manualSharedBundle?: ?string,
1694
- needsStableName?: boolean,
1695
- sourceBundles?: Set<NodeId>,
1696
- target: Target,
1697
- type?: string,
1698
- uniqueKey?: string,
1699
- |}): Bundle {
1729
+ function createBundle(opts: {
1730
+ asset?: Asset;
1731
+ bundleBehavior?: BundleBehavior | null | undefined;
1732
+ env?: Environment;
1733
+ manualSharedBundle?: string | null | undefined;
1734
+ needsStableName?: boolean;
1735
+ sourceBundles?: Set<NodeId>;
1736
+ target: Target;
1737
+ type?: string;
1738
+ uniqueKey?: string;
1739
+ }): Bundle {
1700
1740
  if (opts.asset == null) {
1701
1741
  return {
1702
1742
  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
+ }