@rollup/plugin-commonjs 22.0.0-3 → 22.0.0-7

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.
@@ -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 = "21.0.1";
19
+ var version = "22.0.0-7";
20
20
  var peerDependencies = {
21
- rollup: "^2.60.0"
21
+ rollup: "^2.66.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;
@@ -334,7 +337,8 @@ const WRAPPED_SUFFIX = '?commonjs-wrapped';
334
337
  const EXTERNAL_SUFFIX = '?commonjs-external';
335
338
  const EXPORTS_SUFFIX = '?commonjs-exports';
336
339
  const MODULE_SUFFIX = '?commonjs-module';
337
- const ES_IMPORT_SUFFIX = '?es-import';
340
+ const ENTRY_SUFFIX = '?commonjs-entry';
341
+ const ES_IMPORT_SUFFIX = '?commonjs-es-import';
338
342
 
339
343
  const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
340
344
  const HELPERS_ID = '\0commonjsHelpers.js';
@@ -402,21 +406,15 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
402
406
  return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
403
407
  }
404
408
 
405
- async function getStaticRequireProxy(
406
- id,
407
- requireReturnsDefault,
408
- esModulesWithDefaultExport,
409
- esModulesWithNamedExports,
410
- loadModule
411
- ) {
409
+ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
412
410
  const name = getName(id);
413
411
  const {
414
412
  meta: { commonjs: commonjsMeta }
415
413
  } = await loadModule({ id });
416
- if (commonjsMeta && commonjsMeta.isCommonJS) {
417
- return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
418
- } else if (!commonjsMeta) {
414
+ if (!commonjsMeta) {
419
415
  return getUnknownRequireProxy(id, requireReturnsDefault);
416
+ } else if (commonjsMeta.isCommonJS) {
417
+ return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
420
418
  } else if (!requireReturnsDefault) {
421
419
  return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
422
420
  id
@@ -424,14 +422,30 @@ async function getStaticRequireProxy(
424
422
  } else if (
425
423
  requireReturnsDefault !== true &&
426
424
  (requireReturnsDefault === 'namespace' ||
427
- !esModulesWithDefaultExport.has(id) ||
428
- (requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id)))
425
+ !commonjsMeta.hasDefaultExport ||
426
+ (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
429
427
  ) {
430
428
  return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
431
429
  }
432
430
  return `export { default } from ${JSON.stringify(id)};`;
433
431
  }
434
432
 
433
+ async function getEntryProxy(id, defaultIsModuleExports, loadModule) {
434
+ const {
435
+ meta: { commonjs: commonjsMeta },
436
+ hasDefaultExport
437
+ } = await loadModule({ id, moduleSideEffects: true });
438
+ if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
439
+ const stringifiedId = JSON.stringify(id);
440
+ let code = `export * from ${stringifiedId};`;
441
+ if (hasDefaultExport) {
442
+ code += `export { default } from ${stringifiedId};`;
443
+ }
444
+ return code;
445
+ }
446
+ return getEsImportProxy(id, defaultIsModuleExports);
447
+ }
448
+
435
449
  function getEsImportProxy(id, defaultIsModuleExports) {
436
450
  const name = getName(id);
437
451
  const exportsName = `${name}Exports`;
@@ -500,6 +514,7 @@ function getResolveId(extensions) {
500
514
  }
501
515
 
502
516
  if (
517
+ importee.endsWith(ENTRY_SUFFIX) ||
503
518
  isWrappedId(importee, MODULE_SUFFIX) ||
504
519
  isWrappedId(importee, EXPORTS_SUFFIX) ||
505
520
  isWrappedId(importee, PROXY_SUFFIX) ||
@@ -516,7 +531,8 @@ function getResolveId(extensions) {
516
531
  importer === DYNAMIC_MODULES_ID ||
517
532
  // Proxies are only importing resolved ids, no need to resolve again
518
533
  isWrappedId(importer, PROXY_SUFFIX) ||
519
- isWrappedId(importer, ES_IMPORT_SUFFIX)
534
+ isWrappedId(importer, ES_IMPORT_SUFFIX) ||
535
+ importer.endsWith(ENTRY_SUFFIX)
520
536
  ) {
521
537
  return importee;
522
538
  }
@@ -549,6 +565,10 @@ function getResolveId(extensions) {
549
565
  if (!resolved || resolved.external) {
550
566
  return resolved;
551
567
  }
568
+ if (resolveOptions.isEntry) {
569
+ // We must not precede entry proxies with a `\0` as that will mess up relative external resolution
570
+ return resolved.id + ENTRY_SUFFIX;
571
+ }
552
572
  const {
553
573
  meta: { commonjs: commonjsMeta }
554
574
  } = await this.load(resolved);
@@ -559,7 +579,7 @@ function getResolveId(extensions) {
559
579
  };
560
580
  }
561
581
 
562
- function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional) {
582
+ function getRequireResolver(extensions, detectCyclesAndConditional) {
563
583
  const knownCjsModuleTypes = Object.create(null);
564
584
  const requiredIds = Object.create(null);
565
585
  const unconditionallyRequiredIds = Object.create(null);
@@ -583,11 +603,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
583
603
 
584
604
  const getTypeForFullyAnalyzedModule = (id) => {
585
605
  const knownType = knownCjsModuleTypes[id];
586
- if (
587
- knownType === IS_WRAPPED_COMMONJS ||
588
- !detectCyclesAndConditional ||
589
- fullyAnalyzedModules[id]
590
- ) {
606
+ if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
591
607
  return knownType;
592
608
  }
593
609
  fullyAnalyzedModules[id] = true;
@@ -597,26 +613,80 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
597
613
  return knownType;
598
614
  };
599
615
 
616
+ const setInitialParentType = (id, initialCommonJSType) => {
617
+ // It is possible a transformed module is already fully analyzed when using
618
+ // the cache and one dependency introduces a new cycle. Then transform is
619
+ // run for a fully analzyed module again. Fully analyzed modules may never
620
+ // change their type as importers already trust their type.
621
+ knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
622
+ ? knownCjsModuleTypes[id]
623
+ : initialCommonJSType;
624
+ if (
625
+ detectCyclesAndConditional &&
626
+ knownCjsModuleTypes[id] === true &&
627
+ requiredIds[id] &&
628
+ !unconditionallyRequiredIds[id]
629
+ ) {
630
+ knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
631
+ }
632
+ };
633
+
634
+ const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
635
+ const childId = resolved.id;
636
+ requiredIds[childId] = true;
637
+ if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
638
+ unconditionallyRequiredIds[childId] = true;
639
+ }
640
+
641
+ getDependencies(parentId).add(childId);
642
+ if (!isCyclic(childId)) {
643
+ // This makes sure the current transform handler waits for all direct dependencies to be
644
+ // loaded and transformed and therefore for all transitive CommonJS dependencies to be
645
+ // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
646
+ await loadModule(resolved);
647
+ }
648
+ };
649
+
600
650
  return {
601
651
  getWrappedIds: () =>
602
652
  Object.keys(knownCjsModuleTypes).filter(
603
653
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
604
654
  ),
605
655
  isRequiredId: (id) => requiredIds[id],
606
- resolveRequireSourcesAndGetMeta: (rollupContext) => async (
656
+ async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
657
+ // Ignore modules that did not pass through the original transformer in a previous build
658
+ if (!(parentMeta && parentMeta.requires)) {
659
+ return false;
660
+ }
661
+ setInitialParentType(parentId, parentMeta.initialCommonJSType);
662
+ await Promise.all(
663
+ parentMeta.requires.map(({ resolved, isConditional }) =>
664
+ setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
665
+ )
666
+ );
667
+ if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
668
+ return true;
669
+ }
670
+ for (const {
671
+ resolved: { id }
672
+ } of parentMeta.requires) {
673
+ if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
674
+ return true;
675
+ }
676
+ }
677
+ return false;
678
+ },
679
+ /* eslint-disable no-param-reassign */
680
+ resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
607
681
  parentId,
608
682
  isParentCommonJS,
683
+ parentMeta,
609
684
  sources
610
685
  ) => {
611
- knownCjsModuleTypes[parentId] = isParentCommonJS;
612
- if (
613
- detectCyclesAndConditional &&
614
- knownCjsModuleTypes[parentId] &&
615
- requiredIds[parentId] &&
616
- !unconditionallyRequiredIds[parentId]
617
- ) {
618
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
619
- }
686
+ parentMeta.initialCommonJSType = isParentCommonJS;
687
+ parentMeta.requires = [];
688
+ parentMeta.isRequiredCommonJS = Object.create(null);
689
+ setInitialParentType(parentId, isParentCommonJS);
620
690
  const requireTargets = await Promise.all(
621
691
  sources.map(async ({ source, isConditional }) => {
622
692
  // Never analyze or proxy internal modules
@@ -634,59 +704,58 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
634
704
  if (resolved.external) {
635
705
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
636
706
  }
637
- requiredIds[childId] = true;
638
- if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
639
- unconditionallyRequiredIds[childId] = true;
640
- }
641
-
642
- getDependencies(parentId).add(childId);
643
- if (!isCyclic(childId)) {
644
- // This makes sure the current transform handler waits for all direct dependencies to be
645
- // loaded and transformed and therefore for all transitive CommonJS dependencies to be
646
- // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
647
- await rollupContext.load(resolved);
648
- } else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
649
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
650
- }
707
+ parentMeta.requires.push({ resolved, isConditional });
708
+ await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
651
709
  return { id: childId, allowProxy: true };
652
710
  })
