@rollup/plugin-commonjs 22.0.0-10 → 22.0.0-13

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.
package/dist/es/index.js CHANGED
@@ -7,9 +7,9 @@ import { walk } from 'estree-walker';
7
7
  import MagicString from 'magic-string';
8
8
  import isReference from 'is-reference';
9
9
 
10
- var version = " 22.0.0-10";
10
+ var version = "22.0.0-13";
11
11
  var peerDependencies = {
12
- rollup: "^2.67.0"
12
+ rollup: "^2.68.0"
13
13
  };
14
14
 
15
15
  function tryParse(parse, code, id) {
@@ -453,8 +453,7 @@ function getEsImportProxy(id, defaultIsModuleExports) {
453
453
  }
454
454
  return {
455
455
  code,
456
- syntheticNamedExports: '__moduleExports',
457
- meta: { commonjs: { isCommonJS: false } }
456
+ syntheticNamedExports: '__moduleExports'
458
457
  };
459
458
  }
460
459
 
@@ -562,7 +561,7 @@ function getResolveId(extensions) {
562
561
  meta: { commonjs: commonjsMeta }
563
562
  } = moduleInfo;
564
563
  if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
565
- return wrapId(resolved.id, ES_IMPORT_SUFFIX);
564
+ return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
566
565
  }
567
566
  return resolved;
568
567
  };
@@ -588,6 +587,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
588
587
  return false;
589
588
  };
590
589
 
590
+ // Once a module is listed here, its type (wrapped or not) is fixed and may
591
+ // not change for the rest of the current build, to not break already
592
+ // transformed modules.
591
593
  const fullyAnalyzedModules = Object.create(null);
592
594
 
593
595
  const getTypeForFullyAnalyzedModule = (id) => {
@@ -595,7 +597,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
595
597
  if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
596
598
  return knownType;
597
599
  }
598
- fullyAnalyzedModules[id] = true;
599
600
  if (isCyclic(id)) {
600
601
  return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
601
602
  }
@@ -603,13 +604,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
603
604
  };
604
605
 
605
606
  const setInitialParentType = (id, initialCommonJSType) => {
606
- // It is possible a transformed module is already fully analyzed when using
607
- // the cache and one dependency introduces a new cycle. Then transform is
608
- // run for a fully analzyed module again. Fully analyzed modules may never
609
- // change their type as importers already trust their type.
610
- knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
611
- ? knownCjsModuleTypes[id]
612
- : initialCommonJSType;
607
+ // Fully analyzed modules may never change type
608
+ if (fullyAnalyzedModules[id]) {
609
+ return;
610
+ }
611
+ knownCjsModuleTypes[id] = initialCommonJSType;
613
612
  if (
614
613
  detectCyclesAndConditional &&
615
614
  knownCjsModuleTypes[id] === true &&
@@ -620,7 +619,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
620
619
  }
621
620
  };
622
621
 
623
- const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
622
+ const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
624
623
  const childId = resolved.id;
625
624
  requiredIds[childId] = true;
626
625
  if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
@@ -629,41 +628,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
629
628
 
630
629
  getDependencies(parentId).add(childId);
631
630
  if (!isCyclic(childId)) {
632
- // This makes sure the current transform handler waits for all direct dependencies to be
633
- // loaded and transformed and therefore for all transitive CommonJS dependencies to be
634
- // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
631
+ // This makes sure the current transform handler waits for all direct
632
+ // dependencies to be loaded and transformed and therefore for all
633
+ // transitive CommonJS dependencies to be loaded as well so that all
634
+ // cycles have been found and knownCjsModuleTypes is reliable.
635
635
  await loadModule(resolved);
636
636
  }
637
637
  };
638
638
 
