@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/CHANGELOG.md +48 -0
- package/README.md +5 -39
- package/dist/index.es.js +231 -192
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +229 -190
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/types/index.d.ts +3 -4
- package/LICENSE +0 -21
package/dist/index.es.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { statSync, existsSync,
|
|
2
|
-
import { basename, extname, dirname, sep, resolve, join
|
|
3
|
-
import { sync, isCore } from 'resolve';
|
|
1
|
+
import { statSync, existsSync, readFileSync } from 'fs';
|
|
2
|
+
import { basename, extname, dirname, sep, resolve, join } from 'path';
|
|
4
3
|
import { makeLegalIdentifier, attachScopes, extractAssignedNames, createFilter } from '@rollup/pluginutils';
|
|
4
|
+
import getCommonDir from 'commondir';
|
|
5
5
|
import glob from 'glob';
|
|
6
6
|
import { walk } from 'estree-walker';
|
|
7
7
|
import MagicString from 'magic-string';
|
|
8
|
+
import { sync } from 'resolve';
|
|
8
9
|
import isReference from 'is-reference';
|
|
9
|
-
import getCommonDir from 'commondir';
|
|
10
10
|
|
|
11
11
|
function _slicedToArray(arr, i) {
|
|
12
|
-
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
|
|
12
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function _arrayWithHoles(arr) {
|
|
@@ -17,10 +17,7 @@ function _arrayWithHoles(arr) {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
function _iterableToArrayLimit(arr, i) {
|
|
20
|
-
if (!(Symbol.iterator in Object(arr)
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
20
|
+
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
|
|
24
21
|
var _arr = [];
|
|
25
22
|
var _n = true;
|
|
26
23
|
var _d = false;
|
|
@@ -46,10 +43,86 @@ function _iterableToArrayLimit(arr, i) {
|
|
|
46
43
|
return _arr;
|
|
47
44
|
}
|
|
48
45
|
|
|
46
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
47
|
+
if (!o) return;
|
|
48
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
49
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
50
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
51
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
52
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function _arrayLikeToArray(arr, len) {
|
|
56
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
57
|
+
|
|
58
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
59
|
+
|
|
60
|
+
return arr2;
|
|
61
|
+
}
|
|
62
|
+
|
|
49
63
|
function _nonIterableRest() {
|
|
50
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
|
64
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function _createForOfIteratorHelper(o) {
|
|
68
|
+
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
|
69
|
+
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
|
|
70
|
+
var i = 0;
|
|
71
|
+
|
|
72
|
+
var F = function () {};
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
s: F,
|
|
76
|
+
n: function () {
|
|
77
|
+
if (i >= o.length) return {
|
|
78
|
+
done: true
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
done: false,
|
|
82
|
+
value: o[i++]
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
e: function (e) {
|
|
86
|
+
throw e;
|
|
87
|
+
},
|
|
88
|
+
f: F
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var it,
|
|
96
|
+
normalCompletion = true,
|
|
97
|
+
didErr = false,
|
|
98
|
+
err;
|
|
99
|
+
return {
|
|
100
|
+
s: function () {
|
|
101
|
+
it = o[Symbol.iterator]();
|
|
102
|
+
},
|
|
103
|
+
n: function () {
|
|
104
|
+
var step = it.next();
|
|
105
|
+
normalCompletion = step.done;
|
|
106
|
+
return step;
|
|
107
|
+
},
|
|
108
|
+
e: function (e) {
|
|
109
|
+
didErr = true;
|
|
110
|
+
err = e;
|
|
111
|
+
},
|
|
112
|
+
f: function () {
|
|
113
|
+
try {
|
|
114
|
+
if (!normalCompletion && it.return != null) it.return();
|
|
115
|
+
} finally {
|
|
116
|
+
if (didErr) throw err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
51
120
|
}
|
|
52
121
|
|
|
122
|
+
var peerDependencies = {
|
|
123
|
+
rollup: "^2.3.4"
|
|
124
|
+
};
|
|
125
|
+
|
|
53
126
|
/* eslint-disable no-undefined */
|
|
54
127
|
const operators = {
|
|
55
128
|
'==': x => equals(x.left, x.right, false),
|
|
@@ -125,8 +198,14 @@ export function unwrapExports (x) {
|
|
|
125
198
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
126
199
|
}
|
|
127
200
|
|
|
128
|
-
export function createCommonjsModule(fn, module) {
|
|
129
|
-
return module = {
|
|
201
|
+
export function createCommonjsModule(fn, basedir, module) {
|
|
202
|
+
return module = {
|
|
203
|
+
path: basedir,
|
|
204
|
+
exports: {},
|
|
205
|
+
require: function (path, base) {
|
|
206
|
+
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
|
|
207
|
+
}
|
|
208
|
+
}, fn(module, module.exports), module.exports;
|
|
130
209
|
}
|
|
131
210
|
|
|
132
211
|
export function getCjsExportFromNamespace (n) {
|
|
@@ -189,7 +268,7 @@ function join () {
|
|
|
189
268
|
}
|
|
190
269
|
if (joined === undefined)
|
|
191
270
|
return '.';
|
|
192
|
-
|
|
271
|
+
|
|
193
272
|
return joined;
|
|
194
273
|
}
|
|
195
274
|
|
|
@@ -204,6 +283,27 @@ function isPossibleNodeModulesPath (modulePath) {
|
|
|
204
283
|
return true;
|
|
205
284
|
}
|
|
206
285
|
|
|
286
|
+
function dirname (path) {
|
|
287
|
+
if (path.length === 0)
|
|
288
|
+
return '.';
|
|
289
|
+
|
|
290
|
+
let i = path.length - 1;
|
|
291
|
+
while (i > 0) {
|
|
292
|
+
const c = path.charCodeAt(i);
|
|
293
|
+
if ((c === 47 || c === 92) && i !== path.length - 1)
|
|
294
|
+
break;
|
|
295
|
+
i--;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (i > 0)
|
|
299
|
+
return path.substr(0, i);
|
|
300
|
+
|
|
301
|
+
if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)
|
|
302
|
+
return path.charAt(0);
|
|
303
|
+
|
|
304
|
+
return '.';
|
|
305
|
+
}
|
|
306
|
+
|
|
207
307
|
export function commonjsRequire (path, originalModuleDir) {
|
|
208
308
|
const shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
209
309
|
path = normalize(path);
|
|
@@ -225,11 +325,15 @@ export function commonjsRequire (path, originalModuleDir) {
|
|
|
225
325
|
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {
|
|
226
326
|
id: resolvedPath,
|
|
227
327
|
filename: resolvedPath,
|
|
328
|
+
path: dirname(resolvedPath),
|
|
228
329
|
exports: {},
|
|
229
330
|
parent: DEFAULT_PARENT_MODULE,
|
|
230
331
|
loaded: false,
|
|
231
332
|
children: [],
|
|
232
|
-
paths: []
|
|
333
|
+
paths: [],
|
|
334
|
+
require: function (path, base) {
|
|
335
|
+
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);
|
|
336
|
+
}
|
|
233
337
|
};
|
|
234
338
|
try {
|
|
235
339
|
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);
|
|
@@ -309,12 +413,12 @@ function hasCjsKeywords(code, ignoreGlobal) {
|
|
|
309
413
|
function checkEsModule(parse, code, id) {
|
|
310
414
|
const ast = tryParse(parse, code, id);
|
|
311
415
|
let isEsModule = false;
|
|
312
|
-
|
|
313
|
-
var
|
|
314
|
-
|
|
416
|
+
|
|
417
|
+
var _iterator = _createForOfIteratorHelper(ast.body),
|
|
418
|
+
_step;
|
|
315
419
|
|
|
316
420
|
try {
|
|
317
|
-
for (
|
|
421
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
318
422
|
const node = _step.value;
|
|
319
423
|
if (node.type === 'ExportDefaultDeclaration') return {
|
|
320
424
|
isEsModule: true,
|
|
@@ -324,12 +428,12 @@ function checkEsModule(parse, code, id) {
|
|
|
324
428
|
|
|
325
429
|
if (node.type === 'ExportNamedDeclaration') {
|
|
326
430
|
isEsModule = true;
|
|
327
|
-
|
|
328
|
-
var
|
|
329
|
-
|
|
431
|
+
|
|
432
|
+
var _iterator2 = _createForOfIteratorHelper(node.specifiers),
|
|
433
|
+
_step2;
|
|
330
434
|
|
|
331
435
|
try {
|
|
332
|
-
for (
|
|
436
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
333
437
|
const specifier = _step2.value;
|
|
334
438
|
|
|
335
439
|
if (specifier.exported.name === 'default') {
|
|
@@ -341,34 +445,16 @@ function checkEsModule(parse, code, id) {
|
|
|
341
445
|
}
|
|
342
446
|
}
|
|
343
447
|
} catch (err) {
|
|
344
|
-
|
|
345
|
-
_iteratorError2 = err;
|
|
448
|
+
_iterator2.e(err);
|
|
346
449
|
} finally {
|
|
347
|
-
|
|
348
|
-
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
349
|
-
_iterator2.return();
|
|
350
|
-
}
|
|
351
|
-
} finally {
|
|
352
|
-
if (_didIteratorError2) {
|
|
353
|
-
throw _iteratorError2;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
450
|
+
_iterator2.f();
|
|
356
451
|
}
|
|
357
452
|
} else if (importExportDeclaration.test(node.type)) isEsModule = true;
|
|
358
453
|
}
|
|
359
454
|
} catch (err) {
|
|
360
|
-
|
|
361
|
-
_iteratorError = err;
|
|
455
|
+
_iterator.e(err);
|
|
362
456
|
} finally {
|
|
363
|
-
|
|
364
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
365
|
-
_iterator.return();
|
|
366
|
-
}
|
|
367
|
-
} finally {
|
|
368
|
-
if (_didIteratorError) {
|
|
369
|
-
throw _iteratorError;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
457
|
+
_iterator.f();
|
|
372
458
|
}
|
|
373
459
|
|
|
374
460
|
return {
|
|
@@ -396,7 +482,7 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
396
482
|
return val.value;
|
|
397
483
|
}
|
|
398
484
|
|
|
399
|
-
function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, ignoreRequire,
|
|
485
|
+
function transformCommonjs(parse, code, id, isEntry, hasImporters, isEsModule, ignoreGlobal, ignoreRequire, sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, astCache) {
|
|
400
486
|
const ast = astCache || tryParse(parse, code, id);
|
|
401
487
|
const magicString = new MagicString(code);
|
|
402
488
|
const required = {}; // Because objects have no guaranteed ordering, yet we need it,
|
|
@@ -419,15 +505,38 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
419
505
|
const namedExports = {}; // TODO handle transpiled modules
|
|
420
506
|
|
|
421
507
|
let shouldWrap = /__esModule/.test(code);
|
|
422
|
-
let
|
|
508
|
+
let usesCommonjsHelpers = false;
|
|
423
509
|
|
|
424
510
|
function isRequireStatement(node) {
|
|
425
511
|
if (!node) return false;
|
|
426
|
-
if (node.type !== 'CallExpression') return false;
|
|
427
|
-
if (node.callee.name !== 'require' || scope.contains('require')) return false; // Weird case of require() without arguments
|
|
512
|
+
if (node.type !== 'CallExpression') return false; // Weird case of `require()` or `module.require()` without arguments
|
|
428
513
|
|
|
429
514
|
if (node.arguments.length === 0) return false;
|
|
430
|
-
return
|
|
515
|
+
return isRequireIdentifier(node.callee);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function isRequireIdentifier(node) {
|
|
519
|
+
if (!node) return false;
|
|
520
|
+
|
|
521
|
+
if (node.type === 'Identifier' && node.name === 'require'
|
|
522
|
+
/* `require` */
|
|
523
|
+
) {
|
|
524
|
+
// `require` is hidden by a variable in local scope
|
|
525
|
+
if (scope.contains('require')) return false;
|
|
526
|
+
return true;
|
|
527
|
+
} else if (node.type === 'MemberExpression'
|
|
528
|
+
/* `[something].[something]` */
|
|
529
|
+
) {
|
|
530
|
+
// `module.[something]`
|
|
531
|
+
if (node.object.type !== 'Identifier' || node.object.name !== 'module') return false; // `module` is hidden by a variable in local scope
|
|
532
|
+
|
|
533
|
+
if (scope.contains('module')) return false; // `module.require(...)`
|
|
534
|
+
|
|
535
|
+
if (node.property.type !== 'Identifier' || node.property.name !== 'require') return false;
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return false;
|
|
431
540
|
}
|
|
432
541
|
|
|
433
542
|
function hasDynamicArguments(node) {
|
|
@@ -576,9 +685,14 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
576
685
|
|
|
577
686
|
if (node.type === 'ThisExpression' && lexicalDepth === 0) {
|
|
578
687
|
uses.global = true;
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
688
|
+
|
|
689
|
+
if (!ignoreGlobal) {
|
|
690
|
+
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
691
|
+
storeName: true
|
|
692
|
+
});
|
|
693
|
+
usesCommonjsHelpers = true;
|
|
694
|
+
}
|
|
695
|
+
|
|
582
696
|
return;
|
|
583
697
|
} // rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
|
|
584
698
|
|
|
@@ -600,21 +714,21 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
600
714
|
if (node.type === 'Identifier') {
|
|
601
715
|
if (isReference(node, parent) && !scope.contains(node.name)) {
|
|
602
716
|
if (node.name in uses) {
|
|
603
|
-
if (node
|
|
717
|
+
if (isRequireIdentifier(node)) {
|
|
604
718
|
if (!isDynamicRequireModulesEnabled && isStaticRequireStatement(parent)) {
|
|
605
719
|
return;
|
|
606
720
|
}
|
|
607
721
|
|
|
608
722
|
if (isDynamicRequireModulesEnabled && isRequireStatement(parent)) {
|
|
609
|
-
magicString.appendLeft(parent.end - 1,
|
|
723
|
+
magicString.appendLeft(parent.end - 1, `,${JSON.stringify(dirname(id) === '.' ? null
|
|
610
724
|
/* default behavior */
|
|
611
|
-
: getVirtualPathForDynamicRequirePath(normalizePathSlashes(dirname(id)), commonDir)));
|
|
725
|
+
: getVirtualPathForDynamicRequirePath(normalizePathSlashes(dirname(id)), commonDir))}`);
|
|
612
726
|
}
|
|
613
727
|
|
|
614
728
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
615
729
|
storeName: true
|
|
616
730
|
});
|
|
617
|
-
|
|
731
|
+
usesCommonjsHelpers = true;
|
|
618
732
|
}
|
|
619
733
|
|
|
620
734
|
uses[node.name] = true;
|
|
@@ -623,6 +737,7 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
623
737
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
624
738
|
storeName: true
|
|
625
739
|
});
|
|
740
|
+
usesCommonjsHelpers = true;
|
|
626
741
|
} // if module or exports are used outside the context of an assignment
|
|
627
742
|
// expression, we need to wrap the module
|
|
628
743
|
|
|
@@ -703,7 +818,7 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
703
818
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire(${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizePathSlashes(required.source), commonDir))}, ${JSON.stringify(dirname(id) === '.' ? null
|
|
704
819
|
/* default behavior */
|
|
705
820
|
: getVirtualPathForDynamicRequirePath(normalizePathSlashes(dirname(id)), commonDir))})`);
|
|
706
|
-
|
|
821
|
+
usesCommonjsHelpers = true;
|
|
707
822
|
} else {
|
|
708
823
|
magicString.overwrite(node.start, node.end, required.name);
|
|
709
824
|
}
|
|
@@ -745,16 +860,15 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
745
860
|
});
|
|
746
861
|
|
|
747
862
|
if (!sources.length && !uses.module && !uses.exports && !uses.require && (ignoreGlobal || !uses.global)) {
|
|
748
|
-
|
|
749
|
-
throw new Error(`Custom named exports were specified for ${id} but it does not appear to be a CommonJS module`);
|
|
750
|
-
} // not a CommonJS module
|
|
751
|
-
|
|
752
|
-
|
|
863
|
+
// not a CommonJS module
|
|
753
864
|
return null;
|
|
754
|
-
}
|
|
865
|
+
} // If `isEsModule` is on, it means it has ES6 import/export statements,
|
|
866
|
+
// which just can't be wrapped in a function.
|
|
755
867
|
|
|
756
|
-
|
|
757
|
-
|
|
868
|
+
|
|
869
|
+
if (isEsModule) shouldWrap = false;
|
|
870
|
+
usesCommonjsHelpers = usesCommonjsHelpers || shouldWrap;
|
|
871
|
+
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
|
|
758
872
|
// what kind of proxy to build
|
|
759
873
|
`import '${source}';`), sources.filter(([, importProxy]) => importProxy).map(([source]) => {
|
|
760
874
|
const _required$source = required[source],
|
|
@@ -767,7 +881,7 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
767
881
|
let wrapperEnd = '';
|
|
768
882
|
const moduleName = deconflict(scope, globals, getName(id));
|
|
769
883
|
|
|
770
|
-
if (!isEntry && !isEsModule) {
|
|
884
|
+
if ((!isEntry || hasImporters) && !isEsModule) {
|
|
771
885
|
const exportModuleExports = {
|
|
772
886
|
str: `export { ${moduleName} as __moduleExports };`,
|
|
773
887
|
name: '__moduleExports'
|
|
@@ -775,33 +889,27 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
775
889
|
namedExportDeclarations.push(exportModuleExports);
|
|
776
890
|
}
|
|
777
891
|
|
|
778
|
-
const name = getName(id);
|
|
779
|
-
|
|
780
|
-
function addExport(x) {
|
|
781
|
-
const deconflicted = deconflict(scope, globals, name);
|
|
782
|
-
const declaration = deconflicted === name ? `export var ${x} = ${moduleName}.${x};` : `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
|
|
783
|
-
namedExportDeclarations.push({
|
|
784
|
-
str: declaration,
|
|
785
|
-
name: x
|
|
786
|
-
});
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
if (customNamedExports) customNamedExports.forEach(addExport);
|
|
790
892
|
const defaultExportPropertyAssignments = [];
|
|
791
893
|
let hasDefaultExport = false;
|
|
792
894
|
|
|
793
895
|
if (shouldWrap) {
|
|
794
896
|
const args = `module${uses.exports ? ', exports' : ''}`;
|
|
795
897
|
wrapperStart = `var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`;
|
|
796
|
-
wrapperEnd = `\n}
|
|
898
|
+
wrapperEnd = `\n}`;
|
|
899
|
+
|
|
900
|
+
if (isDynamicRequireModulesEnabled) {
|
|
901
|
+
wrapperEnd += `, ${JSON.stringify(getVirtualPathForDynamicRequirePath(normalizePathSlashes(dirname(id)), commonDir))}`;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
wrapperEnd += `);`;
|
|
797
905
|
} else {
|
|
798
906
|
const names = [];
|
|
799
|
-
|
|
800
|
-
var
|
|
801
|
-
|
|
907
|
+
|
|
908
|
+
var _iterator3 = _createForOfIteratorHelper(ast.body),
|
|
909
|
+
_step3;
|
|
802
910
|
|
|
803
911
|
try {
|
|
804
|
-
for (
|
|
912
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
805
913
|
const node = _step3.value;
|
|
806
914
|
|
|
807
915
|
if (node.type === 'ExpressionStatement' && node.expression.type === 'AssignmentExpression') {
|
|
@@ -838,7 +946,6 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
838
946
|
str: declaration,
|
|
839
947
|
name
|
|
840
948
|
});
|
|
841
|
-
delete namedExports[name];
|
|
842
949
|
}
|
|
843
950
|
|
|
844
951
|
defaultExportPropertyAssignments.push(`${moduleName}.${name} = ${deconflicted};`);
|
|
@@ -846,21 +953,12 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
846
953
|
}
|
|
847
954
|
}
|
|
848
955
|
} catch (err) {
|
|
849
|
-
|
|
850
|
-
_iteratorError3 = err;
|
|
956
|
+
_iterator3.e(err);
|
|
851
957
|
} finally {
|
|
852
|
-
|
|
853
|
-
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
|
|
854
|
-
_iterator3.return();
|
|
855
|
-
}
|
|
856
|
-
} finally {
|
|
857
|
-
if (_didIteratorError3) {
|
|
858
|
-
throw _iteratorError3;
|
|
859
|
-
}
|
|
860
|
-
}
|
|
958
|
+
_iterator3.f();
|
|
861
959
|
}
|
|
862
960
|
|
|
863
|
-
if (!hasDefaultExport && (names.length || !isEntry && !isEsModule)) {
|
|
961
|
+
if (!hasDefaultExport && (names.length || (!isEntry || hasImporters) && !isEsModule)) {
|
|
864
962
|
wrapperEnd = `\n\nvar ${moduleName} = {\n${names.map(({
|
|
865
963
|
name,
|
|
866
964
|
deconflicted
|
|
@@ -868,13 +966,13 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
868
966
|
}
|
|
869
967
|
}
|
|
870
968
|
|
|
871
|
-
|
|
872
|
-
const defaultExport = /__esModule/.test(code) ? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});` : `export default ${moduleName};`;
|
|
969
|
+
const defaultExport = /__esModule/.test(code) ? `export default /*@__PURE__*/${HELPERS_NAME}.unwrapExports(${moduleName});` : `export default ${moduleName};`;
|
|
873
970
|
const named = namedExportDeclarations.filter(x => x.name !== 'default' || !hasDefaultExport).map(x => x.str);
|
|
874
971
|
const exportBlock = `\n\n${(isEsModule ? [] : [defaultExport]).concat(named).concat(hasDefaultExport ? defaultExportPropertyAssignments : []).join('\n')}`;
|
|
875
972
|
magicString.trim().prepend(importBlock + wrapperStart).trim().append(wrapperEnd);
|
|
973
|
+
const injectExportBlock = hasDefaultExport || named.length > 0 || shouldWrap || !isEntry || hasImporters;
|
|
876
974
|
|
|
877
|
-
if (
|
|
975
|
+
if (injectExportBlock) {
|
|
878
976
|
magicString.append(exportBlock);
|
|
879
977
|
}
|
|
880
978
|
|
|
@@ -882,58 +980,41 @@ function transformCommonjs(parse, code, id, isEntry, isEsModule, ignoreGlobal, i
|
|
|
882
980
|
const map = sourceMap ? magicString.generateMap() : null;
|
|
883
981
|
return {
|
|
884
982
|
code,
|
|
885
|
-
map
|
|
983
|
+
map,
|
|
984
|
+
syntheticNamedExports: injectExportBlock
|
|
886
985
|
};
|
|
887
986
|
}
|
|
888
987
|
|
|
889
988
|
function getDynamicRequirePaths(patterns) {
|
|
890
989
|
const dynamicRequireModuleSet = new Set();
|
|
891
|
-
|
|
892
|
-
var
|
|
893
|
-
|
|
990
|
+
|
|
991
|
+
var _iterator = _createForOfIteratorHelper(!patterns || Array.isArray(patterns) ? patterns || [] : [patterns]),
|
|
992
|
+
_step;
|
|
894
993
|
|
|
895
994
|
try {
|
|
896
|
-
for (
|
|
995
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
897
996
|
const pattern = _step.value;
|
|
898
997
|
const isNegated = pattern.startsWith('!');
|
|
899
998
|
const modifySet = Set.prototype[isNegated ? 'delete' : 'add'].bind(dynamicRequireModuleSet);
|
|
900
|
-
|
|
901
|
-
var
|
|
902
|
-
|
|
999
|
+
|
|
1000
|
+
var _iterator2 = _createForOfIteratorHelper(glob.sync(isNegated ? pattern.substr(1) : pattern)),
|
|
1001
|
+
_step2;
|
|
903
1002
|
|
|
904
1003
|
try {
|
|
905
|
-
for (
|
|
1004
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
906
1005
|
const path = _step2.value;
|
|
907
1006
|
modifySet(normalizePathSlashes(resolve(path)));
|
|
908
1007
|
}
|
|
909
1008
|
} catch (err) {
|
|
910
|
-
|
|
911
|
-
_iteratorError2 = err;
|
|
1009
|
+
_iterator2.e(err);
|
|
912
1010
|
} finally {
|
|
913
|
-
|
|
914
|
-
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
|
915
|
-
_iterator2.return();
|
|
916
|
-
}
|
|
917
|
-
} finally {
|
|
918
|
-
if (_didIteratorError2) {
|
|
919
|
-
throw _iteratorError2;
|
|
920
|
-
}
|
|
921
|
-
}
|
|
1011
|
+
_iterator2.f();
|
|
922
1012
|
}
|
|
923
1013
|
}
|
|
924
1014
|
} catch (err) {
|
|
925
|
-
|
|
926
|
-
_iteratorError = err;
|
|
1015
|
+
_iterator.e(err);
|
|
927
1016
|
} finally {
|
|
928
|
-
|
|
929
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
930
|
-
_iterator.return();
|
|
931
|
-
}
|
|
932
|
-
} finally {
|
|
933
|
-
if (_didIteratorError) {
|
|
934
|
-
throw _iteratorError;
|
|
935
|
-
}
|
|
936
|
-
}
|
|
1017
|
+
_iterator.f();
|
|
937
1018
|
}
|
|
938
1019
|
|
|
939
1020
|
const dynamicRequireModuleDirPaths = Array.from(dynamicRequireModuleSet.values()).filter(path => {
|
|
@@ -950,10 +1031,6 @@ function getDynamicRequirePaths(patterns) {
|
|
|
950
1031
|
};
|
|
951
1032
|
}
|
|
952
1033
|
|
|
953
|
-
var peerDependencies = {
|
|
954
|
-
rollup: "^1.20.0||^2.0.0"
|
|
955
|
-
};
|
|
956
|
-
|
|
957
1034
|
/* eslint-disable no-undefined */
|
|
958
1035
|
const isCjsPromises = new Map();
|
|
959
1036
|
function getIsCjsPromise(id) {
|
|
@@ -1075,44 +1152,6 @@ function commonjs(options = {}) {
|
|
|
1075
1152
|
|
|
1076
1153
|
const isDynamicRequireModulesEnabled = dynamicRequireModuleSet.size > 0;
|
|
1077
1154
|
const commonDir = isDynamicRequireModulesEnabled ? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd())) : null;
|
|
1078
|
-
const customNamedExports = {};
|
|
1079
|
-
|
|
1080
|
-
if (options.namedExports) {
|
|
1081
|
-
Object.keys(options.namedExports).forEach(id => {
|
|
1082
|
-
let resolveId = id;
|
|
1083
|
-
let resolvedId;
|
|
1084
|
-
|
|
1085
|
-
if (isCore(id)) {
|
|
1086
|
-
// resolve will not find npm modules with the same name as
|
|
1087
|
-
// core modules without a trailing slash. Since core modules
|
|
1088
|
-
// must be external, we can assume any core modules defined
|
|
1089
|
-
// here are npm modules by that name.
|
|
1090
|
-
resolveId += '/';
|
|
1091
|
-
}
|
|
1092
|
-
|
|
1093
|
-
try {
|
|
1094
|
-
resolvedId = sync(resolveId, {
|
|
1095
|
-
basedir: process.cwd()
|
|
1096
|
-
});
|
|
1097
|
-
} catch (err) {
|
|
1098
|
-
resolvedId = resolve(id);
|
|
1099
|
-
} // Note: customNamedExport's keys must be normalized file paths.
|
|
1100
|
-
// resolve and nodeResolveSync both return normalized file paths
|
|
1101
|
-
// so no additional normalization is necessary.
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
customNamedExports[resolvedId] = options.namedExports[id];
|
|
1105
|
-
|
|
1106
|
-
if (existsSync(resolvedId)) {
|
|
1107
|
-
const realpath = realpathSync(resolvedId);
|
|
1108
|
-
|
|
1109
|
-
if (realpath !== resolvedId) {
|
|
1110
|
-
customNamedExports[realpath] = options.namedExports[id];
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
});
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
1155
|
const esModulesWithoutDefaultExport = new Set();
|
|
1117
1156
|
const esModulesWithDefaultExport = new Set();
|
|
1118
1157
|
const ignoreRequire = typeof options.ignore === 'function' ? options.ignore : Array.isArray(options.ignore) ? id => options.ignore.includes(id) : () => false;
|
|
@@ -1127,17 +1166,22 @@ function commonjs(options = {}) {
|
|
|
1127
1166
|
|
|
1128
1167
|
const isDynamicRequireModule = dynamicRequireModuleSet.has(normalizePathSlashes(id));
|
|
1129
1168
|
|
|
1130
|
-
if (isEsModule && !isDynamicRequireModule) {
|
|
1169
|
+
if (isEsModule && (!isDynamicRequireModule || !options.transformMixedEsModules)) {
|
|
1131
1170
|
(hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport).add(id);
|
|
1132
|
-
|
|
1171
|
+
|
|
1172
|
+
if (!options.transformMixedEsModules) {
|
|
1173
|
+
setIsCjsPromise(id, false);
|
|
1174
|
+
return null;
|
|
1175
|
+
}
|
|
1176
|
+
} // it is not an ES module AND it does not have CJS-specific elements.
|
|
1133
1177
|
else if (!hasCjsKeywords(code, ignoreGlobal)) {
|
|
1134
1178
|
esModulesWithoutDefaultExport.add(id);
|
|
1135
1179
|
setIsCjsPromise(id, false);
|
|
1136
1180
|
return null;
|
|
1137
1181
|
}
|
|
1138
1182
|
|
|
1139
|
-
const
|
|
1140
|
-
const transformed = transformCommonjs(this.parse, code, id,
|
|
1183
|
+
const moduleInfo = this.getModuleInfo(id);
|
|
1184
|
+
const transformed = transformCommonjs(this.parse, code, id, moduleInfo.isEntry, moduleInfo.importers && moduleInfo.importers.length > 0, isEsModule, ignoreGlobal || isEsModule, ignoreRequire, sourceMap, isDynamicRequireModulesEnabled, dynamicRequireModuleSet, commonDir, ast);
|
|
1141
1185
|
setIsCjsPromise(id, isEsModule ? false : Boolean(transformed));
|
|
1142
1186
|
|
|
1143
1187
|
if (!transformed) {
|
|
@@ -1152,6 +1196,10 @@ function commonjs(options = {}) {
|
|
|
1152
1196
|
name: 'commonjs',
|
|
1153
1197
|
|
|
1154
1198
|
buildStart() {
|
|
1199
|
+
if (options.namedExports != null) {
|
|
1200
|
+
this.warn('The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.');
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1155
1203
|
const _this$meta$rollupVers = this.meta.rollupVersion.split('.').map(Number),
|
|
1156
1204
|
_this$meta$rollupVers2 = _slicedToArray(_this$meta$rollupVers, 2),
|
|
1157
1205
|
major = _this$meta$rollupVers2[0],
|
|
@@ -1190,12 +1238,12 @@ function commonjs(options = {}) {
|
|
|
1190
1238
|
|
|
1191
1239
|
if (id === DYNAMIC_PACKAGES_ID) {
|
|
1192
1240
|
let code = `const { commonjsRegister } = require('${HELPERS_ID}');`;
|
|
1193
|
-
|
|
1194
|
-
var
|
|
1195
|
-
|
|
1241
|
+
|
|
1242
|
+
var _iterator = _createForOfIteratorHelper(dynamicRequireModuleDirPaths),
|
|
1243
|
+
_step;
|
|
1196
1244
|
|
|
1197
1245
|
try {
|
|
1198
|
-
for (
|
|
1246
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1199
1247
|
const dir = _step.value;
|
|
1200
1248
|
let entryPoint = 'index.js';
|
|
1201
1249
|
|
|
@@ -1213,18 +1261,9 @@ function commonjs(options = {}) {
|
|
|
1213
1261
|
});`;
|
|
1214
1262
|
}
|
|
1215
1263
|
} catch (err) {
|
|
1216
|
-
|
|
1217
|
-
_iteratorError = err;
|
|
1264
|
+
_iterator.e(err);
|
|
1218
1265
|
} finally {
|
|
1219
|
-
|
|
1220
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
1221
|
-
_iterator.return();
|
|
1222
|
-
}
|
|
1223
|
-
} finally {
|
|
1224
|
-
if (_didIteratorError) {
|
|
1225
|
-
throw _iteratorError;
|
|
1226
|
-
}
|
|
1227
|
-
}
|
|
1266
|
+
_iterator.f();
|
|
1228
1267
|
}
|
|
1229
1268
|
|
|
1230
1269
|
return code;
|
|
@@ -1284,7 +1323,7 @@ function commonjs(options = {}) {
|
|
|
1284
1323
|
dynamicImports += `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + DYNAMIC_PACKAGES_ID)});`;
|
|
1285
1324
|
}
|
|
1286
1325
|
|
|
1287
|
-
code = dynamicImports
|
|
1326
|
+
code = `${dynamicImports}\n${code}`;
|
|
1288
1327
|
return code;
|
|
1289
1328
|
}
|
|
1290
1329
|
|