@atlaspack/bundler-default 2.14.5-canary.3 → 2.14.5-canary.300

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,59 +1,78 @@
1
- // @flow strict-local
2
-
3
1
  import {encodeJSONKeyComponent} from '@atlaspack/diagnostic';
4
2
  import type {
5
3
  Config,
6
4
  PluginOptions,
7
5
  BuildMode,
8
6
  PluginLogger,
9
- } from '@atlaspack/types';
10
- import {type SchemaEntity, validateSchema} from '@atlaspack/utils';
7
+ } from '@atlaspack/types-internal';
8
+ import {getFeatureFlag} from '@atlaspack/feature-flags';
9
+ import {SchemaEntity, validateSchema} from '@atlaspack/utils';
11
10
  import invariant from 'assert';
12
11
 
13
12
  type Glob = string;
14
13
 
15
- type ManualSharedBundles = Array<{|
16
- name: string,
17
- assets: Array<Glob>,
18
- types?: Array<string>,
19
- root?: string,
20
- split?: number,
21
- |}>;
22
-
23
- type BaseBundlerConfig = {|
24
- http?: number,
25
- minBundles?: number,
26
- minBundleSize?: number,
27
- maxParallelRequests?: number,
28
- disableSharedBundles?: boolean,
29
- manualSharedBundles?: ManualSharedBundles,
30
- loadConditionalBundlesInParallel?: boolean,
31
- |};
32
-
33
- type BundlerConfig = {|
34
- [mode: BuildMode]: BaseBundlerConfig,
35
- |} & BaseBundlerConfig;
36
-
37
- export type ResolvedBundlerConfig = {|
38
- minBundles: number,
39
- minBundleSize: number,
40
- maxParallelRequests: number,
41
- projectRoot: string,
42
- disableSharedBundles: boolean,
43
- manualSharedBundles: ManualSharedBundles,
44
- loadConditionalBundlesInParallel?: boolean,
45
- |};
14
+ type ManualSharedBundles = Array<{
15
+ name: string;
16
+ assets: Array<Glob>;
17
+ types?: Array<string>;
18
+ root?: string;
19
+ split?: number;
20
+ }>;
21
+
22
+ export type SharedBundleMergeCandidates = Array<{
23
+ overlapThreshold?: number;
24
+ maxBundleSize?: number;
25
+ sourceBundles?: Array<string>;
26
+ minBundlesInGroup?: number;
27
+ }>;
28
+
29
+ export interface AsyncBundleMerge {
30
+ /** Consider all async bundles smaller than this for merging */
31
+ bundleSize: number;
32
+ /** The max bytes allowed to be potentially overfetched due to a merge */
33
+ maxOverfetchSize: number;
34
+ /** Bundles to ignore from merging */
35
+ ignore?: Array<Glob>;
36
+ }
37
+
38
+ type BaseBundlerConfig = {
39
+ http?: number;
40
+ minBundles?: number;
41
+ minBundleSize?: number;
42
+ maxParallelRequests?: number;
43
+ disableSharedBundles?: boolean;
44
+ manualSharedBundles?: ManualSharedBundles;
45
+ loadConditionalBundlesInParallel?: boolean;
46
+ sharedBundleMerge?: SharedBundleMergeCandidates;
47
+ asyncBundleMerge?: AsyncBundleMerge;
48
+ };
49
+
50
+ type BundlerConfig = Partial<Record<BuildMode, BaseBundlerConfig>> &
51
+ BaseBundlerConfig;
52
+
53
+ export type ResolvedBundlerConfig = {
54
+ minBundles: number;
55
+ minBundleSize: number;
56
+ maxParallelRequests: number;
57
+ projectRoot: string;
58
+ disableSharedBundles: boolean;
59
+ manualSharedBundles: ManualSharedBundles;
60
+ loadConditionalBundlesInParallel?: boolean;
61
+ sharedBundleMerge?: SharedBundleMergeCandidates;
62
+ asyncBundleMerge?: AsyncBundleMerge;
63
+ };
46
64
 
