@parcel/packager-js 2.0.0-canary.1542 → 2.0.0-dev.1424

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.
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ESMOutputFormat = void 0;
7
- var _utils = require("./utils");
8
7
  class ESMOutputFormat {
9
8
  constructor(packager) {
10
9
  this.packager = packager;
@@ -23,9 +22,6 @@ class ESMOutputFormat {
23
22
  namespaceSpecifier = `* as ${symbol}`;
24
23
  } else {
25
24
  let specifier = imported;
26
- if (!(0, _utils.isValidIdentifier)(specifier)) {
27
- specifier = JSON.stringify(specifier);
28
- }
29
25
  if (symbol !== imported) {
30
26
  specifier += ` as ${symbol}`;
31
27
  }
@@ -82,10 +78,7 @@ class ESMOutputFormat {
82
78
  }
83
79
  for (let as of exportAs) {
84
80
  let specifier = local;
85
- if (as !== local) {
86
- if (!(0, _utils.isValidIdentifier)(as)) {
87
- as = JSON.stringify(as);
88
- }
81
+ if (exportAs !== local) {
89
82
  specifier += ` as ${as}`;
90
83
  }
91
84
  exportSpecifiers.push(specifier);
@@ -61,6 +61,11 @@ var _utils2 = require("./utils");
61
61
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
62
62
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
63
63
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
64
+ // https://262.ecma-international.org/6.0/#sec-names-and-keywords
65
+ const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
66
+ const ID_START_RE = /^[$_\p{ID_Start}]/u;
67
+ const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
68
+
64
69
  // General regex used to replace imports with the resolved code, references with resolutions,
65
70
  // and count the number of newlines in the file for source maps.
66
71
  const REPLACEMENT_RE = /\n|import\s+"([0-9a-f]{16}:.+?)";|(?:\$[0-9a-f]{16}\$exports)|(?:\$[0-9a-f]{16}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
@@ -111,10 +116,23 @@ class ScopeHoistingPackager {
111
116
  // of each bundle group pointing at the sibling bundles. These can be
112
117
  // picked up by another bundler later at which point runtimes will be added.
113
118
  if (this.bundle.env.isLibrary || this.bundle.env.outputFormat === 'commonjs') {
114
- for (let b of this.bundleGraph.getReferencedBundles(this.bundle, {
115
- recursive: false
116
- })) {
117
- this.externals.set((0, _utils().relativeBundlePath)(this.bundle, b), new Map());
119
+ for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) {
120
+ let entry = b.getMainEntry();
121
+ let symbols = new Map();
122
+ if (entry && !this.isAsyncBundle && entry.type === 'js') {
123
+ this.externalAssets.add(entry);
124
+ let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set();
125
+ for (let s of usedSymbols) {
126
+ // If the referenced bundle is ESM, and we are importing '*', use 'default' instead.
127
+ // This matches the logic below in buildExportedSymbols.
128
+ let imported = s;
129
+ if (imported === '*' && b.env.outputFormat === 'esmodule') {
130
+ imported = 'default';
131
+ }
132
+ symbols.set(imported, this.getSymbolResolution(entry, entry, s));
133
+ }
134
+ }
135
+ this.externals.set((0, _utils().relativeBundlePath)(this.bundle, b), symbols);
118
136
  }
119
137
  }
120
138
  let res = '';
@@ -238,7 +256,7 @@ class ScopeHoistingPackager {
238
256
  map
239
257
  }];
240
258
  });
241
- if (asset.meta.shouldWrap || this.bundle.env.sourceType === 'script' || this.bundleGraph.isAssetReferenced(this.bundle, asset) || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')) {
259
+ if (asset.meta.shouldWrap || this.isAsyncBundle || this.bundle.env.sourceType === 'script' || this.bundleGraph.isAssetReferenced(this.bundle, asset) || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')) {
242
260
  if (!asset.meta.isConstantModule) {
243
261
  this.wrappedAssets.add(asset.id);
244
262
  wrapped.push(asset);
@@ -264,14 +282,13 @@ class ScopeHoistingPackager {
264
282
  return wrapped;
265
283
  }
266
284
  buildExportedSymbols() {
267
- if (!this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
285
+ if (this.isAsyncBundle || !this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
268
286
  return;
269
287
  }
270
288
 
271
289
  // TODO: handle ESM exports of wrapped entry assets...
272
290
  let entry = this.bundle.getMainEntry();
273
291
  if (entry && !this.wrappedAssets.has(entry.id)) {
274
- let hasNamespace = entry.symbols.hasExportSymbol('*');
275
292
  for (let {
276
293
  asset,
277
294
  exportAs,
@@ -280,12 +297,6 @@ class ScopeHoistingPackager {
280
297
  } of this.bundleGraph.getExportedSymbols(entry)) {
281
298
  if (typeof symbol === 'string') {
282
299
  var _this$exportedSymbols, _entry$symbols$get2;
283
- // If the module has a namespace (e.g. commonjs), and this is not an entry, only export the namespace
284
- // as default, without individual exports. This mirrors the importing logic in addExternal, avoiding
285
- // extra unused exports and potential for non-identifier export names.
286
- if (hasNamespace && this.isAsyncBundle && exportAs !== '*') {
287
- continue;
288
- }
289
300
  let symbols = (_this$exportedSymbols = this.exportedSymbols.get(symbol === '*' ? (0, _nullthrows().default)((_entry$symbols$get2 = entry.symbols.get('*')) === null || _entry$symbols$get2 === void 0 ? void 0 : _entry$symbols$get2.local) : symbol)) === null || _this$exportedSymbols === void 0 ? void 0 : _this$exportedSymbols.exportAs;
290
301
  if (!symbols) {
291
302
  symbols = [];
@@ -320,8 +331,8 @@ class ScopeHoistingPackager {
320
331
  }
321
332
  }
322
333
  getTopLevelName(name) {
323
- name = (0, _utils2.makeValidIdentifier)(name);
324
- if (this.globalNames.has(name)) {
334
+ name = name.replace(NON_ID_CONTINUE_RE, '');
335
+ if (!ID_START_RE.test(name) || this.globalNames.has(name)) {
325
336
  name = '_' + name;
326
337
  }
327
338
  let count = this.topLevelNames.get(name);
@@ -333,7 +344,7 @@ class ScopeHoistingPackager {
333
344
  return name + count;
334
345
  }
335
346
  getPropertyAccess(obj, property) {
336
- if ((0, _utils2.isValidIdentifier)(property)) {
347
+ if (IDENTIFIER_RE.test(property)) {
337
348
  return `${obj}.${property}`;
338
349
  }
339
350
  return `${obj}[${JSON.stringify(property)}]`;
@@ -390,7 +401,7 @@ class ScopeHoistingPackager {
390
401
  code = code.replace('$parcel$filenameReplace', relPath);
391
402
  }
392
403
  let [depMap, replacements] = this.buildReplacements(asset, deps);
393
- let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps, replacements);
404
+ let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps);
394
405
  if (prependLines > 0) {
395
406
  sourceMap === null || sourceMap === void 0 || sourceMap.offsetLines(1, prependLines);
396
407
  code = prepend + code;
@@ -520,7 +531,7 @@ ${code}
520
531
  let replacements = new Map();
521
532
  for (let dep of deps) {
522
533
  let specifierType = dep.specifierType === 'esm' ? `:${dep.specifierType}` : '';
523
- depMap.get(`${assetId}:${(0, _utils2.getSpecifier)(dep)}${!dep.meta.placeholder ? specifierType : ''}`).push(dep);
534
+ depMap.get(`${assetId}:${(0, _utils2.getSpecifier)(dep)}${dep.meta.isESM ? specifierType : ''}`).push(dep);
524
535
  let asyncResolution = this.bundleGraph.resolveAsyncDependency(dep, this.bundle);
525
536
  let resolved = (asyncResolution === null || asyncResolution === void 0 ? void 0 : asyncResolution.type) === 'asset' ?
526
537
  // Prefer the underlying asset over a runtime to load it. It will
@@ -532,16 +543,6 @@ ${code}
532
543
  if (!resolved) {
533
544
  continue;
534
545
  }
535
-
536
- // Handle imports from other bundles in libraries.
537
- if (this.bundle.env.isLibrary && !this.bundle.hasAsset(resolved)) {
538
- let referencedBundle = this.bundleGraph.getReferencedBundle(dep, this.bundle);
539
- if (referencedBundle && referencedBundle.getMainEntry() === resolved && referencedBundle.type === 'js' && !this.bundleGraph.isAssetReferenced(referencedBundle, resolved)) {
540
- this.addExternal(dep, replacements, referencedBundle);
541
- this.externalAssets.add(resolved);
542
- continue;
543
- }
544
- }
545
546
  for (let [imported, {
546
547
  local
547
548
  }] of dep.symbols) {
@@ -574,7 +575,7 @@ ${code}
574
575
  }
575
576
  return [depMap, replacements];
576
577
  }
577
- addExternal(dep, replacements, referencedBundle) {
578
+ addExternal(dep, replacements) {
578
579
  if (this.bundle.env.outputFormat === 'global') {
579
580
  throw new (_diagnostic().default)({
580
581
  diagnostic: {
@@ -586,16 +587,12 @@ ${code}
586
587
  }
587
588
  });
588
589
  }
589
- let specifier = dep.specifier;
590
- if (referencedBundle) {
591
- specifier = (0, _utils().relativeBundlePath)(this.bundle, referencedBundle);
592
- }
593
590
 
594
591
  // Map of DependencySpecifier -> Map<ExportedSymbol, Identifier>>
595
- let external = this.externals.get(specifier);
592
+ let external = this.externals.get(dep.specifier);
596
593
  if (!external) {
597
594
  external = new Map();
598
- this.externals.set(specifier, external);
595
+ this.externals.set(dep.specifier, external);
599
596
  }
600
597
  for (let [imported, {
601
598
  local
@@ -612,13 +609,7 @@ ${code}
612
609
  if (this.bundle.env.outputFormat === 'commonjs') {
613
610
  renamed = external.get('*');
614
611
  if (!renamed) {
615
- if (referencedBundle) {
616
- var _entry$symbols$get$lo, _entry$symbols$get3;
617
- let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
618
- renamed = (_entry$symbols$get$lo = (_entry$symbols$get3 = entry.symbols.get('*')) === null || _entry$symbols$get3 === void 0 ? void 0 : _entry$symbols$get3.local) !== null && _entry$symbols$get$lo !== void 0 ? _entry$symbols$get$lo : `$${String(entry.meta.id)}$exports`;
619
- } else {
620
- renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
621
- }
612
+ renamed = this.getTopLevelName(`$${this.bundle.publicId}$${dep.specifier}`);
622
613
  external.set('*', renamed);
623
614
  }
624
615
  if (local !== '*' && replacements) {
@@ -634,55 +625,19 @@ ${code}
634
625
  replacements.set(local, replacement);
635
626
  }
636
627
  } else {
637
- let property;
638
- if (referencedBundle) {
639
- let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
640
- if (entry.symbols.hasExportSymbol('*')) {
641
- // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
642
- // This mirrors the logic in buildExportedSymbols.
643
- property = imported;
644
- imported = (referencedBundle === null || referencedBundle === void 0 ? void 0 : referencedBundle.env.outputFormat) === 'esmodule' ? 'default' : '*';
645
- } else {
646
- if (imported === '*') {
647
- let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
648
- if (local === '*') {
649
- // Re-export all symbols.
650
- for (let exported of exportedSymbols) {
651
- if (exported.symbol) {
652
- external.set(exported.exportSymbol, exported.symbol);
653
- }
654
- }
655
- continue;
656
- }
657
- }
658
- renamed = this.bundleGraph.getSymbolResolution(entry, imported, this.bundle).symbol;
659
- }
660
- }
661
-
662
628
  // Rename the specifier so that multiple local imports of the same imported specifier
663
629
  // are deduplicated. We have to prefix the imported name with the bundle id so that
664
630
  // local variables do not shadow it.
665
- if (!renamed) {
666
- if (this.exportedSymbols.has(local)) {
667
- renamed = local;
668
- } else if (imported === 'default' || imported === '*') {
669
- renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
670
- } else {
671
- renamed = this.getTopLevelName(`$${this.bundle.publicId}$${imported}`);
672
- }
631
+ if (this.exportedSymbols.has(local)) {
632
+ renamed = local;
633
+ } else if (imported === 'default' || imported === '*') {
634
+ renamed = this.getTopLevelName(`$${this.bundle.publicId}$${dep.specifier}`);
635
+ } else {
636
+ renamed = this.getTopLevelName(`$${this.bundle.publicId}$${imported}`);
673
637
  }
674
638
  external.set(imported, renamed);
675
639
  if (local !== '*' && replacements) {
676
- let replacement = renamed;
677
- if (property === '*') {
678
- replacement = renamed;
679
- } else if (property === 'default') {
680
- replacement = `($parcel$interopDefault(${renamed}))`;
681
- this.usedHelpers.add('$parcel$interopDefault');
682
- } else if (property) {
683
- replacement = this.getPropertyAccess(renamed, property);
684
- }
685
- replacements.set(local, replacement);
640
+ replacements.set(local, renamed);
686
641
  }
687
642
  }
688
643
  }
@@ -694,7 +649,8 @@ ${code}
694
649
  }
695
650
  return !this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved) || this.wrappedAssets.has(resolved.id) && resolved !== parentAsset;
696
651
  }
697
- getSymbolResolution(parentAsset, resolved, imported, dep, replacements) {
652
+ getSymbolResolution(parentAsset, resolved, imported, dep) {
653
+ var _resolvedAsset$symbol;
698
654
  let {
699
655
  asset: resolvedAsset,
700
656
  exportSymbol,
@@ -732,7 +688,7 @@ ${code}
732
688
 
733
689
  // If this is an ESM default import of a CJS module with a `default` symbol,
734
690
  // and no __esModule flag, we need to resolve to the namespace instead.
735
- let isDefaultInterop = exportSymbol === 'default' && staticExports && !isWrapped && ((dep === null || dep === void 0 ? void 0 : dep.meta.kind) === 'Import' || (dep === null || dep === void 0 ? void 0 : dep.meta.kind) === 'Export') && resolvedAsset.symbols.hasExportSymbol('*') && resolvedAsset.symbols.hasExportSymbol('default') && !resolvedAsset.symbols.hasExportSymbol('__esModule');
691
+ let isDefaultInterop = exportSymbol === 'default' && staticExports && !isWrapped && (dep === null || dep === void 0 ? void 0 : dep.meta.isESM) && resolvedAsset.symbols.hasExportSymbol('*') && resolvedAsset.symbols.hasExportSymbol('default') && !resolvedAsset.symbols.hasExportSymbol('__esModule');
736
692
 
737
693
  // Find the namespace object for the resolved module. If wrapped and this
738
694
  // is an inline require (not top-level), use a parcelRequire call, otherwise
@@ -740,17 +696,9 @@ ${code}
740
696
  // namespace export symbol.
741
697
  let assetId = resolvedAsset.meta.id;
742
698
  (0, _assert().default)(typeof assetId === 'string');
743
- let obj;
744
- if (isWrapped && (!dep || dep !== null && dep !== void 0 && dep.meta.shouldWrap)) {
745
- // Wrap in extra parenthesis to not change semantics, e.g.`new (parcelRequire("..."))()`.
746
- obj = `(parcelRequire(${JSON.stringify(publicId)}))`;
747
- } else if (isWrapped && dep) {
748
- obj = `$${publicId}`;
749
- } else {
750
- var _resolvedAsset$symbol;
751
- obj = ((_resolvedAsset$symbol = resolvedAsset.symbols.get('*')) === null || _resolvedAsset$symbol === void 0 ? void 0 : _resolvedAsset$symbol.local) || `$${assetId}$exports`;
752
- obj = (replacements === null || replacements === void 0 ? void 0 : replacements.get(obj)) || obj;
753
- }
699
+ let obj = isWrapped && (!dep || dep !== null && dep !== void 0 && dep.meta.shouldWrap) ?
700
+ // Wrap in extra parenthesis to not change semantics, e.g.`new (parcelRequire("..."))()`.
701
+ `(parcelRequire(${JSON.stringify(publicId)}))` : isWrapped && dep ? `$${publicId}` : ((_resolvedAsset$symbol = resolvedAsset.symbols.get('*')) === null || _resolvedAsset$symbol === void 0 ? void 0 : _resolvedAsset$symbol.local) || `$${assetId}$exports`;
754
702
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
755
703
  // Resolve to the namespace object if requested or this is a CJS default interop reqiure.
756
704
  if (parentAsset === resolvedAsset && this.wrappedAssets.has(resolvedAsset.id)) {
@@ -760,12 +708,12 @@ ${code}
760
708
  return obj;
761
709
  }
762
710
  } else if ((!staticExports || isWrapped || !symbol || isExternalCommonJS) && resolvedAsset !== parentAsset) {
711
+ var _dep$meta;
763
712
  // If the resolved asset is wrapped or has non-static exports,
764
713
  // we need to use a member access off the namespace object rather
765
714
  // than a direct reference. If importing default from a CJS module,
766
715
  // use a helper to check the __esModule flag at runtime.
767
- let kind = dep === null || dep === void 0 ? void 0 : dep.meta.kind;
768
- if ((!dep || kind === 'Import' || kind === 'Export') && exportSymbol === 'default' && resolvedAsset.symbols.hasExportSymbol('*') && this.needsDefaultInterop(resolvedAsset)) {
716
+ if ((!dep || (_dep$meta = dep.meta) !== null && _dep$meta !== void 0 && _dep$meta.isESM) && exportSymbol === 'default' && resolvedAsset.symbols.hasExportSymbol('*') && this.needsDefaultInterop(resolvedAsset)) {
769
717
  this.usedHelpers.add('$parcel$interopDefault');
770
718
  return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
771
719
  } else {
@@ -774,7 +722,7 @@ ${code}
774
722
  } else if (!symbol) {
775
723
  (0, _assert().default)(false, 'Asset was skipped or not found.');
776
724
  } else {
777
- return (replacements === null || replacements === void 0 ? void 0 : replacements.get(symbol)) || symbol;
725
+ return symbol;
778
726
  }
779
727
  }
780
728
  getHoistedParcelRequires(parentAsset, dep, resolved) {
@@ -801,7 +749,7 @@ ${code}
801
749
  }
802
750
  return [res, lineCount];
803
751
  }
804
- buildAssetPrelude(asset, deps, replacements) {
752
+ buildAssetPrelude(asset, deps) {
805
753
  let prepend = '';
806
754
  let prependLineCount = 0;
807
755
  let append = '';
@@ -819,15 +767,13 @@ ${code}
819
767
  // The one case where this isn't true is in ESM library entries, where the only
820
768
  // dependency on * is the entry dependency. In this case, we will use ESM exports
821
769
  // instead of the namespace object.
822
- usedSymbols.has('*') && (this.bundle.env.outputFormat !== 'esmodule' || !this.bundle.env.isLibrary || asset !== this.bundle.getMainEntry() || this.bundleGraph.getIncomingDependencies(asset).some(dep => !dep.isEntry && this.bundle.hasDependency(dep) && (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'))) ||
770
+ usedSymbols.has('*') && (this.bundle.env.outputFormat !== 'esmodule' || !this.bundle.env.isLibrary || asset !== this.bundle.getMainEntry() || this.bundleGraph.getIncomingDependencies(asset).some(dep => !dep.isEntry && (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'))) ||
823
771
  // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
824
772
  // we fallback on the namespace object.
825
773
  asset.symbols.hasExportSymbol('*') && [...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s)) ||
826
774
  // If the exports has this asset's namespace (e.g. ESM output from CJS input),
827
775
  // include the namespace object for the default export.
828
- this.exportedSymbols.has(`$${assetId}$exports`) ||
829
- // CommonJS library bundle entries always need a namespace.
830
- this.bundle.env.isLibrary && this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry();
776
+ this.exportedSymbols.has(`$${assetId}$exports`);
831
777
 
832
778
  // If the asset doesn't have static exports, should wrap, the namespace is used,
833
779
  // or we need default interop, then we need to synthesize a namespace object for
@@ -878,7 +824,7 @@ ${code}
878
824
  if (isWrapped || resolved.meta.staticExports === false || (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
879
825
  // an empty asset
880
826
  !resolved.meta.hasCJSExports && resolved.symbols.hasExportSymbol('*')) {
881
- let obj = this.getSymbolResolution(asset, resolved, '*', dep, replacements);
827
+ let obj = this.getSymbolResolution(asset, resolved, '*', dep);
882
828
  append += `$parcel$exportWildcard($${assetId}$exports, ${obj});\n`;
883
829
  this.usedHelpers.add('$parcel$exportWildcard');
884
830
  } else {
@@ -888,7 +834,7 @@ ${code}
888
834
  symbol === '__esModule') {
889
835
  continue;
890
836
  }
891
- let resolvedSymbol = this.getSymbolResolution(asset, resolved, symbol, undefined, replacements);
837
+ let resolvedSymbol = this.getSymbolResolution(asset, resolved, symbol);
892
838
  let get = this.buildFunctionExpression([], resolvedSymbol);
893
839
  let set = asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolvedSymbol} = v`) : '';
894
840
  prepend += `$parcel$export($${assetId}$exports, ${JSON.stringify(symbol)}, ${get}${set});\n`;
@@ -927,7 +873,7 @@ ${code}
927
873
  // additional assignments after each mutation of the original binding.
928
874
  prepend += `\n${usedExports.map(exp => {
929
875
  var _asset$symbols$get2;
930
- let resolved = this.getSymbolResolution(asset, asset, exp, undefined, replacements);
876
+ let resolved = this.getSymbolResolution(asset, asset, exp);
931
877
  let get = this.buildFunctionExpression([], resolved);
932
878
  let isEsmExport = !!((_asset$symbols$get2 = asset.symbols.get(exp)) !== null && _asset$symbols$get2 !== void 0 && (_asset$symbols$get2 = _asset$symbols$get2.meta) !== null && _asset$symbols$get2 !== void 0 && _asset$symbols$get2.isEsm);
933
879
  let set = !isEsmExport && asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`) : '';
package/lib/utils.js CHANGED
@@ -4,8 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getSpecifier = getSpecifier;
7
- exports.isValidIdentifier = isValidIdentifier;
8
- exports.makeValidIdentifier = makeValidIdentifier;
9
7
  exports.replaceScriptDependencies = replaceScriptDependencies;
10
8
  function _nullthrows() {
11
9
  const data = _interopRequireDefault(require("nullthrows"));
@@ -50,19 +48,4 @@ function getSpecifier(dep) {
50
48
  return dep.meta.placeholder;
51
49
  }
52
50
  return dep.specifier;
53
- }
54
-
55
- // https://262.ecma-international.org/6.0/#sec-names-and-keywords
56
- const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
57
- const ID_START_RE = /^[$_\p{ID_Start}]/u;
58
- const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
59
- function isValidIdentifier(id) {
60
- return IDENTIFIER_RE.test(id);
61
- }
62
- function makeValidIdentifier(name) {
63
- name = name.replace(NON_ID_CONTINUE_RE, '');
64
- if (!ID_START_RE.test(name)) {
65
- name = '_' + name;
66
- }
67
- return name;
68
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/packager-js",
3
- "version": "2.0.0-canary.1542+b6281c770",
3
+ "version": "2.0.0-dev.1424+7f6b4dbbc",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,17 +17,17 @@
17
17
  "source": "src/index.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.0.0-canary.1540+b6281c770"
20
+ "parcel": "^2.0.0-dev.1422+7f6b4dbbc"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/diagnostic": "2.0.0-canary.1542+b6281c770",
24
- "@parcel/plugin": "2.0.0-canary.1542+b6281c770",
25
- "@parcel/rust": "2.12.1-canary.3165+b6281c770",
23
+ "@parcel/diagnostic": "2.0.0-dev.1424+7f6b4dbbc",
24
+ "@parcel/plugin": "2.0.0-dev.1424+7f6b4dbbc",
25
+ "@parcel/rust": "2.12.1-dev.3238+7f6b4dbbc",
26
26
  "@parcel/source-map": "^2.1.1",
27
- "@parcel/types": "2.0.0-canary.1542+b6281c770",
28
- "@parcel/utils": "2.0.0-canary.1542+b6281c770",
27
+ "@parcel/types": "2.0.0-dev.1424+7f6b4dbbc",
28
+ "@parcel/utils": "2.0.0-dev.1424+7f6b4dbbc",
29
29
  "globals": "^13.2.0",
30
30
  "nullthrows": "^1.1.1"
31
31
  },
32
- "gitHead": "b6281c77053015bce0e5b715b0f8d187be177479"
32
+ "gitHead": "7f6b4dbbc56a203e0fce8794856c03598c4f6708"
33
33
  }
@@ -3,7 +3,6 @@ import type {
3
3
  ScopeHoistingPackager,
4
4
  OutputFormat,
5
5
  } from './ScopeHoistingPackager';
6
- import {isValidIdentifier} from './utils';
7
6
 
8
7
  export class ESMOutputFormat implements OutputFormat {
9
8
  packager: ScopeHoistingPackager;
@@ -26,9 +25,6 @@ export class ESMOutputFormat implements OutputFormat {
26
25
  namespaceSpecifier = `* as ${symbol}`;
27
26
  } else {
28
27
  let specifier = imported;
29
- if (!isValidIdentifier(specifier)) {
30
- specifier = JSON.stringify(specifier);
31
- }
32
28
  if (symbol !== imported) {
33
29
  specifier += ` as ${symbol}`;
34
30
  }
@@ -97,10 +93,7 @@ export class ESMOutputFormat implements OutputFormat {
97
93
 
98
94
  for (let as of exportAs) {
99
95
  let specifier = local;
100
- if (as !== local) {
101
- if (!isValidIdentifier(as)) {
102
- as = JSON.stringify(as);
103
- }
96
+ if (exportAs !== local) {
104
97
  specifier += ` as ${as}`;
105
98
  }
106
99
 
@@ -28,12 +28,12 @@ import {ESMOutputFormat} from './ESMOutputFormat';
28
28
  import {CJSOutputFormat} from './CJSOutputFormat';
29
29
  import {GlobalOutputFormat} from './GlobalOutputFormat';
30
30
  import {prelude, helpers, bundleQueuePrelude, fnExpr} from './helpers';
31
- import {
32
- replaceScriptDependencies,
33
- getSpecifier,
34
- isValidIdentifier,
35
- makeValidIdentifier,
36
- } from './utils';
31
+ import {replaceScriptDependencies, getSpecifier} from './utils';
32
+
33
+ // https://262.ecma-international.org/6.0/#sec-names-and-keywords
34
+ const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
35
+ const ID_START_RE = /^[$_\p{ID_Start}]/u;
36
+ const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
37
37
 
38
38
  // General regex used to replace imports with the resolved code, references with resolutions,
39
39
  // and count the number of newlines in the file for source maps.
@@ -134,10 +134,25 @@ export class ScopeHoistingPackager {
134
134
  this.bundle.env.isLibrary ||
135
135
  this.bundle.env.outputFormat === 'commonjs'
136
136
  ) {
137
- for (let b of this.bundleGraph.getReferencedBundles(this.bundle, {
138
- recursive: false,
139
- })) {
140
- this.externals.set(relativeBundlePath(this.bundle, b), new Map());
137
+ for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) {
138
+ let entry = b.getMainEntry();
139
+ let symbols = new Map();
140
+ if (entry && !this.isAsyncBundle && entry.type === 'js') {
141
+ this.externalAssets.add(entry);
142
+
143
+ let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set();
144
+ for (let s of usedSymbols) {
145
+ // If the referenced bundle is ESM, and we are importing '*', use 'default' instead.
146
+ // This matches the logic below in buildExportedSymbols.
147
+ let imported = s;
148
+ if (imported === '*' && b.env.outputFormat === 'esmodule') {
149
+ imported = 'default';
150
+ }
151
+ symbols.set(imported, this.getSymbolResolution(entry, entry, s));
152
+ }
153
+ }
154
+
155
+ this.externals.set(relativeBundlePath(this.bundle, b), symbols);
141
156
  }
142
157
  }
143
158
 
@@ -309,6 +324,7 @@ export class ScopeHoistingPackager {
309
324
 
310
325
  if (
311
326
  asset.meta.shouldWrap ||
327
+ this.isAsyncBundle ||
312
328
  this.bundle.env.sourceType === 'script' ||
313
329
  this.bundleGraph.isAssetReferenced(this.bundle, asset) ||
314
330
  this.bundleGraph
@@ -345,6 +361,7 @@ export class ScopeHoistingPackager {
345
361
 
346
362
  buildExportedSymbols() {
347
363
  if (
364
+ this.isAsyncBundle ||
348
365
  !this.bundle.env.isLibrary ||
349
366
  this.bundle.env.outputFormat !== 'esmodule'
350
367
  ) {
@@ -354,8 +371,6 @@ export class ScopeHoistingPackager {
354
371
  // TODO: handle ESM exports of wrapped entry assets...
355
372
  let entry = this.bundle.getMainEntry();
356
373
  if (entry && !this.wrappedAssets.has(entry.id)) {
357
- let hasNamespace = entry.symbols.hasExportSymbol('*');
358
-
359
374
  for (let {
360
375
  asset,
361
376
  exportAs,
@@ -363,13 +378,6 @@ export class ScopeHoistingPackager {
363
378
  exportSymbol,
364
379
  } of this.bundleGraph.getExportedSymbols(entry)) {
365
380
  if (typeof symbol === 'string') {
366
- // If the module has a namespace (e.g. commonjs), and this is not an entry, only export the namespace
367
- // as default, without individual exports. This mirrors the importing logic in addExternal, avoiding
368
- // extra unused exports and potential for non-identifier export names.
369
- if (hasNamespace && this.isAsyncBundle && exportAs !== '*') {
370
- continue;
371
- }
372
-
373
381
  let symbols = this.exportedSymbols.get(
374
382
  symbol === '*' ? nullthrows(entry.symbols.get('*')?.local) : symbol,
375
383
  )?.exportAs;
@@ -410,8 +418,8 @@ export class ScopeHoistingPackager {
410
418
  }
411
419
 
412
420
  getTopLevelName(name: string): string {
413
- name = makeValidIdentifier(name);
414
- if (this.globalNames.has(name)) {
421
+ name = name.replace(NON_ID_CONTINUE_RE, '');
422
+ if (!ID_START_RE.test(name) || this.globalNames.has(name)) {
415
423
  name = '_' + name;
416
424
  }
417
425
 
@@ -426,7 +434,7 @@ export class ScopeHoistingPackager {
426
434
  }
427
435
 
428
436
  getPropertyAccess(obj: string, property: string): string {
429
- if (isValidIdentifier(property)) {
437
+ if (IDENTIFIER_RE.test(property)) {
430
438
  return `${obj}.${property}`;
431
439
  }
432
440
 
@@ -503,11 +511,7 @@ export class ScopeHoistingPackager {
503
511
  }
504
512
 
505
513
  let [depMap, replacements] = this.buildReplacements(asset, deps);
506
- let [prepend, prependLines, append] = this.buildAssetPrelude(
507
- asset,
508
- deps,
509
- replacements,
510
- );
514
+ let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps);
511
515
  if (prependLines > 0) {
512
516
  sourceMap?.offsetLines(1, prependLines);
513
517
  code = prepend + code;
@@ -672,7 +676,7 @@ ${code}
672
676
  depMap
673
677
  .get(
674
678
  `${assetId}:${getSpecifier(dep)}${
675
- !dep.meta.placeholder ? specifierType : ''
679
+ dep.meta.isESM ? specifierType : ''
676
680
  }`,
677
681
  )
678
682
  .push(dep);
@@ -699,24 +703,6 @@ ${code}
699
703
  continue;
700
704
  }
701
705
 
702
- // Handle imports from other bundles in libraries.
703
- if (this.bundle.env.isLibrary && !this.bundle.hasAsset(resolved)) {
704
- let referencedBundle = this.bundleGraph.getReferencedBundle(
705
- dep,
706
- this.bundle,
707
- );
708
- if (
709
- referencedBundle &&
710
- referencedBundle.getMainEntry() === resolved &&
711
- referencedBundle.type === 'js' &&
712
- !this.bundleGraph.isAssetReferenced(referencedBundle, resolved)
713
- ) {
714
- this.addExternal(dep, replacements, referencedBundle);
715
- this.externalAssets.add(resolved);
716
- continue;
717
- }
718
- }
719
-
720
706
  for (let [imported, {local}] of dep.symbols) {
721
707
  if (local === '*') {
722
708
  continue;
@@ -762,11 +748,7 @@ ${code}
762
748
  return [depMap, replacements];
763
749
  }
764
750
 
765
- addExternal(
766
- dep: Dependency,
767
- replacements?: Map<string, string>,
768
- referencedBundle?: NamedBundle,
769
- ) {
751
+ addExternal(dep: Dependency, replacements?: Map<string, string>) {
770
752
  if (this.bundle.env.outputFormat === 'global') {
771
753
  throw new ThrowableDiagnostic({
772
754
  diagnostic: {
@@ -784,16 +766,11 @@ ${code}
784
766
  });
785
767
  }
786
768
 
787
- let specifier = dep.specifier;
788
- if (referencedBundle) {
789
- specifier = relativeBundlePath(this.bundle, referencedBundle);
790
- }
791
-
792
769
  // Map of DependencySpecifier -> Map<ExportedSymbol, Identifier>>
793
- let external = this.externals.get(specifier);
770
+ let external = this.externals.get(dep.specifier);
794
771
  if (!external) {
795
772
  external = new Map();
796
- this.externals.set(specifier, external);
773
+ this.externals.set(dep.specifier, external);
797
774
  }
798
775
 
799
776
  for (let [imported, {local}] of dep.symbols) {
@@ -809,16 +786,9 @@ ${code}
809
786
  if (this.bundle.env.outputFormat === 'commonjs') {
810
787
  renamed = external.get('*');
811
788
  if (!renamed) {
812
- if (referencedBundle) {
813
- let entry = nullthrows(referencedBundle.getMainEntry());
814
- renamed =
815
- entry.symbols.get('*')?.local ??
816
- `$${String(entry.meta.id)}$exports`;
817
- } else {
818
- renamed = this.getTopLevelName(
819
- `$${this.bundle.publicId}$${specifier}`,
820
- );
821
- }
789
+ renamed = this.getTopLevelName(
790
+ `$${this.bundle.publicId}$${dep.specifier}`,
791
+ );
822
792
 
823
793
  external.set('*', renamed);
824
794
  }
@@ -837,67 +807,24 @@ ${code}
837
807
  replacements.set(local, replacement);
838
808
  }
839
809
  } else {
840
- let property;
841
- if (referencedBundle) {
842
- let entry = nullthrows(referencedBundle.getMainEntry());
843
- if (entry.symbols.hasExportSymbol('*')) {
844
- // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
845
- // This mirrors the logic in buildExportedSymbols.
846
- property = imported;
847
- imported =
848
- referencedBundle?.env.outputFormat === 'esmodule'
849
- ? 'default'
850
- : '*';
851
- } else {
852
- if (imported === '*') {
853
- let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
854
- if (local === '*') {
855
- // Re-export all symbols.
856
- for (let exported of exportedSymbols) {
857
- if (exported.symbol) {
858
- external.set(exported.exportSymbol, exported.symbol);
859
- }
860
- }
861
- continue;
862
- }
863
- }
864
- renamed = this.bundleGraph.getSymbolResolution(
865
- entry,
866
- imported,
867
- this.bundle,
868
- ).symbol;
869
- }
870
- }
871
-
872
810
  // Rename the specifier so that multiple local imports of the same imported specifier
873
811
  // are deduplicated. We have to prefix the imported name with the bundle id so that
874
812
  // local variables do not shadow it.
875
- if (!renamed) {
876
- if (this.exportedSymbols.has(local)) {
877
- renamed = local;
878
- } else if (imported === 'default' || imported === '*') {
879
- renamed = this.getTopLevelName(
880
- `$${this.bundle.publicId}$${specifier}`,
881
- );
882
- } else {
883
- renamed = this.getTopLevelName(
884
- `$${this.bundle.publicId}$${imported}`,
885
- );
886
- }
813
+ if (this.exportedSymbols.has(local)) {
814
+ renamed = local;
815
+ } else if (imported === 'default' || imported === '*') {
816
+ renamed = this.getTopLevelName(
817
+ `$${this.bundle.publicId}$${dep.specifier}`,
818
+ );
819
+ } else {
820
+ renamed = this.getTopLevelName(
821
+ `$${this.bundle.publicId}$${imported}`,
822
+ );
887
823
  }
888
824
 
889
825
  external.set(imported, renamed);
890
826
  if (local !== '*' && replacements) {
891
- let replacement = renamed;
892
- if (property === '*') {
893
- replacement = renamed;
894
- } else if (property === 'default') {
895
- replacement = `($parcel$interopDefault(${renamed}))`;
896
- this.usedHelpers.add('$parcel$interopDefault');
897
- } else if (property) {
898
- replacement = this.getPropertyAccess(renamed, property);
899
- }
900
- replacements.set(local, replacement);
827
+ replacements.set(local, renamed);
901
828
  }
902
829
  }
903
830
  }
@@ -922,7 +849,6 @@ ${code}
922
849
  resolved: Asset,
923
850
  imported: string,
924
851
  dep?: Dependency,
925
- replacements?: Map<string, string>,
926
852
  ): string {
927
853
  let {
928
854
  asset: resolvedAsset,
@@ -985,7 +911,7 @@ ${code}
985
911
  exportSymbol === 'default' &&
986
912
  staticExports &&
987
913
  !isWrapped &&
988
- (dep?.meta.kind === 'Import' || dep?.meta.kind === 'Export') &&
914
+ dep?.meta.isESM &&
989
915
  resolvedAsset.symbols.hasExportSymbol('*') &&
990
916
  resolvedAsset.symbols.hasExportSymbol('default') &&
991
917
  !resolvedAsset.symbols.hasExportSymbol('__esModule');
@@ -996,16 +922,13 @@ ${code}
996
922
  // namespace export symbol.
997
923
  let assetId = resolvedAsset.meta.id;
998
924
  invariant(typeof assetId === 'string');
999
- let obj;
1000
- if (isWrapped && (!dep || dep?.meta.shouldWrap)) {
1001
- // Wrap in extra parenthesis to not change semantics, e.g.`new (parcelRequire("..."))()`.
1002
- obj = `(parcelRequire(${JSON.stringify(publicId)}))`;
1003
- } else if (isWrapped && dep) {
1004
- obj = `$${publicId}`;
1005
- } else {
1006
- obj = resolvedAsset.symbols.get('*')?.local || `$${assetId}$exports`;
1007
- obj = replacements?.get(obj) || obj;
1008
- }
925
+ let obj =
926
+ isWrapped && (!dep || dep?.meta.shouldWrap)
927
+ ? // Wrap in extra parenthesis to not change semantics, e.g.`new (parcelRequire("..."))()`.
928
+ `(parcelRequire(${JSON.stringify(publicId)}))`
929
+ : isWrapped && dep
930
+ ? `$${publicId}`
931
+ : resolvedAsset.symbols.get('*')?.local || `$${assetId}$exports`;
1009
932
 
1010
933
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
1011
934
  // Resolve to the namespace object if requested or this is a CJS default interop reqiure.
@@ -1026,9 +949,8 @@ ${code}
1026
949
  // we need to use a member access off the namespace object rather
1027
950
  // than a direct reference. If importing default from a CJS module,
1028
951
  // use a helper to check the __esModule flag at runtime.
1029
- let kind = dep?.meta.kind;
1030
952
  if (
1031
- (!dep || kind === 'Import' || kind === 'Export') &&
953
+ (!dep || dep.meta?.isESM) &&
1032
954
  exportSymbol === 'default' &&
1033
955
  resolvedAsset.symbols.hasExportSymbol('*') &&
1034
956
  this.needsDefaultInterop(resolvedAsset)
@@ -1041,7 +963,7 @@ ${code}
1041
963
  } else if (!symbol) {
1042
964
  invariant(false, 'Asset was skipped or not found.');
1043
965
  } else {
1044
- return replacements?.get(symbol) || symbol;
966
+ return symbol;
1045
967
  }
1046
968
  }
1047
969
 
@@ -1088,7 +1010,6 @@ ${code}
1088
1010
  buildAssetPrelude(
1089
1011
  asset: Asset,
1090
1012
  deps: Array<Dependency>,
1091
- replacements: Map<string, string>,
1092
1013
  ): [string, number, string] {
1093
1014
  let prepend = '';
1094
1015
  let prependLineCount = 0;
@@ -1121,7 +1042,6 @@ ${code}
1121
1042
  .some(
1122
1043
  dep =>
1123
1044
  !dep.isEntry &&
1124
- this.bundle.hasDependency(dep) &&
1125
1045
  nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
1126
1046
  ))) ||
1127
1047
  // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
@@ -1130,11 +1050,7 @@ ${code}
1130
1050
  [...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s))) ||
1131
1051
  // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1132
1052
  // include the namespace object for the default export.
1133
- this.exportedSymbols.has(`$${assetId}$exports`) ||
1134
- // CommonJS library bundle entries always need a namespace.
1135
- (this.bundle.env.isLibrary &&
1136
- this.bundle.env.outputFormat === 'commonjs' &&
1137
- asset === this.bundle.getMainEntry());
1053
+ this.exportedSymbols.has(`$${assetId}$exports`);
1138
1054
 
1139
1055
  // If the asset doesn't have static exports, should wrap, the namespace is used,
1140
1056
  // or we need default interop, then we need to synthesize a namespace object for
@@ -1201,13 +1117,7 @@ ${code}
1201
1117
  (!resolved.meta.hasCJSExports &&
1202
1118
  resolved.symbols.hasExportSymbol('*'))
1203
1119
  ) {
1204
- let obj = this.getSymbolResolution(
1205
- asset,
1206
- resolved,
1207
- '*',
1208
- dep,
1209
- replacements,
1210
- );
1120
+ let obj = this.getSymbolResolution(asset, resolved, '*', dep);
1211
1121
  append += `$parcel$exportWildcard($${assetId}$exports, ${obj});\n`;
1212
1122
  this.usedHelpers.add('$parcel$exportWildcard');
1213
1123
  } else {
@@ -1225,8 +1135,6 @@ ${code}
1225
1135
  asset,
1226
1136
  resolved,
1227
1137
  symbol,
1228
- undefined,
1229
- replacements,
1230
1138
  );
1231
1139
  let get = this.buildFunctionExpression([], resolvedSymbol);
1232
1140
  let set = asset.meta.hasCJSExports
@@ -1273,13 +1181,7 @@ ${code}
1273
1181
  // additional assignments after each mutation of the original binding.
1274
1182
  prepend += `\n${usedExports
1275
1183
  .map(exp => {
1276
- let resolved = this.getSymbolResolution(
1277
- asset,
1278
- asset,
1279
- exp,
1280
- undefined,
1281
- replacements,
1282
- );
1184
+ let resolved = this.getSymbolResolution(asset, asset, exp);
1283
1185
  let get = this.buildFunctionExpression([], resolved);
1284
1186
  let isEsmExport = !!asset.symbols.get(exp)?.meta?.isEsm;
1285
1187
  let set =
package/src/utils.js CHANGED
@@ -55,20 +55,3 @@ export function getSpecifier(dep: Dependency): string {
55
55
 
56
56
  return dep.specifier;
57
57
  }
58
-
59
- // https://262.ecma-international.org/6.0/#sec-names-and-keywords
60
- const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
61
- const ID_START_RE = /^[$_\p{ID_Start}]/u;
62
- const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
63
-
64
- export function isValidIdentifier(id: string): boolean {
65
- return IDENTIFIER_RE.test(id);
66
- }
67
-
68
- export function makeValidIdentifier(name: string): string {
69
- name = name.replace(NON_ID_CONTINUE_RE, '');
70
- if (!ID_START_RE.test(name)) {
71
- name = '_' + name;
72
- }
73
- return name;
74
- }