@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/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  ## Requirements
15
15
 
16
- This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
16
+ This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v12.0.0+) and Rollup v2.68.0+. If you are using [`@rollup/plugin-node-resolve`](https://github.com/rollup/plugins/tree/master/packages/node-resolve), it should be v13.0.6+.
17
17
 
18
18
  ## Install
19
19
 
package/dist/cjs/index.js CHANGED
@@ -16,9 +16,9 @@ var glob__default = /*#__PURE__*/_interopDefaultLegacy(glob);
16
16
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
17
17
  var isReference__default = /*#__PURE__*/_interopDefaultLegacy(isReference);
18
18
 
19
- var version = " 22.0.0-10";
19
+ var version = "22.0.0-13";
20
20
  var peerDependencies = {
21
- rollup: "^2.67.0"
21
+ rollup: "^2.68.0"
22
22
  };
23
23
 
24
24
  function tryParse(parse, code, id) {
@@ -462,8 +462,7 @@ function getEsImportProxy(id, defaultIsModuleExports) {
462
462
  }
463
463
  return {
464
464
  code,
465
- syntheticNamedExports: '__moduleExports',
466
- meta: { commonjs: { isCommonJS: false } }
465
+ syntheticNamedExports: '__moduleExports'
467
466
  };
468
467
  }
469
468
 
@@ -571,7 +570,7 @@ function getResolveId(extensions) {
571
570
  meta: { commonjs: commonjsMeta }
572
571
  } = moduleInfo;
573
572
  if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
574
- return wrapId(resolved.id, ES_IMPORT_SUFFIX);
573
+ return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
575
574
  }
576
575
  return resolved;
577
576
  };
@@ -597,6 +596,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
597
596
  return false;
598
597
  };
599
598
 
599
+ // Once a module is listed here, its type (wrapped or not) is fixed and may
600
+ // not change for the rest of the current build, to not break already
601
+ // transformed modules.
600
602
  const fullyAnalyzedModules = Object.create(null);
601
603
 
602
604
  const getTypeForFullyAnalyzedModule = (id) => {
@@ -604,7 +606,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
604
606
  if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
605
607
  return knownType;
606
608
  }
607
- fullyAnalyzedModules[id] = true;
608
609
  if (isCyclic(id)) {
609
610
  return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
610
611
  }
@@ -612,13 +613,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
612
613
  };
613
614
 
614
615
  const setInitialParentType = (id, initialCommonJSType) => {
615
- // It is possible a transformed module is already fully analyzed when using
616
- // the cache and one dependency introduces a new cycle. Then transform is
617
- // run for a fully analzyed module again. Fully analyzed modules may never
618
- // change their type as importers already trust their type.
619
- knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
620
- ? knownCjsModuleTypes[id]
621
- : initialCommonJSType;
616
+ // Fully analyzed modules may never change type
617
+ if (fullyAnalyzedModules[id]) {
618
+ return;
619
+ }
620
+ knownCjsModuleTypes[id] = initialCommonJSType;
622
621
  if (
623
622
  detectCyclesAndConditional &&
624
623
  knownCjsModuleTypes[id] === true &&
@@ -629,7 +628,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
629
628
  }
630
629
  };
631
630
 
632
- const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
631
+ const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
633
632
  const childId = resolved.id;
634
633
  requiredIds[childId] = true;
635
634
  if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
@@ -638,41 +637,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
638
637
 
639
638
  getDependencies(parentId).add(childId);
640
639
  if (!isCyclic(childId)) {
641
- // This makes sure the current transform handler waits for all direct dependencies to be
642
- // loaded and transformed and therefore for all transitive CommonJS dependencies to be
643
- // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
640
+ // This makes sure the current transform handler waits for all direct
641
+ // dependencies to be loaded and transformed and therefore for all
642
+ // transitive CommonJS dependencies to be loaded as well so that all
643
+ // cycles have been found and knownCjsModuleTypes is reliable.
644
644
  await loadModule(resolved);
645
645
  }
646
646
  };
647
647
 
