@rollup/plugin-commonjs 18.0.0-0 → 19.0.0

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/index.js CHANGED
@@ -18,7 +18,7 @@ var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
18
18
  var isReference__default = /*#__PURE__*/_interopDefaultLegacy(isReference);
19
19
 
20
20
  var peerDependencies = {
21
- rollup: "^2.38.0"
21
+ rollup: "^2.38.3"
22
22
  };
23
23
 
24
24
  function tryParse(parse, code, id) {
@@ -137,13 +137,15 @@ export function getAugmentedNamespace(n) {
137
137
  }
138
138
  `;
139
139
 
140
+ const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
141
+
140
142
  const HELPER_NON_DYNAMIC = `
141
- export function commonjsRequire (target) {
142
- throw new Error('Could not dynamically require "' + target + '". Please configure the dynamicRequireTargets option of @rollup/plugin-commonjs appropriately for this require call to behave properly.');
143
+ export function commonjsRequire (path) {
144
+ ${FAILED_REQUIRE_ERROR}
143
145
  }
144
146
  `;
145
147
 
146
- const HELPERS_DYNAMIC = `
148
+ const getDynamicHelpers = (ignoreDynamicRequires) => `
147
149
  export function createModule(modulePath) {
148
150
  return {
149
151
  path: modulePath,
@@ -315,34 +317,22 @@ export function commonjsRequire (path, originalModuleDir) {
315
317
  return cachedModule.exports;
316
318
  };
317
319
  }
318
- return require(path);
320
+ ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
319
321
  }
320
322
 
321
323
  commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
322
324
  commonjsRequire.resolve = commonjsResolve;
323
325
  `;
324
326
 
325
- function getHelpersModule(isDynamicRequireModulesEnabled) {
326
- return `${HELPERS}${isDynamicRequireModulesEnabled ? HELPERS_DYNAMIC : HELPER_NON_DYNAMIC}`;
327
+ function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires) {
328
+ return `${HELPERS}${
329
+ isDynamicRequireModulesEnabled ? getDynamicHelpers(ignoreDynamicRequires) : HELPER_NON_DYNAMIC
330
+ }`;
327
331
  }
328
332
 
329
333
  /* eslint-disable import/prefer-default-export */
330
334
 
331
- function deconflict(scope, globals, identifier) {
332
- let i = 1;
333
- let deconflicted = pluginutils.makeLegalIdentifier(identifier);
334
-
335
- while (scope.contains(deconflicted) || globals.has(deconflicted)) {
336
- deconflicted = pluginutils.makeLegalIdentifier(`${identifier}_${i}`);
337
- i += 1;
338
- }
339
- // eslint-disable-next-line no-param-reassign
340
- scope.declarations[deconflicted] = true;
341
-
342
- return deconflicted;
343
- }
344
-
345
- function deconflictScopes(scopes, globals, identifier) {
335
+ function deconflict(scopes, globals, identifier) {
346
336
  let i = 1;
347
337
  let deconflicted = pluginutils.makeLegalIdentifier(identifier);
348
338
  const hasConflicts = () =>
@@ -461,9 +451,7 @@ function getDynamicRequirePaths(patterns) {
461
451
  return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
462
452
  }
463
453
 
464
- const commonJSMetaPromises = new Map();
465
-
466
- function getCommonJSMetaPromise(id) {
454
+ function getCommonJSMetaPromise(commonJSMetaPromises, id) {
467
455
  let commonJSMetaPromise = commonJSMetaPromises.get(id);
468
456
  if (commonJSMetaPromise) return commonJSMetaPromise.promise;
469
457
 
@@ -479,7 +467,7 @@ function getCommonJSMetaPromise(id) {
479
467
  return promise;
480
468
  }
481
469
 
482
- function setCommonJSMetaPromise(id, commonjsMeta) {
470
+ function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
483
471
  const commonJSMetaPromise = commonJSMetaPromises.get(id);
484
472
  if (commonJSMetaPromise) {
485
473
  if (commonJSMetaPromise.resolve) {
@@ -533,10 +521,11 @@ async function getStaticRequireProxy(
533
521
  id,
534
522
  requireReturnsDefault,
535
523
  esModulesWithDefaultExport,
536
- esModulesWithNamedExports
524
+ esModulesWithNamedExports,
525
+ commonJsMetaPromises
537
526
  ) {
538
527
  const name = getName(id);
539
- const commonjsMeta = await getCommonJSMetaPromise(id);
528
+ const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
540
529
  if (commonjsMeta && commonjsMeta.isCommonJS) {
541
530
  return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
542
531
  } else if (commonjsMeta === null) {
@@ -777,17 +766,6 @@ function getDefinePropertyCallName(node, targetName) {
777
766
  return { key: key.value, value: valueProperty.value };
778
767
  }
779
768
 
780
- function isLocallyShadowed(name, scope) {
781
- while (scope.parent) {
782
- if (scope.declarations[name]) {
783
- return true;
784
- }
785
- // eslint-disable-next-line no-param-reassign
786
- scope = scope.parent;
787
- }
788
- return false;
789
- }
790
-
791
769
  function isShorthandProperty(parent) {
792
770
  return parent && parent.type === 'Property' && parent.shorthand;
793
771
  }
@@ -823,7 +801,8 @@ function rewriteExportsAndGetExportsBlock(
823
801
  code,
824
802
  HELPERS_NAME,
825
803
  exportMode,
826
- detectWrappedDefault
804
+ detectWrappedDefault,
805
+ defaultIsModuleExports
827
806
  ) {
828
807
  const exports = [];
829
808
  const exportDeclarations = [];
@@ -832,6 +811,7 @@ function rewriteExportsAndGetExportsBlock(
832
811
  getExportsForReplacedModuleExports(
833
812
  magicString,
834
813
  exports,
814
+ exportDeclarations,
835
815
  moduleExportsAssignments,
836
816
  firstTopLevelModuleExportsAssignment,
837
817
  exportsName
@@ -840,11 +820,11 @@ function rewriteExportsAndGetExportsBlock(
840
820
  exports.push(`${exportsName} as __moduleExports`);
841
821
  if (wrapped) {
842
822
  getExportsWhenWrapping(
843
- exports,
844
823
  exportDeclarations,
845
824
  exportsName,
846
825
  detectWrappedDefault,
847
- HELPERS_NAME
826
+ HELPERS_NAME,
827
+ defaultIsModuleExports
848
828
  );
849
829
  } else {
850
830
  getExports(
@@ -858,7 +838,8 @@ function rewriteExportsAndGetExportsBlock(
858
838
  moduleName,
859
839
  exportsName,
860
840
  defineCompiledEsmExpressions,
861
- HELPERS_NAME
841
+ HELPERS_NAME,
842
+ defaultIsModuleExports
862
843
  );
863
844
  }
864
845
  }
@@ -872,6 +853,7 @@ function rewriteExportsAndGetExportsBlock(
872
853
  function getExportsForReplacedModuleExports(
873
854
  magicString,
874
855
  exports,
856
+ exportDeclarations,
875
857
  moduleExportsAssignments,
876
858
  firstTopLevelModuleExportsAssignment,
877
859
  exportsName
@@ -880,23 +862,26 @@ function getExportsForReplacedModuleExports(
880
862
  magicString.overwrite(left.start, left.end, exportsName);
881
863
  }
882
864
  magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
883
- exports.push(`${exportsName} as __moduleExports`, `${exportsName} as default`);
865
+ exports.push(`${exportsName} as __moduleExports`);
866
+ exportDeclarations.push(`export default ${exportsName};`);
884
867
  }
885
868
 
886
869
  function getExportsWhenWrapping(
887
- exports,
888
870
  exportDeclarations,
889
871
  exportsName,
890
872
  detectWrappedDefault,
891
- HELPERS_NAME
873
+ HELPERS_NAME,
874
+ defaultIsModuleExports
892
875
  ) {
893
- if (detectWrappedDefault) {
894
- exportDeclarations.push(
895
- `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
896
- );
897
- } else {
898
- exports.push(`${exportsName} as default`);
899
- }
876
+ exportDeclarations.push(
877
+ `export default ${
878
+ detectWrappedDefault && defaultIsModuleExports === 'auto'
879
+ ? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
880
+ : defaultIsModuleExports === false
881
+ ? `${exportsName}.default`
882
+ : exportsName
883
+ };`
884
+ );
900
885
  }
901
886
 
902
887
  function getExports(
@@ -910,7 +895,8 @@ function getExports(
910
895
  moduleName,
911
896
  exportsName,
912
897
  defineCompiledEsmExpressions,
913
- HELPERS_NAME
898
+ HELPERS_NAME,
899
+ defaultIsModuleExports
914
900
  ) {
915
901
  let deconflictedDefaultExportName;
916
902
  // Collect and rewrite module.exports assignments
@@ -950,16 +936,14 @@ function getExports(
950
936
  magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
951
937
  }
952
938
 
953
- if (isRestorableCompiledEsm) {
954
- if (moduleExportsAssignments.length === 0) {
955
- exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
956
- } else {
957
- exportDeclarations.push(
958
- `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
959
- );
960
- }
939
+ if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
940
+ exportDeclarations.push(`export default ${exportsName};`);
941
+ } else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
942
+ exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
961
943
  } else {
962
- exports.push(`${exportsName} as default`);
944
+ exportDeclarations.push(
945
+ `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
946
+ );
963
947
  }
964
948
  }
965
949
 
@@ -1085,17 +1069,11 @@ function getRequireHandlers() {
1085
1069
  id,
1086
1070
  exportMode
1087
1071
  ) {
1088
- const removedDeclarators = getDeclaratorsReplacedByImportsAndSetImportNames(
1089
- topLevelRequireDeclarators,
1090
- requiredByNode,
1091
- reassignedNames
1092
- );
1093
1072
  setRemainingImportNamesAndRewriteRequires(
1094
1073
  requireExpressionsWithUsedReturnValue,
1095
1074
  requiredByNode,
1096
1075
  magicString
1097
1076
  );
1098
- removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString);
1099
1077
  const imports = [];
1100
1078
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
1101
1079
  if (exportMode === 'module') {
@@ -1133,30 +1111,6 @@ function getRequireHandlers() {
1133
1111
  };
1134
1112
  }
1135
1113
 
1136
- function getDeclaratorsReplacedByImportsAndSetImportNames(
1137
- topLevelRequireDeclarators,
1138
- requiredByNode,
1139
- reassignedNames
1140
- ) {
1141
- const removedDeclarators = new Set();
1142
- for (const declarator of topLevelRequireDeclarators) {
1143
- const { required } = requiredByNode.get(declarator.init);
1144
- if (!required.name) {
1145
- const potentialName = declarator.id.name;
1146
- if (
1147
- !reassignedNames.has(potentialName) &&
1148
- !required.nodesUsingRequired.some((node) =>
1149
- isLocallyShadowed(potentialName, requiredByNode.get(node).scope)
1150
- )
1151
- ) {
1152
- required.name = potentialName;
1153
- removedDeclarators.add(declarator);
1154
- }
1155
- }
1156
- }
1157
- return removedDeclarators;
1158
- }
1159
-
1160
1114
  function setRemainingImportNamesAndRewriteRequires(
1161
1115
  requireExpressionsWithUsedReturnValue,
1162
1116
  requiredByNode,
@@ -1178,25 +1132,6 @@ function setRemainingImportNamesAndRewriteRequires(
1178
1132
  }
1179
1133
  }
1180
1134
 
1181
- function removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString) {
1182
- for (const declaration of topLevelDeclarations) {
1183
- let keepDeclaration = false;
1184
- let [{ start }] = declaration.declarations;
1185
- for (const declarator of declaration.declarations) {
1186
- if (removedDeclarators.has(declarator)) {
1187
- magicString.remove(start, declarator.end);
1188
- } else if (!keepDeclaration) {
1189
- magicString.remove(start, declarator.start);
1190
- keepDeclaration = true;
1191
- }
1192
- start = declarator.end;
1193
- }
1194
- if (!keepDeclaration) {
1195
- magicString.remove(declaration.start, declaration.end);
1196
- }
1197
- }
1198
- }
1199
-
1200
1135
  /* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
1201
1136
 
1202
1137
  const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
@@ -1210,13 +1145,15 @@ function transformCommonjs(
1210
1145
  isEsModule,
1211
1146
  ignoreGlobal,
1212
1147
  ignoreRequire,
1148
+ ignoreDynamicRequires,
1213
1149
  getIgnoreTryCatchRequireStatementMode,
1214
1150
  sourceMap,
1215
1151
  isDynamicRequireModulesEnabled,
1216
1152
  dynamicRequireModuleSet,
1217
1153
  disableWrap,
1218
1154
  commonDir,
1219
- astCache
1155
+ astCache,
1156
+ defaultIsModuleExports
1220
1157
  ) {
1221
1158
  const ast = astCache || tryParse(parse, code, id);
1222
1159
  const magicString = new MagicString__default['default'](code);
@@ -1238,7 +1175,7 @@ function transformCommonjs(
1238
1175
  const globals = new Set();
1239
1176
 
1240
1177
  // TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
1241
- const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
1178
+ const HELPERS_NAME = deconflict([scope], globals, 'commonjsHelpers');
1242
1179
  const dynamicRegisterSources = new Set();
1243
1180
  let hasRemovedRequire = false;
1244
1181
 
@@ -1489,12 +1426,14 @@ function transformCommonjs(
1489
1426
  )}`
1490
1427
  );
1491
1428
  }
1492
- if (isShorthandProperty(parent)) {
1493
- magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
1494
- } else {
1495
- magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
1496
- storeName: true
1497
- });
1429
+ if (!ignoreDynamicRequires) {
1430
+ if (isShorthandProperty(parent)) {
1431
+ magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
1432
+ } else {
1433
+ magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
1434
+ storeName: true
1435
+ });
1436
+ }
1498
1437
  }
1499
1438
  usesDynamicRequire = true;
1500
1439
  return;
@@ -1581,11 +1520,11 @@ function transformCommonjs(
1581
1520
  });
1582
1521
 
1583
1522
  const nameBase = getName(id);
1584
- const exportsName = deconflictScopes([...exportsAccessScopes], globals, nameBase);
1585
- const moduleName = deconflictScopes([...moduleAccessScopes], globals, `${nameBase}Module`);
1523
+ const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
1524
+ const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
1586
1525
  const deconflictedExportNames = Object.create(null);
1587
1526
  for (const [exportName, { scopes }] of exportsAssignmentsByName) {
1588
- deconflictedExportNames[exportName] = deconflictScopes([...scopes], globals, exportName);
1527
+ deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
1589
1528
  }
1590
1529
 
1591
1530
  // We cannot wrap ES/mixed modules
@@ -1605,7 +1544,8 @@ function transformCommonjs(
1605
1544
  uses.exports ||
1606
1545
  uses.require ||
1607
1546
  usesDynamicRequire ||
1608
- hasRemovedRequire
1547
+ hasRemovedRequire ||
1548
+ topLevelDefineCompiledEsmExpressions.length > 0
1609
1549
  ) &&
1610
1550
  (ignoreGlobal || !uses.global)
1611
1551
  ) {
@@ -1660,7 +1600,8 @@ function transformCommonjs(
1660
1600
  code,
1661
1601
  HELPERS_NAME,
1662
1602
  exportMode,
1663
- detectWrappedDefault
1603
+ detectWrappedDefault,
1604
+ defaultIsModuleExports
1664
1605
  );
1665
1606
 
1666
1607
  if (shouldWrap) {
@@ -1685,6 +1626,7 @@ function commonjs(options = {}) {
1685
1626
  const filter = pluginutils.createFilter(options.include, options.exclude);
1686
1627
  const {
1687
1628
  ignoreGlobal,
1629
+ ignoreDynamicRequires,
1688
1630
  requireReturnsDefault: requireReturnsDefaultOption,
1689
1631
  esmExternals
1690
1632
  } = options;
@@ -1699,6 +1641,8 @@ function commonjs(options = {}) {
1699
1641
  : Array.isArray(esmExternals)
1700
1642
  ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
1701
1643
  : () => esmExternals;
1644
+ const defaultIsModuleExports =
1645
+ typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1702
1646
 
1703
1647
  const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
1704
1648
  options.dynamicRequireTargets
@@ -1710,6 +1654,7 @@ function commonjs(options = {}) {
1710
1654
 
1711
1655
  const esModulesWithDefaultExport = new Set();
1712
1656
  const esModulesWithNamedExports = new Set();
1657
+ const commonJsMetaPromises = new Map();
1713
1658
 
1714
1659
  const ignoreRequire =
1715
1660
  typeof options.ignore === 'function'
@@ -1776,13 +1721,15 @@ function commonjs(options = {}) {
1776
1721
  isEsModule,
1777
1722
  ignoreGlobal || isEsModule,
1778
1723
  ignoreRequire,
1724
+ ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
1779
1725
  getIgnoreTryCatchRequireStatementMode,
1780
1726
  sourceMap,
1781
1727
  isDynamicRequireModulesEnabled,
1782
1728
  dynamicRequireModuleSet,
1783
1729
  disableWrap,
1784
1730
  commonDir,
1785
- ast
1731
+ ast,
1732
+ defaultIsModuleExports
1786
1733
  );
1787
1734
  }
1788
1735
 
@@ -1802,7 +1749,7 @@ function commonjs(options = {}) {
1802
1749
 
1803
1750
  load(id) {
1804
1751
  if (id === HELPERS_ID) {
1805
- return getHelpersModule(isDynamicRequireModulesEnabled);
1752
+ return getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires);
1806
1753
  }
1807
1754
 
1808
1755
  if (id.startsWith(HELPERS_ID)) {
@@ -1875,7 +1822,8 @@ function commonjs(options = {}) {
1875
1822
  actualId,
1876
1823
  getRequireReturnsDefault(actualId),
1877
1824
  esModulesWithDefaultExport,
1878
- esModulesWithNamedExports
1825
+ esModulesWithNamedExports,
1826
+ commonJsMetaPromises
1879
1827
  );
1880
1828
  }
1881
1829
 
@@ -1908,10 +1856,10 @@ function commonjs(options = {}) {
1908
1856
 
1909
1857
  moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
1910
1858
  if (commonjsMeta && commonjsMeta.isCommonJS != null) {
1911
- setCommonJSMetaPromise(id, commonjsMeta);
1859
+ setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
1912
1860
  return;
1913
1861
  }
1914
- setCommonJSMetaPromise(id, null);
1862
+ setCommonJSMetaPromise(commonJsMetaPromises, id, null);
1915
1863
  }
1916
1864
  };
1917
1865
  }