@parcel/packager-js 2.0.0-nightly.1349 → 2.0.0-nightly.1350

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.
@@ -93,6 +93,7 @@ class ScopeHoistingPackager {
93
93
  hoistedRequires = new Map();
94
94
  needsPrelude = false;
95
95
  usedHelpers = new Set();
96
+ externalAssets = new Set();
96
97
  constructor(options, bundleGraph, bundle, parcelRequireName) {
97
98
  this.options = options;
98
99
  this.bundleGraph = bundleGraph;
@@ -114,9 +115,23 @@ class ScopeHoistingPackager {
114
115
  // of each bundle group pointing at the sibling bundles. These can be
115
116
  // picked up by another bundler later at which point runtimes will be added.
116
117
  if (this.bundle.env.isLibrary || this.bundle.env.outputFormat === 'commonjs') {
117
- let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
118
- for (let b of bundles) {
119
- this.externals.set((0, _utils().relativeBundlePath)(this.bundle, b), new Map());
118
+ for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) {
119
+ let entry = b.getMainEntry();
120
+ let symbols = new Map();
121
+ if (entry && !this.isAsyncBundle && entry.type === 'js') {
122
+ this.externalAssets.add(entry);
123
+ let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set();
124
+ for (let s of usedSymbols) {
125
+ // If the referenced bundle is ESM, and we are importing '*', use 'default' instead.
126
+ // This matches the logic below in buildExportedSymbols.
127
+ let imported = s;
128
+ if (imported === '*' && b.env.outputFormat === 'esmodule') {
129
+ imported = 'default';
130
+ }
131
+ symbols.set(imported, this.getSymbolResolution(entry, entry, s));
132
+ }
133
+ }
134
+ this.externals.set((0, _utils().relativeBundlePath)(this.bundle, b), symbols);
120
135
  }
121
136
  }
122
137
  let res = '';
@@ -602,6 +617,9 @@ ${code}
602
617
  }
603
618
  }
604
619
  }
620
+ isWrapped(resolved, parentAsset) {
621
+ return !this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved) || this.wrappedAssets.has(resolved.id) && resolved !== parentAsset;
622
+ }
605
623
  getSymbolResolution(parentAsset, resolved, imported, dep) {
606
624
  var _resolvedAsset$symbol;
607
625
  let {
@@ -614,10 +632,14 @@ ${code}
614
632
  // that is actually unused but we still need a placeholder value.
615
633
  return '{}';
616
634
  }
617
- let isWrapped = !this.bundle.hasAsset(resolvedAsset) || this.wrappedAssets.has(resolvedAsset.id) && resolvedAsset !== parentAsset;
635
+ let isWrapped = this.isWrapped(resolvedAsset, parentAsset);
618
636
  let staticExports = resolvedAsset.meta.staticExports !== false;
619
637
  let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset);
620
638
 
639
+ // External CommonJS dependencies need to be accessed as an object property rather than imported
640
+ // directly to maintain live binding.
641
+ let isExternalCommonJS = !isWrapped && this.bundle.env.isLibrary && this.bundle.env.outputFormat === 'commonjs' && !this.bundle.hasAsset(resolvedAsset);
642
+
621
643
  // If the resolved asset is wrapped, but imported at the top-level by this asset,
622
644
  // then we hoist parcelRequire calls to the top of this asset so side effects run immediately.
623
645
  if (isWrapped && dep && !(dep !== null && dep !== void 0 && dep.meta.shouldWrap) && symbol !== false && (
@@ -656,7 +678,7 @@ ${code}
656
678
  } else {
657
679
  return obj;
658
680
  }
659
- } else if ((!staticExports || isWrapped || !symbol) && resolvedAsset !== parentAsset) {
681
+ } else if ((!staticExports || isWrapped || !symbol || isExternalCommonJS) && resolvedAsset !== parentAsset) {
660
682
  // If the resolved asset is wrapped or has non-static exports,
661
683
  // we need to use a member access off the namespace object rather
662
684
  // than a direct reference. If importing default from a CJS module,
@@ -681,7 +703,7 @@ ${code}
681
703
  let hoisted = this.hoistedRequires.get(dep.id);
682
704
  let res = '';
683
705
  let lineCount = 0;
684
- let isWrapped = !this.bundle.hasAsset(resolved) || this.wrappedAssets.has(resolved.id) && resolved !== parentAsset;
706
+ let isWrapped = this.isWrapped(resolved, parentAsset);
685
707
 
686
708
  // If the resolved asset is wrapped and is imported in the top-level by this asset,
687
709
  // we need to run side effects when this asset runs. If the resolved asset is not
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/packager-js",
3
- "version": "2.0.0-nightly.1349+824ddbe70",
3
+ "version": "2.0.0-nightly.1350+4b8323477",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,16 +17,16 @@
17
17
  "source": "src/index.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "2.0.0-nightly.1347+824ddbe70"
20
+ "parcel": "2.0.0-nightly.1348+4b8323477"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/diagnostic": "2.0.0-nightly.1349+824ddbe70",
24
- "@parcel/hash": "2.9.4-nightly.2972+824ddbe70",
25
- "@parcel/plugin": "2.0.0-nightly.1349+824ddbe70",
23
+ "@parcel/diagnostic": "2.0.0-nightly.1350+4b8323477",
24
+ "@parcel/hash": "2.9.4-nightly.2973+4b8323477",
25
+ "@parcel/plugin": "2.0.0-nightly.1350+4b8323477",
26
26
  "@parcel/source-map": "^2.1.1",
27
- "@parcel/utils": "2.0.0-nightly.1349+824ddbe70",
27
+ "@parcel/utils": "2.0.0-nightly.1350+4b8323477",
28
28
  "globals": "^13.2.0",
29
29
  "nullthrows": "^1.1.1"
30
30
  },
