@atlaspack/packager-js 2.14.5-canary.202 → 2.14.5-canary.204

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.
@@ -90,14 +90,13 @@ export class ScopeHoistingPackager {
90
90
  outputFormat: OutputFormat;
91
91
  isAsyncBundle: boolean;
92
92
  globalNames: ReadonlySet<string>;
93
- // @ts-expect-error TS2564
94
93
  assetOutputs: Map<
95
- string,
94
+ Asset,
96
95
  {
97
96
  code: string;
98
97
  map: Buffer | null | undefined;
99
98
  }
100
- >;
99
+ > = new Map();
101
100
  exportedSymbols: Map<
102
101
  string,
103
102
  {
@@ -109,9 +108,10 @@ export class ScopeHoistingPackager {
109
108
  > = new Map();
110
109
  externals: Map<string, Map<string, string>> = new Map();
111
110
  topLevelNames: Map<string, number> = new Map();
112
- seenAssets: Set<string> = new Set();
113
- wrappedAssets: Set<string> = new Set();
114
- hoistedRequires: Map<string, Map<string, string>> = new Map();
111
+ seenAssets: Set<Asset> = new Set();
112
+ wrappedAssets: Set<Asset> = new Set();
113
+ constantAssets: Set<Asset> = new Set();
114
+ hoistedRequires: Map<Dependency, Map<Asset, string>> = new Map();
115
115
  seenHoistedRequires: Set<string> = new Set();
116
116
  needsPrelude: boolean = false;
117
117
  usedHelpers: Set<string> = new Set();
@@ -152,8 +152,7 @@ export class ScopeHoistingPackager {
152
152
  contents: string;
153
153
  map: SourceMap | null | undefined;
154
154
  }> {
155
- let {wrapped: wrappedAssets, constant: constantAssets} =
156
- await this.loadAssets();
155
+ await this.loadAssets();
157
156
  this.buildExportedSymbols();
158
157
 
159
158
  // If building a library, the target is actually another bundler rather
@@ -174,14 +173,13 @@ export class ScopeHoistingPackager {
174
173
 
175
174
  let res = '';
176
175
  let lineCount = 0;
177
- // @ts-expect-error TS7034
178
- let sourceMap = null;
176
+ let sourceMap: SourceMap | null | undefined = null;
179
177
  let processAsset = (asset: Asset) => {
180
178
  this.seenHoistedRequires.clear();
181
179
  let [content, map, lines] = this.visitAsset(asset);
182
180
 
183
- // @ts-expect-error TS7005
184
181
  if (sourceMap && map) {
182
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
185
183
  sourceMap.addSourceMap(map, lineCount);
186
184
  } else if (this.bundle.env.sourceMap) {
187
185
  sourceMap = map;
@@ -196,8 +194,8 @@ export class ScopeHoistingPackager {
196
194
  this.useBothScopeHoistingImprovements
197
195
  ) {
198
196
  // Write out all constant modules used by this bundle
199
- for (let asset of constantAssets) {
200
- if (!this.seenAssets.has(asset.id)) {
197
+ for (let asset of this.constantAssets) {
198
+ if (!this.seenAssets.has(asset)) {
201
199
  processAsset(asset);
202
200
  }
203
201
  }
@@ -205,8 +203,8 @@ export class ScopeHoistingPackager {
205
203
 
206
204
  // Hoist wrapped asset to the top of the bundle to ensure that they are registered
207
205
  // before they are used.
208
- for (let asset of wrappedAssets) {
209
- if (!this.seenAssets.has(asset.id)) {
206
+ for (let asset of this.wrappedAssets) {
207
+ if (!this.seenAssets.has(asset)) {
210
208
  processAsset(asset);
211
209
  }
212
210
  }
@@ -214,7 +212,7 @@ export class ScopeHoistingPackager {
214
212
  // Add each asset that is directly connected to the bundle. Dependencies will be handled
215
213
  // by replacing `import` statements in the code.
216
214
  this.bundle.traverseAssets((asset, _, actions) => {
217
- if (this.seenAssets.has(asset.id)) {
215
+ if (this.seenAssets.has(asset)) {
218
216
  actions.skipChildren();
219
217
  return;
220
218
  }
@@ -226,7 +224,7 @@ export class ScopeHoistingPackager {
226
224
  let [prelude, preludeLines] = this.buildBundlePrelude();
227
225
  res = prelude + res;
228
226
  lineCount += preludeLines;
229
- // @ts-expect-error TS2339
227
+ // @ts-expect-error TS2339 - offsetLines method exists but missing from @parcel/source-map type definitions
230
228
  sourceMap?.offsetLines(1, preludeLines);
231
229
 
232
230
  let entries = this.bundle.getEntryAssets();
@@ -255,7 +253,7 @@ export class ScopeHoistingPackager {
255
253
 
256
254
  // If any of the entry assets are wrapped, call parcelRequire so they are executed.
257
255
  for (let entry of entries) {
258
- if (this.wrappedAssets.has(entry.id) && !this.isScriptEntry(entry)) {
256
+ if (this.wrappedAssets.has(entry) && !this.isScriptEntry(entry)) {
259
257
  let parcelRequire = `parcelRequire(${JSON.stringify(
260
258
  this.bundleGraph.getAssetPublicId(entry),
261
259
  )});\n`;
@@ -299,9 +297,7 @@ export class ScopeHoistingPackager {
299
297
  lineCount++;
300
298
 
301
299
  let mainEntry = nullthrows(this.bundle.getMainEntry());
302
- let {code, map: mapBuffer} = nullthrows(
303
- this.assetOutputs.get(mainEntry.id),
304
- );
300
+ let {code, map: mapBuffer} = nullthrows(this.assetOutputs.get(mainEntry));
305
301
  let map;
306
302
  if (mapBuffer) {
307
303
  map = new SourceMap(this.options.projectRoot, mapBuffer);
@@ -314,7 +310,7 @@ export class ScopeHoistingPackager {
314
310
  this.parcelRequireName,
315
311
  );
316
312
  if (sourceMap && map) {
317
- // @ts-expect-error TS2339
313
+ // @ts-expect-error TS2339 - addSourceMap method exists but missing from @parcel/source-map type definitions
318
314
  sourceMap.addSourceMap(map, lineCount);
319
315
  }
320
316
  }
@@ -399,13 +395,11 @@ export class ScopeHoistingPackager {
399
395
  return `$parcel$global.rwr(${params.join(', ')});`;
400
396
  }
401
397
 
402
- async loadAssets(): Promise<{
403
- wrapped: Array<Asset>;
404
- constant: Array<Asset>;
405
- }> {
406
- let queue = new PromiseQueue({maxConcurrent: 32});
407
- let wrapped: Array<Asset> = [];
408
- let constant: Array<Asset> = [];
398
+ async loadAssets() {
399
+ type QueueItem = [Asset, {code: string; map: Buffer | undefined | null}];
400
+ let queue = new PromiseQueue<QueueItem>({
401
+ maxConcurrent: 32,
402
+ });
409
403
 
410
404
  this.bundle.traverseAssets((asset) => {
411
405
  queue.add(async () => {
@@ -414,7 +408,7 @@ export class ScopeHoistingPackager {
414
408
  this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
415
409
  ]);
416
410
 
417
- return [asset.id, {code, map}];
411
+ return [asset, {code, map}];
418
412
  });
419
413
 
420
414
  if (
@@ -432,14 +426,13 @@ export class ScopeHoistingPackager {
432
426
  .getIncomingDependencies(asset)
433
427
  .some((dep) => dep.priority === 'lazy')
434
428
  ) {
435
- this.wrappedAssets.add(asset.id);
436
- wrapped.push(asset);
429
+ this.wrappedAssets.add(asset);
437
430
  } else if (
438
431
  (getFeatureFlag('inlineConstOptimisationFix') ||
439
432
  this.useBothScopeHoistingImprovements) &&
440
433
  asset.meta.isConstantModule
441
434
  ) {
442
- constant.push(asset);
435
+ this.constantAssets.add(asset);
443
436
  }
444
437
  }
445
438
  });
@@ -453,20 +446,21 @@ export class ScopeHoistingPackager {
453
446
  if (!getFeatureFlag('applyScopeHoistingImprovementV2')) {
454
447
  // Make all entry assets wrapped, to avoid any top level hoisting
455
448
  for (let entryAsset of this.bundle.getEntryAssets()) {
456
- if (!this.wrappedAssets.has(entryAsset.id)) {
457
- this.wrappedAssets.add(entryAsset.id);
458
- wrapped.push(entryAsset);
449
+ if (!this.wrappedAssets.has(entryAsset)) {
450
+ this.wrappedAssets.add(entryAsset);
459
451
  }
460
452
  }
461
453
  }
462
454
 
463
- let moduleGroupParents = [...wrapped];
455
+ // We need to make a new copy here so that we can add to the list and
456
+ // iterate the newly added items, without mutating the wrappedAssets set
457
+ let moduleGroupParents = [...this.wrappedAssets.values()];
464
458
 
465
459
  if (getFeatureFlag('applyScopeHoistingImprovementV2')) {
466
460
  // The main entry needs to be check to find assets that would have gone in
467
461
  // the top level scope
468
462
  let mainEntry = this.bundle.getMainEntry();
469
- if (mainEntry && !this.wrappedAssets.has(mainEntry.id)) {
463
+ if (mainEntry && !this.wrappedAssets.has(mainEntry)) {
470
464
  moduleGroupParents.unshift(mainEntry);
471
465
  }
472
466
  }
@@ -477,7 +471,7 @@ export class ScopeHoistingPackager {
477
471
  return;
478
472
  }
479
473
 
480
- if (this.wrappedAssets.has(asset.id)) {
474
+ if (this.wrappedAssets.has(asset)) {
481
475
  actions.skipChildren();
482
476
  return;
483
477
  }
@@ -486,8 +480,7 @@ export class ScopeHoistingPackager {
486
480
  !asset.meta.isConstantModule &&
487
481
  (assignedAssets.has(asset) || this.isReExported(asset))
488
482
  ) {
489
- wrapped.push(asset);
490
- this.wrappedAssets.add(asset.id);
483
+ this.wrappedAssets.add(asset);
491
484
 
492
485
  // This also needs to be added to the traversal so that we iterate
493
486
  // it during this check.
@@ -501,28 +494,25 @@ export class ScopeHoistingPackager {
501
494
  }, moduleGroupParentAsset);
502
495
  }
503
496
  } else {
504
- for (let wrappedAssetRoot of [...wrapped]) {
497
+ for (let wrappedAssetRoot of this.wrappedAssets) {
505
498
  this.bundle.traverseAssets((asset, _, actions) => {
506
499
  if (asset === wrappedAssetRoot) {
507
500
  return;
508
501
  }
509
502
 
510
- if (this.wrappedAssets.has(asset.id)) {
503
+ if (this.wrappedAssets.has(asset)) {
511
504
  actions.skipChildren();
512
505
  return;
513
506
  }
514
507
 
515
508
  if (!asset.meta.isConstantModule) {
516
- this.wrappedAssets.add(asset.id);
517
- wrapped.push(asset);
509
+ this.wrappedAssets.add(asset);
518
510
  }
519
511
  }, wrappedAssetRoot);
520
512
  }
521
513
  }
522
514
 
523
- // @ts-expect-error TS2769
524
515
  this.assetOutputs = new Map(await queue.run());
525
- return {wrapped, constant};
526
516
  }
527
517
 
528
518
  isReExported(asset: Asset): boolean {
@@ -555,7 +545,7 @@ export class ScopeHoistingPackager {
555
545
 
556
546
  // TODO: handle ESM exports of wrapped entry assets...
557
547
  let entry = this.bundle.getMainEntry();
558
- if (entry && !this.wrappedAssets.has(entry.id)) {
548
+ if (entry && !this.wrappedAssets.has(entry)) {
559
549
  let hasNamespace = entry.symbols.hasExportSymbol('*');
560
550
 
561
551
  for (let {
@@ -637,10 +627,10 @@ export class ScopeHoistingPackager {
637
627
  }
638
628
 
639
629
  visitAsset(asset: Asset): [string, SourceMap | null | undefined, number] {
640
- invariant(!this.seenAssets.has(asset.id), 'Already visited asset');
641
- this.seenAssets.add(asset.id);
630
+ invariant(!this.seenAssets.has(asset), 'Already visited asset');
631
+ this.seenAssets.add(asset);
642
632
 
643
- let {code, map} = nullthrows(this.assetOutputs.get(asset.id));
633
+ let {code, map} = nullthrows(this.assetOutputs.get(asset));
644
634
  return this.buildAsset(asset, code, map);
645
635
  }
646
636
 
@@ -653,7 +643,7 @@ export class ScopeHoistingPackager {
653
643
  code: string,
654
644
  map?: Buffer | null,
655
645
  ): [string, SourceMap | null | undefined, number] {
656
- let shouldWrap = this.wrappedAssets.has(asset.id);
646
+ let shouldWrap = this.wrappedAssets.has(asset);
657
647
  let deps = this.bundleGraph.getDependencies(asset);
658
648
 
659
649
  let sourceMap =
@@ -680,14 +670,11 @@ export class ScopeHoistingPackager {
680
670
  continue;
681
671
  }
682
672
 
683
- if (
684
- this.bundle.hasAsset(resolved) &&
685
- !this.seenAssets.has(resolved.id)
686
- ) {
673
+ if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved)) {
687
674
  let [code, map, lines] = this.visitAsset(resolved);
688
675
  depCode += code + '\n';
689
676
  if (sourceMap && map) {
690
- // @ts-expect-error TS2551
677
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
691
678
  sourceMap.addSourceMap(map, lineCount);
692
679
  }
693
680
  lineCount += lines + 1;
@@ -724,9 +711,7 @@ export class ScopeHoistingPackager {
724
711
  code += append;
725
712
 
726
713
  let lineCount = 0;
727
- // @ts-expect-error TS2552
728
- let depContent: Array<[string, NodeSourceMap | null | undefined, number]> =
729
- [];
714
+ let depContent: Array<[string, SourceMap | null | undefined, number]> = [];
730
715
  if (depMap.size === 0 && replacements.size === 0) {
731
716
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
732
717
  lineCount = countLines(code) - 1;
@@ -785,7 +770,7 @@ export class ScopeHoistingPackager {
785
770
 
786
771
  if (
787
772
  this.bundle.hasAsset(resolved) &&
788
- !this.seenAssets.has(resolved.id)
773
+ !this.seenAssets.has(resolved)
789
774
  ) {
790
775
  // If this asset is wrapped, we need to hoist the code for the dependency
791
776
  // outside our parcelRequire.register wrapper. This is safe because all
@@ -794,7 +779,7 @@ export class ScopeHoistingPackager {
794
779
  if (this.useBothScopeHoistingImprovements) {
795
780
  if (
796
781
  !resolved.meta.isConstantModule &&
797
- !this.wrappedAssets.has(resolved.id)
782
+ !this.wrappedAssets.has(resolved)
798
783
  ) {
799
784
  let [depCode, depMap, depLines] =
800
785
  this.visitAsset(resolved);
@@ -844,7 +829,7 @@ export class ScopeHoistingPackager {
844
829
  }
845
830
 
846
831
  if (map) {
847
- // @ts-expect-error TS2551
832
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
848
833
  sourceMap.addSourceMap(map, lineCount);
849
834
  }
850
835
  }
@@ -901,7 +886,7 @@ ${code}
901
886
  if (!depCode) continue;
902
887
  code += depCode + '\n';
903
888
  if (sourceMap && map) {
904
- // @ts-expect-error TS2551
889
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
905
890
  sourceMap.addSourceMap(map, lineCount);
906
891
  }
907
892
  lineCount += lines + 1;
@@ -1020,7 +1005,7 @@ ${code}
1020
1005
  // If this asset is wrapped, we need to replace the exports namespace with `module.exports`,
1021
1006
  // which will be provided to us by the wrapper.
1022
1007
  if (
1023
- this.wrappedAssets.has(asset.id) ||
1008
+ this.wrappedAssets.has(asset) ||
1024
1009
  (this.bundle.env.outputFormat === 'commonjs' &&
1025
1010
  asset === this.bundle.getMainEntry())
1026
1011
  ) {
@@ -1147,12 +1132,9 @@ ${code}
1147
1132
  }
1148
1133
  }
1149
1134
 
1150
- // @ts-expect-error TS2322
1151
- renamed = this.bundleGraph.getSymbolResolution(
1152
- entry,
1153
- imported,
1154
- this.bundle,
1155
- ).symbol;
1135
+ renamed =
1136
+ this.bundleGraph.getSymbolResolution(entry, imported, this.bundle)
1137
+ .symbol || undefined;
1156
1138
  }
1157
1139
  }
1158
1140
 
@@ -1210,7 +1192,7 @@ ${code}
1210
1192
  }
1211
1193
  return (
1212
1194
  (!this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved)) ||
1213
- (this.wrappedAssets.has(resolved.id) && resolved !== parentAsset)
1195
+ (this.wrappedAssets.has(resolved) && resolved !== parentAsset)
1214
1196
  );
1215
1197
  }
1216
1198
 
@@ -1260,14 +1242,14 @@ ${code}
1260
1242
  (!this.bundle.hasAsset(resolvedAsset) ||
1261
1243
  !this.shouldSkipAsset(resolvedAsset))
1262
1244
  ) {
1263
- let hoisted = this.hoistedRequires.get(dep.id);
1245
+ let hoisted = this.hoistedRequires.get(dep);
1264
1246
  if (!hoisted) {
1265
1247
  hoisted = new Map();
1266
- this.hoistedRequires.set(dep.id, hoisted);
1248
+ this.hoistedRequires.set(dep, hoisted);
1267
1249
  }
1268
1250
 
1269
1251
  hoisted.set(
1270
- resolvedAsset.id,
1252
+ resolvedAsset,
1271
1253
  `var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`,
1272
1254
  );
1273
1255
  }
@@ -1309,7 +1291,7 @@ ${code}
1309
1291
  // Resolve to the namespace object if requested or this is a CJS default interop reqiure.
1310
1292
  if (
1311
1293
  parentAsset === resolvedAsset &&
1312
- this.wrappedAssets.has(resolvedAsset.id)
1294
+ this.wrappedAssets.has(resolvedAsset)
1313
1295
  ) {
1314
1296
  // Directly use module.exports for wrapped assets importing themselves.
1315
1297
  return 'module.exports';
@@ -1352,7 +1334,7 @@ ${code}
1352
1334
  return ['', 0];
1353
1335
  }
1354
1336
 
1355
- let hoisted = this.hoistedRequires.get(dep.id);
1337
+ let hoisted = this.hoistedRequires.get(dep);
1356
1338
  let res = '';
1357
1339
  let lineCount = 0;
1358
1340
  let isWrapped = this.isWrapped(resolved, parentAsset);
@@ -1364,7 +1346,7 @@ ${code}
1364
1346
  if (
1365
1347
  isWrapped &&
1366
1348
  !dep.meta.shouldWrap &&
1367
- (!hoisted || hoisted.keys().next().value !== resolved.id) &&
1349
+ (!hoisted || hoisted.keys().next().value !== resolved) &&
1368
1350
  !this.bundleGraph.isDependencySkipped(dep) &&
1369
1351
  !this.shouldSkipAsset(resolved)
1370
1352
  ) {
@@ -1406,7 +1388,7 @@ ${code}
1406
1388
  let prependLineCount = 0;
1407
1389
  let append = '';
1408
1390
 
1409
- let shouldWrap = this.wrappedAssets.has(asset.id);
1391
+ let shouldWrap = this.wrappedAssets.has(asset);
1410
1392
  let usedSymbols = nullthrows(this.bundleGraph.getUsedSymbols(asset));
1411
1393
  let assetId = asset.meta.id;
1412
1394
  invariant(typeof assetId === 'string');
@@ -1674,11 +1656,11 @@ ${code}
1674
1656
  }
1675
1657
 
1676
1658
  for (let helper of this.usedHelpers) {
1677
- // @ts-expect-error TS7053
1678
- let currentHelper = helpers[helper];
1659
+ let currentHelper = (helpers as Record<string, any>)[helper];
1679
1660
  if (typeof currentHelper === 'function') {
1680
- // @ts-expect-error TS7053
1681
- currentHelper = helpers[helper](this.bundle.env);
1661
+ currentHelper = (helpers as Record<string, any>)[helper](
1662
+ this.bundle.env,
1663
+ );
1682
1664
  }
1683
1665
  res += currentHelper;
1684
1666
  if (enableSourceMaps) {