@rollup/plugin-commonjs 11.1.0 → 13.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
@@ -4,16 +4,16 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
4
4
 
5
5
  var fs = require('fs');
6
6
  var path = require('path');
7
- var resolve = require('resolve');
8
7
  var pluginutils = require('@rollup/pluginutils');
8
+ var getCommonDir = _interopDefault(require('commondir'));
9
9
  var glob = _interopDefault(require('glob'));
10
10
  var estreeWalker = require('estree-walker');
11
11
  var MagicString = _interopDefault(require('magic-string'));
12
+ var resolve = require('resolve');
12
13
  var isReference = _interopDefault(require('is-reference'));
13
- var getCommonDir = _interopDefault(require('commondir'));
14
14
 
15
15
  function _slicedToArray(arr, i) {
16
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
16
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
17
17
  }
18
18
 
19
19
  function _arrayWithHoles(arr) {
@@ -21,10 +21,7 @@ function _arrayWithHoles(arr) {
21
21
  }
22
22
 
23
23
  function _iterableToArrayLimit(arr, i) {
24
- if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
25
- return;
26
- }
27
-
24
+ if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
28
25
  var _arr = [];
29
26
  var _n = true;
30
27
  var _d = false;
@@ -50,10 +47,86 @@ function _iterableToArrayLimit(arr, i) {
50
47
  return _arr;
51
48
  }
52
49
 
50
+ function _unsupportedIterableToArray(o, minLen) {
51
+ if (!o) return;
52
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
53
+ var n = Object.prototype.toString.call(o).slice(8, -1);
54
+ if (n === "Object" && o.constructor) n = o.constructor.name;
55
+ if (n === "Map" || n === "Set") return Array.from(n);
56
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
57
+ }
58
+
59
+ function _arrayLikeToArray(arr, len) {
60
+ if (len == null || len > arr.length) len = arr.length;
61
+
62
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
63
+
64
+ return arr2;
65
+ }
66
+
53
67
  function _nonIterableRest() {
54
- throw new TypeError("Invalid attempt to destructure non-iterable instance");
68
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
69
+ }
70
+
71
+ function _createForOfIteratorHelper(o) {
72
+ if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
73
+ if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
74
+ var i = 0;
75
+
76
+ var F = function () {};
77
+
78
+ return {
79
+ s: F,
80
+ n: function () {
81
+ if (i >= o.length) return {
82
+ done: true
83
+ };
84
+ return {
85
+ done: false,
86
+ value: o[i++]
87
+ };
88
+ },
89
+ e: function (e) {
90
+ throw e;
91
+ },
92
+ f: F
93
+ };
94
+ }
95
+
96
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
97
+ }
98
+
99
+ var it,
100
+ normalCompletion = true,
101
+ didErr = false,
102
+ err;
103
+ return {
104
+ s: function () {
105
+ it = o[Symbol.iterator]();
106
+ },
107
+ n: function () {
108
+ var step = it.next();
109
+ normalCompletion = step.done;
110
+ return step;
111
+ },
112
+ e: function (e) {
113
+ didErr = true;
114
+ err = e;
115
+ },
116
+ f: function () {
117
+ try {
118
+ if (!normalCompletion && it.return != null) it.return();
119
+ } finally {
120
+ if (didErr) throw err;
121
+ }
122
+ }
123
+ };
55
124
  }
56
125
 
126
+ var peerDependencies = {
127
+ rollup: "^2.3.4"
128
+ };
129
+
57
130
  /* eslint-disable no-undefined */
