@atlaspack/packager-js 2.14.5-canary.76 → 2.14.5-canary.77

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.
@@ -281,36 +281,69 @@ class ScopeHoistingPackager {
281
281
  }
282
282
  }
283
283
  });
284
- for (let wrappedAssetRoot of [...wrapped]) {
285
- this.bundle.traverseAssets((asset, _, actions) => {
286
- if (asset === wrappedAssetRoot) {
287
- return;
288
- }
289
- if (this.wrappedAssets.has(asset.id)) {
290
- actions.skipChildren();
291
- return;
292
- }
293
- // This prevents children of a wrapped asset also being wrapped - it's an "unsafe" optimisation
294
- // that should only be used when you know (or think you know) what you're doing.
295
- //
296
- // In particular this can force an async bundle to be scope hoisted where it previously would not be
297
- // due to the entry asset being wrapped.
298
- if (this.forceSkipWrapAssets.length > 0 && this.forceSkipWrapAssets.some(p => p === _path().default.relative(this.options.projectRoot, asset.filePath))) {
299
- this.logger.verbose({
300
- message: `Force skipping wrapping of ${_path().default.relative(this.options.projectRoot, asset.filePath)}`
301
- });
302
- actions.skipChildren();
303
- return;
304
- }
305
- if (!asset.meta.isConstantModule) {
306
- this.wrappedAssets.add(asset.id);
307
- wrapped.push(asset);
308
- }
309
- }, wrappedAssetRoot);
284
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
285
+ // Tracks which assets have been assigned to a wrap group
286
+ let assignedAssets = new Set();
287
+ for (let wrappedAsset of wrapped) {
288
+ this.bundle.traverseAssets((asset, _, actions) => {
289
+ if (asset === wrappedAsset) {
290
+ return;
291
+ }
292
+ if (this.wrappedAssets.has(asset.id)) {
293
+ actions.skipChildren();
294
+ return;
295
+ }
296
+ if (assignedAssets.has(asset) || this.isReExported(asset)) {
297
+ wrapped.push(asset);
298
+ this.wrappedAssets.add(asset.id);
299
+ actions.skipChildren();
300
+ return;
301
+ }
302
+ assignedAssets.add(asset);
303
+ }, wrappedAsset);
304
+ }
305
+ } else {
306
+ for (let wrappedAssetRoot of [...wrapped]) {
307
+ this.bundle.traverseAssets((asset, _, actions) => {
308
+ if (asset === wrappedAssetRoot) {
309
+ return;
310
+ }
311
+ if (this.wrappedAssets.has(asset.id)) {
312
+ actions.skipChildren();
313
+ return;
314
+ }
315
+ // This prevents children of a wrapped asset also being wrapped - it's an "unsafe" optimisation
316
+ // that should only be used when you know (or think you know) what you're doing.
317
+ //
318
+ // In particular this can force an async bundle to be scope hoisted where it previously would not be
319
+ // due to the entry asset being wrapped.
320
+ if (this.forceSkipWrapAssets.length > 0 && this.forceSkipWrapAssets.some(p => p === _path().default.relative(this.options.projectRoot, asset.filePath))) {
321
+ this.logger.verbose({
322
+ message: `Force skipping wrapping of ${_path().default.relative(this.options.projectRoot, asset.filePath)}`
323
+ });
324
+ actions.skipChildren();
325
+ return;
326
+ }
327
+ if (!asset.meta.isConstantModule) {
328
+ this.wrappedAssets.add(asset.id);
329
+ wrapped.push(asset);
330
+ }
331
+ }, wrappedAssetRoot);
332
+ }
310
333
  }
311
334
  this.assetOutputs = new Map(await queue.run());
312
335
  return wrapped;
313
336
  }
