@atlaspack/packager-js 2.14.5-canary.17 → 2.14.5-canary.171

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;
@@ -55,6 +54,7 @@ const GLOBALS_BY_CONTEXT = {
55
54
  ...Object.keys(globals.serviceworker),
56
55
  ]),
57
56
  worklet: new Set([...BUILTINS]),
57
+ tesseract: new Set([...BUILTINS, ...Object.keys(globals.worker)]),
58
58
  node: new Set([...BUILTINS, ...Object.keys(globals.node)]),
59
59
  'electron-main': new Set([...BUILTINS, ...Object.keys(globals.node)]),
60
60
  'electron-renderer': new Set([
@@ -62,13 +62,13 @@ const GLOBALS_BY_CONTEXT = {
62
62
  ...Object.keys(globals.node),
63
63
  ...Object.keys(globals.browser),
64
64
  ]),
65
- };
65
+ } as const;
66
66
 
67
67
  const OUTPUT_FORMATS = {
68
68
  esmodule: ESMOutputFormat,
69
69
  commonjs: CJSOutputFormat,
70
70
  global: GlobalOutputFormat,
71
- };
71
+ } as const;
72
72
 
73
73
  export interface OutputFormat {
74
74
  buildBundlePrelude(): [string, number];
@@ -83,16 +83,23 @@ export class ScopeHoistingPackager {
83
83
  useAsyncBundleRuntime: boolean;
84
84
  outputFormat: OutputFormat;
85
85
  isAsyncBundle: boolean;
86
- globalNames: $ReadOnlySet<string>;
87
- assetOutputs: Map<string, {|code: string, map: ?Buffer|}>;
86
+ globalNames: ReadonlySet<string>;
87
+ // @ts-expect-error TS2564
88
+ assetOutputs: Map<
89
+ string,
90
+ {
91
+ code: string;
92
+ map: Buffer | null | undefined;
93
+ }
94
+ >;
88
95
  exportedSymbols: Map<
89
96
  string,
90
- {|
91
- asset: Asset,
92
- exportSymbol: string,
93
- local: string,
94
- exportAs: Array<string>,
95
- |},
97
+ {
98
+ asset: Asset;
99
+ exportSymbol: string;
100
+ local: string;
101
+ exportAs: Array<string>;
102
+ }
96
103
  > = new Map();
97
104
  externals: Map<string, Map<string, string>> = new Map();
98
105
  topLevelNames: Map<string, number> = new Map();
@@ -102,7 +109,6 @@ export class ScopeHoistingPackager {
102
109
  needsPrelude: boolean = false;
103
110
  usedHelpers: Set<string> = new Set();
104
111
  externalAssets: Set<Asset> = new Set();
105
- forceSkipWrapAssets: Array<string> = [];
106
112
  logger: PluginLogger;
107
113
 
108
114
  constructor(
@@ -111,7 +117,6 @@ export class ScopeHoistingPackager {
111
117
  bundle: NamedBundle,
112
118
  parcelRequireName: string,
113
119
  useAsyncBundleRuntime: boolean,
114
- forceSkipWrapAssets: Array<string>,
115
120
  logger: PluginLogger,
116
121
  ) {
117
122
  this.options = options;
@@ -119,7 +124,6 @@ export class ScopeHoistingPackager {
119
124
  this.bundle = bundle;
120
125
  this.parcelRequireName = parcelRequireName;
121
126
  this.useAsyncBundleRuntime = useAsyncBundleRuntime;
122
- this.forceSkipWrapAssets = forceSkipWrapAssets ?? [];
123
127
  this.logger = logger;
124
128
 
125
129
  let OutputFormat = OUTPUT_FORMATS[this.bundle.env.outputFormat];
@@ -128,13 +132,18 @@ export class ScopeHoistingPackager {
128
132
  this.isAsyncBundle =
129
133
  this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') &&
130
134
  !this.bundle.env.isIsolated() &&
131
- this.bundle.bundleBehavior !== 'isolated';
135
+ this.bundle.bundleBehavior !== 'isolated' &&
136
+ this.bundle.bundleBehavior !== 'inlineIsolated';
132
137
 
133
138
  this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
134
139
  }
135
140
 
136
- async package(): Promise<{|contents: string, map: ?SourceMap|}> {
137
- let wrappedAssets = await this.loadAssets();
141
+ async package(): Promise<{
142
+ contents: string;
143
+ map: SourceMap | null | undefined;
144
+ }> {
145
+ let {wrapped: wrappedAssets, constant: constantAssets} =
146
+ await this.loadAssets();
138
147
  this.buildExportedSymbols();
139
148
 
140
149
  // If building a library, the target is actually another bundler rather
@@ -155,9 +164,11 @@ export class ScopeHoistingPackager {
155
164
 
156
165
  let res = '';
157
166
  let lineCount = 0;
167
+ // @ts-expect-error TS7034
158
168
  let sourceMap = null;
159
- let processAsset = (asset) => {
169
+ let processAsset = (asset: Asset) => {
160
170
  let [content, map, lines] = this.visitAsset(asset);
171
+ // @ts-expect-error TS7005
161
172
  if (sourceMap && map) {
162
173
  sourceMap.addSourceMap(map, lineCount);
163
174
  } else if (this.bundle.env.sourceMap) {
@@ -168,6 +179,18 @@ export class ScopeHoistingPackager {
168
179
  lineCount += lines + 1;
169
180
  };
170
181
 
182
+ if (
183
+ getFeatureFlag('inlineConstOptimisationFix') ||
184
+ getFeatureFlag('applyScopeHoistingImprovement')
185
+ ) {
186
+ // Write out all constant modules used by this bundle
187
+ for (let asset of constantAssets) {
188
+ if (!this.seenAssets.has(asset.id)) {
189
+ processAsset(asset);
190
+ }
191
+ }
192
+ }
193
+
171
194
  // Hoist wrapped asset to the top of the bundle to ensure that they are registered
172
195
  // before they are used.
173
196
  for (let asset of wrappedAssets) {
@@ -191,14 +214,28 @@ export class ScopeHoistingPackager {
191
214
  let [prelude, preludeLines] = this.buildBundlePrelude();
192
215
  res = prelude + res;
193
216
  lineCount += preludeLines;
217
+ // @ts-expect-error TS2339
194
218
  sourceMap?.offsetLines(1, preludeLines);
195
219
 
196
220
  let entries = this.bundle.getEntryAssets();
197
221
  let mainEntry = this.bundle.getMainEntry();
198
222
  if (this.isAsyncBundle) {
199
- // In async bundles we don't want the main entry to execute until we require it
200
- // 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 (
224
+ getFeatureFlag('applyScopeHoistingImprovement') ||
225
+ getFeatureFlag('supportWebpackChunkName')
226
+ ) {
227
+ // Generally speaking, async bundles should not be executed on load, as
228
+ // they're just collections of assets that other assets require.
229
+ // However, there are some special cases where a runtime asset needs to be
230
+ // injected, but no other asset will require it (mostly the bundle
231
+ // manifest).
232
+ // In this case, those assets need to be required on load.
233
+ entries = entries.filter(
234
+ (a) => a.meta?.runtimeAssetRequiringExecutionOnLoad,
235
+ );
236
+ } else {
237
+ entries = entries.filter((a) => a.id !== mainEntry?.id);
238
+ }
202
239
  mainEntry = null;
203
240
  }
204
241
 
@@ -211,17 +248,20 @@ export class ScopeHoistingPackager {
211
248
  this.bundleGraph.getAssetPublicId(entry),
212
249
  )});\n`;
213
250
 
251
+ // @ts-expect-error TS2345
214
252
  let entryExports = entry.symbols.get('*')?.local;
215
253
 
216
254
  if (
217
255
  entryExports &&
218
256
  entry === mainEntry &&
257
+ // @ts-expect-error TS2345
219
258
  this.exportedSymbols.has(entryExports)
220
259
  ) {
221
260
  invariant(
222
261
  !needsBundleQueue,
223
262
  'Entry exports are not yet compaitble with async bundles',
224
263
  );
264
+ // @ts-expect-error TS2731
225
265
  res += `\nvar ${entryExports} = ${parcelRequire}`;
226
266
  } else {
227
267
  if (needsBundleQueue) {
@@ -265,6 +305,7 @@ export class ScopeHoistingPackager {
265
305
  this.parcelRequireName,
266
306
  );
267
307
  if (sourceMap && map) {
308
+ // @ts-expect-error TS2339
268
309
  sourceMap.addSourceMap(map, lineCount);
269
310
  }
270
311
  }
@@ -281,10 +322,7 @@ export class ScopeHoistingPackager {
281
322
 
282
323
  let hasConditionalReference = false;
283
324
  let isConditionalBundle = false;
284
- if (
285
- getFeatureFlag('conditionalBundlingApi') &&
286
- getFeatureFlag('conditionalBundlingAsyncRuntime')
287
- ) {
325
+ if (getFeatureFlag('conditionalBundlingApi')) {
288
326
  // If the bundle has a conditional bundle reference (has an importCond)
289
327
  hasConditionalReference =
290
328
  this.bundleGraph.getReferencedConditionalBundles(bundle).length > 0;
@@ -296,6 +334,7 @@ export class ScopeHoistingPackager {
296
334
  this.useAsyncBundleRuntime &&
297
335
  bundle.type === 'js' &&
298
336
  bundle.bundleBehavior !== 'inline' &&
337
+ bundle.bundleBehavior !== 'inlineIsolated' &&
299
338
  bundle.env.outputFormat === 'esmodule' &&
300
339
  !bundle.env.isIsolated() &&
301
340
  bundle.bundleBehavior !== 'isolated' &&
@@ -309,11 +348,8 @@ export class ScopeHoistingPackager {
309
348
  .filter((b) => this.shouldBundleQueue(b))
310
349
  .map((b) => b.publicId);
311
350
 
312
- const conditions = [];
313
- if (
314
- getFeatureFlag('conditionalBundlingApi') &&
315
- getFeatureFlag('conditionalBundlingAsyncRuntime')
316
- ) {
351
+ const conditions: Array<string> = [];
352
+ if (getFeatureFlag('conditionalBundlingApi')) {
317
353
  const conditionSet = this.bundleGraph
318
354
  .getConditionalBundleMapping()
319
355
  .get(bundle.id);
@@ -354,15 +390,21 @@ export class ScopeHoistingPackager {
354
390
  return `$parcel$global.rwr(${params.join(', ')});`;
355
391
  }
356
392
 
357
- async loadAssets(): Promise<Array<Asset>> {
393
+ async loadAssets(): Promise<{
394
+ wrapped: Array<Asset>;
395
+ constant: Array<Asset>;
396
+ }> {
358
397
  let queue = new PromiseQueue({maxConcurrent: 32});
359
- let wrapped = [];
398
+ let wrapped: Array<Asset> = [];
399
+ let constant: Array<Asset> = [];
400
+
360
401
  this.bundle.traverseAssets((asset) => {
361
402
  queue.add(async () => {
362
403
  let [code, map] = await Promise.all([
363
404
  asset.getCode(),
364
405
  this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
365
406
  ]);
407
+
366
408
  return [asset.id, {code, map}];
367
409
  });
368
410
 
@@ -383,50 +425,96 @@ export class ScopeHoistingPackager {
383
425
  ) {
384
426
  this.wrappedAssets.add(asset.id);
385
427
  wrapped.push(asset);
428
+ } else if (
429
+ (getFeatureFlag('inlineConstOptimisationFix') ||
430
+ getFeatureFlag('applyScopeHoistingImprovement')) &&
431
+ asset.meta.isConstantModule
432
+ ) {
433
+ constant.push(asset);
386
434
  }
387
435
  }
388
436
  });
389
437
 
390
- for (let wrappedAssetRoot of [...wrapped]) {
391
- this.bundle.traverseAssets((asset, _, actions) => {
392
- if (asset === wrappedAssetRoot) {
393
- return;
438
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
439
+ // Make all entry assets wrapped, to avoid any top level hoisting
440
+ for (let entryAsset of this.bundle.getEntryAssets()) {
441
+ if (!this.wrappedAssets.has(entryAsset.id)) {
442
+ this.wrappedAssets.add(entryAsset.id);
443
+ wrapped.push(entryAsset);
394
444
  }
445
+ }
395
446
 
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);
447
+ // Tracks which assets have been assigned to a wrap group
448
+ let assignedAssets = new Set<Asset>();
449
+
450
+ for (let wrappedAsset of wrapped) {
451
+ this.bundle.traverseAssets((asset, _, actions) => {
452
+ if (asset === wrappedAsset) {
453
+ return;
454
+ }
455
+
456
+ if (this.wrappedAssets.has(asset.id)) {
457
+ actions.skipChildren();
458
+ return;
459
+ }
460
+
461
+ if (
462
+ !asset.meta.isConstantModule &&
463
+ (assignedAssets.has(asset) || this.isReExported(asset))
464
+ ) {
465
+ wrapped.push(asset);
466
+ this.wrappedAssets.add(asset.id);
467
+
468
+ actions.skipChildren();
469
+ return;
470
+ }
471
+
472
+ assignedAssets.add(asset);
473
+ }, wrappedAsset);
474
+ }
475
+ } else {
476
+ for (let wrappedAssetRoot of [...wrapped]) {
477
+ this.bundle.traverseAssets((asset, _, actions) => {
478
+ if (asset === wrappedAssetRoot) {
479
+ return;
480
+ }
481
+
482
+ if (this.wrappedAssets.has(asset.id)) {
483
+ actions.skipChildren();
484
+ return;
485
+ }
486
+
487
+ if (!asset.meta.isConstantModule) {
488
+ this.wrappedAssets.add(asset.id);
489
+ wrapped.push(asset);
490
+ }
491
+ }, wrappedAssetRoot);
492
+ }
426
493
  }
427
494
 
495
+ // @ts-expect-error TS2769
428
496
  this.assetOutputs = new Map(await queue.run());
429
- return wrapped;
497
+ return {wrapped, constant};
498
+ }
499
+
500
+ isReExported(asset: Asset): boolean {
501
+ let parentSymbols = this.bundleGraph
502
+ .getIncomingDependencies(asset)
503
+ .map((dep) => this.bundleGraph.getAssetWithDependency(dep))
504
+ .flatMap((parent) => {
505
+ if (parent == null) {
506
+ return [];
507
+ }
508
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
509
+ });
510
+
511
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
512
+
513
+ return assetSymbols.some((assetSymbol) =>
514
+ parentSymbols.some(
515
+ (parentSymbol) => parentSymbol.symbol === assetSymbol.symbol,
516
+ ),
517
+ );
430
518
  }
431
519
 
432
520
  buildExportedSymbols() {
@@ -440,6 +528,7 @@ export class ScopeHoistingPackager {
440
528
  // TODO: handle ESM exports of wrapped entry assets...
441
529
  let entry = this.bundle.getMainEntry();
442
530
  if (entry && !this.wrappedAssets.has(entry.id)) {
531
+ // @ts-expect-error TS2345
443
532
  let hasNamespace = entry.symbols.hasExportSymbol('*');
444
533
 
445
534
  for (let {
@@ -457,6 +546,7 @@ export class ScopeHoistingPackager {
457
546
  }
458
547
 
459
548
  let symbols = this.exportedSymbols.get(
549
+ // @ts-expect-error TS2345
460
550
  symbol === '*' ? nullthrows(entry.symbols.get('*')?.local) : symbol,
461
551
  )?.exportAs;
462
552
 
@@ -464,6 +554,7 @@ export class ScopeHoistingPackager {
464
554
  symbols = [];
465
555
  this.exportedSymbols.set(symbol, {
466
556
  asset,
557
+ // @ts-expect-error TS2322
467
558
  exportSymbol,
468
559
  local: symbol,
469
560
  exportAs: symbols,
@@ -474,6 +565,7 @@ export class ScopeHoistingPackager {
474
565
  exportAs = 'default';
475
566
  }
476
567
 
568
+ // @ts-expect-error TS2345
477
569
  symbols.push(exportAs);
478
570
  } else if (symbol === null) {
479
571
  // TODO `meta.exportsIdentifier[exportSymbol]` should be exported
@@ -519,7 +611,7 @@ export class ScopeHoistingPackager {
519
611
  return `${obj}[${JSON.stringify(property)}]`;
520
612
  }
521
613
 
522
- visitAsset(asset: Asset): [string, ?SourceMap, number] {
614
+ visitAsset(asset: Asset): [string, SourceMap | null | undefined, number] {
523
615
  invariant(!this.seenAssets.has(asset.id), 'Already visited asset');
524
616
  this.seenAssets.add(asset.id);
525
617
 
@@ -527,11 +619,15 @@ export class ScopeHoistingPackager {
527
619
  return this.buildAsset(asset, code, map);
528
620
  }
529
621
 
622
+ getAssetFilePath(asset: Asset): string {
623
+ return path.relative(this.options.projectRoot, asset.filePath);
624
+ }
625
+
530
626
  buildAsset(
531
627
  asset: Asset,
532
628
  code: string,
533
- map: ?Buffer,
534
- ): [string, ?SourceMap, number] {
629
+ map?: Buffer | null,
630
+ ): [string, SourceMap | null | undefined, number] {
535
631
  let shouldWrap = this.wrappedAssets.has(asset.id);
536
632
  let deps = this.bundleGraph.getDependencies(asset);
537
633
 
@@ -566,6 +662,7 @@ export class ScopeHoistingPackager {
566
662
  let [code, map, lines] = this.visitAsset(resolved);
567
663
  depCode += code + '\n';
568
664
  if (sourceMap && map) {
665
+ // @ts-expect-error TS2551
569
666
  sourceMap.addSourceMap(map, lineCount);
570
667
  }
571
668
  lineCount += lines + 1;
@@ -602,7 +699,9 @@ export class ScopeHoistingPackager {
602
699
  code += append;
603
700
 
604
701
  let lineCount = 0;
605
- let depContent = [];
702
+ // @ts-expect-error TS2552
703
+ let depContent: Array<[string, NodeSourceMap | null | undefined, number]> =
704
+ [];
606
705
  if (depMap.size === 0 && replacements.size === 0) {
607
706
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
608
707
  lineCount = countLines(code) - 1;
@@ -661,13 +760,38 @@ export class ScopeHoistingPackager {
661
760
  // outside our parcelRequire.register wrapper. This is safe because all
662
761
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
663
762
  // asset content where the import statement was.
664
- if (shouldWrap) {
665
- depContent.push(this.visitAsset(resolved));
763
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
764
+ if (
765
+ !resolved.meta.isConstantModule &&
766
+ !this.wrappedAssets.has(resolved.id)
767
+ ) {
768
+ let [depCode, depMap, depLines] =
769
+ this.visitAsset(resolved);
770
+ if (debugTools['asset-file-names-in-output']) {
771
+ let resolvedPath = this.getAssetFilePath(resolved);
772
+ res = outdent`
773
+ /* Scope hoisted asset: ${resolvedPath} */
774
+ ${depCode}
775
+ /* End: ${resolvedPath} */
776
+ ${res}
777
+ `;
778
+ lines += 3 + depLines;
779
+ } else {
780
+ res = depCode + '\n' + res;
781
+ lines += 1 + depLines;
782
+ }
783
+ map = depMap;
784
+ }
666
785
  } else {
667
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
668
- res = depCode + '\n' + res;
669
- lines += 1 + depLines;
670
- map = depMap;
786
+ if (shouldWrap) {
787
+ depContent.push(this.visitAsset(resolved));
788
+ } else {
789
+ let [depCode, depMap, depLines] =
790
+ this.visitAsset(resolved);
791
+ res = depCode + '\n' + res;
792
+ lines += 1 + depLines;
793
+ map = depMap;
794
+ }
671
795
  }
672
796
  }
673
797
 
@@ -679,6 +803,7 @@ export class ScopeHoistingPackager {
679
803
  }
680
804
 
681
805
  if (map) {
806
+ // @ts-expect-error TS2551
682
807
  sourceMap.addSourceMap(map, lineCount);
683
808
  }
684
809
  }
@@ -726,10 +851,16 @@ ${code}
726
851
 
727
852
  lineCount += 2;
728
853
 
854
+ if (debugTools['asset-file-names-in-output']) {
855
+ code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
856
+ lineCount += 1;
857
+ }
858
+
729
859
  for (let [depCode, map, lines] of depContent) {
730
860
  if (!depCode) continue;
731
861
  code += depCode + '\n';
732
862
  if (sourceMap && map) {
863
+ // @ts-expect-error TS2551
733
864
  sourceMap.addSourceMap(map, lineCount);
734
865
  }
735
866
  lineCount += lines + 1;
@@ -812,10 +943,12 @@ ${code}
812
943
  }
813
944
 
814
945
  for (let [imported, {local}] of dep.symbols) {
946
+ // @ts-expect-error TS2367
815
947
  if (local === '*') {
816
948
  continue;
817
949
  }
818
950
 
951
+ // @ts-expect-error TS2345
819
952
  let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
820
953
  replacements.set(
821
954
  local,
@@ -852,6 +985,7 @@ ${code}
852
985
  (this.bundle.env.outputFormat === 'commonjs' &&
853
986
  asset === this.bundle.getMainEntry())
854
987
  ) {
988
+ // @ts-expect-error TS2345
855
989
  let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;
856
990
  replacements.set(exportsName, 'module.exports');
857
991
  }
@@ -895,8 +1029,11 @@ ${code}
895
1029
 
896
1030
  for (let [imported, {local}] of dep.symbols) {
897
1031
  // If already imported, just add the already renamed variable to the mapping.
1032
+ // @ts-expect-error TS2345
898
1033
  let renamed = external.get(imported);
1034
+ // @ts-expect-error TS2367
899
1035
  if (renamed && local !== '*' && replacements) {
1036
+ // @ts-expect-error TS2345
900
1037
  replacements.set(local, renamed);
901
1038
  continue;
902
1039
  }
@@ -908,7 +1045,9 @@ ${code}
908
1045
  if (!renamed) {
909
1046
  if (referencedBundle) {
910
1047
  let entry = nullthrows(referencedBundle.getMainEntry());
1048
+ // @ts-expect-error TS2322
911
1049
  renamed =
1050
+ // @ts-expect-error TS2345
912
1051
  entry.symbols.get('*')?.local ??
913
1052
  `$${String(entry.meta.id)}$exports`;
914
1053
  } else {
@@ -917,13 +1056,17 @@ ${code}
917
1056
  );
918
1057
  }
919
1058
 
1059
+ // @ts-expect-error TS2345
920
1060
  external.set('*', renamed);
921
1061
  }
922
1062
 
1063
+ // @ts-expect-error TS2367
923
1064
  if (local !== '*' && replacements) {
924
1065
  let replacement;
1066
+ // @ts-expect-error TS2367
925
1067
  if (imported === '*') {
926
1068
  replacement = renamed;
1069
+ // @ts-expect-error TS2367
927
1070
  } else if (imported === 'default') {
928
1071
  let needsDefaultInterop = true;
929
1072
  if (referencedBundle) {
@@ -937,36 +1080,44 @@ ${code}
937
1080
  replacement = `${renamed}.default`;
938
1081
  }
939
1082
  } else {
1083
+ // @ts-expect-error TS2345
940
1084
  replacement = this.getPropertyAccess(renamed, imported);
941
1085
  }
942
1086
 
1087
+ // @ts-expect-error TS2345
943
1088
  replacements.set(local, replacement);
944
1089
  }
945
1090
  } else {
946
1091
  let property;
947
1092
  if (referencedBundle) {
948
1093
  let entry = nullthrows(referencedBundle.getMainEntry());
1094
+ // @ts-expect-error TS2345
949
1095
  if (entry.symbols.hasExportSymbol('*')) {
950
1096
  // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
951
1097
  // This mirrors the logic in buildExportedSymbols.
952
1098
  property = imported;
1099
+ // @ts-expect-error TS2322
953
1100
  imported =
954
1101
  referencedBundle?.env.outputFormat === 'esmodule'
955
1102
  ? 'default'
956
1103
  : '*';
957
1104
  } else {
1105
+ // @ts-expect-error TS2367
958
1106
  if (imported === '*') {
959
1107
  let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
1108
+ // @ts-expect-error TS2367
960
1109
  if (local === '*') {
961
1110
  // Re-export all symbols.
962
1111
  for (let exported of exportedSymbols) {
963
1112
  if (exported.symbol) {
1113
+ // @ts-expect-error TS2345
964
1114
  external.set(exported.exportSymbol, exported.symbol);
965
1115
  }
966
1116
  }
967
1117
  continue;
968
1118
  }
969
1119
  }
1120
+ // @ts-expect-error TS2322
970
1121
  renamed = this.bundleGraph.getSymbolResolution(
971
1122
  entry,
972
1123
  imported,
@@ -979,30 +1130,40 @@ ${code}
979
1130
  // are deduplicated. We have to prefix the imported name with the bundle id so that
980
1131
  // local variables do not shadow it.
981
1132
  if (!renamed) {
1133
+ // @ts-expect-error TS2345
982
1134
  if (this.exportedSymbols.has(local)) {
1135
+ // @ts-expect-error TS2322
983
1136
  renamed = local;
1137
+ // @ts-expect-error TS2367
984
1138
  } else if (imported === 'default' || imported === '*') {
985
1139
  renamed = this.getTopLevelName(
986
1140
  `$${this.bundle.publicId}$${specifier}`,
987
1141
  );
988
1142
  } else {
989
1143
  renamed = this.getTopLevelName(
1144
+ // @ts-expect-error TS2731
990
1145
  `$${this.bundle.publicId}$${imported}`,
991
1146
  );
992
1147
  }
993
1148
  }
994
1149
 
1150
+ // @ts-expect-error TS2345
995
1151
  external.set(imported, renamed);
1152
+ // @ts-expect-error TS2367
996
1153
  if (local !== '*' && replacements) {
997
1154
  let replacement = renamed;
1155
+ // @ts-expect-error TS2367
998
1156
  if (property === '*') {
999
1157
  replacement = renamed;
1158
+ // @ts-expect-error TS2367
1000
1159
  } else if (property === 'default') {
1001
1160
  replacement = `($parcel$interopDefault(${renamed}))`;
1002
1161
  this.usedHelpers.add('$parcel$interopDefault');
1003
1162
  } else if (property) {
1163
+ // @ts-expect-error TS2345
1004
1164
  replacement = this.getPropertyAccess(renamed, property);
1005
1165
  }
1166
+ // @ts-expect-error TS2345
1006
1167
  replacements.set(local, replacement);
1007
1168
  }
1008
1169
  }
@@ -1041,6 +1202,7 @@ ${code}
1041
1202
  asset: resolvedAsset,
1042
1203
  exportSymbol,
1043
1204
  symbol,
1205
+ // @ts-expect-error TS2345
1044
1206
  } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
1045
1207
 
1046
1208
  if (
@@ -1099,8 +1261,11 @@ ${code}
1099
1261
  staticExports &&
1100
1262
  !isWrapped &&
1101
1263
  (dep?.meta.kind === 'Import' || dep?.meta.kind === 'Export') &&
1264
+ // @ts-expect-error TS2345
1102
1265
  resolvedAsset.symbols.hasExportSymbol('*') &&
1266
+ // @ts-expect-error TS2345
1103
1267
  resolvedAsset.symbols.hasExportSymbol('default') &&
1268
+ // @ts-expect-error TS2345
1104
1269
  !resolvedAsset.symbols.hasExportSymbol('__esModule');
1105
1270
 
1106
1271
  // Find the namespace object for the resolved module. If wrapped and this
@@ -1116,7 +1281,9 @@ ${code}
1116
1281
  } else if (isWrapped && dep) {
1117
1282
  obj = `$${publicId}`;
1118
1283
  } else {
1284
+ // @ts-expect-error TS2345
1119
1285
  obj = resolvedAsset.symbols.get('*')?.local || `$${assetId}$exports`;
1286
+ // @ts-expect-error TS2345
1120
1287
  obj = replacements?.get(obj) || obj;
1121
1288
  }
1122
1289
 
@@ -1129,6 +1296,7 @@ ${code}
1129
1296
  // Directly use module.exports for wrapped assets importing themselves.
1130
1297
  return 'module.exports';
1131
1298
  } else {
1299
+ // @ts-expect-error TS2322
1132
1300
  return obj;
1133
1301
  }
1134
1302
  } else if (
@@ -1143,17 +1311,21 @@ ${code}
1143
1311
  if (
1144
1312
  (!dep || kind === 'Import' || kind === 'Export') &&
1145
1313
  exportSymbol === 'default' &&
1314
+ // @ts-expect-error TS2345
1146
1315
  resolvedAsset.symbols.hasExportSymbol('*') &&
1147
1316
  this.needsDefaultInterop(resolvedAsset)
1148
1317
  ) {
1149
1318
  this.usedHelpers.add('$parcel$interopDefault');
1319
+ // @ts-expect-error TS2731
1150
1320
  return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
1151
1321
  } else {
1322
+ // @ts-expect-error TS2345
1152
1323
  return this.getPropertyAccess(obj, exportSymbol);
1153
1324
  }
1154
1325
  } else if (!symbol) {
1155
1326
  invariant(false, 'Asset was skipped or not found.');
1156
1327
  } else {
1328
+ // @ts-expect-error TS2322
1157
1329
  return replacements?.get(symbol) || symbol;
1158
1330
  }
1159
1331
  }
@@ -1216,38 +1388,56 @@ ${code}
1216
1388
  // If there's no __esModule flag, and default is a used symbol, we need
1217
1389
  // to insert an interop helper.
1218
1390
  let defaultInterop =
1391
+ // @ts-expect-error TS2345
1219
1392
  asset.symbols.hasExportSymbol('*') &&
1393
+ // @ts-expect-error TS2345
1220
1394
  usedSymbols.has('default') &&
1395
+ // @ts-expect-error TS2345
1221
1396
  !asset.symbols.hasExportSymbol('__esModule');
1222
1397
 
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(
1398
+ let usedNamespace;
1399
+ if (
1400
+ getFeatureFlag('inlineConstOptimisationFix') &&
1401
+ asset.meta.isConstantModule
1402
+ ) {
1403
+ // Only set usedNamespace if there is an incoming dependency in the current bundle that uses '*'
1404
+ usedNamespace = this.bundleGraph.getIncomingDependencies(asset).some(
1405
+ (dep) =>
1406
+ this.bundle.hasDependency(dep) &&
1407
+ // @ts-expect-error TS2345
1408
+ nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1409
+ );
1410
+ } else {
1411
+ usedNamespace =
1412
+ // If the asset has * in its used symbols, we might need the exports namespace.
1413
+ // The one case where this isn't true is in ESM library entries, where the only
1414
+ // dependency on * is the entry dependency. In this case, we will use ESM exports
1415
+ // instead of the namespace object.
1416
+ // @ts-expect-error TS2345
1417
+ (usedSymbols.has('*') &&
1418
+ (this.bundle.env.outputFormat !== 'esmodule' ||
1419
+ !this.bundle.env.isLibrary ||
1420
+ asset !== this.bundle.getMainEntry() ||
1421
+ this.bundleGraph.getIncomingDependencies(asset).some(
1235
1422
  (dep) =>
1236
1423
  !dep.isEntry &&
1237
1424
  this.bundle.hasDependency(dep) &&
1425
+ // @ts-expect-error TS2345
1238
1426
  nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1239
1427
  ))) ||
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());
1428
+ // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
1429
+ // we fallback on the namespace object.
1430
+ // @ts-expect-error TS2345
1431
+ (asset.symbols.hasExportSymbol('*') &&
1432
+ [...usedSymbols].some((s) => !asset.symbols.hasExportSymbol(s))) ||
1433
+ // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1434
+ // include the namespace object for the default export.
1435
+ this.exportedSymbols.has(`$${assetId}$exports`) ||
1436
+ // CommonJS library bundle entries always need a namespace.
1437
+ (this.bundle.env.isLibrary &&
1438
+ this.bundle.env.outputFormat === 'commonjs' &&
1439
+ asset === this.bundle.getMainEntry());
1440
+ }
1251
1441
 
1252
1442
  // If the asset doesn't have static exports, should wrap, the namespace is used,
1253
1443
  // or we need default interop, then we need to synthesize a namespace object for
@@ -1274,6 +1464,7 @@ ${code}
1274
1464
  // Insert the __esModule interop flag for this module if it has a `default` export
1275
1465
  // and the namespace symbol is used.
1276
1466
  // TODO: only if required by CJS?
1467
+ // @ts-expect-error TS2345
1277
1468
  if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
1278
1469
  prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
1279
1470
  prependLineCount += 2;
@@ -1292,6 +1483,7 @@ ${code}
1292
1483
  let isWrapped = resolved && resolved.meta.shouldWrap;
1293
1484
 
1294
1485
  for (let [imported, {local}] of dep.symbols) {
1486
+ // @ts-expect-error TS2367
1295
1487
  if (imported === '*' && local === '*') {
1296
1488
  if (!resolved) {
1297
1489
  // Re-exporting an external module. This should have already been handled in buildReplacements.
@@ -1309,9 +1501,11 @@ ${code}
1309
1501
  if (
1310
1502
  isWrapped ||
1311
1503
  resolved.meta.staticExports === false ||
1504
+ // @ts-expect-error TS2345
1312
1505
  nullthrows(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
1313
1506
  // an empty asset
1314
1507
  (!resolved.meta.hasCJSExports &&
1508
+ // @ts-expect-error TS2345
1315
1509
  resolved.symbols.hasExportSymbol('*'))
1316
1510
  ) {
1317
1511
  let obj = this.getSymbolResolution(
@@ -1328,7 +1522,9 @@ ${code}
1328
1522
  this.bundleGraph.getUsedSymbols(dep),
1329
1523
  )) {
1330
1524
  if (
1525
+ // @ts-expect-error TS2367
1331
1526
  symbol === 'default' || // `export * as ...` does not include the default export
1527
+ // @ts-expect-error TS2367
1332
1528
  symbol === '__esModule'
1333
1529
  ) {
1334
1530
  continue;
@@ -1337,6 +1533,7 @@ ${code}
1337
1533
  let resolvedSymbol = this.getSymbolResolution(
1338
1534
  asset,
1339
1535
  resolved,
1536
+ // @ts-expect-error TS2345
1340
1537
  symbol,
1341
1538
  undefined,
1342
1539
  replacements,
@@ -1362,6 +1559,7 @@ ${code}
1362
1559
  // re-exported symbols rather than only symbols declared in this asset.
1363
1560
  let incomingDeps = this.bundleGraph.getIncomingDependencies(asset);
1364
1561
  let usedExports = [...asset.symbols.exportSymbols()].filter((symbol) => {
1562
+ // @ts-expect-error TS2367
1365
1563
  if (symbol === '*') {
1366
1564
  return false;
1367
1565
  }
@@ -1378,6 +1576,7 @@ ${code}
1378
1576
  // No used symbols available for the asset, make sure we keep all of them
1379
1577
  if (!symbols) return false;
1380
1578
 
1579
+ // @ts-expect-error TS2345
1381
1580
  return !symbols.has(symbol) && !symbols.has('*');
1382
1581
  });
1383
1582
  return !unused;
@@ -1393,6 +1592,7 @@ ${code}
1393
1592
  let resolved = this.getSymbolResolution(
1394
1593
  asset,
1395
1594
  asset,
1595
+ // @ts-expect-error TS2345
1396
1596
  exp,
1397
1597
  undefined,
1398
1598
  replacements,
@@ -1448,8 +1648,10 @@ ${code}
1448
1648
  }
1449
1649
 
1450
1650
  for (let helper of this.usedHelpers) {
1651
+ // @ts-expect-error TS7053
1451
1652
  let currentHelper = helpers[helper];
1452
1653
  if (typeof currentHelper === 'function') {
1654
+ // @ts-expect-error TS7053
1453
1655
  currentHelper = helpers[helper](this.bundle.env);
1454
1656
  }
1455
1657
  res += currentHelper;
@@ -1470,6 +1672,7 @@ ${code}
1470
1672
  .some((g) => this.bundleGraph.isEntryBundleGroup(g)) ||
1471
1673
  this.bundle.env.isIsolated() ||
1472
1674
  this.bundle.bundleBehavior === 'isolated' ||
1675
+ this.bundle.bundleBehavior === 'inlineIsolated' ||
1473
1676
  // Conditional deps may be loaded before entrypoints on the server
1474
1677
  this.hasConditionalDependency();
1475
1678
 
@@ -1498,7 +1701,11 @@ ${code}
1498
1701
  }
1499
1702
 
1500
1703
  // Add importScripts for sibling bundles in workers.
1501
- if (this.bundle.env.isWorker() || this.bundle.env.isWorklet()) {
1704
+ if (
1705
+ this.bundle.env.isWorker() ||
1706
+ this.bundle.env.isTesseract() ||
1707
+ this.bundle.env.isWorklet()
1708
+ ) {
1502
1709
  let importScripts = '';
1503
1710
  let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
1504
1711
  for (let b of bundles) {
@@ -1522,7 +1729,9 @@ ${code}
1522
1729
 
1523
1730
  needsDefaultInterop(asset: Asset): boolean {
1524
1731
  if (
1732
+ // @ts-expect-error TS2345
1525
1733
  asset.symbols.hasExportSymbol('*') &&
1734
+ // @ts-expect-error TS2345
1526
1735
  !asset.symbols.hasExportSymbol('default')
1527
1736
  ) {
1528
1737
  return true;