@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.es.js CHANGED
@@ -7,7 +7,7 @@ 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-2";
11
11
  var peerDependencies = {
12
12
  rollup: "^2.60.0"
13
13
  };
@@ -478,10 +478,11 @@ function resolveExtensions(importee, importer, extensions) {
478
478
 
479
479
  function getResolveId(extensions) {
480
480
  return async function resolveId(importee, importer, resolveOptions) {
481
+ // We assume that all requires are pre-resolved
481
482
  if (
482
483
  resolveOptions.custom &&
483
- resolveOptions.custom.commonjs &&
484
- resolveOptions.custom.commonjs.skipResolver
484
+ resolveOptions.custom['node-resolve'] &&
485
+ resolveOptions.custom['node-resolve'].isRequire
485
486
  ) {
486
487
  return null;
487
488
  }
@@ -553,10 +554,39 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
553
554
  const knownCjsModuleTypes = Object.create(null);
554
555
  const requiredIds = Object.create(null);
555
556
  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));
557
+ const dependencies = Object.create(null);
558
+ const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
559
+
560
+ const isCyclic = (id) => {
561
+ const dependenciesToCheck = new Set(getDependencies(id));
562
+ for (const dependency of dependenciesToCheck) {
563
+ if (dependency === id) {
564
+ return true;
565
+ }
566
+ for (const childDependency of getDependencies(dependency)) {
567
+ dependenciesToCheck.add(childDependency);
568
+ }
569
+ }
570
+ return false;
571
+ };
572
+
573
+ const fullyAnalyzedModules = Object.create(null);
574
+
575
+ const getTypeForFullyAnalyzedModule = (id) => {
576
+ const knownType = knownCjsModuleTypes[id];
577
+ if (
578
+ knownType === IS_WRAPPED_COMMONJS ||
579
+ !detectCyclesAndConditional ||
580
+ fullyAnalyzedModules[id]
581
+ ) {
582
+ return knownType;
583
+ }
584
+ fullyAnalyzedModules[id] = true;
585
+ if (isCyclic(id)) {
586
+ return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
587
+ }
588
+ return knownType;
589
+ };
560
590
 
561
591
  return {
562
592
  getWrappedIds: () =>
@@ -569,8 +599,9 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
569
599
  isParentCommonJS,
570
600
  sources
571
601
  ) => {
572
- knownCjsModuleTypes[parentId] = knownCjsModuleTypes[parentId] || isParentCommonJS;
602
+ knownCjsModuleTypes[parentId] = isParentCommonJS;
573
603
  if (
604
+ detectCyclesAndConditional &&
574
605
  knownCjsModuleTypes[parentId] &&
575
606
  requiredIds[parentId] &&
576
607
  !unconditionallyRequiredIds[parentId]
@@ -585,10 +616,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
585
616
  }
586
617
  const resolved =
587
618
  (await rollupContext.resolve(source, parentId, {
588
- custom: {
589
- 'node-resolve': { isRequire: true },
590
- commonjs: { skipResolver: true }
591
- }
619
+ custom: { 'node-resolve': { isRequire: true } }
592
620
  })) || resolveExtensions(source, parentId, extensions);
593
621
  if (!resolved) {
594
622
  return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
@@ -598,62 +626,25 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
598
626
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
599
627
  }
600
628
  requiredIds[childId] = true;
601
- if (
602
- !(
603
- detectCyclesAndConditional &&
604
- (isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)
605
- )
606
- ) {
629
+ if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
607
630
  unconditionallyRequiredIds[childId] = true;
608
631
  }
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
632
 
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 {
633
+ getDependencies(parentId).add(childId);
634
+ if (!isCyclic(childId)) {
646
635
  // This makes sure the current transform handler waits for all direct dependencies to be
647
636
  // loaded and transformed and therefore for all transitive CommonJS dependencies to be
648
637
  // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
649
638
  await rollupContext.load(resolved);
639
+ } else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
640
+ knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
650
641
  }
651
642
  return { id: childId, allowProxy: true };
652
643
  })
653
644
  );
654
645
  return {
655
646
  requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
656
- const isCommonJS = knownCjsModuleTypes[dependencyId];
647
+ const isCommonJS = getTypeForFullyAnalyzedModule(dependencyId);
657
648
  return {
658
649
  source: sources[index].source,
659
650
  id: allowProxy
@@ -664,7 +655,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
664
655
  isCommonJS
665
656
  };
666
657
  }),
667
- usesRequireWrapper: knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS
658
+ usesRequireWrapper: getTypeForFullyAnalyzedModule(parentId) === IS_WRAPPED_COMMONJS
668
659
  };
669
660
  }
670
661
  };
@@ -1425,7 +1416,7 @@ async function transformCommonjs(
1425
1416
  isRequire(node.callee.object, scope) &&
1426
1417
  node.callee.property.name === 'resolve'
1427
1418
  ) {
1428
- checkDynamicRequire();
1419
+ checkDynamicRequire(node.start);
1429
1420
  uses.require = true;
1430
1421
  const requireNode = node.callee.object;
1431
1422
  magicString.appendLeft(
@@ -1447,6 +1438,7 @@ async function transformCommonjs(
1447
1438
 
1448
1439
  if (hasDynamicArguments(node)) {
1449
1440
  if (isDynamicRequireModulesEnabled) {
1441
+ checkDynamicRequire(node.start);
1450
1442
  magicString.appendLeft(
1451
1443
  node.end - 1,
1452
1444
  `, ${JSON.stringify(
@@ -1455,7 +1447,6 @@ async function transformCommonjs(
1455
1447
  );
1456
1448
  }
1457
1449
  if (!ignoreDynamicRequires) {
1458
- checkDynamicRequire();
1459
1450
  replacedDynamicRequires.push(node.callee);
1460
1451
  }
1461
1452
  return;
@@ -1513,7 +1504,6 @@ async function transformCommonjs(
1513
1504
  return;
1514
1505
  }
1515
1506
  if (!ignoreDynamicRequires) {
1516
- checkDynamicRequire();
1517
1507
  if (isShorthandProperty(parent)) {
1518
1508
  magicString.prependRight(node.start, 'require: ');
1519
1509
  }
@@ -1858,16 +1848,19 @@ function commonjs(options = {}) {
1858
1848
  !isEsModule &&
1859
1849
  (dynamicRequireModules.has(normalizePathSlashes(id)) || strictRequiresFilter(id));
1860
1850
 
1861
- const checkDynamicRequire = () => {
1851
+ const checkDynamicRequire = (position) => {
1862
1852
  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
- });
1853
+ this.error(
1854
+ {
1855
+ code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
1856
+ id,
1857
+ dynamicRequireRoot,
1858
+ message: `"${id}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${dynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname(
1859
+ id
1860
+ )}" or one of its parent directories.`
1861
+ },
1862
+ position
1863
+ );
1871
1864
  }
1872
1865
  };
1873
1866