58
131
  const operators = {
59
132
  '==': x => equals(x.left, x.right, false),
@@ -129,8 +202,14 @@ export function unwrapExports (x) {
129
202
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
130
203
  }
131
204
 
132
- export function createCommonjsModule(fn, module) {
133
- return module = { exports: {} }, fn(module, module.exports), module.exports;
205
+ export function createCommonjsModule(fn, basedir, module) {
206
+ return module = {
207
+ path: basedir,
208
+ exports: {},
209
+ require: function (path, base) {
210
+ return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
211
+ }
212
+ }, fn(module, module.exports), module.exports;
134
213
  }
135
214
 
136
215
  export function getCjsExportFromNamespace (n) {
@@ -193,7 +272,7 @@ function join () {
193
272
  }
194
273
  if (joined === undefined)
195
274
  return '.';
196
-
275
+
197
276
  return joined;
198
277
  }
199
278
 
@@ -208,6 +287,27 @@ function isPossibleNodeModulesPath (modulePath) {
208
287
  return true;
209
288
  }
210
289
 
290
+ function dirname (path) {
291
+ if (path.length === 0)
292
+ return '.';
293
+
294
+ let i = path.length - 1;
295
+ while (i > 0) {
296
+ const c = path.charCodeAt(i);
297
+ if ((c === 47 || c === 92) && i !== path.length - 1)
298
+ break;
299
+ i--;
300
+ }
301
+
302
+ if (i > 0)
303
+ return path.substr(0, i);
304
+
305
+ if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)
306
+ return path.charAt(0);
307
+
308
+ return '.';
309
+ }
310
+
211
311
  export function commonjsRequire (path, originalModuleDir) {
212
312
  const shouldTryNodeModules = isPossibleNodeModulesPath(path);
213
313
  path = normalize(path);
@@ -229,11 +329,15 @@ export function commonjsRequire (path, originalModuleDir) {
229
329
  DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
230
330
  id: resolvedPath,
231
331
  filename: resolvedPath,
332
+ path: dirname(resolvedPath),
232
333
  exports: {},
233
334
  parent: DEFAULT_PARENT_MODULE,
234
335
  loaded: false,
235
336
  children: [],
236
- paths: []
337
+ paths: [],
338
+ require: function (path, base) {
339
+ return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
340
+ }
237
341
  };
238
342
  try {
239
343
  loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
@@ -313,12 +417,12 @@ function hasCjsKeywords(code, ignoreGlobal) {
313
417
  function checkEsModule(parse, code, id) {
314
418
  const ast = tryParse(parse, code, id);
315
419
  let isEsModule = false;
316
- var _iteratorNormalCompletion = true;
317
- var _didIteratorError = false;
318
- var _iteratorError = undefined;
420
+
421
+ var _iterator = _createForOfIteratorHelper(ast.body),
422
+ _step;
319
423
 
320
424
  try {
321
- for (var _iterator = ast.body[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
425
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
322
426
  const node = _step.value;
323
427
  if (node.type === 'ExportDefaultDeclaration') return {
324
428
  isEsModule: true,
@@ -328,12 +432,12 @@ function checkEsModule(parse, code, id) {
328
432
 
329
433
  if (node.type === 'ExportNamedDeclaration') {
330
434
  isEsModule = true;
331
- var _iteratorNormalCompletion2 = true;
332
- var _didIteratorError2 = false;
333
- var _iteratorError2 = undefined;
435
+
436
+ var _iterator2 = _createForOfIteratorHelper(node.specifiers),
437
+ _step2;
334
438
 
335
439
  try {
336
- for (var _iterator2 = node.specifiers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
440
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
337
441
  const specifier = _step2.value;
338
442
 
339
443
  if (specifier.exported.name === 'default') {
@@ -345,34 +449,16 @@ function checkEsModule(parse, code, id) {
345
449
  }
346
450
  }
347
451
  } catch (err) {
348
- _didIteratorError2 = true;
349
- _iteratorError2 = err;
452
+ _iterator2.e(err);
350
453
  } finally {
351
- try {
352
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
353
- _iterator2.return();
354
- }
355
- } finally {
356
- if (_didIteratorError2) {
357
- throw _iteratorError2;
358
- }
359
- }
454
+ _iterator2.f();
360
455
  }
361
456
  } else if (importExportDeclaration.test(node.type)) isEsModule = true;
362
457
  }
363
458
  } catch (err) {
364
- _didIteratorError = true;
365
- _iteratorError = err;
459
+ _iterator.e(err);
366
460
  } finally {
367
- try {
368
- if (!_iteratorNormalCompletion && _iterator.return != null) {
369
- _iterator.return();
370
- }
371
- } finally {
372
- if (_didIteratorError) {
373
- throw _iteratorError;
374
- }
375
- }
461
+ _iterator.f();
376
462
  }
377
463
 
378
464
  return {
@@ -400,7 +486,7 @@ function getDefinePropertyCallName(node, targetName) {
400
486
  return val.value;
401
487
  }
402
488
 
403
- function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, astCache) {
489
+ function transformCommonjs(parse, code, id, isEntry, hasImporters, isEsModule, ignoreGlobal, ignoreRequire, sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, astCache) {
404
490
  const ast = astCache || tryParse(parse, code, id);
405
491
  const magicString = new MagicString(code);
406
492
  const required = {}; // Because objects have no guaranteed ordering, yet we need it,
@@ -423,15 +509,38 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
423
509
  const namedExports = {}; // TODO handle transpiled modules
424
510
 
425
511
  let shouldWrap = /__esModule/.test(code);
426
- let usesDynamicHelpers = false;
512
+ let usesCommonjsHelpers = false;
427
513
 
428
514
  function isRequireStatement(node) {
429
515
  if (!node) return false;
430
- if (node.type !== 'CallExpression') return false;
431
- if (node.callee.name !== 'require' || scope.contains('require')) return false; // Weird case of require() without arguments
516
+ if (node.type !== 'CallExpression') return false; // Weird case of `require()` or `module.require()` without arguments
432
517
 
433
518
  if (node.arguments.length === 0) return false;
434
- return true;
519
+ return isRequireIdentifier(node.callee);
520
+ }
521
+
522
+ function isRequireIdentifier(node) {
523
+ if (!node) return false;
524
+
525
+ if (node.type === 'Identifier' && node.name === 'require'
526
+ /* `require` */
527
+ ) {
528
+ // `require` is hidden by a variable in local scope
529
+ if (scope.contains('require')) return false;
530
+ return true;
531
+ } else if (node.type === 'MemberExpression'
532
+ /* `[something].[something]` */
533
+ ) {
534
+ // `module.[something]`
535
+ if (node.object.type !== 'Identifier' || node.object.name !== 'module') return false; // `module` is hidden by a variable in local scope
536
+
537
+ if (scope.contains('module')) return false; // `module.require(...)`
538
+
539
+ if (node.property.type !== 'Identifier' || node.property.name !== 'require') return false;
540
+ return true;
541
+ }
542
+
543
+ return false;
435
544
  }
436
545
 
437
546
  function hasDynamicArguments(node) {
@@ -580,9 +689,14 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
580
689
 
581
690
  if (node.type === 'ThisExpression' && lexicalDepth === 0) {
582
691
  uses.global = true;
583
- if (!ignoreGlobal) magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
584
- storeName: true
585
- });
692
+
693
+ if (!ignoreGlobal) {
694
+ magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
695
+ storeName: true
696
+ });
697
+ usesCommonjsHelpers = true;
698
+ }
699
+
586
700
  return;
587
701
  } // rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
588
702
 
@@ -604,21 +718,21 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
604
718
  if (node.type === 'Identifier') {
605
719
  if (isReference(node, parent) && !scope.contains(node.name)) {
606
720
  if (node.name in uses) {
607
- if (node.name === 'require') {
721
+ if (isRequireIdentifier(node)) {
608
722
  if (!isDynamicRequireModulesEnabled && isStaticRequireStatement(parent)) {
609
723
  return;
610
724
  }
611
725
 
612
726
  if (isDynamicRequireModulesEnabled && isRequireStatement(parent)) {
613
- magicString.appendLeft(parent.end - 1, ',' + JSON.stringify(path.dirname(id) === '.' ? null
727
+ magicString.appendLeft(parent.end - 1, `,${JSON.stringify(path.dirname(id) === '.' ? null
614
728
  /* default behavior */
615
- : getVirtualPathForDynamicRequirePath(normalizePathSlashes(path.dirname(id)), commonDir)));
729
+ : getVirtualPathForDynamicRequirePath(normalizePathSlashes(path.dirname(id)), commonDir))}`);
616
730
  }
617
731
 
618
732
  magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
619
733
  storeName: true
620
734
  });
621
- usesDynamicHelpers = true;
735
+ usesCommonjsHelpers = true;
622
736
  }
623
737
 
624
738
  uses[node.name] = true;
@@ -627,6 +741,7 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
627
741
  magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
628
742
  storeName: true
629
743
  });
744
+ usesCommonjsHelpers = true;
630
745
  } // if module or exports are used outside the context of an assignment
631
746
  // expression, we need to wrap the module
632
747
 
@@ -707,7 +822,7 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
707
822
  magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire(${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizePathSlashes(required.source), commonDir))}, ${JSON.stringify(path.dirname(id) === '.' ? null
708
823
  /* default behavior */
709
824
  : getVirtualPathForDynamicRequirePath(normalizePathSlashes(path.dirname(id)), commonDir))})`);
710
- usesDynamicHelpers = true;
825
+ usesCommonjsHelpers = true;
711
826
  } else {
712
827
  magicString.overwrite(node.start, node.end, required.name);
713
828
  }
@@ -749,16 +864,15 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
749
864
  });
750
865
 
751
866
  if (!sources.length && !uses.module && !uses.exports && !uses.require && (ignoreGlobal || !uses.global)) {
752
- if (Object.keys(namedExports).length) {
753
- throw new Error(`Custom named exports were specified for ${id} but it does not appear to be a CommonJS module`);
754
- } // not a CommonJS module
755
-
756
-
867
+ // not a CommonJS module
757
868
  return null;
758
- }
869
+ } // If `isEsModule` is on, it means it has ES6 import/export statements,
870
+ // which just can't be wrapped in a function.
871
+
759
872
 
760
- const includeHelpers = usesDynamicHelpers || shouldWrap || uses.global || uses.require;
761
- const importBlock = `${(includeHelpers ? [`import * as ${HELPERS_NAME} from '${HELPERS_ID}';`] : []).concat(sources.map(([source]) => // import the actual module before the proxy, so that we know
873
+ if (isEsModule) shouldWrap = false;
874
+ usesCommonjsHelpers = usesCommonjsHelpers || shouldWrap;
875
+ const importBlock = `${(usesCommonjsHelpers ? [`import * as ${HELPERS_NAME} from '${HELPERS_ID}';`] : []).concat(sources.map(([source]) => // import the actual module before the proxy, so that we know
762
876
  // what kind of proxy to build
763
877
  `import '${source}';`), sources.filter(([, importProxy]) => importProxy).map(([source]) => {
764
878
  const _required$source = required[source],
@@ -771,7 +885,7 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
771
885
  let wrapperEnd = '';
772
886
  const moduleName = deconflict(scope, globals, getName(id));
773
887
 
774
- if (!isEntry && !isEsModule) {
888
+ if ((!isEntry || hasImporters) && !isEsModule) {
775
889
  const exportModuleExports = {
776
890
  str: `export { ${moduleName} as __moduleExports };`,
777
891
  name: '__moduleExports'
@@ -779,33 +893,27 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
779
893
  namedExportDeclarations.push(exportModuleExports);
780
894
  }
781
895
 
782
- const name = getName(id);
783
-
784
- function addExport(x) {
785
- const deconflicted = deconflict(scope, globals, name);
786
- const declaration = deconflicted === name ? `export var ${x} = ${moduleName}.${x};` : `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
787
- namedExportDeclarations.push({
788
- str: declaration,
789
- name: x
790
- });
791
- }
792
-
793
- if (customNamedExports) customNamedExports.forEach(addExport);
794
896
  const defaultExportPropertyAssignments = [];
795
897
  let hasDefaultExport = false;
796
898
 
797
899
  if (shouldWrap) {
798
900
  const args = `module${uses.exports ? ', exports' : ''}`;
799
901
  wrapperStart = `var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`;
800
- wrapperEnd = `\n});`;
902
+ wrapperEnd = `\n}`;
903
+
904
+ if (isDynamicRequireModulesEnabled) {
905
+ wrapperEnd += `, ${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizePathSlashes(path.dirname(id)), commonDir))}`;
906
+ }
907
+
908
+ wrapperEnd += `);`;
801
909
  } else {
802
910
  const names = [];
803
- var _iteratorNormalCompletion3 = true;
804
- var _didIteratorError3 = false;
805
- var _iteratorError3 = undefined;
911
+
912
+ var _iterator3 = _createForOfIteratorHelper(ast.body),
913
+ _step3;
806
914
 
807
915
  try {
808
- for (var _iterator3 = ast.body[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
916
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
809
917
  const node = _step3.value;
810
918
 
811
919
  if (node.type === 'ExpressionStatement' && node.expression.type === 'AssignmentExpression') {
@@ -842,7 +950,6 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
842
950
  str: declaration,
843
951
  name
844
952
  });
845
- delete namedExports[name];
846
953
  }
847
954
 
848
955
  defaultExportPropertyAssignments.push(`${moduleName}.${name} = ${deconflicted};`);
@@ -850,21 +957,12 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
850
957
  }
851
958
  }
852
959
  } catch (err) {
853
- _didIteratorError3 = true;
854
- _iteratorError3 = err;
960
+ _iterator3.e(err);
855
961
  } finally {
856
- try {
857
- if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
858
- _iterator3.return();
859
- }
860
- } finally {
861
- if (_didIteratorError3) {
862
- throw _iteratorError3;
863
- }
864
- }
962
+ _iterator3.f();
865
963
  }
866
964
 
867
- if (!hasDefaultExport && (names.length || !isEntry && !isEsModule)) {
965
+ if (!hasDefaultExport && (names.length || (!isEntry || hasImporters) && !isEsModule)) {
868
966
  wrapperEnd = `\n\nvar ${moduleName} = {\n${names.map(({
869
967
  name,
870
968
  deconflicted
@@ -872,13 +970,13 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
872
970
  }
873
971
  }
874
972
 
875
- Object.keys(namedExports).filter(key => !blacklist[key]).forEach(addExport);
876
- const defaultExport = /__esModule/.test(code) ? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});` : `export default ${moduleName};`;
973
+ const defaultExport = /__esModule/.test(code) ? `export default /*@__PURE__*/${HELPERS_NAME}.unwrapExports(${moduleName});` : `export default ${moduleName};`;
877
974
  const named = namedExportDeclarations.filter(x => x.name !== 'default' || !hasDefaultExport).map(x => x.str);
878
975
  const exportBlock = `\n\n${(isEsModule ? [] : [defaultExport]).concat(named).concat(hasDefaultExport ? defaultExportPropertyAssignments : []).join('\n')}`;
879
976
  magicString.trim().prepend(importBlock + wrapperStart).trim().append(wrapperEnd);
977
+ const injectExportBlock = hasDefaultExport || named.length > 0 || shouldWrap || !isEntry || hasImporters;
880
978
 
881
- if (hasDefaultExport || named.length > 0 || shouldWrap || !isEntry && !isEsModule) {
979
+ if (injectExportBlock) {
882
980
  magicString.append(exportBlock);
883
981
  }
884
982
 
@@ -886,58 +984,41 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
886
984
  const map = sourceMap ? magicString.generateMap() : null;
887
985
  return {
888
986
  code,
889
- map
987
+ map,
988
+ syntheticNamedExports: injectExportBlock
890
989
  };
891
990
  }
892
991
 
893
992
  function getDynamicRequirePaths(patterns) {
894
993
  const dynamicRequireModuleSet = new Set();
895
- var _iteratorNormalCompletion = true;
896
- var _didIteratorError = false;
897
- var _iteratorError = undefined;
994
+
995
+ var _iterator = _createForOfIteratorHelper(!patterns || Array.isArray(patterns) ? patterns || [] : [patterns]),
996
+ _step;
898
997
 
899
998
  try {
900
- for (var _iterator = (!patterns || Array.isArray(patterns) ? patterns || [] : [patterns])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
999
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
901
1000
  const pattern = _step.value;
902
1001
  const isNegated = pattern.startsWith('!');
903
1002
  const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(dynamicRequireModuleSet);
904
- var _iteratorNormalCompletion2 = true;
905
- var _didIteratorError2 = false;
906
- var _iteratorError2 = undefined;
1003
+
1004
+ var _iterator2 = _createForOfIteratorHelper(glob.sync(isNegated ? pattern.substr(1) : pattern)),
1005
+ _step2;
907
1006
 
908
1007
  try {
909
- for (var _iterator2 = glob.sync(isNegated ? pattern.substr(1) : pattern)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
1008
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
910
1009
  const path$1 = _step2.value;
911
1010
  modifySet(normalizePathSlashes(path.resolve(path$1)));
912
1011
  }
913
1012
  } catch (err) {
914
- _didIteratorError2 = true;
915
- _iteratorError2 = err;
1013
+ _iterator2.e(err);
916
1014
  } finally {
917
- try {
918
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
919
- _iterator2.return();
920
- }
921
- } finally {
922
- if (_didIteratorError2) {
923
- throw _iteratorError2;
924
- }
925
- }
1015
+ _iterator2.f();
926
1016
  }
927
1017
  }
928
1018
  } catch (err) {
929
- _didIteratorError = true;
930
- _iteratorError = err;
1019
+ _iterator.e(err);
931
1020
  } finally {
932
- try {
933
- if (!_iteratorNormalCompletion && _iterator.return != null) {
934
- _iterator.return();
935
- }
936
- } finally {
937
- if (_didIteratorError) {
938
- throw _iteratorError;
939
- }
940
- }
1021
+ _iterator.f();
941
1022
  }
942
1023
 
943
1024
  const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(path => {
@@ -954,10 +1035,6 @@ function getDynamicRequirePaths(patterns) {
954
1035
  };
955
1036
  }
956
1037
 
957
- var peerDependencies = {
958
- rollup: "^1.20.0||^2.0.0"
959
- };
960
-
961
1038
  /* eslint-disable no-undefined */
962
1039
  const isCjsPromises = new Map();
963
1040
  function getIsCjsPromise(id) {
@@ -1079,44 +1156,6 @@ function commonjs(options = {}) {
1079
1156
 
1080
1157
  const isDynamicRequireModulesEnabled = dynamicRequireModuleSet.size > 0;
1081
1158
  const commonDir = isDynamicRequireModulesEnabled ? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd())) : null;
1082
- const customNamedExports = {};
1083
-
1084
- if (options.namedExports) {
1085
- Object.keys(options.namedExports).forEach(id => {
1086
- let resolveId = id;
1087
- let resolvedId;
1088
-
1089
- if (resolve.isCore(id)) {
1090
- // resolve will not find npm modules with the same name as
1091
- // core modules without a trailing slash. Since core modules
1092
- // must be external, we can assume any core modules defined
1093
- // here are npm modules by that name.
1094
- resolveId += '/';
1095
- }
1096
-
1097
- try {
1098
- resolvedId = resolve.sync(resolveId, {
1099
- basedir: process.cwd()
1100
- });
1101
- } catch (err) {
1102
- resolvedId = path.resolve(id);
1103
- } // Note: customNamedExport's keys must be normalized file paths.
1104
- // resolve and nodeResolveSync both return normalized file paths
1105
- // so no additional normalization is necessary.
1106
-
1107
-
1108
- customNamedExports[resolvedId] = options.namedExports[id];
1109
-
1110
- if (fs.existsSync(resolvedId)) {
1111
- const realpath = fs.realpathSync(resolvedId);
1112
-
1113
- if (realpath !== resolvedId) {
1114
- customNamedExports[realpath] = options.namedExports[id];
1115
- }
1116
- }
1117
- });
1118
- }
1119
-
1120
1159
  const esModulesWithoutDefaultExport = new Set();
1121
1160
  const esModulesWithDefaultExport = new Set();
1122
1161
  const ignoreRequire = typeof options.ignore === 'function' ? options.ignore : Array.isArray(options.ignore) ? id => options.ignore.includes(id) : () => false;
@@ -1131,17 +1170,22 @@ function commonjs(options = {}) {
1131
1170
 
1132
1171
  const isDynamicRequireModule = dynamicRequireModuleSet.has(normalizePathSlashes(id));
1133
1172
 
1134
- if (isEsModule && !isDynamicRequireModule) {
1173
+ if (isEsModule && (!isDynamicRequireModule || !options.transformMixedEsModules)) {
1135
1174
  (hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport).add(id);
1136
- } // it is not an ES module but it does not have CJS-specific elements.
1175
+
1176
+ if (!options.transformMixedEsModules) {
1177
+ setIsCjsPromise(id, false);
1178
+ return null;
1179
+ }
1180
+ } // it is not an ES module AND it does not have CJS-specific elements.
1137
1181
  else if (!hasCjsKeywords(code, ignoreGlobal)) {
1138
1182
  esModulesWithoutDefaultExport.add(id);
1139
1183
  setIsCjsPromise(id, false);
1140
1184
  return null;
1141
1185
  }
1142
1186
 
1143
- const normalizedId = path.normalize(id);
1144
- const transformed = transformCommonjs(this.parse, code, id, this.getModuleInfo(id).isEntry, isEsModule, ignoreGlobal || isEsModule, ignoreRequire, customNamedExports[normalizedId], sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, ast);
1187
+ const moduleInfo = this.getModuleInfo(id);
1188
+ const transformed = transformCommonjs(this.parse, code, id, moduleInfo.isEntry, moduleInfo.importers && moduleInfo.importers.length > 0, isEsModule, ignoreGlobal || isEsModule, ignoreRequire, sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, ast);
1145
1189
  setIsCjsPromise(id, isEsModule ? false : Boolean(transformed));
1146
1190
 
1147
1191
  if (!transformed) {
@@ -1156,6 +1200,10 @@ function commonjs(options = {}) {
1156
1200
  name: 'commonjs',
1157
1201
 
1158
1202
  buildStart() {
1203
+ if (options.namedExports != null) {
1204
+ this.warn('The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.');
1205
+ }
1206
+
1159
1207
  const _this$meta$rollupVers = this.meta.rollupVersion.split('.').map(Number),
1160
1208
  _this$meta$rollupVers2 = _slicedToArray(_this$meta$rollupVers, 2),
1161
1209
  major = _this$meta$rollupVers2[0],
@@ -1194,12 +1242,12 @@ function commonjs(options = {}) {
1194
1242
 
1195
1243
  if (id === DYNAMIC_PACKAGES_ID) {
1196
1244
  let code = `const { commonjsRegister } = require('${HELPERS_ID}');`;
1197
- var _iteratorNormalCompletion = true;
1198
- var _didIteratorError = false;
1199
- var _iteratorError = undefined;
1245
+
1246
+ var _iterator = _createForOfIteratorHelper(dynamicRequireModuleDirPaths),
1247
+ _step;
1200
1248
 
1201
1249
  try {
1202
- for (var _iterator = dynamicRequireModuleDirPaths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
1250
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1203
1251
  const dir = _step.value;
1204
1252
  let entryPoint = 'index.js';
1205
1253
 
@@ -1217,18 +1265,9 @@ function commonjs(options = {}) {
1217
1265
  });`;
1218
1266
  }
1219
1267
  } catch (err) {
1220
- _didIteratorError = true;
1221
- _iteratorError = err;
1268
+ _iterator.e(err);
1222
1269
  } finally {
1223
- try {
1224
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1225
- _iterator.return();
1226
- }
1227
- } finally {
1228
- if (_didIteratorError) {
1229
- throw _iteratorError;
1230
- }
1231
- }
1270
+ _iterator.f();
1232
1271
  }
1233
1272
 
1234
1273
  return code;
@@ -1288,7 +1327,7 @@ function commonjs(options = {}) {
1288
1327
  dynamicImports += `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + DYNAMIC_PACKAGES_ID)});`;
1289
1328
  }
1290
1329
 
1291
- code = dynamicImports + '\n' + code;
1330
+ code = `${dynamicImports}\n${code}`;
1292
1331
  return code;
1293
1332
  }
1294
1333