@rollup/plugin-commonjs 22.0.0-11 → 22.0.0-14

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-11";
10
+ var version = "22.0.0-14";
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,7 +587,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
588
587
  return false;
589
588
  };
590
589
 
591
- // Once a module is listed here, its type (wrapped or not) is fixed any may
590
+ // Once a module is listed here, its type (wrapped or not) is fixed and may
592
591
  // not change for the rest of the current build, to not break already
593
592
  // transformed modules.
594
593
  const fullyAnalyzedModules = Object.create(null);
@@ -637,42 +636,77 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
637
636
  }
638
637
  };
639
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
+
640
650
  return {
641
651
  getWrappedIds: () =>
642
652
  Object.keys(knownCjsModuleTypes).filter(
643
653
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
644
654
  ),
645
655
  isRequiredId: (id) => requiredIds[id],
646
- async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
647
- // Ignore modules that did not pass through the original transformer in a previous build
648
- if (!(parentMeta && parentMeta.requires)) {
649
- return false;
650
- }
651
- setInitialParentType(parentId, parentMeta.initialCommonJSType);
652
- await Promise.all(
653
- parentMeta.requires.map(({ resolved, isConditional }) =>
654
- analyzeRequiredModule(parentId, resolved, isConditional, this.load)
655
- )
656
- );
657
- if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
658
- return true;
659
- }
660
- for (const {
661
- resolved: { id }
662
- } of parentMeta.requires) {
663
- 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) {
664
673
  return true;
665
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
+ }
666
690
  }
667
- // Now that we decided to go with the cached copy, neither the parent
668
- // module nor any of its children may change types anymore
669
- fullyAnalyzedModules[parentId] = true;
670
- for (const {
671
- resolved: { id }
672
- } of parentMeta.requires) {
673
- fullyAnalyzedModules[id] = true;
674
- }
675
- 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);
676
710
  },
677
711
  /* eslint-disable no-param-reassign */
678
712
  resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
@@ -1168,7 +1202,7 @@ function getRequireStringArg(node) {
1168
1202
  function getRequireHandlers() {
1169
1203
  const requireExpressions = [];
1170
1204
 
1171
- function addRequireStatement(
1205
+ function addRequireExpression(
1172
1206
  sourceId,
1173
1207
  node,
1174
1208
  scope,
@@ -1248,7 +1282,7 @@ function getRequireHandlers() {
1248
1282
  }
1249
1283
 
1250
1284
  return {
1251
- addRequireStatement,
1285
+ addRequireExpression,
1252
1286
  rewriteRequireExpressionsAndGetImportBlock
1253
1287
  };
1254
1288
  }
@@ -1374,7 +1408,7 @@ async function transformCommonjs(
1374
1408
  // unconditional require elsewhere.
1375
1409
  let currentConditionalNodeEnd = null;
1376
1410
  const conditionalNodes = new Set();
1377
- const { addRequireStatement, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
1411
+ const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
1378
1412
 
1379
1413
  // See which names are assigned to. This is necessary to prevent
1380
1414
  // illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
@@ -1529,14 +1563,22 @@ async function transformCommonjs(
1529
1563
  const requireStringArg = getRequireStringArg(node);
1530
1564
  if (!ignoreRequire(requireStringArg)) {
1531
1565
  const usesReturnValue = parent.type !== 'ExpressionStatement';
1532
- addRequireStatement(
1566
+ const toBeRemoved =
1567
+ parent.type === 'ExpressionStatement' &&
1568
+ (!currentConditionalNodeEnd ||
1569
+ // We should completely remove requires directly in a try-catch
1570
+ // so that Rollup can remove up the try-catch
1571
+ (currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
1572
+ ? parent
1573
+ : node;
1574
+ addRequireExpression(
1533
1575
  requireStringArg,
1534
1576
  node,
1535
1577
  scope,
1536
1578
  usesReturnValue,
1537
1579
  currentTryBlockEnd !== null,
1538
1580
  currentConditionalNodeEnd !== null,
1539
- parent.type === 'ExpressionStatement' ? parent : node
1581
+ toBeRemoved
1540
1582
  );
1541
1583
  if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
1542
1584
  for (const name of extractAssignedNames(parent.id)) {
@@ -1738,6 +1780,7 @@ async function transformCommonjs(
1738
1780
  !(
1739
1781
  shouldWrap ||
1740
1782
  isRequired ||
1783
+ needsRequireWrapper ||
1741
1784
  uses.module ||
1742
1785
  uses.exports ||
1743
1786
  uses.require ||
@@ -1755,7 +1798,9 @@ async function transformCommonjs(
1755
1798
  magicString.remove(0, commentEnd).trim();
1756
1799
  }
1757
1800
 
1758
- const exportMode = shouldWrap
1801
+ const exportMode = isEsModule
1802
+ ? 'none'
1803
+ : shouldWrap
1759
1804
  ? uses.module
1760
1805
  ? 'module'
1761
1806
  : 'exports'