@atlaspack/bundler-default 3.1.3-typescript-5b4d3ad41.0 → 3.2.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,3 +1,5 @@
1
+ // @flow strict-local
2
+
1
3
  import {Bundler} from '@atlaspack/plugin';
2
4
  import type {Asset, Dependency, MutableBundleGraph} from '@atlaspack/types';
3
5
  import {DefaultMap} from '@atlaspack/utils';
@@ -23,15 +25,14 @@ import {addJSMonolithBundle} from './MonolithicBundler';
23
25
  * will have two or more distDirs, or output folders.) Then calls create IdealGraph and Decorate per target.
24
26
  *
25
27
  */
26
- export default new Bundler({
28
+ export default (new Bundler({
27
29
  loadConfig({config, options, logger}) {
28
30
  return loadBundlerConfig(config, options, logger);
29
31
  },
30
32
 
31
33
  bundle({bundleGraph, config, logger}) {
32
34
  let targetMap = getEntryByTarget(bundleGraph); // Organize entries by target output folder/ distDir
33
- // @ts-expect-error TS2304
34
- let graphs: Array<IdealGraph> = [];
35
+ let graphs = [];
35
36
 
36
37
  for (let entries of targetMap.values()) {
37
38
  let singleFileEntries = new Map();
@@ -48,6 +49,7 @@ export default new Bundler({
48
49
 
49
50
  // Create separate bundleGraphs per distDir
50
51
  graphs.push(
52
+ // $FlowFixMe
51
53
  createIdealGraph(bundleGraph, config, idealGraphEntries, logger),
52
54
  );
53
55
 
@@ -63,7 +65,7 @@ export default new Bundler({
63
65
  }
64
66
  },
65
67
  optimize() {},
66
- }) as Bundler<unknown>;
68
+ }): Bundler<mixed>);
67
69
 
68
70
  function getEntryByTarget(
69
71
  bundleGraph: MutableBundleGraph,
@@ -73,23 +75,7 @@ function getEntryByTarget(
73
75
  () => new Map(),
74
76
  );
75
77
  bundleGraph.traverse({
76
- enter(
77
- // @ts-expect-error TS2304
78
- node: BundleGraphTraversable,
79
- context:
80
- | {
81
- readonly type: 'asset';
82
- value: Asset;
83
- }
84
- | null
85
- | undefined
86
- | {
87
- readonly type: 'dependency';
88
- value: Dependency;
89
- },
90
- // @ts-expect-error TS2304
91
- actions: TraversalActions,
92
- ) {
78
+ enter(node, context, actions) {
93
79
  if (node.type !== 'asset') {
94
80
  return node;
95
81
  }
@@ -1,3 +1,4 @@
1
+ // @flow strict-local
1
2
  import type {Asset, Dependency, MutableBundleGraph} from '@atlaspack/types';
2
3
  import nullthrows from 'nullthrows';
3
4
 
@@ -1,3 +1,5 @@
1
+ // @flow strict-local
2
+
1
3
  import invariant from 'assert';
2
4
  import nullthrows from 'nullthrows';
3
5
  import {ContentGraph} from '@atlaspack/graph';
@@ -12,7 +14,6 @@ function getBundlesForBundleGroup(
12
14
  ): number {
13
15
  let count = 0;
14
16
  bundleGraph.traverse((nodeId) => {
15
- // @ts-expect-error TS2339
16
17
  if (bundleGraph.getNode(nodeId)?.bundleBehavior !== 'inline') {
17
18
  count++;
18
19
  }
@@ -124,7 +125,7 @@ function getMergeClusters(
124
125
  graph: ContentGraph<NodeId, EdgeType>,
125
126
  candidates: Map<NodeId, EdgeType>,
126
127
  ): Array<Array<NodeId>> {
127
- let clusters: Array<Array<NodeId>> = [];
128
+ let clusters = [];
128
129
 
129
130
  for (let [candidate, edgeType] of candidates.entries()) {
130
131
  let cluster: Array<NodeId> = [];
@@ -144,11 +145,11 @@ function getMergeClusters(
144
145
  return clusters;
145
146
  }
146
147
 
147
- type MergeCandidate = {
148
- bundle: Bundle;
149
- id: NodeId;
150
- contentKey: string;
151
- };
148
+ type MergeCandidate = {|
149
+ bundle: Bundle,
150
+ id: NodeId,
151
+ contentKey: string,
152
+ |};
152
153
  function getPossibleMergeCandidates(
153
154
  bundleGraph: IdealBundleGraph,
154
155
  bundles: Array<NodeId>,
@@ -164,15 +165,17 @@ function getPossibleMergeCandidates(
164
165
  };
165
166
  });
166
167
 
167
- const uniquePairs: Array<[MergeCandidate, MergeCandidate]> = [];
168
+ const uniquePairs = [];
168
169
 
169
170
  for (let i = 0; i < mergeCandidates.length; i++) {
170
171
  for (let j = i + 1; j < mergeCandidates.length; j++) {
171
172
  let a = mergeCandidates[i];
172
173
  let b = mergeCandidates[j];
173
174
 
174
- // @ts-expect-error TS18048
175
- if (a.bundle.internalizedAssets.equals(b.bundle.internalizedAssets)) {
175
+ if (
176
+ // $FlowFixMe both bundles will always have internalizedAssets
177
+ a.bundle.internalizedAssets.equals(b.bundle.internalizedAssets)
178
+ ) {
176
179
  uniquePairs.push([a, b]);
177
180
  }
178
181
  }
@@ -180,12 +183,12 @@ function getPossibleMergeCandidates(
180
183
  return uniquePairs;
181
184
  }
182
185
 
183
- export type MergeGroup = {
184
- overlapThreshold?: number;
185
- maxBundleSize?: number;
186
- sourceBundles?: Array<NodeId>;
187
- minBundlesInGroup?: number;
188
- };
186
+ export type MergeGroup = {|
187
+ overlapThreshold?: number,
188
+ maxBundleSize?: number,
189
+ sourceBundles?: Array<NodeId>,
190
+ minBundlesInGroup?: number,
191
+ |};
189
192
  type EdgeType = number;
190
193
 
191
194
  export function findMergeCandidates(
@@ -1,3 +1,5 @@
1
+ // @flow strict-local
2
+
1
3
  import {encodeJSONKeyComponent} from '@atlaspack/diagnostic';
2
4
  import type {
3
5
  Config,
@@ -6,62 +8,62 @@ import type {
6
8
  PluginLogger,
7
9
  } from '@atlaspack/types';
8
10
  import {getFeatureFlag} from '@atlaspack/feature-flags';
9
- import {SchemaEntity, validateSchema} from '@atlaspack/utils';
11
+ import {type SchemaEntity, validateSchema} from '@atlaspack/utils';
10
12
  import invariant from 'assert';
11
13
 
12
14
  type Glob = string;
13
15
 
14
- type ManualSharedBundles = Array<{
15
- name: string;
16
- assets: Array<Glob>;
17
- types?: Array<string>;
18
- root?: string;
19
- split?: number;
20
- }>;
16
+ type ManualSharedBundles = Array<{|
17
+ name: string,
18
+ assets: Array<Glob>,
19
+ types?: Array<string>,
20
+ root?: string,
21
+ split?: number,
22
+ |}>;
21
23
 
22
- export type MergeCandidates = Array<{
23
- overlapThreshold?: number;
24
- maxBundleSize?: number;
25
- sourceBundles?: Array<string>;
26
- minBundlesInGroup?: number;
27
- }>;
24
+ export type MergeCandidates = Array<{|
25
+ overlapThreshold?: number,
26
+ maxBundleSize?: number,
27
+ sourceBundles?: Array<string>,
28
+ minBundlesInGroup?: number,
29
+ |}>;
28
30
 
29
- type BaseBundlerConfig = {
30
- http?: number;
31
- minBundles?: number;
32
- minBundleSize?: number;
33
- maxParallelRequests?: number;
34
- disableSharedBundles?: boolean;
35
- manualSharedBundles?: ManualSharedBundles;
36
- loadConditionalBundlesInParallel?: boolean;
37
- sharedBundleMerge?: MergeCandidates;
38
- };
31
+ type BaseBundlerConfig = {|
32
+ http?: number,
33
+ minBundles?: number,
34
+ minBundleSize?: number,
35
+ maxParallelRequests?: number,
36
+ disableSharedBundles?: boolean,
37
+ manualSharedBundles?: ManualSharedBundles,
38
+ loadConditionalBundlesInParallel?: boolean,
39
+ sharedBundleMerge?: MergeCandidates,
40
+ |};
39
41
 
40
- type BundlerConfig = Partial<Record<BuildMode, BaseBundlerConfig>> &
41
- BaseBundlerConfig;
42
+ type BundlerConfig = {|
43
+ [mode: BuildMode]: BaseBundlerConfig,
44
+ |} & BaseBundlerConfig;
42
45
 
43
- export type ResolvedBundlerConfig = {
44
- minBundles: number;
45
- minBundleSize: number;
46
- maxParallelRequests: number;
47
- projectRoot: string;
48
- disableSharedBundles: boolean;
49
- manualSharedBundles: ManualSharedBundles;
50
- loadConditionalBundlesInParallel?: boolean;
51
- sharedBundleMerge?: MergeCandidates;
52
- };
46
+ export type ResolvedBundlerConfig = {|
47
+ minBundles: number,
48
+ minBundleSize: number,
49
+ maxParallelRequests: number,
50
+ projectRoot: string,
51
+ disableSharedBundles: boolean,
52
+ manualSharedBundles: ManualSharedBundles,
53
+ loadConditionalBundlesInParallel?: boolean,
54
+ sharedBundleMerge?: MergeCandidates,
55
+ |};
53
56
 
54
57
  function resolveModeConfig(
55
58
  config: BundlerConfig,
56
59
  mode: BuildMode,
57
60
  ): BaseBundlerConfig {
58
- let generalConfig: Record<string, any> = {};
59
- let modeConfig: Record<string, any> = {};
61
+ let generalConfig = {};
62
+ let modeConfig = {};
60
63
 
61
64
  for (const key of Object.keys(config)) {
62
65
  if (key === 'development' || key === 'production') {
63
66
  if (key === mode) {
64
- // @ts-expect-error TS2322
65
67
  modeConfig = config[key];
66
68
  }
67
69
  } else {
@@ -69,6 +71,7 @@ function resolveModeConfig(
69
71
  }
70
72
  }
71
73
 
74
+ // $FlowFixMe Not sure how to convince flow here...
72
75
  return {
73
76
  ...generalConfig,
74
77
  ...modeConfig,
@@ -93,7 +96,7 @@ const HTTP_OPTIONS = {
93
96
  disableSharedBundles: false,
94
97
  sharedBundleMerge: [],
95
98
  },
96
- } as const;
99
+ };
97
100
 
98
101
  const CONFIG_SCHEMA: SchemaEntity = {
99
102
  type: 'object',
@@ -200,8 +203,7 @@ export async function loadBundlerConfig(
200
203
  const modDefault = {
201
204
  ...HTTP_OPTIONS['2'],
202
205
  projectRoot: options.projectRoot,
203
- } as const;
204
- // @ts-expect-error TS2322
206
+ };
205
207
  return modDefault;
206
208
  }
207
209
 
@@ -264,7 +266,6 @@ export async function loadBundlerConfig(
264
266
  );
265
267
 
266
268
  let http = modeConfig.http ?? 2;
267
- // @ts-expect-error TS7053
268
269
  let defaults = HTTP_OPTIONS[http];
269
270
 
270
271
  return {
@@ -1,4 +1,6 @@
1
- import {ALL_EDGE_TYPES, NodeId} from '@atlaspack/graph';
1
+ // @flow strict-local
2
+
3
+ import {ALL_EDGE_TYPES, type NodeId} from '@atlaspack/graph';
2
4
  import type {
3
5
  Bundle as LegacyBundle,
4
6
  BundleGroup,
@@ -23,11 +25,13 @@ export function decorateLegacyGraph(
23
25
  bundleGroupBundleIds,
24
26
  manualAssetToBundle,
25
27
  } = idealGraph;
28
+ // This line can be deleted once supportWebpackChunkName feature flag is removed.
26
29
  let entryBundleToBundleGroup: Map<NodeId, BundleGroup> = new Map();
27
30
  // Step Create Bundles: Create bundle groups, bundles, and shared bundles and add assets to them
28
31
  for (let [bundleNodeId, idealBundle] of idealBundleGraph.nodes.entries()) {
29
32
  if (!idealBundle || idealBundle === 'root') continue;
30
33
  let entryAsset = idealBundle.mainEntryAsset;
34
+ // This line can be deleted once supportWebpackChunkName feature flag is removed.
31
35
  let bundleGroup;
32
36
  let bundle;
33
37
 
@@ -50,18 +54,26 @@ export function decorateLegacyGraph(
50
54
  entryAsset != null,
51
55
  'Processing a bundleGroup with no entry asset',
52
56
  );
57
+
58
+ let bundleGroups = new Map();
53
59
  for (let dependency of dependencies) {
54
60
  bundleGroup = bundleGraph.createBundleGroup(
55
61
  dependency,
56
62
  idealBundle.target,
57
63
  );
64
+ bundleGroups.set(bundleGroup.entryAssetId, bundleGroup);
65
+ }
66
+ if (getFeatureFlag('supportWebpackChunkName')) {
67
+ invariant(bundleGroups.size > 0, 'No bundle groups created');
68
+ } else {
69
+ invariant(bundleGroup);
70
+ entryBundleToBundleGroup.set(bundleNodeId, bundleGroup);
58
71
  }
59
- invariant(bundleGroup);
60
- entryBundleToBundleGroup.set(bundleNodeId, bundleGroup);
61
72
 
62
73
  bundle = nullthrows(
63
74
  bundleGraph.createBundle({
64
75
  entryAsset: nullthrows(entryAsset),
76
+ bundleRoots: Array.from(idealBundle.bundleRoots),
65
77
  needsStableName: idealBundle.needsStableName,
66
78
  bundleBehavior: idealBundle.bundleBehavior,
67
79
  target: idealBundle.target,
@@ -69,7 +81,14 @@ export function decorateLegacyGraph(
69
81
  }),
70
82
  );
71
83
 
72
- bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
84
+ if (getFeatureFlag('supportWebpackChunkName')) {
85
+ for (let bundleGroup of bundleGroups.values()) {
86
+ bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
87
+ }
88
+ } else {
89
+ invariant(bundleGroup);
90
+ bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
91
+ }
73
92
  } else if (
74
93
  idealBundle.sourceBundles.size > 0 &&
75
94
  !idealBundle.mainEntryAsset
@@ -107,6 +126,7 @@ export function decorateLegacyGraph(
107
126
  bundle = nullthrows(
108
127
  bundleGraph.createBundle({
109
128
  entryAsset,
129
+ bundleRoots: Array.from(idealBundle.bundleRoots),
110
130
  needsStableName: idealBundle.needsStableName,
111
131
  bundleBehavior: idealBundle.bundleBehavior,
112
132
  target: idealBundle.target,
@@ -193,13 +213,11 @@ export function decorateLegacyGraph(
193
213
  }
194
214
  }
195
215
 
196
- // @ts-expect-error TS2488
197
216
  for (let {type, from, to} of idealBundleGraph.getAllEdges()) {
198
217
  let sourceBundle = nullthrows(idealBundleGraph.getNode(from));
199
218
  if (sourceBundle === 'root') {
200
219
  continue;
201
220
  }
202
- // @ts-expect-error TS2367
203
221
  invariant(sourceBundle !== 'root');
204
222
 
205
223
  let legacySourceBundle = nullthrows(
@@ -210,7 +228,6 @@ export function decorateLegacyGraph(
210
228
  if (targetBundle === 'root') {
211
229
  continue;
212
230
  }
213
- // @ts-expect-error TS2367
214
231
  invariant(targetBundle !== 'root');
215
232
  let legacyTargetBundle = nullthrows(
216
233
  idealBundleToLegacyBundle.get(targetBundle),