@atlaspack/packager-js 2.14.5-canary.16 → 2.14.5-canary.160

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,6 +214,7 @@ 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();
@@ -198,7 +222,11 @@ export class ScopeHoistingPackager {
198
222
  if (this.isAsyncBundle) {
199
223
  // In async bundles we don't want the main entry to execute until we require it
200
224
  // as there might be dependencies in a sibling bundle that hasn't loaded yet.
201
- entries = entries.filter((a) => a.id !== mainEntry?.id);
225
+ if (getFeatureFlag('supportWebpackChunkName')) {
226
+ entries = [];
227
+ } else {
228
+ entries = entries.filter((a) => a.id !== mainEntry?.id);
229
+ }
202
230
  mainEntry = null;
203
231
  }
204
232
 
@@ -211,17 +239,20 @@ export class ScopeHoistingPackager {
211
239
  this.bundleGraph.getAssetPublicId(entry),
212
240
  )});\n`;
213
241
 
242
+ // @ts-expect-error TS2345
214
243
  let entryExports = entry.symbols.get('*')?.local;
215
244
 
216
245
  if (
217
246
  entryExports &&
218
247
  entry === mainEntry &&
248
+ // @ts-expect-error TS2345
219
249
  this.exportedSymbols.has(entryExports)
220
250
  ) {
221
251
  invariant(
222
252
  !needsBundleQueue,
223
253
  'Entry exports are not yet compaitble with async bundles',
224
254
  );
255
+ // @ts-expect-error TS2731
225
256
  res += `\nvar ${entryExports} = ${parcelRequire}`;
226
257
  } else {
227
258
  if (needsBundleQueue) {
@@ -265,6 +296,7 @@ export class ScopeHoistingPackager {
265
296
  this.parcelRequireName,
266
297
  );
267
298
  if (sourceMap && map) {
299
+ // @ts-expect-error TS2339
268
300
  sourceMap.addSourceMap(map, lineCount);
269
301
  }
270
302
  }
@@ -281,10 +313,7 @@ export class ScopeHoistingPackager {
281
313
 
282
314
  let hasConditionalReference = false;
283
315
  let isConditionalBundle = false;
284
- if (
285
- getFeatureFlag('conditionalBundlingApi') &&
286
- getFeatureFlag('conditionalBundlingAsyncRuntime')
287
- ) {
316
+ if (getFeatureFlag('conditionalBundlingApi')) {
288
317
  // If the bundle has a conditional bundle reference (has an importCond)
289
318
  hasConditionalReference =
290
319
  this.bundleGraph.getReferencedConditionalBundles(bundle).length > 0;
@@ -296,6 +325,7 @@ export class ScopeHoistingPackager {
296
325
  this.useAsyncBundleRuntime &&
297
326
  bundle.type === 'js' &&
298
327
  bundle.bundleBehavior !== 'inline' &&
328
+ bundle.bundleBehavior !== 'inlineIsolated' &&
299
329
  bundle.env.outputFormat === 'esmodule' &&
300
330
  !bundle.env.isIsolated() &&
301
331
  bundle.bundleBehavior !== 'isolated' &&
@@ -309,11 +339,8 @@ export class ScopeHoistingPackager {
309
339
  .filter((b) => this.shouldBundleQueue(b))
310
340
  .map((b) => b.publicId);
311
341
 
312
- const conditions = [];
313
- if (
314
- getFeatureFlag('conditionalBundlingApi') &&
315
- getFeatureFlag('conditionalBundlingAsyncRuntime')
316
- ) {
342
+ const conditions: Array<string> = [];
343
+ if (getFeatureFlag('conditionalBundlingApi')) {
317
344
  const conditionSet = this.bundleGraph
318
345
  .getConditionalBundleMapping()
319
346
  .get(bundle.id);
@@ -354,15 +381,21 @@ export class ScopeHoistingPackager {
354
381
  return `$parcel$global.rwr(${params.join(', ')});`;
355
382
  }
356
383
 
357
- async loadAssets(): Promise<Array<Asset>> {
384
+ async loadAssets(): Promise<{
385
+ wrapped: Array<Asset>;
386
+ constant: Array<Asset>;
387
+ }> {
358
388
  let queue = new PromiseQueue({maxConcurrent: 32});
359
- let wrapped = [];
389
+ let wrapped: Array<Asset> = [];
390
+ let constant: Array<Asset> = [];
391
+
360
392
  this.bundle.traverseAssets((asset) => {
361
393
  queue.add(async () => {
362
394
  let [code, map] = await Promise.all([
363
395
  asset.getCode(),
364
396
  this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
365
397
  ]);
398
+
366
399
  return [asset.id, {code, map}];
367
400
  });
368
401
 
@@ -383,50 +416,96 @@ export class ScopeHoistingPackager {
383
416
  ) {
384
417
  this.wrappedAssets.add(asset.id);
385
418
  wrapped.push(asset);
419
+ } else if (
420
+ (getFeatureFlag('inlineConstOptimisationFix') ||
421
+ getFeatureFlag('applyScopeHoistingImprovement')) &&
422
+ asset.meta.isConstantModule
423
+ ) {
424
+ constant.push(asset);
386
425
  }
387
426
  }
388
427
  });
389
428
 
390
- for (let wrappedAssetRoot of [...wrapped]) {
391
- this.bundle.traverseAssets((asset, _, actions) => {
392
- if (asset === wrappedAssetRoot) {
393
- return;
429
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
430
+ // Make all entry assets wrapped, to avoid any top level hoisting
431
+ for (let entryAsset of this.bundle.getEntryAssets()) {
432
+ if (!this.wrappedAssets.has(entryAsset.id)) {
433
+ this.wrappedAssets.add(entryAsset.id);
434
+ wrapped.push(entryAsset);
394
435
  }
436
+ }
395
437
 
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);
438
+ // Tracks which assets have been assigned to a wrap group
439
+ let assignedAssets = new Set<Asset>();
440
+
441
+ for (let wrappedAsset of wrapped) {
442
+ this.bundle.traverseAssets((asset, _, actions) => {
443
+ if (asset === wrappedAsset) {
444
+ return;
445
+ }
446
+
447
+ if (this.wrappedAssets.has(asset.id)) {
448
+ actions.skipChildren();
449
+ return;
450
+ }
451
+
452
+ if (
453
+ !asset.meta.isConstantModule &&
454
+ (assignedAssets.has(asset) || this.isReExported(asset))
455
+ ) {
456
+ wrapped.push(asset);
457
+ this.wrappedAssets.add(asset.id);
458
+
459
+ actions.skipChildren();
460
+ return;
461
+ }
462
+
463
+ assignedAssets.add(asset);
464
+ }, wrappedAsset);
465
+ }
466
+ } else {
467
+ for (let wrappedAssetRoot of [...wrapped]) {
468
+ this.bundle.traverseAssets((asset, _, actions) => {
469
+ if (asset === wrappedAssetRoot) {
470
+ return;
471
+ }
472
+
473
+ if (this.wrappedAssets.has(asset.id)) {
474
+ actions.skipChildren();
475
+ return;
476
+ }
477
+
478
+ if (!asset.meta.isConstantModule) {
479
+ this.wrappedAssets.add(asset.id);
480
+ wrapped.push(asset);
481
+ }
482
+ }, wrappedAssetRoot);
483
+ }
426
484
  }
427
485
 
486
+ // @ts-expect-error TS2769
428
487
  this.assetOutputs = new Map(await queue.run());
429
- return wrapped;
488
+ return {wrapped, constant};
489
+ }
490
+
491
+ isReExported(asset: Asset): boolean {
492
+ let parentSymbols = this.bundleGraph
493
+ .getIncomingDependencies(asset)
494
+ .map((dep) => this.bundleGraph.getAssetWithDependency(dep))
495
+ .flatMap((parent) => {
496
+ if (parent == null) {
497
+ return [];
498
+ }
499
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
500
+ });
501
+
502
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
503
+
504
+ return assetSymbols.some((assetSymbol) =>
505
+ parentSymbols.some(
506
+ (parentSymbol) => parentSymbol.symbol === assetSymbol.symbol,
507
+ ),
508
+ );
430
509
  }
431
510
 
432
511
  buildExportedSymbols() {
@@ -440,6 +519,7 @@ export class ScopeHoistingPackager {
440
519
  // TODO: handle ESM exports of wrapped entry assets...
441
520
  let entry = this.bundle.getMainEntry();
442
521
  if (entry && !this.wrappedAssets.has(entry.id)) {
522
+ // @ts-expect-error TS2345
443
523
  let hasNamespace = entry.symbols.hasExportSymbol('*');
444
524
 
445
525
  for (let {
@@ -457,6 +537,7 @@ export class ScopeHoistingPackager {
457
537
  }
458
538
 
459
539
  let symbols = this.exportedSymbols.get(
540
+ // @ts-expect-error TS2345
460
541
  symbol === '*' ? nullthrows(entry.symbols.get('*')?.local) : symbol,
461
542
  )?.exportAs;
462
543
 
@@ -464,6 +545,7 @@ export class ScopeHoistingPackager {
464
545
  symbols = [];
465
546
  this.exportedSymbols.set(symbol, {
466
547
  asset,
548
+ // @ts-expect-error TS2322
467
549
  exportSymbol,
468
550
  local: symbol,
469
551
  exportAs: symbols,
@@ -474,6 +556,7 @@ export class ScopeHoistingPackager {
474
556
  exportAs = 'default';
475
557
  }
476
558
 
559
+ // @ts-expect-error TS2345
477
560
  symbols.push(exportAs);
478
561
  } else if (symbol === null) {
479
562
  // TODO `meta.exportsIdentifier[exportSymbol]` should be exported
@@ -519,7 +602,7 @@ export class ScopeHoistingPackager {
519
602
  return `${obj}[${JSON.stringify(property)}]`;
520
603
  }
521
604
 
522
- visitAsset(asset: Asset): [string, ?SourceMap, number] {
605
+ visitAsset(asset: Asset): [string, SourceMap | null | undefined, number] {
523
606
  invariant(!this.seenAssets.has(asset.id), 'Already visited asset');
524
607
  this.seenAssets.add(asset.id);
525
608
 
@@ -527,11 +610,15 @@ export class ScopeHoistingPackager {
527
610
  return this.buildAsset(asset, code, map);
528
611
  }
529
612
 
613
+ getAssetFilePath(asset: Asset): string {
614
+ return path.relative(this.options.projectRoot, asset.filePath);
615
+ }
616
+
530
617
  buildAsset(
531
618
  asset: Asset,
532
619
  code: string,
533
- map: ?Buffer,
534
- ): [string, ?SourceMap, number] {
620
+ map?: Buffer | null,
621
+ ): [string, SourceMap | null | undefined, number] {
535
622
  let shouldWrap = this.wrappedAssets.has(asset.id);
536
623
  let deps = this.bundleGraph.getDependencies(asset);
537
624
 
@@ -566,6 +653,7 @@ export class ScopeHoistingPackager {
566
653
  let [code, map, lines] = this.visitAsset(resolved);
567
654
  depCode += code + '\n';
568
655
  if (sourceMap && map) {
656
+ // @ts-expect-error TS2551
569
657
  sourceMap.addSourceMap(map, lineCount);
570
658
  }
571
659
  lineCount += lines + 1;
@@ -602,7 +690,9 @@ export class ScopeHoistingPackager {
602
690
  code += append;
603
691
 
604
692
  let lineCount = 0;
605
- let depContent = [];
693
+ // @ts-expect-error TS2552
694
+ let depContent: Array<[string, NodeSourceMap | null | undefined, number]> =
695
+ [];
606
696
  if (depMap.size === 0 && replacements.size === 0) {
607
697
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
608
698
  lineCount = countLines(code) - 1;
@@ -661,13 +751,38 @@ export class ScopeHoistingPackager {
661
751
  // outside our parcelRequire.register wrapper. This is safe because all
662
752
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
663
753
  // asset content where the import statement was.
664
- if (shouldWrap) {
665
- depContent.push(this.visitAsset(resolved));
754
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
755
+ if (
756
+ !resolved.meta.isConstantModule &&
757
+ !this.wrappedAssets.has(resolved.id)
758
+ ) {
759
+ let [depCode, depMap, depLines] =
760
+ this.visitAsset(resolved);
761
+ if (debugTools['asset-file-names-in-output']) {
762
+ let resolvedPath = this.getAssetFilePath(resolved);
763
+ res = outdent`
764
+ /* Scope hoisted asset: ${resolvedPath} */
765
+ ${depCode}
766
+ /* End: ${resolvedPath} */
767
+ ${res}
768
+ `;
769
+ lines += 3 + depLines;
770
+ } else {
771
+ res = depCode + '\n' + res;
772
+ lines += 1 + depLines;
773
+ }
774
+ map = depMap;
775
+ }
666
776
  } else {
667
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
668
- res = depCode + '\n' + res;
669
- lines += 1 + depLines;
670
- map = depMap;
777
+ if (shouldWrap) {
778
+ depContent.push(this.visitAsset(resolved));
779
+ } else {
780
+ let [depCode, depMap, depLines] =
781
+ this.visitAsset(resolved);
782
+ res = depCode + '\n' + res;
783
+ lines += 1 + depLines;
784
+ map = depMap;
785
+ }
671
786
  }
672
787
  }
673
788
 
@@ -679,6 +794,7 @@ export class ScopeHoistingPackager {
679
794
  }
680
795
 
681
796
  if (map) {
797
+ // @ts-expect-error TS2551
682
798
  sourceMap.addSourceMap(map, lineCount);
683
799
  }
684
800
  }
@@ -726,10 +842,16 @@ ${code}
726
842
 
727
843
  lineCount += 2;
728
844
 
845
+ if (debugTools['asset-file-names-in-output']) {
846
+ code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
847
+ lineCount += 1;
848
+ }
849
+
729
850
  for (let [depCode, map, lines] of depContent) {
730
851
  if (!depCode) continue;
731
852
  code += depCode + '\n';
732
853
  if (sourceMap && map) {
854
+ // @ts-expect-error TS2551
733
855
  sourceMap.addSourceMap(map, lineCount);
734
856
  }
735
857
  lineCount += lines + 1;
@@ -812,10 +934,12 @@ ${code}
812
934
  }
813
935
 
814
936
  for (let [imported, {local}] of dep.symbols) {
937
+ // @ts-expect-error TS2367
815
938
  if (local === '*') {
816
939
  continue;
817
940
  }
818
941
 
942
+ // @ts-expect-error TS2345
819
943
  let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
820
944
  replacements.set(
821
945
  local,
@@ -852,6 +976,7 @@ ${code}
852
976
  (this.bundle.env.outputFormat === 'commonjs' &&
853
977
  asset === this.bundle.getMainEntry())
854
978
  ) {
979
+ // @ts-expect-error TS2345
855
980
  let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;
856
981
  replacements.set(exportsName, 'module.exports');
857
982
  }
@@ -895,8 +1020,11 @@ ${code}
895
1020
 
896
1021
  for (let [imported, {local}] of dep.symbols) {
897
1022
  // If already imported, just add the already renamed variable to the mapping.
1023
+ // @ts-expect-error TS2345
898
1024
  let renamed = external.get(imported);
1025
+ // @ts-expect-error TS2367
899
1026
  if (renamed && local !== '*' && replacements) {
1027
+ // @ts-expect-error TS2345
900
1028
  replacements.set(local, renamed);
901
1029
  continue;
902
1030
  }
@@ -908,7 +1036,9 @@ ${code}
908
1036
  if (!renamed) {
909
1037
  if (referencedBundle) {
910
1038
  let entry = nullthrows(referencedBundle.getMainEntry());
1039
+ // @ts-expect-error TS2322
911
1040
  renamed =
1041
+ // @ts-expect-error TS2345
912
1042
  entry.symbols.get('*')?.local ??
913
1043
  `$${String(entry.meta.id)}$exports`;
914
1044
  } else {
@@ -917,13 +1047,17 @@ ${code}
917
1047
  );
918
1048
  }
919
1049
 
1050
+ // @ts-expect-error TS2345
920
1051
  external.set('*', renamed);
921
1052
  }
922
1053
 
1054
+ // @ts-expect-error TS2367
923
1055
  if (local !== '*' && replacements) {
924
1056
  let replacement;
1057
+ // @ts-expect-error TS2367
925
1058
  if (imported === '*') {
926
1059
  replacement = renamed;
1060
+ // @ts-expect-error TS2367
927
1061
  } else if (imported === 'default') {
928
1062
  let needsDefaultInterop = true;
929
1063
  if (referencedBundle) {
@@ -937,36 +1071,44 @@ ${code}
937
1071
  replacement = `${renamed}.default`;
938
1072
  }
939
1073
  } else {
1074
+ // @ts-expect-error TS2345
940
1075
  replacement = this.getPropertyAccess(renamed, imported);
941
1076
  }
942
1077
 
1078
+ // @ts-expect-error TS2345
943
1079
  replacements.set(local, replacement);
944
1080
  }
945
1081
  } else {
946
1082
  let property;
947
1083
  if (referencedBundle) {
948
1084
  let entry = nullthrows(referencedBundle.getMainEntry());
1085
+ // @ts-expect-error TS2345
949
1086
  if (entry.symbols.hasExportSymbol('*')) {
950
1087
  // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
951
1088
  // This mirrors the logic in buildExportedSymbols.
952
1089
  property = imported;
1090
+ // @ts-expect-error TS2322
953
1091
  imported =
954
1092
  referencedBundle?.env.outputFormat === 'esmodule'
955
1093
  ? 'default'
956
1094
  : '*';
957
1095
  } else {
1096
+ // @ts-expect-error TS2367
958
1097
  if (imported === '*') {
959
1098
  let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
1099
+ // @ts-expect-error TS2367
960
1100
  if (local === '*') {
961
1101
  // Re-export all symbols.
962
1102
  for (let exported of exportedSymbols) {
963
1103
  if (exported.symbol) {
1104
+ // @ts-expect-error TS2345
964
1105
  external.set(exported.exportSymbol, exported.symbol);
965
1106
  }
966
1107
  }
967
1108
  continue;
968
1109
  }
969
1110
  }
1111
+ // @ts-expect-error TS2322
970
1112
  renamed = this.bundleGraph.getSymbolResolution(
971
1113
  entry,
972
1114
  imported,
@@ -979,30 +1121,40 @@ ${code}
979
1121
  // are deduplicated. We have to prefix the imported name with the bundle id so that
980
1122
  // local variables do not shadow it.
981
1123
  if (!renamed) {
1124
+ // @ts-expect-error TS2345
982
1125
  if (this.exportedSymbols.has(local)) {
1126
+ // @ts-expect-error TS2322
983
1127
  renamed = local;
1128
+ // @ts-expect-error TS2367
984
1129
  } else if (imported === 'default' || imported === '*') {
985
1130
  renamed = this.getTopLevelName(
986
1131
  `$${this.bundle.publicId}$${specifier}`,
987
1132
  );
988
1133
  } else {
989
1134
  renamed = this.getTopLevelName(
1135
+ // @ts-expect-error TS2731
990
1136
  `$${this.bundle.publicId}$${imported}`,
991
1137
  );
992
1138
  }
993
1139
  }
994
1140
 
1141
+ // @ts-expect-error TS2345
995
1142
  external.set(imported, renamed);
1143
+ // @ts-expect-error TS2367
996
1144
  if (local !== '*' && replacements) {
997
1145
  let replacement = renamed;
1146
+ // @ts-expect-error TS2367
998
1147
  if (property === '*') {
999
1148
  replacement = renamed;
1149
+ // @ts-expect-error TS2367
1000
1150
  } else if (property === 'default') {
1001
1151
  replacement = `($parcel$interopDefault(${renamed}))`;
1002
1152
  this.usedHelpers.add('$parcel$interopDefault');
1003
1153
  } else if (property) {
1154
+ // @ts-expect-error TS2345
1004
1155
  replacement = this.getPropertyAccess(renamed, property);
1005
1156
  }
1157
+ // @ts-expect-error TS2345
1006
1158
  replacements.set(local, replacement);
1007
1159
  }
1008
1160
  }
@@ -1041,6 +1193,7 @@ ${code}
1041
1193
  asset: resolvedAsset,
1042
1194
  exportSymbol,
1043
1195
  symbol,
1196
+ // @ts-expect-error TS2345
1044
1197
  } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
1045
1198
 
1046
1199
  if (
@@ -1099,8 +1252,11 @@ ${code}
1099
1252
  staticExports &&
1100
1253
  !isWrapped &&
1101
1254
  (dep?.meta.kind === 'Import' || dep?.meta.kind === 'Export') &&
1255
+ // @ts-expect-error TS2345
1102
1256
  resolvedAsset.symbols.hasExportSymbol('*') &&
1257
+ // @ts-expect-error TS2345
1103
1258
  resolvedAsset.symbols.hasExportSymbol('default') &&
1259
+ // @ts-expect-error TS2345
1104
1260
  !resolvedAsset.symbols.hasExportSymbol('__esModule');
1105
1261
 
1106
1262
  // Find the namespace object for the resolved module. If wrapped and this
@@ -1116,7 +1272,9 @@ ${code}
1116
1272
  } else if (isWrapped && dep) {
1117
1273
  obj = `$${publicId}`;
1118
1274
  } else {
1275
+ // @ts-expect-error TS2345
1119
1276
  obj = resolvedAsset.symbols.get('*')?.local || `$${assetId}$exports`;
1277
+ // @ts-expect-error TS2345
1120
1278
  obj = replacements?.get(obj) || obj;
1121
1279
  }
1122
1280
 
@@ -1129,6 +1287,7 @@ ${code}
1129
1287
  // Directly use module.exports for wrapped assets importing themselves.
1130
1288
  return 'module.exports';
1131
1289
  } else {
1290
+ // @ts-expect-error TS2322
1132
1291
  return obj;
1133
1292
  }
1134
1293
  } else if (
@@ -1143,17 +1302,21 @@ ${code}
1143
1302
  if (
1144
1303
  (!dep || kind === 'Import' || kind === 'Export') &&
1145
1304
  exportSymbol === 'default' &&
1305
+ // @ts-expect-error TS2345
1146
1306
  resolvedAsset.symbols.hasExportSymbol('*') &&
1147
1307
  this.needsDefaultInterop(resolvedAsset)
1148
1308
  ) {
1149
1309
  this.usedHelpers.add('$parcel$interopDefault');
1310
+ // @ts-expect-error TS2731
1150
1311
  return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
1151
1312
  } else {
1313
+ // @ts-expect-error TS2345
1152
1314
  return this.getPropertyAccess(obj, exportSymbol);
1153
1315
  }
1154
1316
  } else if (!symbol) {
1155
1317
  invariant(false, 'Asset was skipped or not found.');
1156
1318
  } else {
1319
+ // @ts-expect-error TS2322
1157
1320
  return replacements?.get(symbol) || symbol;
1158
1321
  }
1159
1322
  }
@@ -1216,38 +1379,56 @@ ${code}
1216
1379
  // If there's no __esModule flag, and default is a used symbol, we need
1217
1380
  // to insert an interop helper.
1218
1381
  let defaultInterop =
1382
+ // @ts-expect-error TS2345
1219
1383
  asset.symbols.hasExportSymbol('*') &&
1384
+ // @ts-expect-error TS2345
1220
1385
  usedSymbols.has('default') &&
1386
+ // @ts-expect-error TS2345
1221
1387
  !asset.symbols.hasExportSymbol('__esModule');
1222
1388
 
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(
1389
+ let usedNamespace;
1390
+ if (
1391
+ getFeatureFlag('inlineConstOptimisationFix') &&
1392
+ asset.meta.isConstantModule
1393
+ ) {
1394
+ // Only set usedNamespace if there is an incoming dependency in the current bundle that uses '*'
1395
+ usedNamespace = this.bundleGraph.getIncomingDependencies(asset).some(
1396
+ (dep) =>
1397
+ this.bundle.hasDependency(dep) &&
1398
+ // @ts-expect-error TS2345
1399
+ nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1400
+ );
1401
+ } else {
1402
+ usedNamespace =
1403
+ // If the asset has * in its used symbols, we might need the exports namespace.
1404
+ // The one case where this isn't true is in ESM library entries, where the only
1405
+ // dependency on * is the entry dependency. In this case, we will use ESM exports
1406
+ // instead of the namespace object.
1407
+ // @ts-expect-error TS2345
1408
+ (usedSymbols.has('*') &&
1409
+ (this.bundle.env.outputFormat !== 'esmodule' ||
1410
+ !this.bundle.env.isLibrary ||
1411
+ asset !== this.bundle.getMainEntry() ||
1412
+ this.bundleGraph.getIncomingDependencies(asset).some(
1235
1413
  (dep) =>
1236
1414
  !dep.isEntry &&
1237
1415
  this.bundle.hasDependency(dep) &&
1416
+ // @ts-expect-error TS2345
1238
1417
  nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1239
1418
  ))) ||
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());
1419
+ // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
1420
+ // we fallback on the namespace object.
1421
+ // @ts-expect-error TS2345
1422
+ (asset.symbols.hasExportSymbol('*') &&
1423
+ [...usedSymbols].some((s) => !asset.symbols.hasExportSymbol(s))) ||
1424
+ // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1425
+ // include the namespace object for the default export.
1426
+ this.exportedSymbols.has(`$${assetId}$exports`) ||
1427
+ // CommonJS library bundle entries always need a namespace.
1428
+ (this.bundle.env.isLibrary &&
1429
+ this.bundle.env.outputFormat === 'commonjs' &&
1430
+ asset === this.bundle.getMainEntry());
1431
+ }
1251
1432
 
1252
1433
  // If the asset doesn't have static exports, should wrap, the namespace is used,
1253
1434
  // or we need default interop, then we need to synthesize a namespace object for
@@ -1274,6 +1455,7 @@ ${code}
1274
1455
  // Insert the __esModule interop flag for this module if it has a `default` export
1275
1456
  // and the namespace symbol is used.
1276
1457
  // TODO: only if required by CJS?
1458
+ // @ts-expect-error TS2345
1277
1459
  if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
1278
1460
  prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
1279
1461
  prependLineCount += 2;
@@ -1292,6 +1474,7 @@ ${code}
1292
1474
  let isWrapped = resolved && resolved.meta.shouldWrap;
1293
1475
 
1294
1476
  for (let [imported, {local}] of dep.symbols) {
1477
+ // @ts-expect-error TS2367
1295
1478
  if (imported === '*' && local === '*') {
1296
1479
  if (!resolved) {
1297
1480
  // Re-exporting an external module. This should have already been handled in buildReplacements.
@@ -1309,9 +1492,11 @@ ${code}
1309
1492
  if (
1310
1493
  isWrapped ||
1311
1494
  resolved.meta.staticExports === false ||
1495
+ // @ts-expect-error TS2345
1312
1496
  nullthrows(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
1313
1497
  // an empty asset
1314
1498
  (!resolved.meta.hasCJSExports &&
1499
+ // @ts-expect-error TS2345
1315
1500
  resolved.symbols.hasExportSymbol('*'))
1316
1501
  ) {
1317
1502
  let obj = this.getSymbolResolution(
@@ -1328,7 +1513,9 @@ ${code}
1328
1513
  this.bundleGraph.getUsedSymbols(dep),
1329
1514
  )) {
1330
1515
  if (
1516
+ // @ts-expect-error TS2367
1331
1517
  symbol === 'default' || // `export * as ...` does not include the default export
1518
+ // @ts-expect-error TS2367
1332
1519
  symbol === '__esModule'
1333
1520
  ) {
1334
1521
  continue;
@@ -1337,6 +1524,7 @@ ${code}
1337
1524
  let resolvedSymbol = this.getSymbolResolution(
1338
1525
  asset,
1339
1526
  resolved,
1527
+ // @ts-expect-error TS2345
1340
1528
  symbol,
1341
1529
  undefined,
1342
1530
  replacements,
@@ -1362,6 +1550,7 @@ ${code}
1362
1550
  // re-exported symbols rather than only symbols declared in this asset.
1363
1551
  let incomingDeps = this.bundleGraph.getIncomingDependencies(asset);
1364
1552
  let usedExports = [...asset.symbols.exportSymbols()].filter((symbol) => {
1553
+ // @ts-expect-error TS2367
1365
1554
  if (symbol === '*') {
1366
1555
  return false;
1367
1556
  }
@@ -1378,6 +1567,7 @@ ${code}
1378
1567
  // No used symbols available for the asset, make sure we keep all of them
1379
1568
  if (!symbols) return false;
1380
1569
 
1570
+ // @ts-expect-error TS2345
1381
1571
  return !symbols.has(symbol) && !symbols.has('*');
1382
1572
  });
1383
1573
  return !unused;
@@ -1393,6 +1583,7 @@ ${code}
1393
1583
  let resolved = this.getSymbolResolution(
1394
1584
  asset,
1395
1585
  asset,
1586
+ // @ts-expect-error TS2345
1396
1587
  exp,
1397
1588
  undefined,
1398
1589
  replacements,
@@ -1448,8 +1639,10 @@ ${code}
1448
1639
  }
1449
1640
 
1450
1641
  for (let helper of this.usedHelpers) {
1642
+ // @ts-expect-error TS7053
1451
1643
  let currentHelper = helpers[helper];
1452
1644
  if (typeof currentHelper === 'function') {
1645
+ // @ts-expect-error TS7053
1453
1646
  currentHelper = helpers[helper](this.bundle.env);
1454
1647
  }
1455
1648
  res += currentHelper;
@@ -1470,6 +1663,7 @@ ${code}
1470
1663
  .some((g) => this.bundleGraph.isEntryBundleGroup(g)) ||
1471
1664
  this.bundle.env.isIsolated() ||
1472
1665
  this.bundle.bundleBehavior === 'isolated' ||
1666
+ this.bundle.bundleBehavior === 'inlineIsolated' ||
1473
1667
  // Conditional deps may be loaded before entrypoints on the server
1474
1668
  this.hasConditionalDependency();
1475
1669
 
@@ -1498,7 +1692,11 @@ ${code}
1498
1692
  }
1499
1693
 
1500
1694
  // Add importScripts for sibling bundles in workers.
1501
- if (this.bundle.env.isWorker() || this.bundle.env.isWorklet()) {
1695
+ if (
1696
+ this.bundle.env.isWorker() ||
1697
+ this.bundle.env.isTesseract() ||
1698
+ this.bundle.env.isWorklet()
1699
+ ) {
1502
1700
  let importScripts = '';
1503
1701
  let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
1504
1702
  for (let b of bundles) {
@@ -1522,7 +1720,9 @@ ${code}
1522
1720
 
1523
1721
  needsDefaultInterop(asset: Asset): boolean {
1524
1722
  if (
1723
+ // @ts-expect-error TS2345
1525
1724
  asset.symbols.hasExportSymbol('*') &&
1725
+ // @ts-expect-error TS2345
1526
1726
  !asset.symbols.hasExportSymbol('default')
1527
1727
  ) {
1528
1728
  return true;