31
- "gitHead": "824ddbe70ac5c513062a114eb6dc0f163e4d234c"
31
+ "gitHead": "4b8323477dc806796d88c770aa96429d8095dc5d"
32
32
  }
@@ -94,6 +94,7 @@ export class ScopeHoistingPackager {
94
94
  hoistedRequires: Map<string, Map<string, string>> = new Map();
95
95
  needsPrelude: boolean = false;
96
96
  usedHelpers: Set<string> = new Set();
97
+ externalAssets: Set<Asset> = new Set();
97
98
 
98
99
  constructor(
99
100
  options: PluginOptions,
@@ -130,9 +131,25 @@ export class ScopeHoistingPackager {
130
131
  this.bundle.env.isLibrary ||
131
132
  this.bundle.env.outputFormat === 'commonjs'
132
133
  ) {
133
- let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
134
- for (let b of bundles) {
135
- this.externals.set(relativeBundlePath(this.bundle, b), new Map());
134
+ for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) {
135
+ let entry = b.getMainEntry();
136
+ let symbols = new Map();
137
+ if (entry && !this.isAsyncBundle && entry.type === 'js') {
138
+ this.externalAssets.add(entry);
139
+
140
+ let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set();
141
+ for (let s of usedSymbols) {
142
+ // If the referenced bundle is ESM, and we are importing '*', use 'default' instead.
143
+ // This matches the logic below in buildExportedSymbols.
144
+ let imported = s;
145
+ if (imported === '*' && b.env.outputFormat === 'esmodule') {
146
+ imported = 'default';
147
+ }
148
+ symbols.set(imported, this.getSymbolResolution(entry, entry, s));
149
+ }
150
+ }
151
+
152
+ this.externals.set(relativeBundlePath(this.bundle, b), symbols);
136
153
  }
137
154
  }
138
155
 
@@ -756,6 +773,13 @@ ${code}
756
773
  }
757
774
  }
758
775
 
776
+ isWrapped(resolved: Asset, parentAsset: Asset): boolean {
777
+ return (
778
+ (!this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved)) ||
779
+ (this.wrappedAssets.has(resolved.id) && resolved !== parentAsset)
780
+ );
781
+ }
782
+
759
783
  getSymbolResolution(
760
784
  parentAsset: Asset,
761
785
  resolved: Asset,
@@ -777,13 +801,18 @@ ${code}
777
801
  return '{}';
778
802
  }
779
803
 
780
- let isWrapped =
781
- !this.bundle.hasAsset(resolvedAsset) ||
782
- (this.wrappedAssets.has(resolvedAsset.id) &&
783
- resolvedAsset !== parentAsset);
804
+ let isWrapped = this.isWrapped(resolvedAsset, parentAsset);
784
805
  let staticExports = resolvedAsset.meta.staticExports !== false;
785
806
  let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset);
786
807
 
808
+ // External CommonJS dependencies need to be accessed as an object property rather than imported
809
+ // directly to maintain live binding.
810
+ let isExternalCommonJS =
811
+ !isWrapped &&
812
+ this.bundle.env.isLibrary &&
813
+ this.bundle.env.outputFormat === 'commonjs' &&
814
+ !this.bundle.hasAsset(resolvedAsset);
815
+
787
816
  // If the resolved asset is wrapped, but imported at the top-level by this asset,
788
817
  // then we hoist parcelRequire calls to the top of this asset so side effects run immediately.
789
818
  if (
@@ -849,7 +878,7 @@ ${code}
849
878
  return obj;
850
879
  }
851
880
  } else if (
852
- (!staticExports || isWrapped || !symbol) &&
881
+ (!staticExports || isWrapped || !symbol || isExternalCommonJS) &&
853
882
  resolvedAsset !== parentAsset
854
883
  ) {
855
884
  // If the resolved asset is wrapped or has non-static exports,
@@ -887,9 +916,7 @@ ${code}
887
916
  let hoisted = this.hoistedRequires.get(dep.id);
888
917
  let res = '';
889
918
  let lineCount = 0;
890
- let isWrapped =
891
- !this.bundle.hasAsset(resolved) ||
892
- (this.wrappedAssets.has(resolved.id) && resolved !== parentAsset);
919
+ let isWrapped = this.isWrapped(resolved, parentAsset);
893
920
 
894
921
  // If the resolved asset is wrapped and is imported in the top-level by this asset,
895
922
  // we need to run side effects when this asset runs. If the resolved asset is not