@atlaspack/packager-js 2.14.5-canary.14 → 2.14.5-canary.141

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
2
-
3
1
  import type {
4
2
  Asset,
5
3
  BundleGraph,
@@ -15,6 +13,7 @@ import {
15
13
  relativeBundlePath,
16
14
  countLines,
17
15
  normalizeSeparators,
16
+ debugTools,
18
17
  } from '@atlaspack/utils';
19
18
  import SourceMap from '@parcel/source-map';
20
19
  import nullthrows from 'nullthrows';
@@ -25,6 +24,7 @@ import ThrowableDiagnostic, {
25
24
  import globals from 'globals';
26
25
  import path from 'path';
27
26
  import {getFeatureFlag} from '@atlaspack/feature-flags';
27
+ import {outdent} from 'outdent';
28
28
 
29
29
  import {ESMOutputFormat} from './ESMOutputFormat';
30
30
  import {CJSOutputFormat} from './CJSOutputFormat';
@@ -39,7 +39,6 @@ import {
39
39
 
40
40
  // General regex used to replace imports with the resolved code, references with resolutions,
41
41
  // and count the number of newlines in the file for source maps.
42
- //
43
42
  // For conditional bundling the only difference in this regex is adding `importCond` where we have `importAsync` etc..
44
43
  const REPLACEMENT_RE_CONDITIONAL =
45
44
  /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require|importCond)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
@@ -62,13 +61,13 @@ const GLOBALS_BY_CONTEXT = {
62
61
  ...Object.keys(globals.node),
63
62
  ...Object.keys(globals.browser),
64
63
  ]),
65
- };
64
+ } as const;
66
65
 
67
66
  const OUTPUT_FORMATS = {
68
67
  esmodule: ESMOutputFormat,
69
68
  commonjs: CJSOutputFormat,
70
69
  global: GlobalOutputFormat,
71
- };
70
+ } as const;
72
71
 
