@rollup/plugin-commonjs 18.0.0-1 → 19.0.1

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
@@ -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,
@@ -158,8 +160,18 @@ export function commonjsRegister (path, loader) {
158
160
  DYNAMIC_REQUIRE_LOADERS[path] = loader;
159
161
  }
160
162
 
163
+ export function commonjsRegisterOrShort (path, to) {
164
+ const resolvedPath = commonjsResolveImpl(path, null, true);
165
+ if (resolvedPath !== null && DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
166
+ DYNAMIC_REQUIRE_CACHE[path] = DYNAMIC_REQUIRE_CACHE[resolvedPath];
167
+ } else {
168
+ DYNAMIC_REQUIRE_SHORTS[path] = to;
169
+ }
170
+ }
171
+
161
172
  const DYNAMIC_REQUIRE_LOADERS = Object.create(null);
162
173
  const DYNAMIC_REQUIRE_CACHE = Object.create(null);
174
+ const DYNAMIC_REQUIRE_SHORTS = Object.create(null);
163
175
  const DEFAULT_PARENT_MODULE = {
164
176
  id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []
165
177
  };
@@ -264,10 +276,13 @@ export function commonjsResolveImpl (path, originalModuleDir, testCache) {
264
276
  const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];
265
277
  if (DYNAMIC_REQUIRE_CACHE[resolvedPath]) {
266
278
  return resolvedPath;
267
- };
279
+ }
280
+ if (DYNAMIC_REQUIRE_SHORTS[resolvedPath]) {
281
+ return resolvedPath;
282
+ }
268
283
  if (DYNAMIC_REQUIRE_LOADERS[resolvedPath]) {
269
284
  return resolvedPath;
270
- };
285
+ }
271
286
  }
272
287
  if (!shouldTryNodeModules) break;
273
288
  const nextDir = normalize(originalModuleDir + '/..');
@@ -286,10 +301,17 @@ export function commonjsResolve (path, originalModuleDir) {
286
301
  }
287
302
 
288
303
  export function commonjsRequire (path, originalModuleDir) {
289
- const resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
304
+ let resolvedPath = commonjsResolveImpl(path, originalModuleDir, true);
290
305
  if (resolvedPath !== null) {
291
306
  let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];
292
307
  if (cachedModule) return cachedModule.exports;
308
+ let shortTo = DYNAMIC_REQUIRE_SHORTS[resolvedPath];
309
+ if (shortTo) {
310
+ cachedModule = DYNAMIC_REQUIRE_CACHE[shortTo];
311
+ if (cachedModule)
312
+ return cachedModule.exports;
313
+ resolvedPath = commonjsResolveImpl(shortTo, null, true);
314
+ }
293
315
  const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];
294
316
  if (loader) {
295
317
  DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
@@ -315,15 +337,17 @@ export function commonjsRequire (path, originalModuleDir) {
315
337
  return cachedModule.exports;
316
338
  };
317
339
  }
318
- return require(path);
340
+ ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
319
341
  }
320
342
 
321
343
  commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;
322
344
  commonjsRequire.resolve = commonjsResolve;
323
345
  `;
324
346
 
325
- function getHelpersModule(isDynamicRequireModulesEnabled) {
326
- return `${HELPERS}${isDynamicRequireModulesEnabled ? HELPERS_DYNAMIC : HELPER_NON_DYNAMIC}`;
347
+ function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires) {
348
+ return `${HELPERS}${
349
+ isDynamicRequireModulesEnabled ? getDynamicHelpers(ignoreDynamicRequires) : HELPER_NON_DYNAMIC
350
+ }`;
327
351
  }
328
352
 
329
353
  /* eslint-disable import/prefer-default-export */
@@ -384,15 +408,13 @@ function getPackageEntryPoint(dirPath) {
384
408
  }
385
409
 
386
410
  function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) {
387
- let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`;
411
+ let code = `const commonjsRegisterOrShort = require('${HELPERS_ID}?commonjsRegisterOrShort');`;
388
412
  for (const dir of dynamicRequireModuleDirPaths) {
389
413
  const entryPoint = getPackageEntryPoint(dir);
390
414
 
391
- code += `\ncommonjsRegister(${JSON.stringify(
415
+ code += `\ncommonjsRegisterOrShort(${JSON.stringify(
392
416
  getVirtualPathForDynamicRequirePath(dir, commonDir)
393
- )}, function (module, exports) {
394
- module.exports = require(${JSON.stringify(normalizePathSlashes(path.join(dir, entryPoint)))});
395
- });`;
417
+ )}, ${JSON.stringify(getVirtualPathForDynamicRequirePath(path.join(dir, entryPoint), commonDir))});`;
396
418
  }
397
419
  return code;
398
420
  }
@@ -447,9 +469,7 @@ function getDynamicRequirePaths(patterns) {
447
469
  return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
448
470
  }
449
471
 
450
- const commonJSMetaPromises = new Map();
451
-
452
- function getCommonJSMetaPromise(id) {
472
+ function getCommonJSMetaPromise(commonJSMetaPromises, id) {
453
473
  let commonJSMetaPromise = commonJSMetaPromises.get(id);
454
474
  if (commonJSMetaPromise) return commonJSMetaPromise.promise;
455
475
 
@@ -465,7 +485,7 @@ function getCommonJSMetaPromise(id) {
465
485
  return promise;
466
486
  }
467
487
 
468
- function setCommonJSMetaPromise(id, commonjsMeta) {
488
+ function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
469
489
  const commonJSMetaPromise = commonJSMetaPromises.get(id);
470
490
  if (commonJSMetaPromise) {
471
491
  if (commonJSMetaPromise.resolve) {
@@ -519,10 +539,11 @@ async function getStaticRequireProxy(
519
539
  id,
520
540
  requireReturnsDefault,
521
541
  esModulesWithDefaultExport,
522
- esModulesWithNamedExports
542
+ esModulesWithNamedExports,
543
+ commonJsMetaPromises
523
544
  ) {
524
545
  const name = getName(id);
525
- const commonjsMeta = await getCommonJSMetaPromise(id);
546
+ const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
526
547
  if (commonjsMeta && commonjsMeta.isCommonJS) {
527
548
  return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
528
549
  } else if (commonjsMeta === null) {
@@ -798,7 +819,8 @@ function rewriteExportsAndGetExportsBlock(
798
819
  code,
799
820
  HELPERS_NAME,
800
821
  exportMode,
801
- detectWrappedDefault
822
+ detectWrappedDefault,
823
+ defaultIsModuleExports
802
824
  ) {
803
825
  const exports = [];
804
826
  const exportDeclarations = [];
@@ -815,7 +837,13 @@ function rewriteExportsAndGetExportsBlock(
815
837
  } else {
816
838
  exports.push(`${exportsName} as __moduleExports`);
817
839
  if (wrapped) {
818
- getExportsWhenWrapping(exportDeclarations, exportsName, detectWrappedDefault, HELPERS_NAME);
840
+ getExportsWhenWrapping(
841
+ exportDeclarations,
842
+ exportsName,
843
+ detectWrappedDefault,
844
+ HELPERS_NAME,
845
+ defaultIsModuleExports
846
+ );
819
847
  } else {
820
848
  getExports(
821
849
  magicString,
@@ -828,7 +856,8 @@ function rewriteExportsAndGetExportsBlock(
828
856
  moduleName,
829
857
  exportsName,
830
858
  defineCompiledEsmExpressions,
831
- HELPERS_NAME
859
+ HELPERS_NAME,
860
+ defaultIsModuleExports
832
861
  );
833
862
  }
834
863
  }
@@ -859,12 +888,15 @@ function getExportsWhenWrapping(
859
888
  exportDeclarations,
860
889
  exportsName,
861
890
  detectWrappedDefault,
862
- HELPERS_NAME
891
+ HELPERS_NAME,
892
+ defaultIsModuleExports
863
893
  ) {
864
894
  exportDeclarations.push(
865
895
  `export default ${
866
- detectWrappedDefault
896
+ detectWrappedDefault && defaultIsModuleExports === 'auto'
867
897
  ? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
898
+ : defaultIsModuleExports === false
899
+ ? `${exportsName}.default`
868
900
  : exportsName
869
901
  };`
870
902
  );
@@ -881,7 +913,8 @@ function getExports(
881
913
  moduleName,
882
914
  exportsName,
883
915
  defineCompiledEsmExpressions,
884
- HELPERS_NAME
916
+ HELPERS_NAME,
917
+ defaultIsModuleExports
885
918
  ) {
886
919
  let deconflictedDefaultExportName;
887
920
  // Collect and rewrite module.exports assignments
@@ -921,16 +954,14 @@ function getExports(
921
954
  magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
922
955
  }
923
956
 
924
- if (isRestorableCompiledEsm) {
925
- if (moduleExportsAssignments.length === 0) {
926
- exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
927
- } else {
928
- exportDeclarations.push(
929
- `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
930
- );
931
- }
932
- } else {
957
+ if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
933
958
  exportDeclarations.push(`export default ${exportsName};`);
959
+ } else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
960
+ exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
961
+ } else {
962
+ exportDeclarations.push(
963
+ `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
964
+ );
934
965
  }
935
966
  }
936
967
 
@@ -1132,13 +1163,15 @@ function transformCommonjs(
1132
1163
  isEsModule,
1133
1164
  ignoreGlobal,
1134
1165
  ignoreRequire,
1166
+ ignoreDynamicRequires,
1135
1167
  getIgnoreTryCatchRequireStatementMode,
1136
1168
  sourceMap,
1137
1169
  isDynamicRequireModulesEnabled,
1138
1170
  dynamicRequireModuleSet,
1139
1171
  disableWrap,
1140
1172
  commonDir,
1141
- astCache
1173
+ astCache,
1174
+ defaultIsModuleExports
1142
1175
  ) {
1143
1176
  const ast = astCache || tryParse(parse, code, id);
1144
1177
  const magicString = new MagicString__default['default'](code);
@@ -1411,12 +1444,14 @@ function transformCommonjs(
1411
1444
  )}`
1412
1445
  );
1413
1446
  }
1414
- if (isShorthandProperty(parent)) {
1415
- magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
1416
- } else {
1417
- magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
1418
- storeName: true
1419
- });
1447
+ if (!ignoreDynamicRequires) {
1448
+ if (isShorthandProperty(parent)) {
1449
+ magicString.appendRight(node.end, `: ${HELPERS_NAME}.commonjsRequire`);
1450
+ } else {
1451
+ magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
1452
+ storeName: true
1453
+ });
1454
+ }
1420
1455
  }
1421
1456
  usesDynamicRequire = true;
1422
1457
  return;
@@ -1527,7 +1562,8 @@ function transformCommonjs(
1527
1562
  uses.exports ||
1528
1563
  uses.require ||
1529
1564
  usesDynamicRequire ||
1530
- hasRemovedRequire
1565
+ hasRemovedRequire ||
1566
+ topLevelDefineCompiledEsmExpressions.length > 0
1531
1567
  ) &&
1532
1568
  (ignoreGlobal || !uses.global)
1533
1569
  ) {
@@ -1582,7 +1618,8 @@ function transformCommonjs(
1582
1618
  code,
1583
1619
  HELPERS_NAME,
1584
1620
  exportMode,
1585
- detectWrappedDefault
1621
+ detectWrappedDefault,
1622
+ defaultIsModuleExports
1586
1623
  );
1587
1624
 
1588
1625
  if (shouldWrap) {
@@ -1607,6 +1644,7 @@ function commonjs(options = {}) {
1607
1644
  const filter = pluginutils.createFilter(options.include, options.exclude);
1608
1645
  const {
1609
1646
  ignoreGlobal,
1647
+ ignoreDynamicRequires,
1610
1648
  requireReturnsDefault: requireReturnsDefaultOption,
1611
1649
  esmExternals
1612
1650
  } = options;
@@ -1621,6 +1659,8 @@ function commonjs(options = {}) {
1621
1659
  : Array.isArray(esmExternals)
1622
1660
  ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
1623
1661
  : () => esmExternals;
1662
+ const defaultIsModuleExports =
1663
+ typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1624
1664
 
1625
1665
  const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
1626
1666
  options.dynamicRequireTargets
@@ -1632,6 +1672,7 @@ function commonjs(options = {}) {
1632
1672
 
1633
1673
  const esModulesWithDefaultExport = new Set();
1634
1674
  const esModulesWithNamedExports = new Set();
1675
+ const commonJsMetaPromises = new Map();
1635
1676
 
1636
1677
  const ignoreRequire =
1637
1678
  typeof options.ignore === 'function'
@@ -1698,13 +1739,15 @@ function commonjs(options = {}) {
1698
1739
  isEsModule,
1699
1740
  ignoreGlobal || isEsModule,
1700
1741
  ignoreRequire,
1742
+ ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
1701
1743
  getIgnoreTryCatchRequireStatementMode,
1702
1744
  sourceMap,
1703
1745
  isDynamicRequireModulesEnabled,
1704
1746
  dynamicRequireModuleSet,
1705
1747
  disableWrap,
1706
1748
  commonDir,
1707
- ast
1749
+ ast,
1750
+ defaultIsModuleExports
1708
1751
  );
1709
1752
  }
1710
1753
 
@@ -1724,7 +1767,7 @@ function commonjs(options = {}) {
1724
1767
 
1725
1768
  load(id) {
1726
1769
  if (id === HELPERS_ID) {
1727
- return getHelpersModule(isDynamicRequireModulesEnabled);
1770
+ return getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires);
1728
1771
  }
1729
1772
 
1730
1773
  if (id.startsWith(HELPERS_ID)) {
@@ -1797,7 +1840,8 @@ function commonjs(options = {}) {
1797
1840
  actualId,
1798
1841
  getRequireReturnsDefault(actualId),
1799
1842
  esModulesWithDefaultExport,
1800
- esModulesWithNamedExports
1843
+ esModulesWithNamedExports,
1844
+ commonJsMetaPromises
1801
1845
  );
1802
1846
  }
1803
1847
 
@@ -1830,10 +1874,10 @@ function commonjs(options = {}) {
1830
1874
 
1831
1875
  moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
1832
1876
  if (commonjsMeta && commonjsMeta.isCommonJS != null) {
1833
- setCommonJSMetaPromise(id, commonjsMeta);
1877
+ setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
1834
1878
  return;
1835
1879
  }
1836
- setCommonJSMetaPromise(id, null);
1880
+ setCommonJSMetaPromise(commonJsMetaPromises, id, null);
1837
1881
  }
1838
1882
  };
1839
1883
  }