@rollup/plugin-commonjs 22.0.0-1 → 22.0.0-5

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.
@@ -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-1";
10
+ var version = "22.0.0-5";
11
11
  var peerDependencies = {
12
- rollup: "^2.60.0"
12
+ rollup: "^2.64.0"
13
13
  };
14
14
 
15
15
  function tryParse(parse, code, id) {
@@ -194,6 +194,9 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
194
194
 
195
195
  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.');`;
196
196
 
197
+ const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
198
+ const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
199
+
197
200
  function getDynamicModuleRegistry(
198
201
  isDynamicRequireModulesEnabled,
199
202
  dynamicRequireModules,
@@ -201,7 +204,7 @@ function getDynamicModuleRegistry(
201
204
  ignoreDynamicRequires
202
205
  ) {
203
206
  if (!isDynamicRequireModulesEnabled) {
204
- return `export function commonjsRequire(path) {
207
+ return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
205
208
  ${FAILED_REQUIRE_ERROR}
206
209
  }`;
207
210
  }
@@ -231,25 +234,25 @@ ${dynamicModuleProps}
231
234
  });
232
235
  }
233
236
 
234
- export function commonjsRequire(path, originalModuleDir) {
235
- var resolvedPath = commonjsResolveImpl(path, originalModuleDir);
236
- if (resolvedPath !== null) {
237
- return getDynamicModules()[resolvedPath]();
237
+ export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
238
+ function handleRequire(path) {
239
+ var resolvedPath = commonjsResolve(path, originalModuleDir);
240
+ if (resolvedPath !== null) {
241
+ return getDynamicModules()[resolvedPath]();
242
+ }
243
+ ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
238
244
  }
239
- ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
240
- }
241
-
242
- function commonjsResolve (path, originalModuleDir) {
243
- const resolvedPath = commonjsResolveImpl(path, originalModuleDir);
244
- if (resolvedPath !== null) {
245
- return resolvedPath;
245
+ handleRequire.resolve = function (path) {
246
+ var resolvedPath = commonjsResolve(path, originalModuleDir);
247
+ if (resolvedPath !== null) {
248
+ return resolvedPath;
249
+ }
250
+ return require.resolve(path);
246
251
  }
247
- return require.resolve(path);
252
+ return handleRequire;
248
253
  }
249
254
 
250
- commonjsRequire.resolve = commonjsResolve;
251
-
252
- function commonjsResolveImpl (path, originalModuleDir) {
255
+ function commonjsResolve (path, originalModuleDir) {
253
256
  var shouldTryNodeModules = isPossibleNodeModulesPath(path);
254
257
  path = normalize(path);
255
258
  var relPath;
@@ -393,21 +396,15 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
393
396
  return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
394
397
  }
395
398
 
396
- async function getStaticRequireProxy(
397
- id,
398
- requireReturnsDefault,
399
- esModulesWithDefaultExport,
400
- esModulesWithNamedExports,
401
- loadModule
402
- ) {
399
+ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
403
400
  const name = getName(id);
404
401
  const {
405
402
  meta: { commonjs: commonjsMeta }
406
403
  } = await loadModule({ id });
407
- if (commonjsMeta && commonjsMeta.isCommonJS) {
408
- return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
409
- } else if (!commonjsMeta) {
404
+ if (!commonjsMeta) {
410
405
  return getUnknownRequireProxy(id, requireReturnsDefault);
406
+ } else if (commonjsMeta.isCommonJS) {
407
+ return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
411
408
  } else if (!requireReturnsDefault) {
412
409
  return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
413
410
  id
@@ -415,8 +412,8 @@ async function getStaticRequireProxy(
415
412
  } else if (
416
413
  requireReturnsDefault !== true &&
417
414
  (requireReturnsDefault === 'namespace' ||
418
- !esModulesWithDefaultExport.has(id) ||
419
- (requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id)))
415
+ !commonjsMeta.hasDefaultExport ||
416
+ (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
420
417
  ) {
421
418
  return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
422
419
  }
@@ -478,10 +475,11 @@ function resolveExtensions(importee, importer, extensions) {
478
475
 
479
476
  function getResolveId(extensions) {
480
477
  return async function resolveId(importee, importer, resolveOptions) {
478
+ // We assume that all requires are pre-resolved
481
479
  if (
482
480
  resolveOptions.custom &&
483
- resolveOptions.custom.commonjs &&
484
- resolveOptions.custom.commonjs.skipResolver
481
+ resolveOptions.custom['node-resolve'] &&
482
+ resolveOptions.custom['node-resolve'].isRequire
485
483
  ) {
486
484
  return null;
487
485
  }
@@ -549,14 +547,73 @@ function getResolveId(extensions) {
549
547
  };
550
548
  }
551
549
 
552
- function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional) {
550
+ function getRequireResolver(extensions, detectCyclesAndConditional) {
553
551
  const knownCjsModuleTypes = Object.create(null);
554
552
  const requiredIds = Object.create(null);
555
553
  const unconditionallyRequiredIds = Object.create(null);
556
- const parentModules = Object.create(null);
557
- const childModules = Object.create(null);
558
- const getParentModules = (id) => parentModules[id] || (parentModules[id] = Object.create(null));
559
- const getChildModules = (id) => childModules[id] || (childModules[id] = Object.create(null));
554
+ const dependencies = Object.create(null);
555
+ const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
556
+
557
+ const isCyclic = (id) => {
558
+ const dependenciesToCheck = new Set(getDependencies(id));
559
+ for (const dependency of dependenciesToCheck) {
560
+ if (dependency === id) {
561
+ return true;
562
+ }
563
+ for (const childDependency of getDependencies(dependency)) {
564
+ dependenciesToCheck.add(childDependency);
565
+ }
566
+ }
567
+ return false;
568
+ };
569
+
570
+ const fullyAnalyzedModules = Object.create(null);
571
+
572
+ const getTypeForFullyAnalyzedModule = (id) => {
573
+ const knownType = knownCjsModuleTypes[id];
574
+ if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
575
+ return knownType;
576
+ }
577
+ fullyAnalyzedModules[id] = true;
578
+ if (isCyclic(id)) {
579
+ return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
580
+ }
581
+ return knownType;
582
+ };
583
+
584
+ const setInitialParentType = (id, initialCommonJSType) => {
585
+ // It is possible a transformed module is already fully analyzed when using
586
+ // the cache and one dependency introduces a new cycle. Then transform is
587
+ // run for a fully analzyed module again. Fully analyzed modules may never
588
+ // change their type as importers already trust their type.
589
+ knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
590
+ ? knownCjsModuleTypes[id]
591
+ : initialCommonJSType;
592
+ if (
593
+ detectCyclesAndConditional &&
594
+ knownCjsModuleTypes[id] === true &&
595
+ requiredIds[id] &&
596
+ !unconditionallyRequiredIds[id]
597
+ ) {
598
+ knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
599
+ }
600
+ };
601
+
602
+ const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
603
+ const childId = resolved.id;
604
+ requiredIds[childId] = true;
605
+ if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
606
+ unconditionallyRequiredIds[childId] = true;
607
+ }
608
+
609
+ getDependencies(parentId).add(childId);
610
+ if (!isCyclic(childId)) {
611
+ // This makes sure the current transform handler waits for all direct dependencies to be
612
+ // loaded and transformed and therefore for all transitive CommonJS dependencies to be
613
+ // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
614
+ await loadModule(resolved);
615
+ }
616
+ };
560
617
 
561
618
  return {
562
619
  getWrappedIds: () =>
@@ -564,19 +621,40 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
564
621
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
565
622
  ),
566
623
  isRequiredId: (id) => requiredIds[id],
567
- resolveRequireSourcesAndGetMeta: (rollupContext) => async (
624
+ async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
625
+ // Ignore modules that did not pass through the original transformer in a previous build
626
+ if (!(parentMeta && parentMeta.requires)) {
627
+ return false;
628
+ }
629
+ setInitialParentType(parentId, parentMeta.initialCommonJSType);
630
+ await Promise.all(
631
+ parentMeta.requires.map(({ resolved, isConditional }) =>
632
+ setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
633
+ )
634
+ );
635
+ if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
636
+ return true;
637
+ }
638
+ for (const {
639
+ resolved: { id }
640
+ } of parentMeta.requires) {
641
+ if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
642
+ return true;
643
+ }
644
+ }
645
+ return false;
646
+ },
647
+ /* eslint-disable no-param-reassign */
648
+ resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
568
649
  parentId,
569
650
  isParentCommonJS,
651
+ parentMeta,
570
652
  sources
571
653
  ) => {
572
- knownCjsModuleTypes[parentId] = knownCjsModuleTypes[parentId] || isParentCommonJS;
573
- if (
574
- knownCjsModuleTypes[parentId] &&
575
- requiredIds[parentId] &&
576
- !unconditionallyRequiredIds[parentId]
577
- ) {
578
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
579
- }
654
+ parentMeta.initialCommonJSType = isParentCommonJS;
655
+ parentMeta.requires = [];
656
+ parentMeta.isRequiredCommonJS = Object.create(null);
657
+ setInitialParentType(parentId, isParentCommonJS);
580
658
  const requireTargets = await Promise.all(
581
659
  sources.map(async ({ source, isConditional }) => {
582
660
  // Never analyze or proxy internal modules
@@ -585,10 +663,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
585
663
  }
586
664
  const resolved =
587
665
  (await rollupContext.resolve(source, parentId, {
588
- custom: {
589
- 'node-resolve': { isRequire: true },
590
- commonjs: { skipResolver: true }
591
- }
666
+ custom: { 'node-resolve': { isRequire: true } }
592
667
  })) || resolveExtensions(source, parentId, extensions);
593
668
  if (!resolved) {
594
669
  return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
@@ -597,96 +672,58 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
597
672
  if (resolved.external) {
598
673
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
599
674
  }
600
- requiredIds[childId] = true;
601
- if (
602
- !(
603
- detectCyclesAndConditional &&
604
- (isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)
605
- )
606
- ) {
607
- unconditionallyRequiredIds[childId] = true;
608
- }
609
- const parentDependentModules = getParentModules(parentId);
610
- const childDependentModules = getParentModules(childId);
611
-
612
- // Copy the parents of the current parent to the child
613
- for (const dependentId of Object.keys(parentDependentModules)) {
614
- childDependentModules[dependentId] = true;
615
- }
616
-
617
- // Add the current parent to the child as well. If the child module already has known
618
- // dependencies because it has already been loaded, add the current parent module to their
619
- // parent modules
620
- childDependentModules[parentId] = true;
621
- const dependenciesToUpdate = new Set(Object.keys(getChildModules(childId)));
622
- for (const dependencyId of dependenciesToUpdate) {
623
- getParentModules(dependencyId)[parentId] = true;
624
- for (const subDependencyId of Object.keys(getChildModules(dependencyId))) {
625
- dependenciesToUpdate.add(subDependencyId);
626
- }
627
- }
628
-
629
- // Add the child as a dependency to the parent
630
- getChildModules(parentId)[childId] = true;
631
-
632
- // If we depend on one of our dependencies, we have a cycle. Then all modules that
633
- // we depend on that also depend on the same module are part of a cycle as well. Trying
634
- // to wait for loading this module would lead to a deadlock.
635
- if (parentDependentModules[childId]) {
636
- if (detectCyclesAndConditional && isParentCommonJS) {
637
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
638
- knownCjsModuleTypes[childId] = IS_WRAPPED_COMMONJS;
639
- for (const dependentId of Object.keys(parentDependentModules)) {
640
- if (getParentModules(dependentId)[childId]) {
641
- knownCjsModuleTypes[dependentId] = IS_WRAPPED_COMMONJS;
642
- }
643
- }
644
- }
645
- } else {
646
- // This makes sure the current transform handler waits for all direct dependencies to be
647
- // loaded and transformed and therefore for all transitive CommonJS dependencies to be
648
- // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
649
- await rollupContext.load(resolved);
650
- }
675
+ parentMeta.requires.push({ resolved, isConditional });
676
+ await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
651
677
  return { id: childId, allowProxy: true };
652
678
  })
653
679
  );
654
- return {
655
- requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
656
- const isCommonJS = knownCjsModuleTypes[dependencyId];
657
- return {
658
- source: sources[index].source,
659
- id: allowProxy
660
- ? isCommonJS === IS_WRAPPED_COMMONJS
661
- ? wrapId(dependencyId, WRAPPED_SUFFIX)
662
- : wrapId(dependencyId, PROXY_SUFFIX)
663
- : dependencyId,
664
- isCommonJS
665
- };
666
- }),
667
- usesRequireWrapper: knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS
668
- };
680
+ parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
681
+ return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
682
+ // eslint-disable-next-line no-multi-assign
683
+ const isCommonJS = (parentMeta.isRequiredCommonJS[
684
+ dependencyId
685
+ ] = getTypeForFullyAnalyzedModule(dependencyId));
686
+ return {
687
+ source: sources[index].source,
688
+ id: allowProxy
689
+ ? isCommonJS === IS_WRAPPED_COMMONJS
690
+ ? wrapId(dependencyId, WRAPPED_SUFFIX)
691
+ : wrapId(dependencyId, PROXY_SUFFIX)
692
+ : dependencyId,
693
+ isCommonJS
694
+ };
695
+ });
669
696
  }