648
+ const getTypeForImportedModule = async (resolved, loadModule) => {
649
+ if (resolved.id in knownCjsModuleTypes) {
650
+ // This handles cyclic ES dependencies
651
+ return knownCjsModuleTypes[resolved.id];
652
+ }
653
+ const {
654
+ meta: { commonjs }
655
+ } = await loadModule(resolved);
656
+ return (commonjs && commonjs.isCommonJS) || false;
657
+ };
658
+
648
659
  return {
649
660
  getWrappedIds: () =>
650
661
  Object.keys(knownCjsModuleTypes).filter(
651
662
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
652
663
  ),
653
664
  isRequiredId: (id) => requiredIds[id],
654
- async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
655
- // Ignore modules that did not pass through the original transformer in a previous build
656
- if (!(parentMeta && parentMeta.requires)) {
657
- return false;
658
- }
659
- setInitialParentType(parentId, parentMeta.initialCommonJSType);
660
- await Promise.all(
661
- parentMeta.requires.map(({ resolved, isConditional }) =>
662
- setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
663
- )
664
- );
665
- if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
666
- return true;
667
- }
668
- for (const {
669
- resolved: { id }
670
- } of parentMeta.requires) {
671
- if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
665
+ async shouldTransformCachedModule({
666
+ id: parentId,
667
+ resolvedSources,
668
+ meta: { commonjs: parentMeta }
669
+ }) {
670
+ // We explicitly track ES modules to handle circular imports
671
+ if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
672
+ if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
673
+ const parentRequires = parentMeta && parentMeta.requires;
674
+ if (parentRequires) {
675
+ setInitialParentType(parentId, parentMeta.initialCommonJSType);
676
+ await Promise.all(
677
+ parentRequires.map(({ resolved, isConditional }) =>
678
+ analyzeRequiredModule(parentId, resolved, isConditional, this.load)
679
+ )
680
+ );
681
+ if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
672
682
  return true;
673
683
  }
684
+ for (const {
685
+ resolved: { id }
686
+ } of parentRequires) {
687
+ if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
688
+ return true;
689
+ }
690
+ }
691
+ // Now that we decided to go with the cached copy, neither the parent
692
+ // module nor any of its children may change types anymore
693
+ fullyAnalyzedModules[parentId] = true;
694
+ for (const {
695
+ resolved: { id }
696
+ } of parentRequires) {
697
+ fullyAnalyzedModules[id] = true;
698
+ }
674
699
  }
675
- return false;
700
+ const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
701
+ return (
702
+ await Promise.all(
703
+ Object.keys(resolvedSources)
704
+ .map((source) => resolvedSources[source])
705
+ .filter(({ id, external }) => !(external || parentRequireSet.has(id)))
706
+ .map(async (resolved) => {
707
+ if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
708
+ return (
709
+ (await getTypeForImportedModule(
710
+ (await this.load({ id: resolved.id })).meta.commonjs.resolved,
711
+ this.load
712
+ )) !== IS_WRAPPED_COMMONJS
713
+ );
714
+ }
715
+ return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
716
+ })
717
+ )
718
+ ).some((shouldTransform) => shouldTransform);
676
719
  },
677
720
  /* eslint-disable no-param-reassign */
678
721
  resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
@@ -703,16 +746,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
703
746
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
704
747
  }
705
748
  parentMeta.requires.push({ resolved, isConditional });
706
- await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
749
+ await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
707
750
  return { id: childId, allowProxy: true };
708
751
  })
709
752
  );
710
753
  parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
754
+ fullyAnalyzedModules[parentId] = true;
711
755
  return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
712
756
  // eslint-disable-next-line no-multi-assign
713
757
  const isCommonJS = (parentMeta.isRequiredCommonJS[
714
758
  dependencyId
715
759
  ] = getTypeForFullyAnalyzedModule(dependencyId));
760
+ fullyAnalyzedModules[dependencyId] = true;
716
761
  return {
717
762
  source: sources[index].source,
718
763
  id: allowProxy
@@ -1753,7 +1798,9 @@ async function transformCommonjs(
1753
1798
  magicString.remove(0, commentEnd).trim();
1754
1799
  }
1755
1800
 
1756
- const exportMode = shouldWrap
1801
+ const exportMode = isEsModule
1802
+ ? 'none'
1803
+ : shouldWrap
1757
1804
  ? uses.module
1758
1805
  ? 'module'
1759
1806
  : 'exports'