@rollup/plugin-commonjs 22.0.0-4 → 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-4";
10
+ var version = "22.0.0-5";
11
11
  var peerDependencies = {
12
- rollup: "^2.61.1"
12
+ rollup: "^2.64.0"
13
13
  };
14
14
 
15
15
  function tryParse(parse, code, id) {
@@ -396,21 +396,15 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
396
396
  return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
397
397
  }
398
398
 
399
- async function getStaticRequireProxy(
400
- id,
401
- requireReturnsDefault,
402
- esModulesWithDefaultExport,
403
- esModulesWithNamedExports,
404
- loadModule
405
- ) {
399
+ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
406
400
  const name = getName(id);
407
401
  const {
408
402
  meta: { commonjs: commonjsMeta }
409
403
  } = await loadModule({ id });
410
- if (commonjsMeta && commonjsMeta.isCommonJS) {
411
- return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
412
- } else if (!commonjsMeta) {
404
+ if (!commonjsMeta) {
413
405
  return getUnknownRequireProxy(id, requireReturnsDefault);
406
+ } else if (commonjsMeta.isCommonJS) {
407
+ return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
414
408
  } else if (!requireReturnsDefault) {
415
409
  return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
416
410
  id
@@ -418,8 +412,8 @@ async function getStaticRequireProxy(
418
412
  } else if (
419
413
  requireReturnsDefault !== true &&
420
414
  (requireReturnsDefault === 'namespace' ||
421
- !esModulesWithDefaultExport.has(id) ||
422
- (requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id)))
415
+ !commonjsMeta.hasDefaultExport ||
416
+ (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
423
417
  ) {
424
418
  return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
425
419
  }
@@ -553,7 +547,7 @@ function getResolveId(extensions) {
553
547
  };
554
548
  }
555
549
 
556
- function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional) {
550
+ function getRequireResolver(extensions, detectCyclesAndConditional) {
557
551
  const knownCjsModuleTypes = Object.create(null);
558
552
  const requiredIds = Object.create(null);
559
553
  const unconditionallyRequiredIds = Object.create(null);
@@ -577,11 +571,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
577
571
 
578
572
  const getTypeForFullyAnalyzedModule = (id) => {
579
573
  const knownType = knownCjsModuleTypes[id];
580
- if (
581
- knownType === IS_WRAPPED_COMMONJS ||
582
- !detectCyclesAndConditional ||
583
- fullyAnalyzedModules[id]
584
- ) {
574
+ if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
585
575
  return knownType;
586
576
  }
587
577
  fullyAnalyzedModules[id] = true;
@@ -591,26 +581,80 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
591
581
  return knownType;
592
582
  };
593
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
+ };
617
+
594
618
  return {
595
619
  getWrappedIds: () =>
596
620
  Object.keys(knownCjsModuleTypes).filter(
597
621
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
598
622
  ),
599
623
  isRequiredId: (id) => requiredIds[id],
600
- 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 (
601
649
  parentId,
602
650
  isParentCommonJS,
651
+ parentMeta,
603
652
  sources
604
653
  ) => {
605
- knownCjsModuleTypes[parentId] = isParentCommonJS;
606
- if (
607
- detectCyclesAndConditional &&
608
- knownCjsModuleTypes[parentId] &&
609
- requiredIds[parentId] &&
610
- !unconditionallyRequiredIds[parentId]
611
- ) {
612
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
613
- }
654
+ parentMeta.initialCommonJSType = isParentCommonJS;
655
+ parentMeta.requires = [];
656
+ parentMeta.isRequiredCommonJS = Object.create(null);
657
+ setInitialParentType(parentId, isParentCommonJS);
614
658
  const requireTargets = await Promise.all(
615
659
  sources.map(async ({ source, isConditional }) => {
616
660
  // Never analyze or proxy internal modules
@@ -628,38 +672,27 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
628
672
  if (resolved.external) {
629
673
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
630
674
  }
631
- requiredIds[childId] = true;
632
- if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
633
- unconditionallyRequiredIds[childId] = true;
634
- }
635
-
636
- getDependencies(parentId).add(childId);
637
- if (!isCyclic(childId)) {
638
- // This makes sure the current transform handler waits for all direct dependencies to be
639
- // loaded and transformed and therefore for all transitive CommonJS dependencies to be
640
- // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
641
- await rollupContext.load(resolved);
642
- } else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
643
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
644
- }
675
+ parentMeta.requires.push({ resolved, isConditional });
676
+ await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
645
677
  return { id: childId, allowProxy: true };
646
678
  })
647
679
  );
648
- return {
649
- requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
650
- const isCommonJS = getTypeForFullyAnalyzedModule(dependencyId);
651
- return {
652
- source: sources[index].source,
653
- id: allowProxy
654
- ? isCommonJS === IS_WRAPPED_COMMONJS
655
- ? wrapId(dependencyId, WRAPPED_SUFFIX)
656
- : wrapId(dependencyId, PROXY_SUFFIX)
657
- : dependencyId,
658
- isCommonJS
659
- };
660
- }),
661
- usesRequireWrapper: getTypeForFullyAnalyzedModule(parentId) === IS_WRAPPED_COMMONJS
662
- };
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
+ });
663
696
  }
664
697
  };
665
698
  }
@@ -1133,11 +1166,12 @@ function getRequireHandlers() {
1133
1166
  exportsName,
1134
1167
  id,
1135
1168
  exportMode,
1136
- resolveRequireSourcesAndGetMeta,
1169
+ resolveRequireSourcesAndUpdateMeta,
1137
1170
  needsRequireWrapper,
1138
1171
  isEsModule,
1139
1172
  isDynamicRequireModulesEnabled,
1140
- getIgnoreTryCatchRequireStatementMode
1173
+ getIgnoreTryCatchRequireStatementMode,
1174
+ commonjsMeta
1141
1175
  ) {
1142
1176
  const imports = [];
1143
1177
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
@@ -1160,9 +1194,10 @@ function getRequireHandlers() {
1160
1194
  );
1161
1195
  }
1162
1196
  const requiresBySource = collectSources(requireExpressions);
1163
- const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
1197
+ const requireTargets = await resolveRequireSourcesAndUpdateMeta(
1164
1198
  id,
1165
1199
  needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
1200
+ commonjsMeta,
1166
1201
  Object.keys(requiresBySource).map((source) => {
1167
1202
  return {
1168
1203
  source,
@@ -1177,10 +1212,7 @@ function getRequireHandlers() {
1177
1212
  getIgnoreTryCatchRequireStatementMode,
1178
1213
  magicString
1179
1214
  );
1180
- return {
1181
- importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
1182
- usesRequireWrapper
1183
- };
1215
+ return imports.length ? `${imports.join('\n')}\n\n` : '';
1184
1216
  }
1185
1217
 
1186
1218
  return {
@@ -1283,9 +1315,10 @@ async function transformCommonjs(
1283
1315
  astCache,
1284
1316
  defaultIsModuleExports,
1285
1317
  needsRequireWrapper,
1286
- resolveRequireSourcesAndGetMeta,
1318
+ resolveRequireSourcesAndUpdateMeta,
1287
1319
  isRequired,
1288
- checkDynamicRequire
1320
+ checkDynamicRequire,
1321
+ commonjsMeta
1289
1322
  ) {
1290
1323
  const ast = astCache || tryParse(parse, code, id);
1291
1324
  const magicString = new MagicString(code);
@@ -1669,7 +1702,7 @@ async function transformCommonjs(
1669
1702
  ) &&
1670
1703
  (ignoreGlobal || !uses.global)
1671
1704
  ) {
1672
- return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
1705
+ return { meta: { commonjs: { isCommonJS: false } } };
1673
1706
  }
1674
1707
 
1675
1708
  let leadingComment = '';
@@ -1691,7 +1724,7 @@ async function transformCommonjs(
1691
1724
  ? 'exports'
1692
1725
  : 'module';
1693
1726
 
1694
- const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
1727
+ const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
1695
1728
  magicString,
1696
1729
  topLevelDeclarations,
1697
1730
  reassignedNames,
@@ -1701,12 +1734,14 @@ async function transformCommonjs(
1701
1734
  exportsName,
1702
1735
  id,
1703
1736
  exportMode,
1704
- resolveRequireSourcesAndGetMeta,
1737
+ resolveRequireSourcesAndUpdateMeta,
1705
1738
  needsRequireWrapper,
1706
1739
  isEsModule,
1707
1740
  isDynamicRequireModulesEnabled,
1708
- getIgnoreTryCatchRequireStatementMode
1741
+ getIgnoreTryCatchRequireStatementMode,
1742
+ commonjsMeta
1709
1743
  );
1744
+ const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
1710
1745
  const exportBlock = isEsModule
1711
1746
  ? ''
1712
1747
  : rewriteExportsAndGetExportsBlock(
@@ -1759,12 +1794,7 @@ function ${requireName} () {
1759
1794
  code: magicString.toString(),
1760
1795
  map: sourceMap ? magicString.generateMap() : null,
1761
1796
  syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
1762
- meta: {
1763
- commonjs: {
1764
- isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
1765
- isMixedModule: isEsModule
1766
- }
1767
- }
1797
+ meta: { commonjs: commonjsMeta }
1768
1798
  };
1769
1799
  }
1770
1800
 
@@ -1795,11 +1825,6 @@ function commonjs(options = {}) {
1795
1825
  const defaultIsModuleExports =
1796
1826
  typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1797
1827
 
1798
- const {
1799
- resolveRequireSourcesAndGetMeta,
1800
- getWrappedIds,
1801
- isRequiredId
1802
- } = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
1803
1828
  const dynamicRequireRoot =
1804
1829
  typeof options.dynamicRequireRoot === 'string'
1805
1830
  ? resolve(options.dynamicRequireRoot)
@@ -1810,9 +1835,6 @@ function commonjs(options = {}) {
1810
1835
  );
1811
1836
  const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
1812
1837
 
1813
- const esModulesWithDefaultExport = new Set();
1814
- const esModulesWithNamedExports = new Set();
1815
-
1816
1838
  const ignoreRequire =
1817
1839
  typeof options.ignore === 'function'
1818
1840
  ? options.ignore
@@ -1840,25 +1862,31 @@ function commonjs(options = {}) {
1840
1862
 
1841
1863
  const sourceMap = options.sourceMap !== false;
1842
1864
 
1865
+ // Initialized in buildStart
1866
+ let requireResolver;
1867
+
1843
1868
  function transformAndCheckExports(code, id) {
1844
1869
  const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
1845
1870
  this.parse,
1846
1871
  code,
1847
1872
  id
1848
1873
  );
1874
+
1875
+ const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
1849
1876
  if (hasDefaultExport) {
1850
- esModulesWithDefaultExport.add(id);
1877
+ commonjsMeta.hasDefaultExport = true;
1851
1878
  }
1852
1879
  if (hasNamedExports) {
1853
- esModulesWithNamedExports.add(id);
1880
+ commonjsMeta.hasNamedExports = true;
1854
1881
  }
1855
1882
 
1856
1883
  if (
1857
1884
  !dynamicRequireModules.has(normalizePathSlashes(id)) &&
1858
- (!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
1885
+ (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
1859
1886
  (isEsModule && !options.transformMixedEsModules))
1860
1887
  ) {
1861
- return { meta: { commonjs: { isCommonJS: false } } };
1888
+ commonjsMeta.isCommonJS = false;
1889
+ return { meta: { commonjs: commonjsMeta } };
1862
1890
  }
1863
1891
 
1864
1892
  const needsRequireWrapper =
@@ -1897,9 +1925,10 @@ function commonjs(options = {}) {
1897
1925
  ast,
1898
1926
  defaultIsModuleExports,
1899
1927
  needsRequireWrapper,
1900
- resolveRequireSourcesAndGetMeta(this),
1901
- isRequiredId(id),
1902
- checkDynamicRequire
1928
+ requireResolver.resolveRequireSourcesAndUpdateMeta(this),
1929
+ requireResolver.isRequiredId(id),
1930
+ checkDynamicRequire,
1931
+ commonjsMeta
1903
1932
  );
1904
1933
  }
1905
1934
 
@@ -1934,11 +1963,12 @@ function commonjs(options = {}) {
1934
1963
  'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
1935
1964
  );
1936
1965
  }
1966
+ requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
1937
1967
  },
1938
1968
 
1939
1969
  buildEnd() {
1940
1970
  if (options.strictRequires === 'debug') {
1941
- const wrappedIds = getWrappedIds();
1971
+ const wrappedIds = requireResolver.getWrappedIds();
1942
1972
  if (wrappedIds.length) {
1943
1973
  this.warn({
1944
1974
  code: 'WRAPPED_IDS',
@@ -2002,18 +2032,16 @@ function commonjs(options = {}) {
2002
2032
 
2003
2033
  if (isWrappedId(id, PROXY_SUFFIX)) {
2004
2034
  const actualId = unwrapId(id, PROXY_SUFFIX);
2005
- return getStaticRequireProxy(
2006
- actualId,
2007
- getRequireReturnsDefault(actualId),
2008
- esModulesWithDefaultExport,
2009
- esModulesWithNamedExports,
2010
- this.load
2011
- );
2035
+ return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
2012
2036
  }
2013
2037
 
2014
2038
  return null;
2015
2039
  },
2016
2040
 
2041
+ shouldTransformCachedModule(...args) {
2042
+ return requireResolver.shouldTransformCachedModule.call(this, ...args);
2043
+ },
2044
+
2017
2045
  transform(code, id) {
2018
2046
  const extName = extname(id);
2019
2047
  if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
@@ -2030,4 +2058,4 @@ function commonjs(options = {}) {
2030
2058
  }
2031
2059
 
2032
2060
  export { commonjs as default };
2033
- //# sourceMappingURL=index.es.js.map
2061
+ //# sourceMappingURL=index.js.map