@rollup/plugin-commonjs 22.0.0-0 → 22.0.0-4

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
@@ -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-0";
19
+ var version = "22.0.0-4";
20
20
  var peerDependencies = {
21
- rollup: "^2.60.0"
21
+ rollup: "^2.61.1"
22
22
  };
23
23
 
24
24
  function tryParse(parse, code, id) {
@@ -203,6 +203,9 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
203
203
 
204
204
  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.');`;
205
205
 
206
+ const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
207
+ const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
208
+
206
209
  function getDynamicModuleRegistry(
207
210
  isDynamicRequireModulesEnabled,
208
211
  dynamicRequireModules,
@@ -210,7 +213,7 @@ function getDynamicModuleRegistry(
210
213
  ignoreDynamicRequires
211
214
  ) {
212
215
  if (!isDynamicRequireModulesEnabled) {
213
- return `export function commonjsRequire(path) {
216
+ return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
214
217
  ${FAILED_REQUIRE_ERROR}
215
218
  }`;
216
219
  }
@@ -240,25 +243,25 @@ ${dynamicModuleProps}
240
243
  });
241
244
  }
242
245
 
243
- export function commonjsRequire(path, originalModuleDir) {
244
- var resolvedPath = commonjsResolveImpl(path, originalModuleDir);
245
- if (resolvedPath !== null) {
246
- return getDynamicModules()[resolvedPath]();
246
+ export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
247
+ function handleRequire(path) {
248
+ var resolvedPath = commonjsResolve(path, originalModuleDir);
249
+ if (resolvedPath !== null) {
250
+ return getDynamicModules()[resolvedPath]();
251
+ }
252
+ ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
247
253
  }
248
- ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
249
- }
250
-
251
- function commonjsResolve (path, originalModuleDir) {
252
- const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
253
- if (resolvedPath !== null) {
254
- return resolvedPath;
254
+ handleRequire.resolve = function (path) {
255
+ var resolvedPath = commonjsResolve(path, originalModuleDir);
256
+ if (resolvedPath !== null) {
257
+ return resolvedPath;
258
+ }
259
+ return require.resolve(path);
255
260
  }
256
- return require.resolve(path);
261
+ return handleRequire;
257
262
  }
258
263
 
259
- commonjsRequire.resolve = commonjsResolve;
260
-
261
- function commonjsResolveImpl (path, originalModuleDir) {
264
+ function commonjsResolve (path, originalModuleDir) {
262
265
  var shouldTryNodeModules = isPossibleNodeModulesPath(path);
263
266
  path = normalize(path);
264
267
  var relPath;
@@ -487,6 +490,14 @@ function resolveExtensions(importee, importer, extensions) {
487
490
 
488
491
  function getResolveId(extensions) {
489
492
  return async function resolveId(importee, importer, resolveOptions) {
493
+ // We assume that all requires are pre-resolved
494
+ if (
495
+ resolveOptions.custom &&
496
+ resolveOptions.custom['node-resolve'] &&
497
+ resolveOptions.custom['node-resolve'].isRequire
498
+ ) {
499
+ return null;
500
+ }
490
501
  if (isWrappedId(importee, WRAPPED_SUFFIX)) {
491
502
  return unwrapId(importee, WRAPPED_SUFFIX);
492
503
  }
@@ -506,7 +517,7 @@ function getResolveId(extensions) {
506
517
  if (importer) {
507
518
  if (
508
519
  importer === DYNAMIC_MODULES_ID ||
509
- // Except for exports, proxies are only importing resolved ids, no need to resolve again
520
+ // Proxies are only importing resolved ids, no need to resolve again
510
521
  isWrappedId(importer, PROXY_SUFFIX) ||
511
522
  isWrappedId(importer, ES_IMPORT_SUFFIX)
512
523
  ) {
@@ -555,9 +566,39 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
555
566
  const knownCjsModuleTypes = Object.create(null);
556
567
  const requiredIds = Object.create(null);
557
568
  const unconditionallyRequiredIds = Object.create(null);
558
- const dependentModules = Object.create(null);
559
- const getDependentModules = (id) =>
560
- dependentModules[id] || (dependentModules[id] = Object.create(null));
569
+ const dependencies = Object.create(null);
570
+ const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
571
+
572
+ const isCyclic = (id) => {
573
+ const dependenciesToCheck = new Set(getDependencies(id));
574
+ for (const dependency of dependenciesToCheck) {
575
+ if (dependency === id) {
576
+ return true;
577
+ }
578
+ for (const childDependency of getDependencies(dependency)) {
579
+ dependenciesToCheck.add(childDependency);
580
+ }
581
+ }
582
+ return false;
583
+ };
584
+
585
+ const fullyAnalyzedModules = Object.create(null);
586
+
587
+ const getTypeForFullyAnalyzedModule = (id) => {
588
+ const knownType = knownCjsModuleTypes[id];
589
+ if (
590
+ knownType === IS_WRAPPED_COMMONJS ||
591
+ !detectCyclesAndConditional ||
592
+ fullyAnalyzedModules[id]
593
+ ) {
594
+ return knownType;
595
+ }
596
+ fullyAnalyzedModules[id] = true;
597
+ if (isCyclic(id)) {
598
+ return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
599
+ }
600
+ return knownType;
601
+ };
561
602
 
562
603
  return {
563
604
  getWrappedIds: () =>
@@ -570,8 +611,9 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
570
611
  isParentCommonJS,
571
612
  sources
572
613
  ) => {
573
- knownCjsModuleTypes[parentId] = knownCjsModuleTypes[parentId] || isParentCommonJS;
614
+ knownCjsModuleTypes[parentId] = isParentCommonJS;
574
615
  if (
616
+ detectCyclesAndConditional &&
575
617
  knownCjsModuleTypes[parentId] &&
576
618
  requiredIds[parentId] &&
577
619
  !unconditionallyRequiredIds[parentId]
@@ -586,9 +628,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
586
628
  }
587
629
  const resolved =
588
630
  (await rollupContext.resolve(source, parentId, {
589
- custom: {
590
- 'node-resolve': { isRequire: true }
591
- }
631
+ custom: { 'node-resolve': { isRequire: true } }
592
632
  })) || resolveExtensions(source, parentId, extensions);
593
633
  if (!resolved) {
594
634
  return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
@@ -598,44 +638,25 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
598
638
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
599
639
  }
600
640
  requiredIds[childId] = true;
601
- if (
602
- !(
603
- detectCyclesAndConditional &&
604
- (isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)
605
- )
606
- ) {
641
+ if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
607
642
  unconditionallyRequiredIds[childId] = true;
608
643
  }
609
- const parentDependentModules = getDependentModules(parentId);
610
- const childDependentModules = getDependentModules(childId);
611
- childDependentModules[parentId] = true;
612
- for (const dependentId of Object.keys(parentDependentModules)) {
613
- childDependentModules[dependentId] = true;
614
- }
615
- if (parentDependentModules[childId]) {
616
- // If we depend on one of our dependencies, we have a cycle. Then all modules that
617
- // we depend on that also depend on the same module are part of a cycle as well.
618
- if (detectCyclesAndConditional && isParentCommonJS) {
619
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
620
- knownCjsModuleTypes[childId] = IS_WRAPPED_COMMONJS;
621
- for (const dependentId of Object.keys(parentDependentModules)) {
622
- if (getDependentModules(dependentId)[childId]) {
623
- knownCjsModuleTypes[dependentId] = IS_WRAPPED_COMMONJS;
624
- }
625
- }
626
- }
627
- } else {
644
+
645
+ getDependencies(parentId).add(childId);
646
+ if (!isCyclic(childId)) {
628
647
  // This makes sure the current transform handler waits for all direct dependencies to be
629
648
  // loaded and transformed and therefore for all transitive CommonJS dependencies to be
630
649
  // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
631
650
  await rollupContext.load(resolved);
651
+ } else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
652
+ knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
632
653
  }
633
654
  return { id: childId, allowProxy: true };
634
655
  })
635
656
  );
636
657
  return {
637
658
  requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
638
- const isCommonJS = knownCjsModuleTypes[dependencyId];
659
+ const isCommonJS = getTypeForFullyAnalyzedModule(dependencyId);
639
660
  return {
640
661
  source: sources[index].source,
641
662
  id: allowProxy
@@ -646,29 +667,39 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
646
667
  isCommonJS
647
668
  };
648
669
  }),
649
- usesRequireWrapper: knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS
670
+ usesRequireWrapper: getTypeForFullyAnalyzedModule(parentId) === IS_WRAPPED_COMMONJS
650
671
  };
651
672
  }
652
673
  };
653
674
  }
654
675
 
655
- function validateRollupVersion(rollupVersion, peerDependencyVersion) {
656
- const [major, minor] = rollupVersion.split('.').map(Number);
657
- const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
676
+ function validateVersion(actualVersion, peerDependencyVersion, name) {
677
+ const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
658
678
  let minMajor = Infinity;
659
679
  let minMinor = Infinity;
680
+ let minPatch = Infinity;
660
681
  let foundVersion;
661
682
  // eslint-disable-next-line no-cond-assign
662
683
  while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
663
- const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
684
+ const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
664
685
  if (foundMajor < minMajor) {
665
686
  minMajor = foundMajor;
666
687
  minMinor = foundMinor;
688
+ minPatch = foundPatch;
667
689
  }
668
690
  }
669
- if (major < minMajor || (major === minMajor && minor < minMinor)) {
691
+ if (!actualVersion) {
670
692
  throw new Error(
671
- `Insufficient Rollup version: "@rollup/plugin-commonjs" requires at least rollup@${minMajor}.${minMinor} but found rollup@${rollupVersion}.`
693
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
694
+ );
695
+ }
696
+ const [major, minor, patch] = actualVersion.split('.').map(Number);
697
+ if (
698
+ major < minMajor ||
699
+ (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
700
+ ) {
701
+ throw new Error(
702
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
672
703
  );
673
704
  }
674
705
  }
@@ -1114,14 +1145,16 @@ function getRequireHandlers() {
1114
1145
  resolveRequireSourcesAndGetMeta,
1115
1146
  needsRequireWrapper,
1116
1147
  isEsModule,
1117
- usesRequire,
1148
+ isDynamicRequireModulesEnabled,
1118
1149
  getIgnoreTryCatchRequireStatementMode
1119
1150
  ) {
1120
1151
  const imports = [];
1121
1152
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
1122
- if (usesRequire) {
1153
+ if (dynamicRequireName) {
1123
1154
  imports.push(
1124
- `import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1155
+ `import { ${
1156
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1157
+ } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1125
1158
  );
1126
1159
  }
1127
1160
  if (exportMode === 'module') {
@@ -1407,15 +1440,9 @@ async function transformCommonjs(
1407
1440
  isRequire(node.callee.object, scope) &&
1408
1441
  node.callee.property.name === 'resolve'
1409
1442
  ) {
1410
- checkDynamicRequire();
1443
+ checkDynamicRequire(node.start);
1411
1444
  uses.require = true;
1412
1445
  const requireNode = node.callee.object;
1413
- magicString.appendLeft(
1414
- node.end - 1,
1415
- `,${JSON.stringify(
1416
- path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1417
- )}`
1418
- );
1419
1446
  replacedDynamicRequires.push(requireNode);
1420
1447
  return;
1421
1448
  }
@@ -1429,15 +1456,9 @@ async function transformCommonjs(
1429
1456
 
1430
1457
  if (hasDynamicArguments(node)) {
1431
1458
  if (isDynamicRequireModulesEnabled) {
1432
- magicString.appendLeft(
1433
- node.end - 1,
1434
- `, ${JSON.stringify(
1435
- path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1436
- )}`
1437
- );
1459
+ checkDynamicRequire(node.start);
1438
1460
  }
1439
1461
  if (!ignoreDynamicRequires) {
1440
- checkDynamicRequire();
1441
1462
  replacedDynamicRequires.push(node.callee);
1442
1463
  }
1443
1464
  return;
@@ -1495,7 +1516,6 @@ async function transformCommonjs(
1495
1516
  return;
1496
1517
  }
1497
1518
  if (!ignoreDynamicRequires) {
1498
- checkDynamicRequire();
1499
1519
  if (isShorthandProperty(parent)) {
1500
1520
  magicString.prependRight(node.start, 'require: ');
1501
1521
  }
@@ -1579,9 +1599,10 @@ async function transformCommonjs(
1579
1599
  if (scope.contains(flattened.name)) return;
1580
1600
 
1581
1601
  if (
1582
- flattened.keypath === 'module.exports' ||
1583
- flattened.keypath === 'module' ||
1584
- flattened.keypath === 'exports'
1602
+ !isEsModule &&
1603
+ (flattened.keypath === 'module.exports' ||
1604
+ flattened.keypath === 'module' ||
1605
+ flattened.keypath === 'exports')
1585
1606
  ) {
1586
1607
  magicString.overwrite(node.start, node.end, `'object'`, {
1587
1608
  storeName: false
@@ -1609,7 +1630,13 @@ async function transformCommonjs(
1609
1630
  const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
1610
1631
  const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
1611
1632
  const helpersName = deconflict([scope], globals, 'commonjsHelpers');
1612
- const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
1633
+ const dynamicRequireName =
1634
+ replacedDynamicRequires.length > 0 &&
1635
+ deconflict(
1636
+ [scope],
1637
+ globals,
1638
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1639
+ );
1613
1640
  const deconflictedExportNames = Object.create(null);
1614
1641
  for (const [exportName, { scopes }] of exportsAssignmentsByName) {
1615
1642
  deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
@@ -1621,10 +1648,17 @@ async function transformCommonjs(
1621
1648
  });
1622
1649
  }
1623
1650
  for (const node of replacedDynamicRequires) {
1624
- magicString.overwrite(node.start, node.end, dynamicRequireName, {
1625
- contentOnly: true,
1626
- storeName: true
1627
- });
1651
+ magicString.overwrite(
1652
+ node.start,
1653
+ node.end,
1654
+ isDynamicRequireModulesEnabled
1655
+ ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
1656
+ : dynamicRequireName,
1657
+ {
1658
+ contentOnly: true,
1659
+ storeName: true
1660
+ }
1661
+ );
1628
1662
  }
1629
1663
 
1630
1664
  // We cannot wrap ES/mixed modules
@@ -1679,7 +1713,7 @@ async function transformCommonjs(
1679
1713
  resolveRequireSourcesAndGetMeta,
1680
1714
  needsRequireWrapper,
1681
1715
  isEsModule,
1682
- uses.require,
1716
+ isDynamicRequireModulesEnabled,
1683
1717
  getIgnoreTryCatchRequireStatementMode
1684
1718
  );
1685
1719
  const exportBlock = isEsModule
@@ -1840,16 +1874,19 @@ function commonjs(options = {}) {
1840
1874
  !isEsModule &&
1841
1875
  (dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
1842
1876
 
1843
- const checkDynamicRequire = () => {
1877
+ const checkDynamicRequire = (position) => {
1844
1878
  if (id.indexOf(dynamicRequireRoot) !== 0) {
1845
- this.error({
1846
- code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
1847
- id,
1848
- dynamicRequireRoot,
1849
- message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${path.dirname(
1850
- id
1851
- )}" or one of its parent directories.`
1852
- });
1879
+ this.error(
1880
+ {
1881
+ code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
1882
+ id,
1883
+ dynamicRequireRoot,
1884
+ message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${path.dirname(
1885
+ id
1886
+ )}" or one of its parent directories.`
1887
+ },
1888
+ position
1889
+ );
1853
1890
  }
1854
1891
  };
1855
1892
 
@@ -1895,8 +1932,12 @@ function commonjs(options = {}) {
1895
1932
  return { ...rawOptions, plugins };
1896
1933
  },
1897
1934
 
1898
- buildStart() {
1899
- validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
1935
+ buildStart({ plugins }) {
1936
+ validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
1937
+ const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
1938
+ if (nodeResolve) {
1939
+ validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
1940
+ }
1900
1941
  if (options.namedExports != null) {
1901
1942
  this.warn(
1902
1943
  'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'