47
65
  function resolveModeConfig(
48
66
  config: BundlerConfig,
49
67
  mode: BuildMode,
50
68
  ): BaseBundlerConfig {
51
- let generalConfig = {};
52
- let modeConfig = {};
69
+ let generalConfig: Record<string, any> = {};
70
+ let modeConfig: Record<string, any> = {};
53
71
 
54
72
  for (const key of Object.keys(config)) {
55
73
  if (key === 'development' || key === 'production') {
56
74
  if (key === mode) {
75
+ // @ts-expect-error TS2322
57
76
  modeConfig = config[key];
58
77
  }
59
78
  } else {
@@ -61,7 +80,6 @@ function resolveModeConfig(
61
80
  }
62
81
  }
63
82
 
64
- // $FlowFixMe Not sure how to convince flow here...
65
83
  return {
66
84
  ...generalConfig,
67
85
  ...modeConfig,
@@ -76,6 +94,7 @@ const HTTP_OPTIONS = {
76
94
  minBundleSize: 30000,
77
95
  maxParallelRequests: 6,
78
96
  disableSharedBundles: false,
97
+ sharedBundleMerge: [],
79
98
  },
80
99
  '2': {
81
100
  minBundles: 1,
@@ -83,8 +102,9 @@ const HTTP_OPTIONS = {
83
102
  minBundleSize: 20000,
84
103
  maxParallelRequests: 25,
85
104
  disableSharedBundles: false,
105
+ sharedBundleMerge: [],
86
106
  },
87
- };
107
+ } as const;
88
108
 
89
109
  const CONFIG_SCHEMA: SchemaEntity = {
90
110
  type: 'object',
@@ -124,6 +144,50 @@ const CONFIG_SCHEMA: SchemaEntity = {
124
144
  additionalProperties: false,
125
145
  },
126
146
  },
147
+ sharedBundleMerge: {
148
+ type: 'array',
149
+ items: {
150
+ type: 'object',
151
+ properties: {
152
+ overlapThreshold: {
153
+ type: 'number',
154
+ },
155
+ maxBundleSize: {
156
+ type: 'number',
157
+ },
158
+ sourceBundles: {
159
+ type: 'array',
160
+ items: {
161
+ type: 'string',
162
+ },
163
+ },
164
+ minBundlesInGroup: {
165
+ type: 'number',
166
+ },
167
+ },
168
+ additionalProperties: false,
169
+ },
170
+ },
171
+ asyncBundleMerge: {
172
+ type: 'object',
173
+ properties: {
174
+ bundleSize: {
175
+ type: 'number',
176
+ required: true,
177
+ },
178
+ maxOverfetchSize: {
179
+ type: 'number',
180
+ required: true,
181
+ },
182
+ ignore: {
183
+ type: 'array',
184
+ items: {
185
+ type: 'string',
186
+ },
187
+ },
188
+ },
189
+ additionalProperties: false,
190
+ },
127
191
  minBundles: {
128
192
  type: 'number',
129
193
  },
@@ -139,6 +203,9 @@ const CONFIG_SCHEMA: SchemaEntity = {
139
203
  loadConditionalBundlesInParallel: {
140
204
  type: 'boolean',
141
205
  },
206
+ sharedBundleMergeThreshold: {
207
+ type: 'number',
208
+ },
142
209
  },
143
210
  additionalProperties: false,
144
211
  };
@@ -148,15 +215,24 @@ export async function loadBundlerConfig(
148
215
  options: PluginOptions,
149
216
  logger: PluginLogger,
150
217
  ): Promise<ResolvedBundlerConfig> {
151
- let conf = await config.getConfig<BundlerConfig>([], {
152
- packageKey: '@atlaspack/bundler-default',
153
- });
218
+ let conf;
219
+
220
+ if (getFeatureFlag('resolveBundlerConfigFromCwd')) {
221
+ conf = await config.getConfigFrom(`${process.cwd()}/index`, [], {
222
+ packageKey: '@atlaspack/bundler-default',
223
+ });
224
+ } else {
225
+ conf = await config.getConfig<BundlerConfig>([], {
226
+ packageKey: '@atlaspack/bundler-default',
227
+ });
228
+ }
154
229
 
155
230
  if (!conf) {
156
231
  const modDefault = {
157
232
  ...HTTP_OPTIONS['2'],
158
233
  projectRoot: options.projectRoot,
159
- };
234
+ } as const;
235
+ // @ts-expect-error TS2322
160
236
  return modDefault;
161
237
  }
162
238
 
@@ -210,7 +286,7 @@ export async function loadBundlerConfig(
210
286
  CONFIG_SCHEMA,
211
287
  {
212
288
  data: modeConfig,
213
- source: await options.inputFS.readFile(conf.filePath, 'utf8'),
289
+ source: () => options.inputFS.readFileSync(conf.filePath, 'utf8'),
214
290
  filePath: conf.filePath,
215
291
  prependKey: `/${encodeJSONKeyComponent('@atlaspack/bundler-default')}`,
216
292
  },
@@ -219,11 +295,15 @@ export async function loadBundlerConfig(
219
295
  );
220
296
 
221
297
  let http = modeConfig.http ?? 2;
298
+ // @ts-expect-error TS7053
222
299
  let defaults = HTTP_OPTIONS[http];
223
300
 
224
301
  return {
225
302
  minBundles: modeConfig.minBundles ?? defaults.minBundles,
226
303
  minBundleSize: modeConfig.minBundleSize ?? defaults.minBundleSize,
304
+ sharedBundleMerge:
305
+ modeConfig.sharedBundleMerge ?? defaults.sharedBundleMerge,
306
+ asyncBundleMerge: modeConfig.asyncBundleMerge,
227
307
  maxParallelRequests:
228
308
  modeConfig.maxParallelRequests ?? defaults.maxParallelRequests,
229
309
  projectRoot: options.projectRoot,
@@ -1,11 +1,9 @@
1
- // @flow strict-local
2
-
3
- import {ALL_EDGE_TYPES, type NodeId} from '@atlaspack/graph';
1
+ import {ALL_EDGE_TYPES, NodeId} from '@atlaspack/graph';
4
2
  import type {
5
3
  Bundle as LegacyBundle,
6
4
  BundleGroup,
7
5
  MutableBundleGraph,
8
- } from '@atlaspack/types';
6
+ } from '@atlaspack/types-internal';
9
7
  import {getFeatureFlag} from '@atlaspack/feature-flags';
10
8
  import invariant from 'assert';
11
9
  import nullthrows from 'nullthrows';
@@ -25,11 +23,13 @@ export function decorateLegacyGraph(
25
23
  bundleGroupBundleIds,
26
24
  manualAssetToBundle,
27
25
  } = idealGraph;
26
+ // This line can be deleted once supportWebpackChunkName feature flag is removed.
28
27
  let entryBundleToBundleGroup: Map<NodeId, BundleGroup> = new Map();
29
28
  // Step Create Bundles: Create bundle groups, bundles, and shared bundles and add assets to them
30
29
  for (let [bundleNodeId, idealBundle] of idealBundleGraph.nodes.entries()) {
31
30
  if (!idealBundle || idealBundle === 'root') continue;
32
31
  let entryAsset = idealBundle.mainEntryAsset;
32
+ // This line can be deleted once supportWebpackChunkName feature flag is removed.
33
33
  let bundleGroup;
34
34
  let bundle;
35
35
 
@@ -52,18 +52,26 @@ export function decorateLegacyGraph(
52
52
  entryAsset != null,
53
53
  'Processing a bundleGroup with no entry asset',
54
54
  );
55
+
56
+ let bundleGroups = new Map();
55
57
  for (let dependency of dependencies) {
56
58
  bundleGroup = bundleGraph.createBundleGroup(
57
59
  dependency,
58
60
  idealBundle.target,
59
61
  );
62
+ bundleGroups.set(bundleGroup.entryAssetId, bundleGroup);
63
+ }
64
+ if (getFeatureFlag('supportWebpackChunkName')) {
65
+ invariant(bundleGroups.size > 0, 'No bundle groups created');
66
+ } else {
67
+ invariant(bundleGroup);
68
+ entryBundleToBundleGroup.set(bundleNodeId, bundleGroup);
60
69
  }
61
- invariant(bundleGroup);
62
- entryBundleToBundleGroup.set(bundleNodeId, bundleGroup);
63
70
 
64
71
  bundle = nullthrows(
65
72
  bundleGraph.createBundle({
66
73
  entryAsset: nullthrows(entryAsset),
74
+ bundleRoots: Array.from(idealBundle.bundleRoots),
67
75
  needsStableName: idealBundle.needsStableName,
68
76
  bundleBehavior: idealBundle.bundleBehavior,
69
77
  target: idealBundle.target,
@@ -71,7 +79,14 @@ export function decorateLegacyGraph(
71
79
  }),
72
80
  );
73
81
 
74
- bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
82
+ if (getFeatureFlag('supportWebpackChunkName')) {
83
+ for (let bundleGroup of bundleGroups.values()) {
84
+ bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
85
+ }
86
+ } else {
87
+ invariant(bundleGroup);
88
+ bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
89
+ }
75
90
  } else if (
76
91
  idealBundle.sourceBundles.size > 0 &&
77
92
  !idealBundle.mainEntryAsset
@@ -109,6 +124,7 @@ export function decorateLegacyGraph(
109
124
  bundle = nullthrows(
110
125
  bundleGraph.createBundle({
111
126
  entryAsset,
127
+ bundleRoots: Array.from(idealBundle.bundleRoots),
112
128
  needsStableName: idealBundle.needsStableName,
113
129
  bundleBehavior: idealBundle.bundleBehavior,
114
130
  target: idealBundle.target,
@@ -195,11 +211,13 @@ export function decorateLegacyGraph(
195
211
  }
196
212
  }
197
213
 
214
+ // @ts-expect-error TS2488
198
215
  for (let {type, from, to} of idealBundleGraph.getAllEdges()) {
199
216
  let sourceBundle = nullthrows(idealBundleGraph.getNode(from));
200
217
  if (sourceBundle === 'root') {
201
218
  continue;
202
219
  }
220
+ // @ts-expect-error TS2367
203
221
  invariant(sourceBundle !== 'root');
204
222
 
205
223
  let legacySourceBundle = nullthrows(
@@ -210,6 +228,7 @@ export function decorateLegacyGraph(
210
228
  if (targetBundle === 'root') {
211
229
  continue;
212
230
  }
231
+ // @ts-expect-error TS2367
213
232
  invariant(targetBundle !== 'root');
214
233
  let legacyTargetBundle = nullthrows(
215
234
  idealBundleToLegacyBundle.get(targetBundle),