73
72
  export interface OutputFormat {
74
73
  buildBundlePrelude(): [string, number];
@@ -83,16 +82,23 @@ export class ScopeHoistingPackager {
83
82
  useAsyncBundleRuntime: boolean;
84
83
  outputFormat: OutputFormat;
85
84
  isAsyncBundle: boolean;
86
- globalNames: $ReadOnlySet<string>;
87
- assetOutputs: Map<string, {|code: string, map: ?Buffer|}>;
85
+ globalNames: ReadonlySet<string>;
86
+ // @ts-expect-error TS2564
87
+ assetOutputs: Map<
88
+ string,
89
+ {
90
+ code: string;
91
+ map: Buffer | null | undefined;
92
+ }
93
+ >;
88
94
  exportedSymbols: Map<
89
95
  string,
90
- {|
91
- asset: Asset,
92
- exportSymbol: string,
93
- local: string,
94
- exportAs: Array<string>,
95
- |},
96
+ {
97
+ asset: Asset;
98
+ exportSymbol: string;
99
+ local: string;
100
+ exportAs: Array<string>;
101
+ }
96
102
  > = new Map();
97
103
  externals: Map<string, Map<string, string>> = new Map();
98
104
  topLevelNames: Map<string, number> = new Map();
@@ -102,7 +108,6 @@ export class ScopeHoistingPackager {
102
108
  needsPrelude: boolean = false;
103
109
  usedHelpers: Set<string> = new Set();
104
110
  externalAssets: Set<Asset> = new Set();
105
- forceSkipWrapAssets: Array<string> = [];
106
111
  logger: PluginLogger;
107
112
 
108
113
  constructor(
@@ -111,7 +116,6 @@ export class ScopeHoistingPackager {
111
116
  bundle: NamedBundle,
112
117
  parcelRequireName: string,
113
118
  useAsyncBundleRuntime: boolean,
114
- forceSkipWrapAssets: Array<string>,
115
119
  logger: PluginLogger,
116
120
  ) {
117
121
  this.options = options;
@@ -119,7 +123,6 @@ export class ScopeHoistingPackager {
119
123
  this.bundle = bundle;
120
124
  this.parcelRequireName = parcelRequireName;
121
125
  this.useAsyncBundleRuntime = useAsyncBundleRuntime;
122
- this.forceSkipWrapAssets = forceSkipWrapAssets ?? [];
123
126
  this.logger = logger;
124
127
 
125
128
  let OutputFormat = OUTPUT_FORMATS[this.bundle.env.outputFormat];
@@ -133,8 +136,12 @@ export class ScopeHoistingPackager {
133
136
  this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
134
137
  }
135
138
 
136
- async package(): Promise<{|contents: string, map: ?SourceMap|}> {
137
- let wrappedAssets = await this.loadAssets();
139
+ async package(): Promise<{
140
+ contents: string;
141
+ map: SourceMap | null | undefined;
142
+ }> {
143
+ let {wrapped: wrappedAssets, constant: constantAssets} =
144
+ await this.loadAssets();
138
145
  this.buildExportedSymbols();
139
146
 
140
147
  // If building a library, the target is actually another bundler rather
@@ -155,9 +162,11 @@ export class ScopeHoistingPackager {
155
162
 
156
163
  let res = '';
157
164
  let lineCount = 0;
165
+ // @ts-expect-error TS7034
158
166
  let sourceMap = null;
159
- let processAsset = (asset) => {
167
+ let processAsset = (asset: Asset) => {
160
168
  let [content, map, lines] = this.visitAsset(asset);
169
+ // @ts-expect-error TS7005
161
170
  if (sourceMap && map) {
162
171
  sourceMap.addSourceMap(map, lineCount);
163
172
  } else if (this.bundle.env.sourceMap) {
@@ -168,6 +177,18 @@ export class ScopeHoistingPackager {
168
177
  lineCount += lines + 1;
169
178
  };
170
179
 
180
+ if (
181
+ getFeatureFlag('inlineConstOptimisationFix') ||
182
+ getFeatureFlag('applyScopeHoistingImprovement')
183
+ ) {
184
+ // Write out all constant modules used by this bundle
185
+ for (let asset of constantAssets) {
186
+ if (!this.seenAssets.has(asset.id)) {
187
+ processAsset(asset);
188
+ }
189
+ }
190
+ }
191
+
171
192
  // Hoist wrapped asset to the top of the bundle to ensure that they are registered
172
193
  // before they are used.
173
194
  for (let asset of wrappedAssets) {
@@ -191,6 +212,7 @@ export class ScopeHoistingPackager {
191
212
  let [prelude, preludeLines] = this.buildBundlePrelude();
192
213
  res = prelude + res;
193
214
  lineCount += preludeLines;
215
+ // @ts-expect-error TS2339
194
216
  sourceMap?.offsetLines(1, preludeLines);
195
217
 
196
218
  let entries = this.bundle.getEntryAssets();
@@ -198,7 +220,11 @@ export class ScopeHoistingPackager {
198
220
  if (this.isAsyncBundle) {
199
221
  // In async bundles we don't want the main entry to execute until we require it
200
222
  // as there might be dependencies in a sibling bundle that hasn't loaded yet.
201
- entries = entries.filter((a) => a.id !== mainEntry?.id);
223
+ if (getFeatureFlag('supportWebpackChunkName')) {
224
+ entries = [];
225
+ } else {
226
+ entries = entries.filter((a) => a.id !== mainEntry?.id);
227
+ }
202
228
  mainEntry = null;
203
229
  }
204
230
 
@@ -211,17 +237,20 @@ export class ScopeHoistingPackager {
211
237
  this.bundleGraph.getAssetPublicId(entry),
212
238
  )});\n`;
213
239
 
240
+ // @ts-expect-error TS2345
214
241
  let entryExports = entry.symbols.get('*')?.local;
215
242
 
216
243
  if (
217
244
  entryExports &&
218
245
  entry === mainEntry &&
246
+ // @ts-expect-error TS2345
219
247
  this.exportedSymbols.has(entryExports)
220
248
  ) {
221
249
  invariant(
222
250
  !needsBundleQueue,
223
251
  'Entry exports are not yet compaitble with async bundles',
224
252
  );
253
+ // @ts-expect-error TS2731
225
254
  res += `\nvar ${entryExports} = ${parcelRequire}`;
226
255
  } else {
227
256
  if (needsBundleQueue) {
@@ -265,6 +294,7 @@ export class ScopeHoistingPackager {
265
294
  this.parcelRequireName,
266
295
  );
267
296
  if (sourceMap && map) {
297
+ // @ts-expect-error TS2339
268
298
  sourceMap.addSourceMap(map, lineCount);
269
299
  }
270
300
  }
@@ -281,10 +311,7 @@ export class ScopeHoistingPackager {
281
311
 
282
312
  let hasConditionalReference = false;
283
313
  let isConditionalBundle = false;
284
- if (
285
- getFeatureFlag('conditionalBundlingApi') &&
286
- getFeatureFlag('conditionalBundlingAsyncRuntime')
287
- ) {
314
+ if (getFeatureFlag('conditionalBundlingApi')) {
288
315
  // If the bundle has a conditional bundle reference (has an importCond)
289
316
  hasConditionalReference =
290
317
  this.bundleGraph.getReferencedConditionalBundles(bundle).length > 0;
@@ -309,11 +336,8 @@ export class ScopeHoistingPackager {
309
336
  .filter((b) => this.shouldBundleQueue(b))
310
337
  .map((b) => b.publicId);
311
338
 
312
- const conditions = [];
313
- if (
314
- getFeatureFlag('conditionalBundlingApi') &&
315
- getFeatureFlag('conditionalBundlingAsyncRuntime')
316
- ) {
339
+ const conditions: Array<string> = [];
340
+ if (getFeatureFlag('conditionalBundlingApi')) {
317
341
  const conditionSet = this.bundleGraph
318
342
  .getConditionalBundleMapping()
319
343
  .get(bundle.id);
@@ -354,15 +378,20 @@ export class ScopeHoistingPackager {
354
378
  return `$parcel$global.rwr(${params.join(', ')});`;
355
379
  }
356
380
 
357
- async loadAssets(): Promise<Array<Asset>> {
381
+ async loadAssets(): Promise<{
382
+ wrapped: Array<Asset>;
383
+ constant: Array<Asset>;
384
+ }> {
358
385
  let queue = new PromiseQueue({maxConcurrent: 32});
359
- let wrapped = [];
386
+ let wrapped: Array<Asset> = [];
387
+ let constant: Array<Asset> = [];
360
388
  this.bundle.traverseAssets((asset) => {
361
389
  queue.add(async () => {
362
390
  let [code, map] = await Promise.all([
363
391
  asset.getCode(),
364
392
  this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
365
393
  ]);
394
+
366
395
  return [asset.id, {code, map}];
367
396
  });
368
397
 
@@ -383,50 +412,88 @@ export class ScopeHoistingPackager {
383
412
  ) {
384
413
  this.wrappedAssets.add(asset.id);
385
414
  wrapped.push(asset);
415
+ } else if (
416
+ (getFeatureFlag('inlineConstOptimisationFix') ||
417
+ getFeatureFlag('applyScopeHoistingImprovement')) &&
418
+ asset.meta.isConstantModule
419
+ ) {
420
+ constant.push(asset);
386
421
  }
387
422
  }
388
423
  });
389
424
 
390
- for (let wrappedAssetRoot of [...wrapped]) {
391
- this.bundle.traverseAssets((asset, _, actions) => {
392
- if (asset === wrappedAssetRoot) {
393
- return;
394
- }
425
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
426
+ // Tracks which assets have been assigned to a wrap group
427
+ let assignedAssets = new Set<Asset>();
395
428
 
396
- if (this.wrappedAssets.has(asset.id)) {
397
- actions.skipChildren();
398
- return;
399
- }
400
- // This prevents children of a wrapped asset also being wrapped - it's an "unsafe" optimisation
401
- // that should only be used when you know (or think you know) what you're doing.
402
- //
403
- // In particular this can force an async bundle to be scope hoisted where it previously would not be
404
- // due to the entry asset being wrapped.
405
- if (
406
- this.forceSkipWrapAssets.length > 0 &&
407
- this.forceSkipWrapAssets.some(
408
- (p) =>
409
- p === path.relative(this.options.projectRoot, asset.filePath),
410
- )
411
- ) {
412
- this.logger.verbose({
413
- message: `Force skipping wrapping of ${path.relative(
414
- this.options.projectRoot,
415
- asset.filePath,
416
- )}`,
417
- });
418
- actions.skipChildren();
419
- return;
420
- }
421
- if (!asset.meta.isConstantModule) {
422
- this.wrappedAssets.add(asset.id);
423
- wrapped.push(asset);
424
- }
425
- }, wrappedAssetRoot);
429
+ for (let wrappedAsset of wrapped) {
430
+ this.bundle.traverseAssets((asset, _, actions) => {
431
+ if (asset === wrappedAsset) {
432
+ return;
433
+ }
434
+
435
+ if (this.wrappedAssets.has(asset.id)) {
436
+ actions.skipChildren();
437
+ return;
438
+ }
439
+
440
+ if (
441
+ !asset.meta.isConstantModule &&
442
+ (assignedAssets.has(asset) || this.isReExported(asset))
443
+ ) {
444
+ wrapped.push(asset);
445
+ this.wrappedAssets.add(asset.id);
446
+
447
+ actions.skipChildren();
448
+ return;
449
+ }
450
+
451
+ assignedAssets.add(asset);
452
+ }, wrappedAsset);
453
+ }
454
+ } else {
455
+ for (let wrappedAssetRoot of [...wrapped]) {
456
+ this.bundle.traverseAssets((asset, _, actions) => {
457
+ if (asset === wrappedAssetRoot) {
458
+ return;
459
+ }
460
+
461
+ if (this.wrappedAssets.has(asset.id)) {
462
+ actions.skipChildren();
463
+ return;
464
+ }
465
+
466
+ if (!asset.meta.isConstantModule) {
467
+ this.wrappedAssets.add(asset.id);
468
+ wrapped.push(asset);
469
+ }
470
+ }, wrappedAssetRoot);
471
+ }
426
472
  }
427
473
 
474
+ // @ts-expect-error TS2769
428
475
  this.assetOutputs = new Map(await queue.run());
429
- return wrapped;
476
+ return {wrapped, constant};
477
+ }
478
+
479
+ isReExported(asset: Asset): boolean {
480
+ let parentSymbols = this.bundleGraph
481
+ .getIncomingDependencies(asset)
482
+ .map((dep) => this.bundleGraph.getAssetWithDependency(dep))
483
+ .flatMap((parent) => {
484
+ if (parent == null) {
485
+ return [];
486
+ }
487
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
488
+ });
489
+
490
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
491
+
492
+ return assetSymbols.some((assetSymbol) =>
493
+ parentSymbols.some(
494
+ (parentSymbol) => parentSymbol.symbol === assetSymbol.symbol,
495
+ ),
496
+ );
430
497
  }
431
498
 
432
499
  buildExportedSymbols() {
@@ -440,6 +507,7 @@ export class ScopeHoistingPackager {
440
507
  // TODO: handle ESM exports of wrapped entry assets...
441
508
  let entry = this.bundle.getMainEntry();
442
509
  if (entry && !this.wrappedAssets.has(entry.id)) {
510
+ // @ts-expect-error TS2345
443
511
  let hasNamespace = entry.symbols.hasExportSymbol('*');
444
512
 
445
513
  for (let {
@@ -457,6 +525,7 @@ export class ScopeHoistingPackager {
457
525
  }
458
526
 
459
527
  let symbols = this.exportedSymbols.get(
528
+ // @ts-expect-error TS2345
460
529
  symbol === '*' ? nullthrows(entry.symbols.get('*')?.local) : symbol,
461
530
  )?.exportAs;
462
531
 
@@ -464,6 +533,7 @@ export class ScopeHoistingPackager {
464
533
  symbols = [];
465
534
  this.exportedSymbols.set(symbol, {
466
535
  asset,
536
+ // @ts-expect-error TS2322
467
537
  exportSymbol,
468
538
  local: symbol,
469
539
  exportAs: symbols,
@@ -474,6 +544,7 @@ export class ScopeHoistingPackager {
474
544
  exportAs = 'default';
475
545
  }
476
546
 
547
+ // @ts-expect-error TS2345
477
548
  symbols.push(exportAs);
478
549
  } else if (symbol === null) {
479
550
  // TODO `meta.exportsIdentifier[exportSymbol]` should be exported
@@ -519,7 +590,7 @@ export class ScopeHoistingPackager {
519
590
  return `${obj}[${JSON.stringify(property)}]`;
520
591
  }
521
592
 
522
- visitAsset(asset: Asset): [string, ?SourceMap, number] {
593
+ visitAsset(asset: Asset): [string, SourceMap | null | undefined, number] {
523
594
  invariant(!this.seenAssets.has(asset.id), 'Already visited asset');
524
595
  this.seenAssets.add(asset.id);
525
596
 
@@ -527,11 +598,15 @@ export class ScopeHoistingPackager {
527
598
  return this.buildAsset(asset, code, map);
528
599
  }
529
600
 
601
+ getAssetFilePath(asset: Asset): string {
602
+ return path.relative(this.options.projectRoot, asset.filePath);
603
+ }
604
+
530
605
  buildAsset(
531
606
  asset: Asset,
532
607
  code: string,
533
- map: ?Buffer,
534
- ): [string, ?SourceMap, number] {
608
+ map?: Buffer | null,
609
+ ): [string, SourceMap | null | undefined, number] {
535
610
  let shouldWrap = this.wrappedAssets.has(asset.id);
536
611
  let deps = this.bundleGraph.getDependencies(asset);
537
612
 
@@ -566,6 +641,7 @@ export class ScopeHoistingPackager {
566
641
  let [code, map, lines] = this.visitAsset(resolved);
567
642
  depCode += code + '\n';
568
643
  if (sourceMap && map) {
644
+ // @ts-expect-error TS2551
569
645
  sourceMap.addSourceMap(map, lineCount);
570
646
  }
571
647
  lineCount += lines + 1;
@@ -602,7 +678,9 @@ export class ScopeHoistingPackager {
602
678
  code += append;
603
679
 
604
680
  let lineCount = 0;
605
- let depContent = [];
681
+ // @ts-expect-error TS2552
682
+ let depContent: Array<[string, NodeSourceMap | null | undefined, number]> =
683
+ [];
606
684
  if (depMap.size === 0 && replacements.size === 0) {
607
685
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
608
686
  lineCount = countLines(code) - 1;
@@ -661,13 +739,38 @@ export class ScopeHoistingPackager {
661
739
  // outside our parcelRequire.register wrapper. This is safe because all
662
740
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
663
741
  // asset content where the import statement was.
664
- if (shouldWrap) {
665
- depContent.push(this.visitAsset(resolved));
742
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
743
+ if (
744
+ !resolved.meta.isConstantModule &&
745
+ !this.wrappedAssets.has(resolved.id)
746
+ ) {
747
+ let [depCode, depMap, depLines] =
748
+ this.visitAsset(resolved);
749
+ if (debugTools['asset-file-names-in-output']) {
750
+ let resolvedPath = this.getAssetFilePath(resolved);
751
+ res = outdent`
752
+ /* Scope hoisted asset: ${resolvedPath} */
753
+ ${depCode}
754
+ /* End: ${resolvedPath} */
755
+ ${res}
756
+ `;
757
+ lines += 3 + depLines;
758
+ } else {
759
+ res = depCode + '\n' + res;
760
+ lines += 1 + depLines;
761
+ }
762
+ map = depMap;
763
+ }
666
764
  } else {
667
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
668
- res = depCode + '\n' + res;
669
- lines += 1 + depLines;
670
- map = depMap;
765
+ if (shouldWrap) {
766
+ depContent.push(this.visitAsset(resolved));
767
+ } else {
768
+ let [depCode, depMap, depLines] =
769
+ this.visitAsset(resolved);
770
+ res = depCode + '\n' + res;
771
+ lines += 1 + depLines;
772
+ map = depMap;
773
+ }
671
774
  }
672
775
  }
673
776
 
@@ -679,6 +782,7 @@ export class ScopeHoistingPackager {
679
782
  }
680
783
 
681
784
  if (map) {
785
+ // @ts-expect-error TS2551
682
786
  sourceMap.addSourceMap(map, lineCount);
683
787
  }
684
788
  }
@@ -726,10 +830,16 @@ ${code}
726
830
 
727
831
  lineCount += 2;
728
832
 
833
+ if (debugTools['asset-file-names-in-output']) {
834
+ code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
835
+ lineCount += 1;
836
+ }
837
+
729
838
  for (let [depCode, map, lines] of depContent) {
730
839
  if (!depCode) continue;
731
840
  code += depCode + '\n';
732
841
  if (sourceMap && map) {
842
+ // @ts-expect-error TS2551
733
843
  sourceMap.addSourceMap(map, lineCount);
734
844
  }
735
845
  lineCount += lines + 1;
@@ -812,10 +922,12 @@ ${code}
812
922
  }
813
923
 
814
924
  for (let [imported, {local}] of dep.symbols) {
925
+ // @ts-expect-error TS2367
815
926
  if (local === '*') {
816
927
  continue;
817
928
  }
818
929
 
930
+ // @ts-expect-error TS2345
819
931
  let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
820
932
  replacements.set(
821
933
  local,
@@ -852,6 +964,7 @@ ${code}
852
964
  (this.bundle.env.outputFormat === 'commonjs' &&
853
965
  asset === this.bundle.getMainEntry())
854
966
  ) {
967
+ // @ts-expect-error TS2345
855
968
  let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;
856
969
  replacements.set(exportsName, 'module.exports');
857
970
  }
@@ -895,8 +1008,11 @@ ${code}
895
1008
 
896
1009
  for (let [imported, {local}] of dep.symbols) {
897
1010
  // If already imported, just add the already renamed variable to the mapping.
1011
+ // @ts-expect-error TS2345
898
1012
  let renamed = external.get(imported);
1013
+ // @ts-expect-error TS2367
899
1014
  if (renamed && local !== '*' && replacements) {
1015
+ // @ts-expect-error TS2345
900
1016
  replacements.set(local, renamed);
901
1017
  continue;
902
1018
  }
@@ -908,7 +1024,9 @@ ${code}
908
1024
  if (!renamed) {
909
1025
  if (referencedBundle) {
910
1026
  let entry = nullthrows(referencedBundle.getMainEntry());
1027
+ // @ts-expect-error TS2322
911
1028
  renamed =
1029
+ // @ts-expect-error TS2345
912
1030
  entry.symbols.get('*')?.local ??
913
1031
  `$${String(entry.meta.id)}$exports`;
914
1032
  } else {
@@ -917,13 +1035,17 @@ ${code}
917
1035
  );
918
1036
  }
919
1037
 
1038
+ // @ts-expect-error TS2345
920
1039
  external.set('*', renamed);
921
1040
  }
922
1041
 
1042
+ // @ts-expect-error TS2367
923
1043
  if (local !== '*' && replacements) {
924
1044
  let replacement;
1045
+ // @ts-expect-error TS2367
925
1046
  if (imported === '*') {
926
1047
  replacement = renamed;
1048
+ // @ts-expect-error TS2367
927
1049
  } else if (imported === 'default') {
928
1050
  let needsDefaultInterop = true;
929
1051
  if (referencedBundle) {
@@ -937,36 +1059,44 @@ ${code}
937
1059
  replacement = `${renamed}.default`;
938
1060
  }
939
1061
  } else {
1062
+ // @ts-expect-error TS2345
940
1063
  replacement = this.getPropertyAccess(renamed, imported);
941
1064
  }
942
1065
 
1066
+ // @ts-expect-error TS2345
943
1067
  replacements.set(local, replacement);
944
1068
  }
945
1069
  } else {
946
1070
  let property;
947
1071
  if (referencedBundle) {
948
1072
  let entry = nullthrows(referencedBundle.getMainEntry());
1073
+ // @ts-expect-error TS2345
949
1074
  if (entry.symbols.hasExportSymbol('*')) {
950
1075
  // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
951
1076
  // This mirrors the logic in buildExportedSymbols.
952
1077
  property = imported;
1078
+ // @ts-expect-error TS2322
953
1079
  imported =
954
1080
  referencedBundle?.env.outputFormat === 'esmodule'
955
1081
  ? 'default'
956
1082
  : '*';
957
1083
  } else {
1084
+ // @ts-expect-error TS2367
958
1085
  if (imported === '*') {
959
1086
  let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
1087
+ // @ts-expect-error TS2367
960
1088
  if (local === '*') {
961
1089
  // Re-export all symbols.
962
1090
  for (let exported of exportedSymbols) {
963
1091
  if (exported.symbol) {
1092
+ // @ts-expect-error TS2345
964
1093
  external.set(exported.exportSymbol, exported.symbol);
965
1094
  }
966
1095
  }
967
1096
  continue;
968
1097
  }
969
1098
  }
1099
+ // @ts-expect-error TS2322
970
1100
  renamed = this.bundleGraph.getSymbolResolution(
971
1101
  entry,
972
1102
  imported,
@@ -979,30 +1109,40 @@ ${code}
979
1109
  // are deduplicated. We have to prefix the imported name with the bundle id so that
980
1110
  // local variables do not shadow it.
981
1111
  if (!renamed) {
1112
+ // @ts-expect-error TS2345
982
1113
  if (this.exportedSymbols.has(local)) {
1114
+ // @ts-expect-error TS2322
983
1115
  renamed = local;
1116
+ // @ts-expect-error TS2367
984
1117
  } else if (imported === 'default' || imported === '*') {
985
1118
  renamed = this.getTopLevelName(
986
1119
  `$${this.bundle.publicId}$${specifier}`,
987
1120
  );
988
1121
  } else {
989
1122
  renamed = this.getTopLevelName(
1123
+ // @ts-expect-error TS2731
990
1124
  `$${this.bundle.publicId}$${imported}`,
991
1125
  );
992
1126
  }
993
1127
  }
994
1128
 
1129
+ // @ts-expect-error TS2345
995
1130
  external.set(imported, renamed);
1131
+ // @ts-expect-error TS2367
996
1132
  if (local !== '*' && replacements) {
997
1133
  let replacement = renamed;
1134
+ // @ts-expect-error TS2367
998
1135
  if (property === '*') {
999
1136
  replacement = renamed;
1137
+ // @ts-expect-error TS2367
1000
1138
  } else if (property === 'default') {
1001
1139
  replacement = `($parcel$interopDefault(${renamed}))`;
1002
1140
  this.usedHelpers.add('$parcel$interopDefault');
1003
1141
  } else if (property) {
1142
+ // @ts-expect-error TS2345
1004
1143
  replacement = this.getPropertyAccess(renamed, property);
1005
1144
  }
1145
+ // @ts-expect-error TS2345
1006
1146
  replacements.set(local, replacement);
1007
1147
  }
1008
1148
  }
@@ -1041,6 +1181,7 @@ ${code}
1041
1181
  asset: resolvedAsset,
1042
1182
  exportSymbol,
1043
1183
  symbol,
1184
+ // @ts-expect-error TS2345
1044
1185
  } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
1045
1186
 
1046
1187
  if (
@@ -1099,8 +1240,11 @@ ${code}
1099
1240
  staticExports &&
1100
1241
  !isWrapped &&
1101
1242
  (dep?.meta.kind === 'Import' || dep?.meta.kind === 'Export') &&
1243
+ // @ts-expect-error TS2345
1102
1244
  resolvedAsset.symbols.hasExportSymbol('*') &&
1245
+ // @ts-expect-error TS2345
1103
1246
  resolvedAsset.symbols.hasExportSymbol('default') &&
1247
+ // @ts-expect-error TS2345
1104
1248
  !resolvedAsset.symbols.hasExportSymbol('__esModule');
1105
1249
 
1106
1250
  // Find the namespace object for the resolved module. If wrapped and this
@@ -1116,7 +1260,9 @@ ${code}
1116
1260
  } else if (isWrapped && dep) {
1117
1261
  obj = `$${publicId}`;
1118
1262
  } else {
1263
+ // @ts-expect-error TS2345
1119
1264
  obj = resolvedAsset.symbols.get('*')?.local || `$${assetId}$exports`;
1265
+ // @ts-expect-error TS2345
1120
1266
  obj = replacements?.get(obj) || obj;
1121
1267
  }
1122
1268
 
@@ -1129,6 +1275,7 @@ ${code}
1129
1275
  // Directly use module.exports for wrapped assets importing themselves.
1130
1276
  return 'module.exports';
1131
1277
  } else {
1278
+ // @ts-expect-error TS2322
1132
1279
  return obj;
1133
1280
  }
1134
1281
  } else if (
@@ -1143,17 +1290,21 @@ ${code}
1143
1290
  if (
1144
1291
  (!dep || kind === 'Import' || kind === 'Export') &&
1145
1292
  exportSymbol === 'default' &&
1293
+ // @ts-expect-error TS2345
1146
1294
  resolvedAsset.symbols.hasExportSymbol('*') &&
1147
1295
  this.needsDefaultInterop(resolvedAsset)
1148
1296
  ) {
1149
1297
  this.usedHelpers.add('$parcel$interopDefault');
1298
+ // @ts-expect-error TS2731
1150
1299
  return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
1151
1300
  } else {
1301
+ // @ts-expect-error TS2345
1152
1302
  return this.getPropertyAccess(obj, exportSymbol);
1153
1303
  }
1154
1304
  } else if (!symbol) {
1155
1305
  invariant(false, 'Asset was skipped or not found.');
1156
1306
  } else {
1307
+ // @ts-expect-error TS2322
1157
1308
  return replacements?.get(symbol) || symbol;
1158
1309
  }
1159
1310
  }
@@ -1216,38 +1367,56 @@ ${code}
1216
1367
  // If there's no __esModule flag, and default is a used symbol, we need
1217
1368
  // to insert an interop helper.
1218
1369
  let defaultInterop =
1370
+ // @ts-expect-error TS2345
1219
1371
  asset.symbols.hasExportSymbol('*') &&
1372
+ // @ts-expect-error TS2345
1220
1373
  usedSymbols.has('default') &&
1374
+ // @ts-expect-error TS2345
1221
1375
  !asset.symbols.hasExportSymbol('__esModule');
1222
1376
 
1223
- let usedNamespace =
1224
- // If the asset has * in its used symbols, we might need the exports namespace.
1225
- // The one case where this isn't true is in ESM library entries, where the only
1226
- // dependency on * is the entry dependency. In this case, we will use ESM exports
1227
- // instead of the namespace object.
1228
- (usedSymbols.has('*') &&
1229
- (this.bundle.env.outputFormat !== 'esmodule' ||
1230
- !this.bundle.env.isLibrary ||
1231
- asset !== this.bundle.getMainEntry() ||
1232
- this.bundleGraph
1233
- .getIncomingDependencies(asset)
1234
- .some(
1377
+ let usedNamespace;
1378
+ if (
1379
+ getFeatureFlag('inlineConstOptimisationFix') &&
1380
+ asset.meta.isConstantModule
1381
+ ) {
1382
+ // Only set usedNamespace if there is an incoming dependency in the current bundle that uses '*'
1383
+ usedNamespace = this.bundleGraph.getIncomingDependencies(asset).some(
1384
+ (dep) =>
1385
+ this.bundle.hasDependency(dep) &&
1386
+ // @ts-expect-error TS2345
1387
+ nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1388
+ );
1389
+ } else {
1390
+ usedNamespace =
1391
+ // If the asset has * in its used symbols, we might need the exports namespace.
1392
+ // The one case where this isn't true is in ESM library entries, where the only
1393
+ // dependency on * is the entry dependency. In this case, we will use ESM exports
1394
+ // instead of the namespace object.
1395
+ // @ts-expect-error TS2345
1396
+ (usedSymbols.has('*') &&
1397
+ (this.bundle.env.outputFormat !== 'esmodule' ||
1398
+ !this.bundle.env.isLibrary ||
1399
+ asset !== this.bundle.getMainEntry() ||
1400
+ this.bundleGraph.getIncomingDependencies(asset).some(
1235
1401
  (dep) =>
1236
1402
  !dep.isEntry &&
1237
1403
  this.bundle.hasDependency(dep) &&
1404
+ // @ts-expect-error TS2345
1238
1405
  nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1239
1406
  ))) ||
1240
- // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
1241
- // we fallback on the namespace object.
1242
- (asset.symbols.hasExportSymbol('*') &&
1243
- [...usedSymbols].some((s) => !asset.symbols.hasExportSymbol(s))) ||
1244
- // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1245
- // include the namespace object for the default export.
1246
- this.exportedSymbols.has(`$${assetId}$exports`) ||
1247
- // CommonJS library bundle entries always need a namespace.
1248
- (this.bundle.env.isLibrary &&
1249
- this.bundle.env.outputFormat === 'commonjs' &&
1250
- asset === this.bundle.getMainEntry());
1407
+ // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
1408
+ // we fallback on the namespace object.
1409
+ // @ts-expect-error TS2345
1410
+ (asset.symbols.hasExportSymbol('*') &&
1411
+ [...usedSymbols].some((s) => !asset.symbols.hasExportSymbol(s))) ||
1412
+ // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1413
+ // include the namespace object for the default export.
1414
+ this.exportedSymbols.has(`$${assetId}$exports`) ||
1415
+ // CommonJS library bundle entries always need a namespace.
1416
+ (this.bundle.env.isLibrary &&
1417
+ this.bundle.env.outputFormat === 'commonjs' &&
1418
+ asset === this.bundle.getMainEntry());
1419
+ }
1251
1420
 
1252
1421
  // If the asset doesn't have static exports, should wrap, the namespace is used,
1253
1422
  // or we need default interop, then we need to synthesize a namespace object for
@@ -1274,6 +1443,7 @@ ${code}
1274
1443
  // Insert the __esModule interop flag for this module if it has a `default` export
1275
1444
  // and the namespace symbol is used.
1276
1445
  // TODO: only if required by CJS?
1446
+ // @ts-expect-error TS2345
1277
1447
  if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
1278
1448
  prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
1279
1449
  prependLineCount += 2;
@@ -1292,6 +1462,7 @@ ${code}
1292
1462
  let isWrapped = resolved && resolved.meta.shouldWrap;
1293
1463
 
1294
1464
  for (let [imported, {local}] of dep.symbols) {
1465
+ // @ts-expect-error TS2367
1295
1466
  if (imported === '*' && local === '*') {
1296
1467
  if (!resolved) {
1297
1468
  // Re-exporting an external module. This should have already been handled in buildReplacements.
@@ -1309,9 +1480,11 @@ ${code}
1309
1480
  if (
1310
1481
  isWrapped ||
1311
1482
  resolved.meta.staticExports === false ||
1483
+ // @ts-expect-error TS2345
1312
1484
  nullthrows(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
1313
1485
  // an empty asset
1314
1486
  (!resolved.meta.hasCJSExports &&
1487
+ // @ts-expect-error TS2345
1315
1488
  resolved.symbols.hasExportSymbol('*'))
1316
1489
  ) {
1317
1490
  let obj = this.getSymbolResolution(
@@ -1328,7 +1501,9 @@ ${code}
1328
1501
  this.bundleGraph.getUsedSymbols(dep),
1329
1502
  )) {
1330
1503
  if (
1504
+ // @ts-expect-error TS2367
1331
1505
  symbol === 'default' || // `export * as ...` does not include the default export
1506
+ // @ts-expect-error TS2367
1332
1507
  symbol === '__esModule'
1333
1508
  ) {
1334
1509
  continue;
@@ -1337,6 +1512,7 @@ ${code}
1337
1512
  let resolvedSymbol = this.getSymbolResolution(
1338
1513
  asset,
1339
1514
  resolved,
1515
+ // @ts-expect-error TS2345
1340
1516
  symbol,
1341
1517
  undefined,
1342
1518
  replacements,
@@ -1362,6 +1538,7 @@ ${code}
1362
1538
  // re-exported symbols rather than only symbols declared in this asset.
1363
1539
  let incomingDeps = this.bundleGraph.getIncomingDependencies(asset);
1364
1540
  let usedExports = [...asset.symbols.exportSymbols()].filter((symbol) => {
1541
+ // @ts-expect-error TS2367
1365
1542
  if (symbol === '*') {
1366
1543
  return false;
1367
1544
  }
@@ -1378,6 +1555,7 @@ ${code}
1378
1555
  // No used symbols available for the asset, make sure we keep all of them
1379
1556
  if (!symbols) return false;
1380
1557
 
1558
+ // @ts-expect-error TS2345
1381
1559
  return !symbols.has(symbol) && !symbols.has('*');
1382
1560
  });
1383
1561
  return !unused;
@@ -1393,6 +1571,7 @@ ${code}
1393
1571
  let resolved = this.getSymbolResolution(
1394
1572
  asset,
1395
1573
  asset,
1574
+ // @ts-expect-error TS2345
1396
1575
  exp,
1397
1576
  undefined,
1398
1577
  replacements,
@@ -1448,8 +1627,10 @@ ${code}
1448
1627
  }
1449
1628
 
1450
1629
  for (let helper of this.usedHelpers) {
1630
+ // @ts-expect-error TS7053
1451
1631
  let currentHelper = helpers[helper];
1452
1632
  if (typeof currentHelper === 'function') {
1633
+ // @ts-expect-error TS7053
1453
1634
  currentHelper = helpers[helper](this.bundle.env);
1454
1635
  }
1455
1636
  res += currentHelper;
@@ -1522,7 +1703,9 @@ ${code}
1522
1703
 
1523
1704
  needsDefaultInterop(asset: Asset): boolean {
1524
1705
  if (
1706
+ // @ts-expect-error TS2345
1525
1707
  asset.symbols.hasExportSymbol('*') &&
1708
+ // @ts-expect-error TS2345
1526
1709
  !asset.symbols.hasExportSymbol('default')
1527
1710
  ) {
1528
1711
  return true;