337
+ isReExported(asset) {
338
+ let parentSymbols = this.bundleGraph.getIncomingDependencies(asset).map(dep => this.bundleGraph.getAssetWithDependency(dep)).flatMap(parent => {
339
+ if (parent == null) {
340
+ return [];
341
+ }
342
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
343
+ });
344
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
345
+ return assetSymbols.some(assetSymbol => parentSymbols.some(parentSymbol => parentSymbol.symbol === assetSymbol.symbol));
346
+ }
314
347
  buildExportedSymbols() {
315
348
  if (!this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
316
349
  return;
@@ -489,13 +522,22 @@ class ScopeHoistingPackager {
489
522
  // outside our parcelRequire.register wrapper. This is safe because all
490
523
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
491
524
  // asset content where the import statement was.
492
- if (shouldWrap) {
493
- depContent.push(this.visitAsset(resolved));
525
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
526
+ if (!this.wrappedAssets.has(resolved.id)) {
527
+ let [depCode, depMap, depLines] = this.visitAsset(resolved);
528
+ res = depCode + '\n' + res;
529
+ lines += 1 + depLines;
530
+ map = depMap;
531
+ }
494
532
  } else {
495
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
496
- res = depCode + '\n' + res;
497
- lines += 1 + depLines;
498
- map = depMap;
533
+ if (shouldWrap) {
534
+ depContent.push(this.visitAsset(resolved));
535
+ } else {
536
+ let [depCode, depMap, depLines] = this.visitAsset(resolved);
537
+ res = depCode + '\n' + res;
538
+ lines += 1 + depLines;
539
+ map = depMap;
540
+ }
499
541
  }
500
542
  }
501
543
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/packager-js",
3
- "version": "2.14.5-canary.76+9c1fa1a29",
3
+ "version": "2.14.5-canary.77+85c52d3f7",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -15,16 +15,16 @@
15
15
  "node": ">= 16.0.0"
16
16
  },
17
17
  "dependencies": {
18
- "@atlaspack/diagnostic": "2.14.1-canary.144+9c1fa1a29",
19
- "@atlaspack/feature-flags": "2.14.1-canary.144+9c1fa1a29",
20
- "@atlaspack/plugin": "2.14.5-canary.76+9c1fa1a29",
21
- "@atlaspack/rust": "3.2.1-canary.76+9c1fa1a29",
22
- "@atlaspack/types": "2.14.5-canary.76+9c1fa1a29",
23
- "@atlaspack/utils": "2.14.5-canary.76+9c1fa1a29",
18
+ "@atlaspack/diagnostic": "2.14.1-canary.145+85c52d3f7",
19
+ "@atlaspack/feature-flags": "2.14.1-canary.145+85c52d3f7",
20
+ "@atlaspack/plugin": "2.14.5-canary.77+85c52d3f7",
21
+ "@atlaspack/rust": "3.2.1-canary.77+85c52d3f7",
22
+ "@atlaspack/types": "2.14.5-canary.77+85c52d3f7",
23
+ "@atlaspack/utils": "2.14.5-canary.77+85c52d3f7",
24
24
  "@parcel/source-map": "^2.1.1",
25
25
  "globals": "^13.2.0",
26
26
  "nullthrows": "^1.1.1"
27
27
  },
28
28
  "type": "commonjs",
29
- "gitHead": "9c1fa1a29518656838078ec75ae0ac6548daf5d6"
29
+ "gitHead": "85c52d3f7717b3c84a118d18ab98cfbfd71dcbd2"
30
30
  }
@@ -363,6 +363,7 @@ export class ScopeHoistingPackager {
363
363
  asset.getCode(),
364
364
  this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
365
365
  ]);
366
+
366
367
  return [asset.id, {code, map}];
367
368
  });
368
369
 
@@ -387,48 +388,97 @@ export class ScopeHoistingPackager {
387
388
  }
388
389
  });
389
390
 
390
- for (let wrappedAssetRoot of [...wrapped]) {
391
- this.bundle.traverseAssets((asset, _, actions) => {
392
- if (asset === wrappedAssetRoot) {
393
- return;
394
- }
391
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
392
+ // Tracks which assets have been assigned to a wrap group
393
+ let assignedAssets = new Set<Asset>();
395
394
 
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);
395
+ for (let wrappedAsset of wrapped) {
396
+ this.bundle.traverseAssets((asset, _, actions) => {
397
+ if (asset === wrappedAsset) {
398
+ return;
399
+ }
400
+
401
+ if (this.wrappedAssets.has(asset.id)) {
402
+ actions.skipChildren();
403
+ return;
404
+ }
405
+
406
+ if (assignedAssets.has(asset) || this.isReExported(asset)) {
407
+ wrapped.push(asset);
408
+ this.wrappedAssets.add(asset.id);
409
+
410
+ actions.skipChildren();
411
+ return;
412
+ }
413
+
414
+ assignedAssets.add(asset);
415
+ }, wrappedAsset);
416
+ }
417
+ } else {
418
+ for (let wrappedAssetRoot of [...wrapped]) {
419
+ this.bundle.traverseAssets((asset, _, actions) => {
420
+ if (asset === wrappedAssetRoot) {
421
+ return;
422
+ }
423
+
424
+ if (this.wrappedAssets.has(asset.id)) {
425
+ actions.skipChildren();
426
+ return;
427
+ }
428
+ // This prevents children of a wrapped asset also being wrapped - it's an "unsafe" optimisation
429
+ // that should only be used when you know (or think you know) what you're doing.
430
+ //
431
+ // In particular this can force an async bundle to be scope hoisted where it previously would not be
432
+ // due to the entry asset being wrapped.
433
+ if (
434
+ this.forceSkipWrapAssets.length > 0 &&
435
+ this.forceSkipWrapAssets.some(
436
+ (p) =>
437
+ p === path.relative(this.options.projectRoot, asset.filePath),
438
+ )
439
+ ) {
440
+ this.logger.verbose({
441
+ message: `Force skipping wrapping of ${path.relative(
442
+ this.options.projectRoot,
443
+ asset.filePath,
444
+ )}`,
445
+ });
446
+ actions.skipChildren();
447
+ return;
448
+ }
449
+ if (!asset.meta.isConstantModule) {
450
+ this.wrappedAssets.add(asset.id);
451
+ wrapped.push(asset);
452
+ }
453
+ }, wrappedAssetRoot);
454
+ }
426
455
  }
427
456
 
428
457
  this.assetOutputs = new Map(await queue.run());
458
+
429
459
  return wrapped;
430
460
  }
431
461
 
462
+ isReExported(asset: Asset): boolean {
463
+ let parentSymbols = this.bundleGraph
464
+ .getIncomingDependencies(asset)
465
+ .map((dep) => this.bundleGraph.getAssetWithDependency(dep))
466
+ .flatMap((parent) => {
467
+ if (parent == null) {
468
+ return [];
469
+ }
470
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
471
+ });
472
+
473
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
474
+
475
+ return assetSymbols.some((assetSymbol) =>
476
+ parentSymbols.some(
477
+ (parentSymbol) => parentSymbol.symbol === assetSymbol.symbol,
478
+ ),
479
+ );
480
+ }
481
+
432
482
  buildExportedSymbols() {
433
483
  if (
434
484
  !this.bundle.env.isLibrary ||
@@ -661,13 +711,24 @@ export class ScopeHoistingPackager {
661
711
  // outside our parcelRequire.register wrapper. This is safe because all
662
712
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
663
713
  // asset content where the import statement was.
664
- if (shouldWrap) {
665
- depContent.push(this.visitAsset(resolved));
714
+ if (getFeatureFlag('applyScopeHoistingImprovement')) {
715
+ if (!this.wrappedAssets.has(resolved.id)) {
716
+ let [depCode, depMap, depLines] =
717
+ this.visitAsset(resolved);
718
+ res = depCode + '\n' + res;
719
+ lines += 1 + depLines;
720
+ map = depMap;
721
+ }
666
722
  } else {
667
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
668
- res = depCode + '\n' + res;
669
- lines += 1 + depLines;
670
- map = depMap;
723
+ if (shouldWrap) {
724
+ depContent.push(this.visitAsset(resolved));
725
+ } else {
726
+ let [depCode, depMap, depLines] =
727
+ this.visitAsset(resolved);
728
+ res = depCode + '\n' + res;
729
+ lines += 1 + depLines;
730
+ map = depMap;
731
+ }
671
732
  }
672
733
  }
673
734