@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.
@@ -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 = "22.0.0-4";
19
+ var version = "22.0.0-5";
20
20
  var peerDependencies = {
21
- rollup: "^2.61.1"
21
+ rollup: "^2.64.0"
22
22
  };
23
23
 
24
24
  function tryParse(parse, code, id) {
@@ -405,21 +405,15 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
405
405
  return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
406
406
  }
407
407
 
408
- async function getStaticRequireProxy(
409
- id,
410
- requireReturnsDefault,
411
- esModulesWithDefaultExport,
412
- esModulesWithNamedExports,
413
- loadModule
414
- ) {
408
+ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
415
409
  const name = getName(id);
416
410
  const {
417
411
  meta: { commonjs: commonjsMeta }
418
412
  } = await loadModule({ id });
419
- if (commonjsMeta && commonjsMeta.isCommonJS) {
420
- return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
421
- } else if (!commonjsMeta) {
413
+ if (!commonjsMeta) {
422
414
  return getUnknownRequireProxy(id, requireReturnsDefault);
415
+ } else if (commonjsMeta.isCommonJS) {
416
+ return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
423
417
  } else if (!requireReturnsDefault) {
424
418
  return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
425
419
  id
@@ -427,8 +421,8 @@ async function getStaticRequireProxy(
427
421
  } else if (
428
422
  requireReturnsDefault !== true &&
429
423
  (requireReturnsDefault === 'namespace' ||
430
- !esModulesWithDefaultExport.has(id) ||
431
- (requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id)))
424
+ !commonjsMeta.hasDefaultExport ||
425
+ (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
432
426
  ) {
433
427
  return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
434
428
  }
@@ -562,7 +556,7 @@ function getResolveId(extensions) {
562
556
  };
563
557
  }
564
558
 
565
- function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional) {
559
+ function getRequireResolver(extensions, detectCyclesAndConditional) {
566
560
  const knownCjsModuleTypes = Object.create(null);
567
561
  const requiredIds = Object.create(null);
568
562
  const unconditionallyRequiredIds = Object.create(null);
@@ -586,11 +580,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
586
580
 
587
581
  const getTypeForFullyAnalyzedModule = (id) => {
588
582
  const knownType = knownCjsModuleTypes[id];
589
- if (
590
- knownType === IS_WRAPPED_COMMONJS ||
591
- !detectCyclesAndConditional ||
592
- fullyAnalyzedModules[id]
593
- ) {
583
+ if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
594
584
  return knownType;
595
585
  }
596
586
  fullyAnalyzedModules[id] = true;
@@ -600,26 +590,80 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
600
590
  return knownType;
601
591
  };
602
592
 
593
+ const setInitialParentType = (id, initialCommonJSType) => {
594
+ // It is possible a transformed module is already fully analyzed when using
595
+ // the cache and one dependency introduces a new cycle. Then transform is
596
+ // run for a fully analzyed module again. Fully analyzed modules may never
597
+ // change their type as importers already trust their type.
598
+ knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
599
+ ? knownCjsModuleTypes[id]
600
+ : initialCommonJSType;
601
+ if (
602
+ detectCyclesAndConditional &&
603
+ knownCjsModuleTypes[id] === true &&
604
+ requiredIds[id] &&
605
+ !unconditionallyRequiredIds[id]
606
+ ) {
607
+ knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
608
+ }
609
+ };
610
+
611
+ const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
612
+ const childId = resolved.id;
613
+ requiredIds[childId] = true;
614
+ if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
615
+ unconditionallyRequiredIds[childId] = true;
616
+ }
617
+
618
+ getDependencies(parentId).add(childId);
619
+ if (!isCyclic(childId)) {
620
+ // This makes sure the current transform handler waits for all direct dependencies to be
621
+ // loaded and transformed and therefore for all transitive CommonJS dependencies to be
622
+ // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
623
+ await loadModule(resolved);
624
+ }
625
+ };
626
+
603
627
  return {
604
628
  getWrappedIds: () =>
605
629
  Object.keys(knownCjsModuleTypes).filter(
606
630
  (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
607
631
  ),
608
632
  isRequiredId: (id) => requiredIds[id],
609
- resolveRequireSourcesAndGetMeta: (rollupContext) => async (
633
+ async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
634
+ // Ignore modules that did not pass through the original transformer in a previous build
635
+ if (!(parentMeta && parentMeta.requires)) {
636
+ return false;
637
+ }
638
+ setInitialParentType(parentId, parentMeta.initialCommonJSType);
639
+ await Promise.all(
640
+ parentMeta.requires.map(({ resolved, isConditional }) =>
641
+ setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
642
+ )
643
+ );
644
+ if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
645
+ return true;
646
+ }
647
+ for (const {
648
+ resolved: { id }
649
+ } of parentMeta.requires) {
650
+ if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
651
+ return true;
652
+ }
653
+ }
654
+ return false;
655
+ },
656
+ /* eslint-disable no-param-reassign */
657
+ resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
610
658
  parentId,
611
659
  isParentCommonJS,
660
+ parentMeta,
612
661
  sources
613
662
  ) => {
614
- knownCjsModuleTypes[parentId] = isParentCommonJS;
615
- if (
616
- detectCyclesAndConditional &&
617
- knownCjsModuleTypes[parentId] &&
618
- requiredIds[parentId] &&
619
- !unconditionallyRequiredIds[parentId]
620
- ) {
621
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
622
- }
663
+ parentMeta.initialCommonJSType = isParentCommonJS;
664
+ parentMeta.requires = [];
665
+ parentMeta.isRequiredCommonJS = Object.create(null);
666
+ setInitialParentType(parentId, isParentCommonJS);
623
667
  const requireTargets = await Promise.all(
624
668
  sources.map(async ({ source, isConditional }) => {
625
669
  // Never analyze or proxy internal modules
@@ -637,38 +681,27 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
637
681
  if (resolved.external) {
638
682
  return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
639
683
  }
640
- requiredIds[childId] = true;
641
- if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
642
- unconditionallyRequiredIds[childId] = true;
643
- }
644
-
645
- getDependencies(parentId).add(childId);
646
- if (!isCyclic(childId)) {
647
- // This makes sure the current transform handler waits for all direct dependencies to be
648
- // loaded and transformed and therefore for all transitive CommonJS dependencies to be
649
- // loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
650
- await rollupContext.load(resolved);
651
- } else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
652
- knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
653
- }
684
+ parentMeta.requires.push({ resolved, isConditional });
685
+ await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
654
686
  return { id: childId, allowProxy: true };
655
687
  })
656
688
  );
657
- return {
658
- requireTargets: requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
659
- const isCommonJS = getTypeForFullyAnalyzedModule(dependencyId);
660
- return {
661
- source: sources[index].source,
662
- id: allowProxy
663
- ? isCommonJS === IS_WRAPPED_COMMONJS
664
- ? wrapId(dependencyId, WRAPPED_SUFFIX)
665
- : wrapId(dependencyId, PROXY_SUFFIX)
666
- : dependencyId,
667
- isCommonJS
668
- };
669
- }),
670
- usesRequireWrapper: getTypeForFullyAnalyzedModule(parentId) === IS_WRAPPED_COMMONJS
671
- };
689
+ parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
690
+ return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
691
+ // eslint-disable-next-line no-multi-assign
692
+ const isCommonJS = (parentMeta.isRequiredCommonJS[
693
+ dependencyId
694
+ ] = getTypeForFullyAnalyzedModule(dependencyId));
695
+ return {
696
+ source: sources[index].source,
697
+ id: allowProxy
698
+ ? isCommonJS === IS_WRAPPED_COMMONJS
699
+ ? wrapId(dependencyId, WRAPPED_SUFFIX)
700
+ : wrapId(dependencyId, PROXY_SUFFIX)
701
+ : dependencyId,
702
+ isCommonJS
703
+ };
704
+ });
672
705
  }
673
706
  };
674
707
  }
@@ -1142,11 +1175,12 @@ function getRequireHandlers() {
1142
1175
  exportsName,
1143
1176
  id,
1144
1177
  exportMode,
1145
- resolveRequireSourcesAndGetMeta,
1178
+ resolveRequireSourcesAndUpdateMeta,
1146
1179
  needsRequireWrapper,
1147
1180
  isEsModule,
1148
1181
  isDynamicRequireModulesEnabled,
1149
- getIgnoreTryCatchRequireStatementMode
1182
+ getIgnoreTryCatchRequireStatementMode,
1183
+ commonjsMeta
1150
1184
  ) {
1151
1185
  const imports = [];
1152
1186
  imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
@@ -1169,9 +1203,10 @@ function getRequireHandlers() {
1169
1203
  );
1170
1204
  }
1171
1205
  const requiresBySource = collectSources(requireExpressions);
1172
- const { requireTargets, usesRequireWrapper } = await resolveRequireSourcesAndGetMeta(
1206
+ const requireTargets = await resolveRequireSourcesAndUpdateMeta(
1173
1207
  id,
1174
1208
  needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
1209
+ commonjsMeta,
1175
1210
  Object.keys(requiresBySource).map((source) => {
1176
1211
  return {
1177
1212
  source,
@@ -1186,10 +1221,7 @@ function getRequireHandlers() {
1186
1221
  getIgnoreTryCatchRequireStatementMode,
1187
1222
  magicString
1188
1223
  );
1189
- return {
1190
- importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
1191
- usesRequireWrapper
1192
- };
1224
+ return imports.length ? `${imports.join('\n')}\n\n` : '';
1193
1225
  }
1194
1226
 
1195
1227
  return {
@@ -1292,9 +1324,10 @@ async function transformCommonjs(
1292
1324
  astCache,
1293
1325
  defaultIsModuleExports,
1294
1326
  needsRequireWrapper,
1295
- resolveRequireSourcesAndGetMeta,
1327
+ resolveRequireSourcesAndUpdateMeta,
1296
1328
  isRequired,
1297
- checkDynamicRequire
1329
+ checkDynamicRequire,
1330
+ commonjsMeta
1298
1331
  ) {
1299
1332
  const ast = astCache || tryParse(parse, code, id);
1300
1333
  const magicString = new MagicString__default["default"](code);
@@ -1678,7 +1711,7 @@ async function transformCommonjs(
1678
1711
  ) &&
1679
1712
  (ignoreGlobal || !uses.global)
1680
1713
  ) {
1681
- return { meta: { commonjs: { isCommonJS: false, isMixedModule: false } } };
1714
+ return { meta: { commonjs: { isCommonJS: false } } };
1682
1715
  }
1683
1716
 
1684
1717
  let leadingComment = '';
@@ -1700,7 +1733,7 @@ async function transformCommonjs(
1700
1733
  ? 'exports'
1701
1734
  : 'module';
1702
1735
 
1703
- const { importBlock, usesRequireWrapper } = await rewriteRequireExpressionsAndGetImportBlock(
1736
+ const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
1704
1737
  magicString,
1705
1738
  topLevelDeclarations,
1706
1739
  reassignedNames,
@@ -1710,12 +1743,14 @@ async function transformCommonjs(
1710
1743
  exportsName,
1711
1744
  id,
1712
1745
  exportMode,
1713
- resolveRequireSourcesAndGetMeta,
1746
+ resolveRequireSourcesAndUpdateMeta,
1714
1747
  needsRequireWrapper,
1715
1748
  isEsModule,
1716
1749
  isDynamicRequireModulesEnabled,
1717
- getIgnoreTryCatchRequireStatementMode
1750
+ getIgnoreTryCatchRequireStatementMode,
1751
+ commonjsMeta
1718
1752
  );
1753
+ const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
1719
1754
  const exportBlock = isEsModule
1720
1755
  ? ''
1721
1756
  : rewriteExportsAndGetExportsBlock(
@@ -1768,12 +1803,7 @@ function ${requireName} () {
1768
1803
  code: magicString.toString(),
1769
1804
  map: sourceMap ? magicString.generateMap() : null,
1770
1805
  syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
1771
- meta: {
1772
- commonjs: {
1773
- isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
1774
- isMixedModule: isEsModule
1775
- }
1776
- }
1806
+ meta: { commonjs: commonjsMeta }
1777
1807
  };
1778
1808
  }
1779
1809
 
@@ -1804,11 +1834,6 @@ function commonjs(options = {}) {
1804
1834
  const defaultIsModuleExports =
1805
1835
  typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1806
1836
 
1807
- const {
1808
- resolveRequireSourcesAndGetMeta,
1809
- getWrappedIds,
1810
- isRequiredId
1811
- } = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
1812
1837
  const dynamicRequireRoot =
1813
1838
  typeof options.dynamicRequireRoot === 'string'
1814
1839
  ? path.resolve(options.dynamicRequireRoot)
@@ -1819,9 +1844,6 @@ function commonjs(options = {}) {
1819
1844
  );
1820
1845
  const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
1821
1846
 
1822
- const esModulesWithDefaultExport = new Set();
1823
- const esModulesWithNamedExports = new Set();
1824
-
1825
1847
  const ignoreRequire =
1826
1848
  typeof options.ignore === 'function'
1827
1849
  ? options.ignore
@@ -1849,25 +1871,31 @@ function commonjs(options = {}) {
1849
1871
 
1850
1872
  const sourceMap = options.sourceMap !== false;
1851
1873
 
1874
+ // Initialized in buildStart
1875
+ let requireResolver;
1876
+
1852
1877
  function transformAndCheckExports(code, id) {
1853
1878
  const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
1854
1879
  this.parse,
1855
1880
  code,
1856
1881
  id
1857
1882
  );
1883
+
1884
+ const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
1858
1885
  if (hasDefaultExport) {
1859
- esModulesWithDefaultExport.add(id);
1886
+ commonjsMeta.hasDefaultExport = true;
1860
1887
  }
1861
1888
  if (hasNamedExports) {
1862
- esModulesWithNamedExports.add(id);
1889
+ commonjsMeta.hasNamedExports = true;
1863
1890
  }
1864
1891
 
1865
1892
  if (
1866
1893
  !dynamicRequireModules.has(normalizePathSlashes(id)) &&
1867
- (!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
1894
+ (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
1868
1895
  (isEsModule && !options.transformMixedEsModules))
1869
1896
  ) {
1870
- return { meta: { commonjs: { isCommonJS: false } } };
1897
+ commonjsMeta.isCommonJS = false;
1898
+ return { meta: { commonjs: commonjsMeta } };
1871
1899
  }
1872
1900
 
1873
1901
  const needsRequireWrapper =
@@ -1906,9 +1934,10 @@ function commonjs(options = {}) {
1906
1934
  ast,
1907
1935
  defaultIsModuleExports,
1908
1936
  needsRequireWrapper,
1909
- resolveRequireSourcesAndGetMeta(this),
1910
- isRequiredId(id),
1911
- checkDynamicRequire
1937
+ requireResolver.resolveRequireSourcesAndUpdateMeta(this),
1938
+ requireResolver.isRequiredId(id),
1939
+ checkDynamicRequire,
1940
+ commonjsMeta
1912
1941
  );
1913
1942
  }
1914
1943
 
@@ -1943,11 +1972,12 @@ function commonjs(options = {}) {
1943
1972
  'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
1944
1973
  );
1945
1974
  }
1975
+ requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
1946
1976
  },
1947
1977
 
1948
1978
  buildEnd() {
1949
1979
  if (options.strictRequires === 'debug') {
1950
- const wrappedIds = getWrappedIds();
1980
+ const wrappedIds = requireResolver.getWrappedIds();
1951
1981
  if (wrappedIds.length) {
1952
1982
  this.warn({
1953
1983
  code: 'WRAPPED_IDS',
@@ -2011,18 +2041,16 @@ function commonjs(options = {}) {
2011
2041
 
2012
2042
  if (isWrappedId(id, PROXY_SUFFIX)) {
2013
2043
  const actualId = unwrapId(id, PROXY_SUFFIX);
2014
- return getStaticRequireProxy(
2015
- actualId,
2016
- getRequireReturnsDefault(actualId),
2017
- esModulesWithDefaultExport,
2018
- esModulesWithNamedExports,
2019
- this.load
2020
- );
2044
+ return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
2021
2045
  }
2022
2046
 
2023
2047
  return null;
2024
2048
  },
2025
2049
 
2050
+ shouldTransformCachedModule(...args) {
2051
+ return requireResolver.shouldTransformCachedModule.call(this, ...args);
2052
+ },
2053
+
2026
2054
  transform(code, id) {
2027
2055
  const extName = path.extname(id);
2028
2056
  if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {