@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.
@@ -70,7 +70,7 @@ class ESMOutputFormat {
70
70
  let lines = 0;
71
71
  let exportSpecifiers = [];
72
72
  for (let { asset, exportSymbol, local, exportAs, } of this.packager.exportedSymbols.values()) {
73
- if (this.packager.wrappedAssets.has(asset.id)) {
73
+ if (this.packager.wrappedAssets.has(asset)) {
74
74
  let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
75
75
  res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
76
76
  lines++;
@@ -81,11 +81,13 @@ const OUTPUT_FORMATS = {
81
81
  };
82
82
  class ScopeHoistingPackager {
83
83
  constructor(options, bundleGraph, bundle, parcelRequireName, useAsyncBundleRuntime, logger) {
84
+ this.assetOutputs = new Map();
84
85
  this.exportedSymbols = new Map();
85
86
  this.externals = new Map();
86
87
  this.topLevelNames = new Map();
87
88
  this.seenAssets = new Set();
88
89
  this.wrappedAssets = new Set();
90
+ this.constantAssets = new Set();
89
91
  this.hoistedRequires = new Map();
90
92
  this.seenHoistedRequires = new Set();
91
93
  this.needsPrelude = false;
@@ -109,7 +111,7 @@ class ScopeHoistingPackager {
109
111
  this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
110
112
  }
111
113
  async package() {
112
- let { wrapped: wrappedAssets, constant: constantAssets } = await this.loadAssets();
114
+ await this.loadAssets();
113
115
  this.buildExportedSymbols();
114
116
  // If building a library, the target is actually another bundler rather
115
117
  // than the final output that could be loaded in a browser. So, loader
@@ -126,13 +128,12 @@ class ScopeHoistingPackager {
126
128
  }
127
129
  let res = '';
128
130
  let lineCount = 0;
129
- // @ts-expect-error TS7034
130
131
  let sourceMap = null;
131
132
  let processAsset = (asset) => {
132
133
  this.seenHoistedRequires.clear();
133
134
  let [content, map, lines] = this.visitAsset(asset);
134
- // @ts-expect-error TS7005
135
135
  if (sourceMap && map) {
136
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
136
137
  sourceMap.addSourceMap(map, lineCount);
137
138
  }
138
139
  else if (this.bundle.env.sourceMap) {
@@ -144,23 +145,23 @@ class ScopeHoistingPackager {
144
145
  if ((0, feature_flags_1.getFeatureFlag)('inlineConstOptimisationFix') ||
145
146
  this.useBothScopeHoistingImprovements) {
146
147
  // Write out all constant modules used by this bundle
147
- for (let asset of constantAssets) {
148
- if (!this.seenAssets.has(asset.id)) {
148
+ for (let asset of this.constantAssets) {
149
+ if (!this.seenAssets.has(asset)) {
149
150
  processAsset(asset);
150
151
  }
151
152
  }
152
153
  }
153
154
  // Hoist wrapped asset to the top of the bundle to ensure that they are registered
154
155
  // before they are used.
155
- for (let asset of wrappedAssets) {
156
- if (!this.seenAssets.has(asset.id)) {
156
+ for (let asset of this.wrappedAssets) {
157
+ if (!this.seenAssets.has(asset)) {
157
158
  processAsset(asset);
158
159
  }
159
160
  }
160
161
  // Add each asset that is directly connected to the bundle. Dependencies will be handled
161
162
  // by replacing `import` statements in the code.
162
163
  this.bundle.traverseAssets((asset, _, actions) => {
163
- if (this.seenAssets.has(asset.id)) {
164
+ if (this.seenAssets.has(asset)) {
164
165
  actions.skipChildren();
165
166
  return;
166
167
  }
@@ -170,7 +171,7 @@ class ScopeHoistingPackager {
170
171
  let [prelude, preludeLines] = this.buildBundlePrelude();
171
172
  res = prelude + res;
172
173
  lineCount += preludeLines;
173
- // @ts-expect-error TS2339
174
+ // @ts-expect-error TS2339 - offsetLines method exists but missing from @parcel/source-map type definitions
174
175
  sourceMap?.offsetLines(1, preludeLines);
175
176
  let entries = this.bundle.getEntryAssets();
176
177
  let mainEntry = this.bundle.getMainEntry();
@@ -193,7 +194,7 @@ class ScopeHoistingPackager {
193
194
  let needsBundleQueue = this.shouldBundleQueue(this.bundle);
194
195
  // If any of the entry assets are wrapped, call parcelRequire so they are executed.
195
196
  for (let entry of entries) {
196
- if (this.wrappedAssets.has(entry.id) && !this.isScriptEntry(entry)) {
197
+ if (this.wrappedAssets.has(entry) && !this.isScriptEntry(entry)) {
197
198
  let parcelRequire = `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(entry))});\n`;
198
199
  let entryExports = entry.symbols.get('*')?.local;
199
200
  if (entryExports &&
@@ -222,14 +223,14 @@ class ScopeHoistingPackager {
222
223
  res += '\n';
223
224
  lineCount++;
224
225
  let mainEntry = (0, nullthrows_1.default)(this.bundle.getMainEntry());
225
- let { code, map: mapBuffer } = (0, nullthrows_1.default)(this.assetOutputs.get(mainEntry.id));
226
+ let { code, map: mapBuffer } = (0, nullthrows_1.default)(this.assetOutputs.get(mainEntry));
226
227
  let map;
227
228
  if (mapBuffer) {
228
229
  map = new source_map_1.default(this.options.projectRoot, mapBuffer);
229
230
  }
230
231
  res += (0, utils_2.replaceScriptDependencies)(this.bundleGraph, this.bundle, code, map, this.parcelRequireName);
231
232
  if (sourceMap && map) {
232
- // @ts-expect-error TS2339
233
+ // @ts-expect-error TS2339 - addSourceMap method exists but missing from @parcel/source-map type definitions
233
234
  sourceMap.addSourceMap(map, lineCount);
234
235
  }
235
236
  }
@@ -295,16 +296,16 @@ class ScopeHoistingPackager {
295
296
  return `$parcel$global.rwr(${params.join(', ')});`;
296
297
  }
297
298
  async loadAssets() {
298
- let queue = new utils_1.PromiseQueue({ maxConcurrent: 32 });
299
- let wrapped = [];
300
- let constant = [];
299
+ let queue = new utils_1.PromiseQueue({
300
+ maxConcurrent: 32,
301
+ });
301
302
  this.bundle.traverseAssets((asset) => {
302
303
  queue.add(async () => {
303
304
  let [code, map] = await Promise.all([
304
305
  asset.getCode(),
305
306
  this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
306
307
  ]);
307
- return [asset.id, { code, map }];
308
+ return [asset, { code, map }];
308
309
  });
309
310
  if (asset.meta.shouldWrap ||
310
311
  this.bundle.env.sourceType === 'script' ||
@@ -317,13 +318,12 @@ class ScopeHoistingPackager {
317
318
  this.bundleGraph
318
319
  .getIncomingDependencies(asset)
319
320
  .some((dep) => dep.priority === 'lazy')) {
320
- this.wrappedAssets.add(asset.id);
321
- wrapped.push(asset);
321
+ this.wrappedAssets.add(asset);
322
322
  }
323
323
  else if (((0, feature_flags_1.getFeatureFlag)('inlineConstOptimisationFix') ||
324
324
  this.useBothScopeHoistingImprovements) &&
325
325
  asset.meta.isConstantModule) {
326
- constant.push(asset);
326
+ this.constantAssets.add(asset);
327
327
  }
328
328
  }
329
329
  });
@@ -335,18 +335,19 @@ class ScopeHoistingPackager {
335
335
  if (!(0, feature_flags_1.getFeatureFlag)('applyScopeHoistingImprovementV2')) {
336
336
  // Make all entry assets wrapped, to avoid any top level hoisting
337
337
  for (let entryAsset of this.bundle.getEntryAssets()) {
338
- if (!this.wrappedAssets.has(entryAsset.id)) {
339
- this.wrappedAssets.add(entryAsset.id);
340
- wrapped.push(entryAsset);
338
+ if (!this.wrappedAssets.has(entryAsset)) {
339
+ this.wrappedAssets.add(entryAsset);
341
340
  }
342
341
  }
343
342
  }
344
- let moduleGroupParents = [...wrapped];
343
+ // We need to make a new copy here so that we can add to the list and
344
+ // iterate the newly added items, without mutating the wrappedAssets set
345
+ let moduleGroupParents = [...this.wrappedAssets.values()];
345
346
  if ((0, feature_flags_1.getFeatureFlag)('applyScopeHoistingImprovementV2')) {
346
347
  // The main entry needs to be check to find assets that would have gone in
347
348
  // the top level scope
348
349
  let mainEntry = this.bundle.getMainEntry();
349
- if (mainEntry && !this.wrappedAssets.has(mainEntry.id)) {
350
+ if (mainEntry && !this.wrappedAssets.has(mainEntry)) {
350
351
  moduleGroupParents.unshift(mainEntry);
351
352
  }
352
353
  }
@@ -355,14 +356,13 @@ class ScopeHoistingPackager {
355
356
  if (asset === moduleGroupParentAsset) {
356
357
  return;
357
358
  }
358
- if (this.wrappedAssets.has(asset.id)) {
359
+ if (this.wrappedAssets.has(asset)) {
359
360
  actions.skipChildren();
360
361
  return;
361
362
  }
362
363
  if (!asset.meta.isConstantModule &&
363
364
  (assignedAssets.has(asset) || this.isReExported(asset))) {
364
- wrapped.push(asset);
365
- this.wrappedAssets.add(asset.id);
365
+ this.wrappedAssets.add(asset);
366
366
  // This also needs to be added to the traversal so that we iterate
367
367
  // it during this check.
368
368
  moduleGroupParents.push(asset);
@@ -374,25 +374,22 @@ class ScopeHoistingPackager {
374
374
  }
375
375
  }
376
376
  else {
377
- for (let wrappedAssetRoot of [...wrapped]) {
377
+ for (let wrappedAssetRoot of this.wrappedAssets) {
378
378
  this.bundle.traverseAssets((asset, _, actions) => {
379
379
  if (asset === wrappedAssetRoot) {
380
380
  return;
381
381
  }
382
- if (this.wrappedAssets.has(asset.id)) {
382
+ if (this.wrappedAssets.has(asset)) {
383
383
  actions.skipChildren();
384
384
  return;
385
385
  }
386
386
  if (!asset.meta.isConstantModule) {
387
- this.wrappedAssets.add(asset.id);
388
- wrapped.push(asset);
387
+ this.wrappedAssets.add(asset);
389
388
  }
390
389
  }, wrappedAssetRoot);
391
390
  }
392
391
  }
393
- // @ts-expect-error TS2769
394
392
  this.assetOutputs = new Map(await queue.run());
395
- return { wrapped, constant };
396
393
  }
397
394
  isReExported(asset) {
398
395
  let parentSymbols = this.bundleGraph
@@ -414,7 +411,7 @@ class ScopeHoistingPackager {
414
411
  }
415
412
  // TODO: handle ESM exports of wrapped entry assets...
416
413
  let entry = this.bundle.getMainEntry();
417
- if (entry && !this.wrappedAssets.has(entry.id)) {
414
+ if (entry && !this.wrappedAssets.has(entry)) {
418
415
  let hasNamespace = entry.symbols.hasExportSymbol('*');
419
416
  for (let { asset, exportAs, symbol, exportSymbol, } of this.bundleGraph.getExportedSymbols(entry)) {
420
417
  if (typeof symbol === 'string') {
@@ -479,16 +476,16 @@ class ScopeHoistingPackager {
479
476
  return `${obj}[${JSON.stringify(property)}]`;
480
477
  }
481
478
  visitAsset(asset) {
482
- (0, assert_1.default)(!this.seenAssets.has(asset.id), 'Already visited asset');
483
- this.seenAssets.add(asset.id);
484
- let { code, map } = (0, nullthrows_1.default)(this.assetOutputs.get(asset.id));
479
+ (0, assert_1.default)(!this.seenAssets.has(asset), 'Already visited asset');
480
+ this.seenAssets.add(asset);
481
+ let { code, map } = (0, nullthrows_1.default)(this.assetOutputs.get(asset));
485
482
  return this.buildAsset(asset, code, map);
486
483
  }
487
484
  getAssetFilePath(asset) {
488
485
  return path_1.default.relative(this.options.projectRoot, asset.filePath);
489
486
  }
490
487
  buildAsset(asset, code, map) {
491
- let shouldWrap = this.wrappedAssets.has(asset.id);
488
+ let shouldWrap = this.wrappedAssets.has(asset);
492
489
  let deps = this.bundleGraph.getDependencies(asset);
493
490
  let sourceMap = this.bundle.env.sourceMap && map
494
491
  ? new source_map_1.default(this.options.projectRoot, map)
@@ -509,12 +506,11 @@ class ScopeHoistingPackager {
509
506
  }
510
507
  continue;
511
508
  }
512
- if (this.bundle.hasAsset(resolved) &&
513
- !this.seenAssets.has(resolved.id)) {
509
+ if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved)) {
514
510
  let [code, map, lines] = this.visitAsset(resolved);
515
511
  depCode += code + '\n';
516
512
  if (sourceMap && map) {
517
- // @ts-expect-error TS2551
513
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
518
514
  sourceMap.addSourceMap(map, lineCount);
519
515
  }
520
516
  lineCount += lines + 1;
@@ -539,7 +535,6 @@ class ScopeHoistingPackager {
539
535
  }
540
536
  code += append;
541
537
  let lineCount = 0;
542
- // @ts-expect-error TS2552
543
538
  let depContent = [];
544
539
  if (depMap.size === 0 && replacements.size === 0) {
545
540
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
@@ -585,14 +580,14 @@ class ScopeHoistingPackager {
585
580
  [res, lines] = this.getHoistedParcelRequires(asset, dep, resolved);
586
581
  }
587
582
  if (this.bundle.hasAsset(resolved) &&
588
- !this.seenAssets.has(resolved.id)) {
583
+ !this.seenAssets.has(resolved)) {
589
584
  // If this asset is wrapped, we need to hoist the code for the dependency
590
585
  // outside our parcelRequire.register wrapper. This is safe because all
591
586
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
592
587
  // asset content where the import statement was.
593
588
  if (this.useBothScopeHoistingImprovements) {
594
589
  if (!resolved.meta.isConstantModule &&
595
- !this.wrappedAssets.has(resolved.id)) {
590
+ !this.wrappedAssets.has(resolved)) {
596
591
  let [depCode, depMap, depLines] = this.visitAsset(resolved);
597
592
  if (utils_1.debugTools['asset-file-names-in-output']) {
598
593
  let resolvedPath = this.getAssetFilePath(resolved);
@@ -637,7 +632,7 @@ class ScopeHoistingPackager {
637
632
  sourceMap.offsetLines(lineCount + 1, lines);
638
633
  }
639
634
  if (map) {
640
- // @ts-expect-error TS2551
635
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
641
636
  sourceMap.addSourceMap(map, lineCount);
642
637
  }
643
638
  }
@@ -681,7 +676,7 @@ ${code}
681
676
  continue;
682
677
  code += depCode + '\n';
683
678
  if (sourceMap && map) {
684
- // @ts-expect-error TS2551
679
+ // @ts-expect-error TS2551 - addSourceMap method exists but missing from @parcel/source-map type definitions
685
680
  sourceMap.addSourceMap(map, lineCount);
686
681
  }
687
682
  lineCount += lines + 1;
@@ -759,7 +754,7 @@ ${code}
759
754
  }
760
755
  // If this asset is wrapped, we need to replace the exports namespace with `module.exports`,
761
756
  // which will be provided to us by the wrapper.
762
- if (this.wrappedAssets.has(asset.id) ||
757
+ if (this.wrappedAssets.has(asset) ||
763
758
  (this.bundle.env.outputFormat === 'commonjs' &&
764
759
  asset === this.bundle.getMainEntry())) {
765
760
  let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;
@@ -867,8 +862,9 @@ ${code}
867
862
  continue;
868
863
  }
869
864
  }
870
- // @ts-expect-error TS2322
871
- renamed = this.bundleGraph.getSymbolResolution(entry, imported, this.bundle).symbol;
865
+ renamed =
866
+ this.bundleGraph.getSymbolResolution(entry, imported, this.bundle)
867
+ .symbol || undefined;
872
868
  }
873
869
  }
874
870
  // Rename the specifier so that multiple local imports of the same imported specifier
@@ -913,7 +909,7 @@ ${code}
913
909
  return false;
914
910
  }
915
911
  return ((!this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved)) ||
916
- (this.wrappedAssets.has(resolved.id) && resolved !== parentAsset));
912
+ (this.wrappedAssets.has(resolved) && resolved !== parentAsset));
917
913
  }
918
914
  getSymbolResolution(parentAsset, resolved, imported, dep, replacements) {
919
915
  let { asset: resolvedAsset, exportSymbol, symbol, } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
@@ -942,12 +938,12 @@ ${code}
942
938
  // parcelRequire.register'ed there), or if it is indeed registered in this bundle.
943
939
  (!this.bundle.hasAsset(resolvedAsset) ||
944
940
  !this.shouldSkipAsset(resolvedAsset))) {
945
- let hoisted = this.hoistedRequires.get(dep.id);
941
+ let hoisted = this.hoistedRequires.get(dep);
946
942
  if (!hoisted) {
947
943
  hoisted = new Map();
948
- this.hoistedRequires.set(dep.id, hoisted);
944
+ this.hoistedRequires.set(dep, hoisted);
949
945
  }
950
- hoisted.set(resolvedAsset.id, `var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`);
946
+ hoisted.set(resolvedAsset, `var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`);
951
947
  }
952
948
  if (isWrapped) {
953
949
  this.needsPrelude = true;
@@ -982,7 +978,7 @@ ${code}
982
978
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
983
979
  // Resolve to the namespace object if requested or this is a CJS default interop reqiure.
984
980
  if (parentAsset === resolvedAsset &&
985
- this.wrappedAssets.has(resolvedAsset.id)) {
981
+ this.wrappedAssets.has(resolvedAsset)) {
986
982
  // Directly use module.exports for wrapped assets importing themselves.
987
983
  return 'module.exports';
988
984
  }
@@ -1019,7 +1015,7 @@ ${code}
1019
1015
  if (resolved.type !== 'js') {
1020
1016
  return ['', 0];
1021
1017
  }
1022
- let hoisted = this.hoistedRequires.get(dep.id);
1018
+ let hoisted = this.hoistedRequires.get(dep);
1023
1019
  let res = '';
1024
1020
  let lineCount = 0;
1025
1021
  let isWrapped = this.isWrapped(resolved, parentAsset);
@@ -1029,7 +1025,7 @@ ${code}
1029
1025
  // so it runs first.
1030
1026
  if (isWrapped &&
1031
1027
  !dep.meta.shouldWrap &&
1032
- (!hoisted || hoisted.keys().next().value !== resolved.id) &&
1028
+ (!hoisted || hoisted.keys().next().value !== resolved) &&
1033
1029
  !this.bundleGraph.isDependencySkipped(dep) &&
1034
1030
  !this.shouldSkipAsset(resolved)) {
1035
1031
  this.needsPrelude = true;
@@ -1056,7 +1052,7 @@ ${code}
1056
1052
  let prepend = '';
1057
1053
  let prependLineCount = 0;
1058
1054
  let append = '';
1059
- let shouldWrap = this.wrappedAssets.has(asset.id);
1055
+ let shouldWrap = this.wrappedAssets.has(asset);
1060
1056
  let usedSymbols = (0, nullthrows_1.default)(this.bundleGraph.getUsedSymbols(asset));
1061
1057
  let assetId = asset.meta.id;
1062
1058
  (0, assert_1.default)(typeof assetId === 'string');
@@ -1252,10 +1248,8 @@ ${code}
1252
1248
  this.usedHelpers.add('$parcel$global');
1253
1249
  }
1254
1250
  for (let helper of this.usedHelpers) {
1255
- // @ts-expect-error TS7053
1256
1251
  let currentHelper = helpers_1.helpers[helper];
1257
1252
  if (typeof currentHelper === 'function') {
1258
- // @ts-expect-error TS7053
1259
1253
  currentHelper = helpers_1.helpers[helper](this.bundle.env);
1260
1254
  }
1261
1255
  res += currentHelper;
@@ -75,7 +75,7 @@ class ESMOutputFormat {
75
75
  local,
76
76
  exportAs
77
77
  } of this.packager.exportedSymbols.values()) {
78
- if (this.packager.wrappedAssets.has(asset.id)) {
78
+ if (this.packager.wrappedAssets.has(asset)) {
79
79
  let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
80
80
  res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
81
81
  lines++;