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

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,7 +16,7 @@ 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-1";
19
+ var version = "22.0.0-2";
20
20
  var peerDependencies = {
21
21
  rollup: "^2.60.0"
22
22
  };
@@ -487,10 +487,11 @@ function resolveExtensions(importee, importer, extensions) {
487
487
 
488
488
  function getResolveId(extensions) {
489
489
  return async function resolveId(importee, importer, resolveOptions) {
490
+ // We assume that all requires are pre-resolved
490
491
  if (
491
492
  resolveOptions.custom &&
492
- resolveOptions.custom.commonjs &&
493
- resolveOptions.custom.commonjs.skipResolver
493
+ resolveOptions.custom['node-resolve'] &&
494
+ resolveOptions.custom['node-resolve'].isRequire
494
495
  ) {
495
496
  return null;
496
497
  }
@@ -562,10 +563,39 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
562
563
  const knownCjsModuleTypes = Object.create(null);
563
564
  const requiredIds = Object.create(null);
564
565
  const unconditionallyRequiredIds = Object.create(null);
565
- const parentModules = Object.create(null);
566
- const childModules = Object.create(null);
567
- const getParentModules = (id) => parentModules[id] || (parentModules[id] = Object.create(null));
568
- const getChildModules = (id) => childModules[id] || (childModules[id] = Object.create(null));
566
+ const dependencies = Object.create(null);
567
+ const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
568
+
569
+ const isCyclic = (id) => {
570
+ const dependenciesToCheck = new Set(getDependencies(id));
571
+ for (const dependency of dependenciesToCheck) {
572
+ if (dependency === id) {
573
+ return true;
574
+ }
575
+ for (const childDependency of getDependencies(dependency)) {
576
+ dependenciesToCheck.add(childDependency);
577
+ }
578
+ }
579
+ return false;
580
+ };
581
+
582
+ const fullyAnalyzedModules = Object.create(null);
583
+
584
+ const getTypeForFullyAnalyzedModule = (id) => {
585
+ const knownType = knownCjsModuleTypes[id];
586
+ if (
587
+ knownType === IS_WRAPPED_COMMONJS ||
588
+ !detectCyclesAndConditional ||
589
+ fullyAnalyzedModules[id]
590
+ ) {
591
+ return knownType;
592
+ }
593
+ fullyAnalyzedModules[id] = true;
594
+ if (isCyclic(id)) {
595
+ return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
596
+ }
597
+ return knownType;
598
+ };
569
599
 
570
600
  return {
571
601
  getWrappedIds: () =>
@@ -578,8 +608,9 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
578
608
  isParentCommonJS,
579
609
  sources
580
610
  ) => {
581
- knownCjsModuleTypes[parentId] = knownCjsModuleTypes[parentId] || isParentCommonJS;
611
+ knownCjsModuleTypes[parentId] = isParentCommonJS;
582
612
  if (
613
+ detectCyclesAndConditional &&
583
614
  knownCjsModuleTypes[parentId] &&
584
615
  requiredIds[parentId] &&
585
616
  !unconditionallyRequiredIds[parentId]
@@ -594,10 +625,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
594
625
  }
595
626
  const resolved =
596
627
  (await rollupContext.resolve(source, parentId, {
597
- custom: {
598
- 'node-resolve': { isRequire: true },
599
- commonjs: { skipResolver: true }
600
- }
628
+ custom: { 'node-resolve': { isRequire: true } }
601
629
  })) || resolveExtensions(source, parentId, extensions);
602
630
  if (!resolved) {
603
631
  return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
@@ -607,62 +635,25 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
607
635
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
608
636
  }
609
637
  requiredIds[childId] = true;
610
- if (
611
- !(
612
- detectCyclesAndConditional &&
613
- (isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)
614
- )
615
- ) {
638
+ if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
616
639
  unconditionallyRequiredIds[childId] = true;
617
640
  }
618
- const parentDependentModules = getParentModules(parentId);
619
- const childDependentModules = getParentModules(childId);
620
-
621
- // Copy the parents of the current parent to the child
622
- for (const dependentId of Object.keys(parentDependentModules)) {
623
- childDependentModules[dependentId] = true;
624
- }
625
-
626
- // Add the current parent to the child as well. If the child module already has known
627
- // dependencies because it has already been loaded, add the current parent module to their
628
- // parent modules
629
- childDependentModules[parentId] = true;
630
- const dependenciesToUpdate = new Set(Object.keys(getChildModules(childId)));
631
- for (const dependencyId of dependenciesToUpdate) {
632
- getParentModules(dependencyId)[parentId] = true;
633
- for (const subDependencyId of Object.keys(getChildModules(dependencyId))) {
634
- dependenciesToUpdate.add(subDependencyId);
635
- }
636
- }
637
641
 
638
- // Add the child as a dependency to the parent
639
- getChildModules(parentId)[childId] = true;
640
-
641
- // If we depend on one of our dependencies, we have a cycle. Then all modules that
642
- // we depend on that also depend on the same module are part of a cycle as well. Trying
643
- // to wait for loading this module would lead to a deadlock.
644
- if (parentDependentModules[childId]) {
645
- if (detectCyclesAndConditional && isParentCommonJS) {
646
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
647
- knownCjsModuleTypes[childId] = IS_WRAPPED_COMMONJS;
648
- for (const dependentId of Object.keys(parentDependentModules)) {
649
- if (getParentModules(dependentId)[childId]) {
650
- knownCjsModuleTypes[dependentId] = IS_WRAPPED_COMMONJS;
651
- }
652
- }
653
- }
654
- } else {
642
+ getDependencies(parentId).add(childId);
643
+ if (!isCyclic(childId)) {
655
644
  // This makes sure the current transform handler waits for all direct dependencies to be
656
645
  // loaded and transformed and therefore for all transitive CommonJS dependencies to be
657
646
  // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
658
647
  await rollupContext.load(resolved);
648
+ } else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
649
+ knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
659
650
  }
660
651
  return { id: childId, allowProxy: true };
661
652
  })
662
653
  );
663
654
  return {
664
655
  requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
665
- const isCommonJS = knownCjsModuleTypes[dependencyId];
656
+ const isCommonJS = getTypeForFullyAnalyzedModule(dependencyId);
666
657
  return {
667
658
  source: sources[index].source,
668
659
  id: allowProxy
@@ -673,7 +664,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
673
664
  isCommonJS
674
665
  };
675
666
  }),
676
- usesRequireWrapper: knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS
667
+ usesRequireWrapper: getTypeForFullyAnalyzedModule(parentId) === IS_WRAPPED_COMMONJS
677
668
  };
678
669
  }
679
670
  };
@@ -1434,7 +1425,7 @@ async function transformCommonjs(
1434
1425
  isRequire(node.callee.object, scope) &&
1435
1426
  node.callee.property.name === 'resolve'
1436
1427
  ) {
1437
- checkDynamicRequire();
1428
+ checkDynamicRequire(node.start);
1438
1429
  uses.require = true;
1439
1430
  const requireNode = node.callee.object;
1440
1431
  magicString.appendLeft(
@@ -1456,6 +1447,7 @@ async function transformCommonjs(
1456
1447
 
1457
1448
  if (hasDynamicArguments(node)) {
1458
1449
  if (isDynamicRequireModulesEnabled) {
1450
+ checkDynamicRequire(node.start);
1459
1451
  magicString.appendLeft(
1460
1452
  node.end - 1,
1461
1453
  `, ${JSON.stringify(
@@ -1464,7 +1456,6 @@ async function transformCommonjs(
1464
1456
  );
1465
1457
  }
1466
1458
  if (!ignoreDynamicRequires) {
1467
- checkDynamicRequire();
1468
1459
  replacedDynamicRequires.push(node.callee);
1469
1460
  }
1470
1461
  return;
@@ -1522,7 +1513,6 @@ async function transformCommonjs(
1522
1513
  return;
1523
1514
  }
1524
1515
  if (!ignoreDynamicRequires) {
1525
- checkDynamicRequire();
1526
1516
  if (isShorthandProperty(parent)) {
1527
1517
  magicString.prependRight(node.start, 'require: ');
1528
1518
  }
@@ -1867,16 +1857,19 @@ function commonjs(options = {}) {
1867
1857
  !isEsModule &&
1868
1858
  (dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
1869
1859
 
1870
- const checkDynamicRequire = () => {
1860
+ const checkDynamicRequire = (position) => {
1871
1861
  if (id.indexOf(dynamicRequireRoot) !== 0) {
1872
- this.error({
1873
- code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
1874
- id,
1875
- dynamicRequireRoot,
1876
- message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${path.dirname(
1877
- id
1878
- )}" or one of its parent directories.`
1879
- });
1862
+ this.error(
1863
+ {
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 "${path.dirname(
1868
+ id
1869
+ )}" or one of its parent directories.`
1870
+ },
1871
+ position
1872
+ );
1880
1873
  }
1881
1874
  };
1882
1875