@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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @rollup/plugin-commonjs ChangeLog
2
2
 
3
+ ## v21.0.3
4
+
5
+ _2022-03-27_
6
+
7
+ ### Updates
8
+
9
+ - docs: sync required rollup version (#1118)
10
+
11
+ ## v21.0.2
12
+
13
+ _2022-02-23_
14
+
15
+ ### Updates
16
+
17
+ - chore: transpile dynamic helper to ES5 (#1082)
18
+
3
19
  ## v21.0.1
4
20
 
5
21
  _2021-10-19_
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-11";
19
+ var version = "22.0.0-14";
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,7 +596,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
597
596
  return false;
598
597
  };
599
598
 
600
- // Once a module is listed here, its type (wrapped or not) is fixed any may
599
+ // Once a module is listed here, its type (wrapped or not) is fixed and may
601
600
  // not change for the rest of the current build, to not break already
602
601
  // transformed modules.
603
602
  const fullyAnalyzedModules = Object.create(null);
@@ -646,42 +645,77 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
646
645
  }
647
646
  };
648
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
+
649
659
  return {
650
660
  getWrappedIds: () =>
651
661
  Object.keys(knownCjsModuleTypes).filter(
652
662
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
653
663
  ),
654
664
  isRequiredId: (id) => requiredIds[id],
655
- async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
656
- // Ignore modules that did not pass through the original transformer in a previous build
657
- if (!(parentMeta && parentMeta.requires)) {
658
- return false;
659
- }
660
- setInitialParentType(parentId, parentMeta.initialCommonJSType);
661
- await Promise.all(
662
- parentMeta.requires.map(({ resolved, isConditional }) =>
663
- analyzeRequiredModule(parentId, resolved, isConditional, this.load)
664
- )
665
- );
666
- if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
667
- return true;
668
- }
669
- for (const {
670
- resolved: { id }
671
- } of parentMeta.requires) {
672
- 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) {
673
682
  return true;
674
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
+ }
675
699
  }
676
- // Now that we decided to go with the cached copy, neither the parent
677
- // module nor any of its children may change types anymore
678
- fullyAnalyzedModules[parentId] = true;
679
- for (const {
680
- resolved: { id }
681
- } of parentMeta.requires) {
682
- fullyAnalyzedModules[id] = true;
683
- }
684
- 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);
685
719
  },
686
720
  /* eslint-disable no-param-reassign */
687
721
  resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
@@ -1177,7 +1211,7 @@ function getRequireStringArg(node) {
1177
1211
  function getRequireHandlers() {
1178
1212
  const requireExpressions = [];
1179
1213
 
1180
- function addRequireStatement(
1214
+ function addRequireExpression(
1181
1215
  sourceId,
1182
1216
  node,
1183
1217
  scope,
@@ -1257,7 +1291,7 @@ function getRequireHandlers() {
1257
1291
  }
1258
1292
 
1259
1293
  return {
1260
- addRequireStatement,
1294
+ addRequireExpression,
1261
1295
  rewriteRequireExpressionsAndGetImportBlock
1262
1296
  };
1263
1297
  }
@@ -1383,7 +1417,7 @@ async function transformCommonjs(
1383
1417
  // unconditional require elsewhere.
1384
1418
  let currentConditionalNodeEnd = null;
1385
1419
  const conditionalNodes = new Set();
1386
- const { addRequireStatement, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
1420
+ const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
1387
1421
 
1388
1422
  // See which names are assigned to. This is necessary to prevent
1389
1423
  // illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
@@ -1538,14 +1572,22 @@ async function transformCommonjs(
1538
1572
  const requireStringArg = getRequireStringArg(node);
1539
1573
  if (!ignoreRequire(requireStringArg)) {
1540
1574
  const usesReturnValue = parent.type !== 'ExpressionStatement';
1541
- addRequireStatement(
1575
+ const toBeRemoved =
1576
+ parent.type === 'ExpressionStatement' &&
1577
+ (!currentConditionalNodeEnd ||
1578
+ // We should completely remove requires directly in a try-catch
1579
+ // so that Rollup can remove up the try-catch
1580
+ (currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
1581
+ ? parent
1582
+ : node;
1583
+ addRequireExpression(
1542
1584
  requireStringArg,
1543
1585
  node,
1544
1586
  scope,
1545
1587
  usesReturnValue,
1546
1588
  currentTryBlockEnd !== null,
1547
1589
  currentConditionalNodeEnd !== null,
1548
- parent.type === 'ExpressionStatement' ? parent : node
1590
+ toBeRemoved
1549
1591
  );
1550
1592
  if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
1551
1593
  for (const name of pluginutils.extractAssignedNames(parent.id)) {
@@ -1747,6 +1789,7 @@ async function transformCommonjs(
1747
1789
  !(
1748
1790
  shouldWrap ||
1749
1791
  isRequired ||
1792
+ needsRequireWrapper ||
1750
1793
  uses.module ||
1751
1794
  uses.exports ||
1752
1795
  uses.require ||
@@ -1764,7 +1807,9 @@ async function transformCommonjs(
1764
1807
  magicString.remove(0, commentEnd).trim();
1765
1808
  }
1766
1809
 
1767
- const exportMode = shouldWrap
1810
+ const exportMode = isEsModule
1811
+ ? 'none'
1812
+ : shouldWrap
1768
1813
  ? uses.module
1769
1814
  ? 'module'
1770
1815
  : 'exports'