639
+ const getTypeForImportedModule = async (resolved, loadModule) => {
640
+ if (resolved.id in knownCjsModuleTypes) {
641
+ // This handles cyclic ES dependencies
642
+ return knownCjsModuleTypes[resolved.id];
643
+ }
644
+ const {
645
+ meta: { commonjs }
646
+ } = await loadModule(resolved);
647
+ return (commonjs && commonjs.isCommonJS) || false;
648
+ };
649
+
639
650
  return {
640
651
  getWrappedIds: () =>
641
652
  Object.keys(knownCjsModuleTypes).filter(
642
653
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
643
654
  ),
644
655
  isRequiredId: (id) => requiredIds[id],
645
- async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
646
- // Ignore modules that did not pass through the original transformer in a previous build
647
- if (!(parentMeta && parentMeta.requires)) {
648
- return false;
649
- }
650
- setInitialParentType(parentId, parentMeta.initialCommonJSType);
651
- await Promise.all(
652
- parentMeta.requires.map(({ resolved, isConditional }) =>
653
- setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
654
- )
655
- );
656
- if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
657
- return true;
658
- }
659
- for (const {
660
- resolved: { id }
661
- } of parentMeta.requires) {
662
- if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
656
+ async shouldTransformCachedModule({
657
+ id: parentId,
658
+ resolvedSources,
659
+ meta: { commonjs: parentMeta }
660
+ }) {
661
+ // We explicitly track ES modules to handle circular imports
662
+ if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
663
+ if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
664
+ const parentRequires = parentMeta && parentMeta.requires;
665
+ if (parentRequires) {
666
+ setInitialParentType(parentId, parentMeta.initialCommonJSType);
667
+ await Promise.all(
668
+ parentRequires.map(({ resolved, isConditional }) =>
669
+ analyzeRequiredModule(parentId, resolved, isConditional, this.load)
670
+ )
671
+ );
672
+ if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
663
673
  return true;
664
674
  }
675
+ for (const {
676
+ resolved: { id }
677
+ } of parentRequires) {
678
+ if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
679
+ return true;
680
+ }
681
+ }
682
+ // Now that we decided to go with the cached copy, neither the parent
683
+ // module nor any of its children may change types anymore
684
+ fullyAnalyzedModules[parentId] = true;
685
+ for (const {
686
+ resolved: { id }
687
+ } of parentRequires) {
688
+ fullyAnalyzedModules[id] = true;
689
+ }
665
690
  }
666
- return false;
691
+ const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
692
+ return (
693
+ await Promise.all(
694
+ Object.keys(resolvedSources)
695
+ .map((source) => resolvedSources[source])
696
+ .filter(({ id, external }) => !(external || parentRequireSet.has(id)))
697
+ .map(async (resolved) => {
698
+ if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
699
+ return (
700
+ (await getTypeForImportedModule(
701
+ (await this.load({ id: resolved.id })).meta.commonjs.resolved,
702
+ this.load
703
+ )) !== IS_WRAPPED_COMMONJS
704
+ );
705
+ }
706
+ return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
707
+ })
708
+ )
709
+ ).some((shouldTransform) => shouldTransform);
667
710
  },
668
711
  /* eslint-disable no-param-reassign */
669
712
  resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
@@ -694,16 +737,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
694
737
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
695
738
  }
696
739
  parentMeta.requires.push({ resolved, isConditional });
697
- await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
740
+ await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
698
741
  return { id: childId, allowProxy: true };
699
742
  })
700
743
  );
701
744
  parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
745
+ fullyAnalyzedModules[parentId] = true;
702
746
  return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
703
747
  // eslint-disable-next-line no-multi-assign
704
748
  const isCommonJS = (parentMeta.isRequiredCommonJS[
705
749
  dependencyId
706
750
  ] = getTypeForFullyAnalyzedModule(dependencyId));
751
+ fullyAnalyzedModules[dependencyId] = true;
707
752
  return {
708
753
  source: sources[index].source,
709
754
  id: allowProxy
@@ -1744,7 +1789,9 @@ async function transformCommonjs(
1744
1789
  magicString.remove(0, commentEnd).trim();
1745
1790
  }
1746
1791
 
1747
- const exportMode = shouldWrap
1792
+ const exportMode = isEsModule
1793
+ ? 'none'
1794
+ : shouldWrap
1748
1795
  ? uses.module
1749
1796
  ? 'module'
1750
1797
  : 'exports'