670
697
  };
671
698
  }
672
699
 
673
- function validateRollupVersion(rollupVersion, peerDependencyVersion) {
674
- const [major, minor] = rollupVersion.split('.').map(Number);
675
- const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
700
+ function validateVersion(actualVersion, peerDependencyVersion, name) {
701
+ const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
676
702
  let minMajor = Infinity;
677
703
  let minMinor = Infinity;
704
+ let minPatch = Infinity;
678
705
  let foundVersion;
679
706
  // eslint-disable-next-line no-cond-assign
680
707
  while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
681
- const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
708
+ const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
682
709
  if (foundMajor < minMajor) {
683
710
  minMajor = foundMajor;
684
711
  minMinor = foundMinor;
712
+ minPatch = foundPatch;
685
713
  }
686
714
  }
687
- if (major < minMajor || (major === minMajor && minor < minMinor)) {
715
+ if (!actualVersion) {
716
+ throw new Error(
717
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
718
+ );
719
+ }
720
+ const [major, minor, patch] = actualVersion.split('.').map(Number);
721
+ if (
722
+ major < minMajor ||
723
+ (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
724
+ ) {
688
725
  throw new Error(
689
- `Insufficient Rollup version: "@rollup/plugin-commonjs" requires at least rollup@${minMajor}.${minMinor} but found rollup@${rollupVersion}.`
726
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
690
727
  );
691
728
  }
692
729
  }
@@ -1129,17 +1166,20 @@ function getRequireHandlers() {
1129
1166
  exportsName,
1130
1167
  id,
1131
1168
  exportMode,
1132
- resolveRequireSourcesAndGetMeta,
1169
+ resolveRequireSourcesAndUpdateMeta,
1133
1170
  needsRequireWrapper,
1134
1171
  isEsModule,
1135
- usesRequire,
1136
- getIgnoreTryCatchRequireStatementMode
1172
+ isDynamicRequireModulesEnabled,
1173
+ getIgnoreTryCatchRequireStatementMode,
1174
+ commonjsMeta
1137
1175
  ) {
1138
1176
  const imports = [];
1139
1177
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
1140
- if (usesRequire) {
1178
+ if (dynamicRequireName) {
1141
1179
  imports.push(
1142
- `import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1180
+ `import { ${
1181
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1182
+ } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1143
1183
  );
1144
1184
  }
1145
1185
  if (exportMode === 'module') {
@@ -1154,9 +1194,10 @@ function getRequireHandlers() {
1154
1194
  );
1155
1195
  }
1156
1196
  const requiresBySource = collectSources(requireExpressions);
1157
- const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
1197
+ const requireTargets = await resolveRequireSourcesAndUpdateMeta(
1158
1198
  id,
1159
1199
  needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
1200
+ commonjsMeta,
1160
1201
  Object.keys(requiresBySource).map((source) => {
1161
1202
  return {
1162
1203
  source,
@@ -1171,10 +1212,7 @@ function getRequireHandlers() {
1171
1212
  getIgnoreTryCatchRequireStatementMode,
1172
1213
  magicString
1173
1214
  );
1174
- return {
1175
- importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
1176
- usesRequireWrapper
1177
- };
1215
+ return imports.length ? `${imports.join('\n')}\n\n` : '';
1178
1216
  }
1179
1217
 
1180
1218
  return {
@@ -1277,9 +1315,10 @@ async function transformCommonjs(
1277
1315
  astCache,
1278
1316
  defaultIsModuleExports,
1279
1317
  needsRequireWrapper,
1280
- resolveRequireSourcesAndGetMeta,
1318
+ resolveRequireSourcesAndUpdateMeta,
1281
1319
  isRequired,
1282
- checkDynamicRequire
1320
+ checkDynamicRequire,
1321
+ commonjsMeta
1283
1322
  ) {
1284
1323
  const ast = astCache || tryParse(parse, code, id);
1285
1324
  const magicString = new MagicString(code);
@@ -1425,15 +1464,9 @@ async function transformCommonjs(
1425
1464
  isRequire(node.callee.object, scope) &&
1426
1465
  node.callee.property.name === 'resolve'
1427
1466
  ) {
1428
- checkDynamicRequire();
1467
+ checkDynamicRequire(node.start);
1429
1468
  uses.require = true;
1430
1469
  const requireNode = node.callee.object;
1431
- magicString.appendLeft(
1432
- node.end - 1,
1433
- `,${JSON.stringify(
1434
- dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1435
- )}`
1436
- );
1437
1470
  replacedDynamicRequires.push(requireNode);
1438
1471
  return;
1439
1472
  }
@@ -1447,15 +1480,9 @@ async function transformCommonjs(
1447
1480
 
1448
1481
  if (hasDynamicArguments(node)) {
1449
1482
  if (isDynamicRequireModulesEnabled) {
1450
- magicString.appendLeft(
1451
- node.end - 1,
1452
- `, ${JSON.stringify(
1453
- dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1454
- )}`
1455
- );
1483
+ checkDynamicRequire(node.start);
1456
1484
  }
1457
1485
  if (!ignoreDynamicRequires) {
1458
- checkDynamicRequire();
1459
1486
  replacedDynamicRequires.push(node.callee);
1460
1487
  }
1461
1488
  return;
@@ -1513,7 +1540,6 @@ async function transformCommonjs(
1513
1540
  return;
1514
1541
  }
1515
1542
  if (!ignoreDynamicRequires) {
1516
- checkDynamicRequire();
1517
1543
  if (isShorthandProperty(parent)) {
1518
1544
  magicString.prependRight(node.start, 'require: ');
1519
1545
  }
@@ -1597,9 +1623,10 @@ async function transformCommonjs(
1597
1623
  if (scope.contains(flattened.name)) return;
1598
1624
 
1599
1625
  if (
1600
- flattened.keypath === 'module.exports' ||
1601
- flattened.keypath === 'module' ||
1602
- flattened.keypath === 'exports'
1626
+ !isEsModule &&
1627
+ (flattened.keypath === 'module.exports' ||
1628
+ flattened.keypath === 'module' ||
1629
+ flattened.keypath === 'exports')
1603
1630
  ) {
1604
1631
  magicString.overwrite(node.start, node.end, `'object'`, {
1605
1632
  storeName: false
@@ -1627,7 +1654,13 @@ async function transformCommonjs(
1627
1654
  const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
1628
1655
  const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
1629
1656
  const helpersName = deconflict([scope], globals, 'commonjsHelpers');
1630
- const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
1657
+ const dynamicRequireName =
1658
+ replacedDynamicRequires.length > 0 &&
1659
+ deconflict(
1660
+ [scope],
1661
+ globals,
1662
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1663
+ );
1631
1664
  const deconflictedExportNames = Object.create(null);
1632
1665
  for (const [exportName, { scopes }] of exportsAssignmentsByName) {
1633
1666
  deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
@@ -1639,10 +1672,17 @@ async function transformCommonjs(
1639
1672
  });
1640
1673
  }
1641
1674
  for (const node of replacedDynamicRequires) {
1642
- magicString.overwrite(node.start, node.end, dynamicRequireName, {
1643
- contentOnly: true,
1644
- storeName: true
1645
- });
1675
+ magicString.overwrite(
1676
+ node.start,
1677
+ node.end,
1678
+ isDynamicRequireModulesEnabled
1679
+ ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
1680
+ : dynamicRequireName,
1681
+ {
1682
+ contentOnly: true,
1683
+ storeName: true
1684
+ }
1685
+ );
1646
1686
  }
1647
1687
 
1648
1688
  // We cannot wrap ES/mixed modules
@@ -1662,7 +1702,7 @@ async function transformCommonjs(
1662
1702
  ) &&
1663
1703
  (ignoreGlobal || !uses.global)
1664
1704
  ) {
1665
- return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
1705
+ return { meta: { commonjs: { isCommonJS: false } } };
1666
1706
  }
1667
1707
 
1668
1708
  let leadingComment = '';
@@ -1684,7 +1724,7 @@ async function transformCommonjs(
1684
1724
  ? 'exports'
1685
1725
  : 'module';
1686
1726
 
1687
- const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
1727
+ const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
1688
1728
  magicString,
1689
1729
  topLevelDeclarations,
1690
1730
  reassignedNames,
@@ -1694,12 +1734,14 @@ async function transformCommonjs(
1694
1734
  exportsName,
1695
1735
  id,
1696
1736
  exportMode,
1697
- resolveRequireSourcesAndGetMeta,
1737
+ resolveRequireSourcesAndUpdateMeta,
1698
1738
  needsRequireWrapper,
1699
1739
  isEsModule,
1700
- uses.require,
1701
- getIgnoreTryCatchRequireStatementMode
1740
+ isDynamicRequireModulesEnabled,
1741
+ getIgnoreTryCatchRequireStatementMode,
1742
+ commonjsMeta
1702
1743
  );
1744
+ const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
1703
1745
  const exportBlock = isEsModule
1704
1746
  ? ''
1705
1747
  : rewriteExportsAndGetExportsBlock(
@@ -1752,12 +1794,7 @@ function ${requireName} () {
1752
1794
  code: magicString.toString(),
1753
1795
  map: sourceMap ? magicString.generateMap() : null,
1754
1796
  syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
1755
- meta: {
1756
- commonjs: {
1757
- isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
1758
- isMixedModule: isEsModule
1759
- }
1760
- }
1797
+ meta: { commonjs: commonjsMeta }
1761
1798
  };
1762
1799
  }
1763
1800
 
@@ -1788,11 +1825,6 @@ function commonjs(options = {}) {
1788
1825
  const defaultIsModuleExports =
1789
1826
  typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1790
1827
 
1791
- const {
1792
- resolveRequireSourcesAndGetMeta,
1793
- getWrappedIds,
1794
- isRequiredId
1795
- } = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
1796
1828
  const dynamicRequireRoot =
1797
1829
  typeof options.dynamicRequireRoot === 'string'
1798
1830
  ? resolve(options.dynamicRequireRoot)
@@ -1803,9 +1835,6 @@ function commonjs(options = {}) {
1803
1835
  );
1804
1836
  const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
1805
1837
 
1806
- const esModulesWithDefaultExport = new Set();
1807
- const esModulesWithNamedExports = new Set();
1808
-
1809
1838
  const ignoreRequire =
1810
1839
  typeof options.ignore === 'function'
1811
1840
  ? options.ignore
@@ -1833,41 +1862,50 @@ function commonjs(options = {}) {
1833
1862
 
1834
1863
  const sourceMap = options.sourceMap !== false;
1835
1864
 
1865
+ // Initialized in buildStart
1866
+ let requireResolver;
1867
+
1836
1868
  function transformAndCheckExports(code, id) {
1837
1869
  const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
1838
1870
  this.parse,
1839
1871
  code,
1840
1872
  id
1841
1873
  );
1874
+
1875
+ const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
1842
1876
  if (hasDefaultExport) {
1843
- esModulesWithDefaultExport.add(id);
1877
+ commonjsMeta.hasDefaultExport = true;
1844
1878
  }
1845
1879
  if (hasNamedExports) {
1846
- esModulesWithNamedExports.add(id);
1880
+ commonjsMeta.hasNamedExports = true;
1847
1881
  }
1848
1882
 
1849
1883
  if (
1850
1884
  !dynamicRequireModules.has(normalizePathSlashes(id)) &&
1851
- (!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
1885
+ (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
1852
1886
  (isEsModule && !options.transformMixedEsModules))
1853
1887
  ) {
1854
- return { meta: { commonjs: { isCommonJS: false } } };
1888
+ commonjsMeta.isCommonJS = false;
1889
+ return { meta: { commonjs: commonjsMeta } };
1855
1890
  }
1856
1891
 
1857
1892
  const needsRequireWrapper =
1858
1893
  !isEsModule &&
1859
1894
  (dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
1860
1895
 
1861
- const checkDynamicRequire = () => {
1896
+ const checkDynamicRequire = (position) => {
1862
1897
  if (id.indexOf(dynamicRequireRoot) !== 0) {
1863
- this.error({
1864
- code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
1865
- id,
1866
- dynamicRequireRoot,
1867
- message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname(
1868
- id
1869
- )}" or one of its parent directories.`
1870
- });
1898
+ this.error(
1899
+ {
1900
+ code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
1901
+ id,
1902
+ dynamicRequireRoot,
1903
+ message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname(
1904
+ id
1905
+ )}" or one of its parent directories.`
1906
+ },
1907
+ position
1908
+ );
1871
1909
  }
1872
1910
  };
1873
1911
 
@@ -1887,9 +1925,10 @@ function commonjs(options = {}) {
1887
1925
  ast,
1888
1926
  defaultIsModuleExports,
1889
1927
  needsRequireWrapper,
1890
- resolveRequireSourcesAndGetMeta(this),
1891
- isRequiredId(id),
1892
- checkDynamicRequire
1928
+ requireResolver.resolveRequireSourcesAndUpdateMeta(this),
1929
+ requireResolver.isRequiredId(id),
1930
+ checkDynamicRequire,
1931
+ commonjsMeta
1893
1932
  );
1894
1933
  }
1895
1934
 
@@ -1913,18 +1952,23 @@ function commonjs(options = {}) {
1913
1952
  return { ...rawOptions, plugins };
1914
1953
  },
1915
1954
 
1916
- buildStart() {
1917
- validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
1955
+ buildStart({ plugins }) {
1956
+ validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
1957
+ const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
1958
+ if (nodeResolve) {
1959
+ validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
1960
+ }
1918
1961
  if (options.namedExports != null) {
1919
1962
  this.warn(
1920
1963
  'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
1921
1964
  );
1922
1965
  }
1966
+ requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
1923
1967
  },
1924
1968
 
1925
1969
  buildEnd() {
1926
1970
  if (options.strictRequires === 'debug') {
1927
- const wrappedIds = getWrappedIds();
1971
+ const wrappedIds = requireResolver.getWrappedIds();
1928
1972
  if (wrappedIds.length) {
1929
1973
  this.warn({
1930
1974
  code: 'WRAPPED_IDS',
@@ -1988,18 +2032,16 @@ function commonjs(options = {}) {
1988
2032
 
1989
2033
  if (isWrappedId(id, PROXY_SUFFIX)) {
1990
2034
  const actualId = unwrapId(id, PROXY_SUFFIX);
1991
- return getStaticRequireProxy(
1992
- actualId,
1993
- getRequireReturnsDefault(actualId),
1994
- esModulesWithDefaultExport,
1995
- esModulesWithNamedExports,
1996
- this.load
1997
- );
2035
+ return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
1998
2036
  }
1999
2037
 
2000
2038
  return null;
2001
2039
  },
2002
2040
 
2041
+ shouldTransformCachedModule(...args) {
2042
+ return requireResolver.shouldTransformCachedModule.call(this, ...args);
2043
+ },
2044
+
2003
2045
  transform(code, id) {
2004
2046
  const extName = extname(id);
2005
2047
  if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
@@ -2016,4 +2058,4 @@ function commonjs(options = {}) {
2016
2058
  }
2017
2059
 
2018
2060
  export { commonjs as default };
2019
- //# sourceMappingURL=index.es.js.map
2061
+ //# sourceMappingURL=index.js.map