653
711
  );
654
- return {
655
- requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
656
- const isCommonJS = getTypeForFullyAnalyzedModule(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: getTypeForFullyAnalyzedModule(parentId) === IS_WRAPPED_COMMONJS
668
- };
712
+ parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
713
+ return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
714
+ // eslint-disable-next-line no-multi-assign
715
+ const isCommonJS = (parentMeta.isRequiredCommonJS[
716
+ dependencyId
717
+ ] = getTypeForFullyAnalyzedModule(dependencyId));
718
+ return {
719
+ source: sources[index].source,
720
+ id: allowProxy
721
+ ? isCommonJS === IS_WRAPPED_COMMONJS
722
+ ? wrapId(dependencyId, WRAPPED_SUFFIX)
723
+ : wrapId(dependencyId, PROXY_SUFFIX)
724
+ : dependencyId,
725
+ isCommonJS
726
+ };
727
+ });
669
728
  }
670
729
  };
671
730
  }
672
731
 
673
- function validateRollupVersion(rollupVersion, peerDependencyVersion) {
674
- const [major, minor] = rollupVersion.split('.').map(Number);
675
- const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
732
+ function validateVersion(actualVersion, peerDependencyVersion, name) {
733
+ const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
676
734
  let minMajor = Infinity;
677
735
  let minMinor = Infinity;
736
+ let minPatch = Infinity;
678
737
  let foundVersion;
679
738
  // eslint-disable-next-line no-cond-assign
680
739
  while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
681
- const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
740
+ const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
682
741
  if (foundMajor < minMajor) {
683
742
  minMajor = foundMajor;
684
743
  minMinor = foundMinor;
744
+ minPatch = foundPatch;
685
745
  }
686
746
  }
687
- if (major < minMajor || (major === minMajor && minor < minMinor)) {
747
+ if (!actualVersion) {
688
748
  throw new Error(
689
- `Insufficient Rollup version: "@rollup/plugin-commonjs" requires at least rollup@${minMajor}.${minMinor} but found rollup@${rollupVersion}.`
749
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
750
+ );
751
+ }
752
+ const [major, minor, patch] = actualVersion.split('.').map(Number);
753
+ if (
754
+ major < minMajor ||
755
+ (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
756
+ ) {
757
+ throw new Error(
758
+ `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
690
759
  );
691
760
  }
692
761
  }
@@ -1129,17 +1198,20 @@ function getRequireHandlers() {
1129
1198
  exportsName,
1130
1199
  id,
1131
1200
  exportMode,
1132
- resolveRequireSourcesAndGetMeta,
1201
+ resolveRequireSourcesAndUpdateMeta,
1133
1202
  needsRequireWrapper,
1134
1203
  isEsModule,
1135
- usesRequire,
1136
- getIgnoreTryCatchRequireStatementMode
1204
+ isDynamicRequireModulesEnabled,
1205
+ getIgnoreTryCatchRequireStatementMode,
1206
+ commonjsMeta
1137
1207
  ) {
1138
1208
  const imports = [];
1139
1209
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
1140
- if (usesRequire) {
1210
+ if (dynamicRequireName) {
1141
1211
  imports.push(
1142
- `import { commonjsRequire as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1212
+ `import { ${
1213
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1214
+ } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
1143
1215
  );
1144
1216
  }
1145
1217
  if (exportMode === 'module') {
@@ -1154,9 +1226,10 @@ function getRequireHandlers() {
1154
1226
  );
1155
1227
  }
1156
1228
  const requiresBySource = collectSources(requireExpressions);
1157
- const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
1229
+ const requireTargets = await resolveRequireSourcesAndUpdateMeta(
1158
1230
  id,
1159
1231
  needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
1232
+ commonjsMeta,
1160
1233
  Object.keys(requiresBySource).map((source) => {
1161
1234
  return {
1162
1235
  source,
@@ -1171,10 +1244,7 @@ function getRequireHandlers() {
1171
1244
  getIgnoreTryCatchRequireStatementMode,
1172
1245
  magicString
1173
1246
  );
1174
- return {
1175
- importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
1176
- usesRequireWrapper
1177
- };
1247
+ return imports.length ? `${imports.join('\n')}\n\n` : '';
1178
1248
  }
1179
1249
 
1180
1250
  return {
@@ -1277,9 +1347,10 @@ async function transformCommonjs(
1277
1347
  astCache,
1278
1348
  defaultIsModuleExports,
1279
1349
  needsRequireWrapper,
1280
- resolveRequireSourcesAndGetMeta,
1350
+ resolveRequireSourcesAndUpdateMeta,
1281
1351
  isRequired,
1282
- checkDynamicRequire
1352
+ checkDynamicRequire,
1353
+ commonjsMeta
1283
1354
  ) {
1284
1355
  const ast = astCache || tryParse(parse, code, id);
1285
1356
  const magicString = new MagicString__default["default"](code);
@@ -1428,12 +1499,6 @@ async function transformCommonjs(
1428
1499
  checkDynamicRequire(node.start);
1429
1500
  uses.require = true;
1430
1501
  const requireNode = node.callee.object;
1431
- magicString.appendLeft(
1432
- node.end - 1,
1433
- `,${JSON.stringify(
1434
- path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1435
- )}`
1436
- );
1437
1502
  replacedDynamicRequires.push(requireNode);
1438
1503
  return;
1439
1504
  }
@@ -1448,12 +1513,6 @@ async function transformCommonjs(
1448
1513
  if (hasDynamicArguments(node)) {
1449
1514
  if (isDynamicRequireModulesEnabled) {
1450
1515
  checkDynamicRequire(node.start);
1451
- magicString.appendLeft(
1452
- node.end - 1,
1453
- `, ${JSON.stringify(
1454
- path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
1455
- )}`
1456
- );
1457
1516
  }
1458
1517
  if (!ignoreDynamicRequires) {
1459
1518
  replacedDynamicRequires.push(node.callee);
@@ -1627,7 +1686,13 @@ async function transformCommonjs(
1627
1686
  const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
1628
1687
  const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
1629
1688
  const helpersName = deconflict([scope], globals, 'commonjsHelpers');
1630
- const dynamicRequireName = deconflict([scope], globals, 'commonjsRequire');
1689
+ const dynamicRequireName =
1690
+ replacedDynamicRequires.length > 0 &&
1691
+ deconflict(
1692
+ [scope],
1693
+ globals,
1694
+ isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
1695
+ );
1631
1696
  const deconflictedExportNames = Object.create(null);
1632
1697
  for (const [exportName, { scopes }] of exportsAssignmentsByName) {
1633
1698
  deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
@@ -1639,10 +1704,17 @@ async function transformCommonjs(
1639
1704
  });
1640
1705
  }
1641
1706
  for (const node of replacedDynamicRequires) {
1642
- magicString.overwrite(node.start, node.end, dynamicRequireName, {
1643
- contentOnly: true,
1644
- storeName: true
1645
- });
1707
+ magicString.overwrite(
1708
+ node.start,
1709
+ node.end,
1710
+ isDynamicRequireModulesEnabled
1711
+ ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
1712
+ : dynamicRequireName,
1713
+ {
1714
+ contentOnly: true,
1715
+ storeName: true
1716
+ }
1717
+ );
1646
1718
  }
1647
1719
 
1648
1720
  // We cannot wrap ES/mixed modules
@@ -1662,7 +1734,7 @@ async function transformCommonjs(
1662
1734
  ) &&
1663
1735
  (ignoreGlobal || !uses.global)
1664
1736
  ) {
1665
- return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
1737
+ return { meta: { commonjs: { isCommonJS: false } } };
1666
1738
  }
1667
1739
 
1668
1740
  let leadingComment = '';
@@ -1684,7 +1756,7 @@ async function transformCommonjs(
1684
1756
  ? 'exports'
1685
1757
  : 'module';
1686
1758
 
1687
- const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
1759
+ const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
1688
1760
  magicString,
1689
1761
  topLevelDeclarations,
1690
1762
  reassignedNames,
@@ -1694,12 +1766,14 @@ async function transformCommonjs(
1694
1766
  exportsName,
1695
1767
  id,
1696
1768
  exportMode,
1697
- resolveRequireSourcesAndGetMeta,
1769
+ resolveRequireSourcesAndUpdateMeta,
1698
1770
  needsRequireWrapper,
1699
1771
  isEsModule,
1700
- uses.require,
1701
- getIgnoreTryCatchRequireStatementMode
1772
+ isDynamicRequireModulesEnabled,
1773
+ getIgnoreTryCatchRequireStatementMode,
1774
+ commonjsMeta
1702
1775
  );
1776
+ const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
1703
1777
  const exportBlock = isEsModule
1704
1778
  ? ''
1705
1779
  : rewriteExportsAndGetExportsBlock(
@@ -1752,12 +1826,7 @@ function ${requireName} () {
1752
1826
  code: magicString.toString(),
1753
1827
  map: sourceMap ? magicString.generateMap() : null,
1754
1828
  syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
1755
- meta: {
1756
- commonjs: {
1757
- isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
1758
- isMixedModule: isEsModule
1759
- }
1760
- }
1829
+ meta: { commonjs: commonjsMeta }
1761
1830
  };
1762
1831
  }
1763
1832
 
@@ -1788,11 +1857,6 @@ function commonjs(options = {}) {
1788
1857
  const defaultIsModuleExports =
1789
1858
  typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1790
1859
 
1791
- const {
1792
- resolveRequireSourcesAndGetMeta,
1793
- getWrappedIds,
1794
- isRequiredId
1795
- } = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
1796
1860
  const dynamicRequireRoot =
1797
1861
  typeof options.dynamicRequireRoot === 'string'
1798
1862
  ? path.resolve(options.dynamicRequireRoot)
@@ -1803,9 +1867,6 @@ function commonjs(options = {}) {
1803
1867
  );
1804
1868
  const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
1805
1869
 
1806
- const esModulesWithDefaultExport = new Set();
1807
- const esModulesWithNamedExports = new Set();
1808
-
1809
1870
  const ignoreRequire =
1810
1871
  typeof options.ignore === 'function'
1811
1872
  ? options.ignore
@@ -1833,25 +1894,31 @@ function commonjs(options = {}) {
1833
1894
 
1834
1895
  const sourceMap = options.sourceMap !== false;
1835
1896
 
1897
+ // Initialized in buildStart
1898
+ let requireResolver;
1899
+
1836
1900
  function transformAndCheckExports(code, id) {
1837
1901
  const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
1838
1902
  this.parse,
1839
1903
  code,
1840
1904
  id
1841
1905
  );
1906
+
1907
+ const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
1842
1908
  if (hasDefaultExport) {
1843
- esModulesWithDefaultExport.add(id);
1909
+ commonjsMeta.hasDefaultExport = true;
1844
1910
  }
1845
1911
  if (hasNamedExports) {
1846
- esModulesWithNamedExports.add(id);
1912
+ commonjsMeta.hasNamedExports = true;
1847
1913
  }
1848
1914
 
1849
1915
  if (
1850
1916
  !dynamicRequireModules.has(normalizePathSlashes(id)) &&
1851
- (!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
1917
+ (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
1852
1918
  (isEsModule && !options.transformMixedEsModules))
1853
1919
  ) {
1854
- return { meta: { commonjs: { isCommonJS: false } } };
1920
+ commonjsMeta.isCommonJS = false;
1921
+ return { meta: { commonjs: commonjsMeta } };
1855
1922
  }
1856
1923
 
1857
1924
  const needsRequireWrapper =
@@ -1890,9 +1957,10 @@ function commonjs(options = {}) {
1890
1957
  ast,
1891
1958
  defaultIsModuleExports,
1892
1959
  needsRequireWrapper,
1893
- resolveRequireSourcesAndGetMeta(this),
1894
- isRequiredId(id),
1895
- checkDynamicRequire
1960
+ requireResolver.resolveRequireSourcesAndUpdateMeta(this),
1961
+ requireResolver.isRequiredId(id),
1962
+ checkDynamicRequire,
1963
+ commonjsMeta
1896
1964
  );
1897
1965
  }
1898
1966
 
@@ -1916,18 +1984,23 @@ function commonjs(options = {}) {
1916
1984
  return { ...rawOptions, plugins };
1917
1985
  },
1918
1986
 
1919
- buildStart() {
1920
- validateRollupVersion(this.meta.rollupVersion, peerDependencies.rollup);
1987
+ buildStart({ plugins }) {
1988
+ validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
1989
+ const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
1990
+ if (nodeResolve) {
1991
+ validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
1992
+ }
1921
1993
  if (options.namedExports != null) {
1922
1994
  this.warn(
1923
1995
  'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
1924
1996
  );
1925
1997
  }
1998
+ requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
1926
1999
  },
1927
2000
 
1928
2001
  buildEnd() {
1929
2002
  if (options.strictRequires === 'debug') {
1930
- const wrappedIds = getWrappedIds();
2003
+ const wrappedIds = requireResolver.getWrappedIds();
1931
2004
  if (wrappedIds.length) {
1932
2005
  this.warn({
1933
2006
  code: 'WRAPPED_IDS',
@@ -1976,6 +2049,11 @@ function commonjs(options = {}) {
1976
2049
  );
1977
2050
  }
1978
2051
 
2052
+ // entry suffix is just appended to not mess up relative external resolution
2053
+ if (id.endsWith(ENTRY_SUFFIX)) {
2054
+ return getEntryProxy(id.slice(0, -ENTRY_SUFFIX.length), defaultIsModuleExports, this.load);
2055
+ }
2056
+
1979
2057
  if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
1980
2058
  return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
1981
2059
  }
@@ -1991,18 +2069,16 @@ function commonjs(options = {}) {
1991
2069
 
1992
2070
  if (isWrappedId(id, PROXY_SUFFIX)) {
1993
2071
  const actualId = unwrapId(id, PROXY_SUFFIX);
1994
- return getStaticRequireProxy(
1995
- actualId,
1996
- getRequireReturnsDefault(actualId),
1997
- esModulesWithDefaultExport,
1998
- esModulesWithNamedExports,
1999
- this.load
2000
- );
2072
+ return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
2001
2073
  }
2002
2074
 
2003
2075
  return null;
2004
2076
  },
2005
2077
 
2078
+ shouldTransformCachedModule(...args) {
2079
+ return requireResolver.shouldTransformCachedModule.call(this, ...args);
2080
+ },
2081
+
2006
2082
  transform(code, id) {
2007
2083
  const extName = path.extname(id);
2008
2084
  if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {