@rollup/plugin-commonjs 18.0.0 → 18.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @rollup/plugin-commonjs ChangeLog
2
2
 
3
+ ## v18.1.0
4
+
5
+ _2021-05-04_
6
+
7
+ ### Bugfixes
8
+
9
+ - fix: idempotence issue (#871)
10
+
11
+ ### Features
12
+
13
+ - feat: Add `defaultIsModuleExports` option to match Node.js behavior (#838)
14
+
3
15
  ## v18.0.0
4
16
 
5
17
  _2021-03-26_
package/README.md CHANGED
@@ -174,6 +174,49 @@ If you set `esmExternals` to `true`, this plugins assumes that all external depe
174
174
 
175
175
  You can also supply an array of ids to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module.
176
176
 
177
+ ### `defaultIsModuleExports`
178
+
179
+ Type: `boolean | "auto"`<br>
180
+ Default: `"auto"`
181
+
182
+ Controls what is the default export when importing a CommonJS file from an ES module.
183
+
184
+ - `true`: The value of the default export is `module.exports`. This currently matches the behavior of Node.js when importing a CommonJS file.
185
+ ```js
186
+ // mod.cjs
187
+ exports.default = 3;
188
+ ```
189
+ ```js
190
+ import foo from './mod.cjs';
191
+ console.log(foo); // { default: 3 }
192
+ ```
193
+ - `false`: The value of the default export is `exports.default`.
194
+ ```js
195
+ // mod.cjs
196
+ exports.default = 3;
197
+ ```
198
+ ```js
199
+ import foo from './mod.cjs';
200
+ console.log(foo); // 3
201
+ ```
202
+ - `"auto"`: The value of the default export is `exports.default` if the CommonJS file has an `exports.__esModule === true` property; otherwise it's `module.exports`. This makes it possible to import
203
+ the default export of ES modules compiled to CommonJS as if they were not compiled.
204
+ ```js
205
+ // mod.cjs
206
+ exports.default = 3;
207
+ ```
208
+ ```js
209
+ // mod-compiled.cjs
210
+ exports.__esModule = true;
211
+ exports.default = 3;
212
+ ```
213
+ ```js
214
+ import foo from './mod.cjs';
215
+ import bar from './mod-compiled.cjs';
216
+ console.log(foo); // { default: 3 }
217
+ console.log(bar); // 3
218
+ ```
219
+
177
220
  ### `requireReturnsDefault`
178
221
 
179
222
  Type: `boolean | "namespace" | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`<br>
package/dist/index.es.js CHANGED
@@ -451,9 +451,7 @@ function getDynamicRequirePaths(patterns) {
451
451
  return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
452
452
  }
453
453
 
454
- const isCjsPromises = new Map();
455
-
456
- function getIsCjsPromise(id) {
454
+ function getIsCjsPromise(isCjsPromises, id) {
457
455
  let isCjsPromise = isCjsPromises.get(id);
458
456
  if (isCjsPromise) return isCjsPromise.promise;
459
457
 
@@ -469,7 +467,7 @@ function getIsCjsPromise(id) {
469
467
  return promise;
470
468
  }
471
469
 
472
- function setIsCjsPromise(id, resolution) {
470
+ function setIsCjsPromise(isCjsPromises, id, resolution) {
473
471
  const isCjsPromise = isCjsPromises.get(id);
474
472
  if (isCjsPromise) {
475
473
  if (isCjsPromise.resolve) {
@@ -523,10 +521,11 @@ async function getStaticRequireProxy(
523
521
  id,
524
522
  requireReturnsDefault,
525
523
  esModulesWithDefaultExport,
526
- esModulesWithNamedExports
524
+ esModulesWithNamedExports,
525
+ isCjsPromises
527
526
  ) {
528
527
  const name = getName(id);
529
- const isCjs = await getIsCjsPromise(id);
528
+ const isCjs = await getIsCjsPromise(isCjsPromises, id);
530
529
  if (isCjs) {
531
530
  return `import { __moduleExports } from ${JSON.stringify(id)}; export default __moduleExports;`;
532
531
  } else if (isCjs === null) {
@@ -800,7 +799,8 @@ function rewriteExportsAndGetExportsBlock(
800
799
  isRestorableCompiledEsm,
801
800
  code,
802
801
  uses,
803
- HELPERS_NAME
802
+ HELPERS_NAME,
803
+ defaultIsModuleExports
804
804
  ) {
805
805
  const namedExportDeclarations = [`export { ${moduleName} as __moduleExports };`];
806
806
  const moduleExportsPropertyAssignments = [];
@@ -855,19 +855,29 @@ function rewriteExportsAndGetExportsBlock(
855
855
 
856
856
  // Generate default export
857
857
  const defaultExport = [];
858
- if (isRestorableCompiledEsm) {
859
- defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
860
- } else if (
861
- (wrapped || deconflictedDefaultExportName) &&
862
- (defineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0)
863
- ) {
864
- // eslint-disable-next-line no-param-reassign
865
- uses.commonjsHelpers = true;
866
- defaultExport.push(
867
- `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
868
- );
869
- } else {
858
+ if (defaultIsModuleExports === 'auto') {
859
+ if (isRestorableCompiledEsm) {
860
+ defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
861
+ } else if (
862
+ (wrapped || deconflictedDefaultExportName) &&
863
+ (defineCompiledEsmExpressions.length > 0 || code.includes('__esModule'))
864
+ ) {
865
+ // eslint-disable-next-line no-param-reassign
866
+ uses.commonjsHelpers = true;
867
+ defaultExport.push(
868
+ `export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
869
+ );
870
+ } else {
871
+ defaultExport.push(`export default ${moduleName};`);
872
+ }
873
+ } else if (defaultIsModuleExports === true) {
870
874
  defaultExport.push(`export default ${moduleName};`);
875
+ } else if (defaultIsModuleExports === false) {
876
+ if (deconflictedDefaultExportName) {
877
+ defaultExport.push(`export default ${deconflictedDefaultExportName};`);
878
+ } else {
879
+ defaultExport.push(`export default ${moduleName}.default;`);
880
+ }
871
881
  }
872
882
 
873
883
  return `\n\n${defaultExport
@@ -1122,7 +1132,8 @@ function transformCommonjs(
1122
1132
  dynamicRequireModuleSet,
1123
1133
  disableWrap,
1124
1134
  commonDir,
1125
- astCache
1135
+ astCache,
1136
+ defaultIsModuleExports
1126
1137
  ) {
1127
1138
  const ast = astCache || tryParse(parse, code, id);
1128
1139
  const magicString = new MagicString(code);
@@ -1539,7 +1550,8 @@ function transformCommonjs(
1539
1550
  isRestorableCompiledEsm,
1540
1551
  code,
1541
1552
  uses,
1542
- HELPERS_NAME
1553
+ HELPERS_NAME,
1554
+ defaultIsModuleExports
1543
1555
  );
1544
1556
 
1545
1557
  const importBlock = rewriteRequireExpressionsAndGetImportBlock(
@@ -1588,6 +1600,8 @@ function commonjs(options = {}) {
1588
1600
  : Array.isArray(esmExternals)
1589
1601
  ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
1590
1602
  : () => esmExternals;
1603
+ const defaultIsModuleExports =
1604
+ typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
1591
1605
 
1592
1606
  const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
1593
1607
  options.dynamicRequireTargets
@@ -1599,6 +1613,7 @@ function commonjs(options = {}) {
1599
1613
 
1600
1614
  const esModulesWithDefaultExport = new Set();
1601
1615
  const esModulesWithNamedExports = new Set();
1616
+ const isCjsPromises = new Map();
1602
1617
 
1603
1618
  const ignoreRequire =
1604
1619
  typeof options.ignore === 'function'
@@ -1674,7 +1689,8 @@ function commonjs(options = {}) {
1674
1689
  dynamicRequireModuleSet,
1675
1690
  disableWrap,
1676
1691
  commonDir,
1677
- ast
1692
+ ast,
1693
+ defaultIsModuleExports
1678
1694
  );
1679
1695
  }
1680
1696
 
@@ -1734,7 +1750,8 @@ function commonjs(options = {}) {
1734
1750
  actualId,
1735
1751
  getRequireReturnsDefault(actualId),
1736
1752
  esModulesWithDefaultExport,
1737
- esModulesWithNamedExports
1753
+ esModulesWithNamedExports,
1754
+ isCjsPromises
1738
1755
  );
1739
1756
  }
1740
1757
 
@@ -1770,11 +1787,11 @@ function commonjs(options = {}) {
1770
1787
  if (commonjs) {
1771
1788
  const isCjs = commonjs.isCommonJS;
1772
1789
  if (isCjs != null) {
1773
- setIsCjsPromise(id, isCjs);
1790
+ setIsCjsPromise(isCjsPromises, id, isCjs);
1774
1791
  return;
1775
1792
  }
1776
1793
  }
1777
- setIsCjsPromise(id, null);
1794
+ setIsCjsPromise(isCjsPromises, id, null);
1778
1795
  }
1779
1796
  };
1780
1797
  }