@rollup/plugin-commonjs 22.0.0-8 → 22.0.1
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/README.md +3 -1
- package/dist/cjs/index.js +253 -129
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +253 -129
- package/dist/es/index.js.map +1 -1
- package/package.json +3 -3
- package/types/index.d.ts +8 -0
- package/CHANGELOG.md +0 -590
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
## Requirements
|
|
15
15
|
|
|
16
|
-
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (
|
|
16
|
+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v12.0.0+) and Rollup v2.68.0+. If you are using [`@rollup/plugin-node-resolve`](https://github.com/rollup/plugins/tree/master/packages/node-resolve), it should be v13.0.6+.
|
|
17
17
|
|
|
18
18
|
## Install
|
|
19
19
|
|
|
@@ -68,6 +68,8 @@ You can also provide a [minimatch pattern](https://github.com/isaacs/minimatch),
|
|
|
68
68
|
Type: `string | string[]`<br>
|
|
69
69
|
Default: `[]`
|
|
70
70
|
|
|
71
|
+
_Note: In previous versions, this option would spin up a rather comprehensive mock environment that was capable of handling modules that manipulate `require.cache`. This is no longer supported. If you rely on this e.g. when using request-promise-native, use version 21 of this plugin._
|
|
72
|
+
|
|
71
73
|
Some modules contain dynamic `require` calls, or require modules that contain circular dependencies, which are not handled well by static imports.
|
|
72
74
|
Including those modules as `dynamicRequireTargets` will simulate a CommonJS (NodeJS-like) environment for them with support for dynamic dependencies. It also enables `strictRequires` for those modules, see above.
|
|
73
75
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -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.
|
|
19
|
+
var version = "22.0.1";
|
|
20
20
|
var peerDependencies = {
|
|
21
|
-
rollup: "^2.
|
|
21
|
+
rollup: "^2.68.0"
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
function tryParse(parse, code, id) {
|
|
@@ -430,11 +430,11 @@ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
|
430
430
|
return `export { default } from ${JSON.stringify(id)};`;
|
|
431
431
|
}
|
|
432
432
|
|
|
433
|
-
|
|
433
|
+
function getEntryProxy(id, defaultIsModuleExports, getModuleInfo) {
|
|
434
434
|
const {
|
|
435
435
|
meta: { commonjs: commonjsMeta },
|
|
436
436
|
hasDefaultExport
|
|
437
|
-
} =
|
|
437
|
+
} = getModuleInfo(id);
|
|
438
438
|
if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
|
|
439
439
|
const stringifiedId = JSON.stringify(id);
|
|
440
440
|
let code = `export * from ${stringifiedId};`;
|
|
@@ -462,8 +462,7 @@ function getEsImportProxy(id, defaultIsModuleExports) {
|
|
|
462
462
|
}
|
|
463
463
|
return {
|
|
464
464
|
code,
|
|
465
|
-
syntheticNamedExports: '__moduleExports'
|
|
466
|
-
meta: { commonjs: { isCommonJS: false } }
|
|
465
|
+
syntheticNamedExports: '__moduleExports'
|
|
467
466
|
};
|
|
468
467
|
}
|
|
469
468
|
|
|
@@ -499,93 +498,126 @@ function resolveExtensions(importee, importer, extensions) {
|
|
|
499
498
|
return undefined;
|
|
500
499
|
}
|
|
501
500
|
|
|
502
|
-
function getResolveId(extensions) {
|
|
503
|
-
|
|
504
|
-
// We assume that all requires are pre-resolved
|
|
505
|
-
if (
|
|
506
|
-
resolveOptions.custom &&
|
|
507
|
-
resolveOptions.custom['node-resolve'] &&
|
|
508
|
-
resolveOptions.custom['node-resolve'].isRequire
|
|
509
|
-
) {
|
|
510
|
-
return null;
|
|
511
|
-
}
|
|
512
|
-
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
513
|
-
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
514
|
-
}
|
|
501
|
+
function getResolveId(extensions, isPossibleCjsId) {
|
|
502
|
+
const currentlyResolving = new Map();
|
|
515
503
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
504
|
+
return {
|
|
505
|
+
/**
|
|
506
|
+
* This is a Maps of importers to Sets of require sources being resolved at
|
|
507
|
+
* the moment by resolveRequireSourcesAndUpdateMeta
|
|
508
|
+
*/
|
|
509
|
+
currentlyResolving,
|
|
510
|
+
async resolveId(importee, importer, resolveOptions) {
|
|
511
|
+
const customOptions = resolveOptions.custom;
|
|
512
|
+
// All logic below is specific to ES imports.
|
|
513
|
+
// Also, if we do not skip this logic for requires that are resolved while
|
|
514
|
+
// transforming a commonjs file, it can easily lead to deadlocks.
|
|
515
|
+
if (
|
|
516
|
+
customOptions &&
|
|
517
|
+
customOptions['node-resolve'] &&
|
|
518
|
+
customOptions['node-resolve'].isRequire
|
|
519
|
+
) {
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
const currentlyResolvingForParent = currentlyResolving.get(importer);
|
|
523
|
+
if (currentlyResolvingForParent && currentlyResolvingForParent.has(importee)) {
|
|
524
|
+
this.warn({
|
|
525
|
+
code: 'THIS_RESOLVE_WITHOUT_OPTIONS',
|
|
526
|
+
message:
|
|
527
|
+
'It appears a plugin has implemented a "resolveId" hook that uses "this.resolve" without forwarding the third "options" parameter of "resolveId". This is problematic as it can lead to wrong module resolutions especially for the node-resolve plugin and in certain cases cause early exit errors for the commonjs plugin.\nIn rare cases, this warning can appear if the same file is both imported and required from the same mixed ES/CommonJS module, in which case it can be ignored.',
|
|
528
|
+
url: 'https://rollupjs.org/guide/en/#resolveid'
|
|
529
|
+
});
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
534
|
+
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
535
|
+
}
|
|
528
536
|
|
|
529
|
-
if (importer) {
|
|
530
537
|
if (
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
isWrappedId(
|
|
534
|
-
isWrappedId(
|
|
535
|
-
|
|
538
|
+
importee.endsWith(ENTRY_SUFFIX) ||
|
|
539
|
+
isWrappedId(importee, MODULE_SUFFIX) ||
|
|
540
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
541
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
542
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
543
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
544
|
+
importee.startsWith(HELPERS_ID) ||
|
|
545
|
+
importee === DYNAMIC_MODULES_ID
|
|
536
546
|
) {
|
|
537
547
|
return importee;
|
|
538
548
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (
|
|
542
|
-
|
|
549
|
+
|
|
550
|
+
if (importer) {
|
|
551
|
+
if (
|
|
552
|
+
importer === DYNAMIC_MODULES_ID ||
|
|
553
|
+
// Proxies are only importing resolved ids, no need to resolve again
|
|
554
|
+
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
555
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX) ||
|
|
556
|
+
importer.endsWith(ENTRY_SUFFIX)
|
|
557
|
+
) {
|
|
558
|
+
return importee;
|
|
559
|
+
}
|
|
560
|
+
if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
|
|
561
|
+
// We need to return null for unresolved imports so that the proper warning is shown
|
|
562
|
+
if (
|
|
563
|
+
!(await this.resolve(
|
|
564
|
+
importee,
|
|
565
|
+
importer,
|
|
566
|
+
Object.assign({ skipSelf: true }, resolveOptions)
|
|
567
|
+
))
|
|
568
|
+
) {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
// For other external imports, we need to make sure they are handled as external
|
|
572
|
+
return { id: importee, external: true };
|
|
543
573
|
}
|
|
544
|
-
// For other external imports, we need to make sure they are handled as external
|
|
545
|
-
return { id: importee, external: true };
|
|
546
574
|
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
if (importee.startsWith('\0')) {
|
|
550
|
-
return null;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
554
|
-
// if that is the case, we need to add a proxy.
|
|
555
|
-
const customOptions = resolveOptions.custom;
|
|
556
575
|
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
}
|
|
576
|
+
if (importee.startsWith('\0')) {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
561
579
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
580
|
+
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
581
|
+
// if that is the case, we need to add a proxy.
|
|
582
|
+
const resolved =
|
|
583
|
+
(await this.resolve(
|
|
584
|
+
importee,
|
|
585
|
+
importer,
|
|
586
|
+
Object.assign({ skipSelf: true }, resolveOptions)
|
|
587
|
+
)) || resolveExtensions(importee, importer, extensions);
|
|
588
|
+
// Make sure that even if other plugins resolve again, we ignore our own proxies
|
|
589
|
+
if (
|
|
590
|
+
!resolved ||
|
|
591
|
+
resolved.external ||
|
|
592
|
+
resolved.id.endsWith(ENTRY_SUFFIX) ||
|
|
593
|
+
isWrappedId(resolved.id, ES_IMPORT_SUFFIX) ||
|
|
594
|
+
!isPossibleCjsId(resolved.id)
|
|
595
|
+
) {
|
|
596
|
+
return resolved;
|
|
597
|
+
}
|
|
598
|
+
const moduleInfo = await this.load(resolved);
|
|
599
|
+
const {
|
|
600
|
+
meta: { commonjs: commonjsMeta }
|
|
601
|
+
} = moduleInfo;
|
|
602
|
+
if (commonjsMeta) {
|
|
603
|
+
const { isCommonJS } = commonjsMeta;
|
|
604
|
+
if (isCommonJS) {
|
|
605
|
+
if (resolveOptions.isEntry) {
|
|
606
|
+
moduleInfo.moduleSideEffects = true;
|
|
607
|
+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
608
|
+
return resolved.id + ENTRY_SUFFIX;
|
|
609
|
+
}
|
|
610
|
+
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
611
|
+
return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
572
615
|
return resolved;
|
|
573
616
|
}
|
|
574
|
-
if (resolveOptions.isEntry) {
|
|
575
|
-
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
576
|
-
return resolved.id + ENTRY_SUFFIX;
|
|
577
|
-
}
|
|
578
|
-
const {
|
|
579
|
-
meta: { commonjs: commonjsMeta }
|
|
580
|
-
} = await this.load(resolved);
|
|
581
|
-
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
582
|
-
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
583
|
-
}
|
|
584
|
-
return resolved;
|
|
585
617
|
};
|
|
586
618
|
}
|
|
587
619
|
|
|
588
|
-
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
620
|
+
function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
|
|
589
621
|
const knownCjsModuleTypes = Object.create(null);
|
|
590
622
|
const requiredIds = Object.create(null);
|
|
591
623
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -605,6 +637,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
605
637
|
return false;
|
|
606
638
|
};
|
|
607
639
|
|
|
640
|
+
// Once a module is listed here, its type (wrapped or not) is fixed and may
|
|
641
|
+
// not change for the rest of the current build, to not break already
|
|
642
|
+
// transformed modules.
|
|
608
643
|
const fullyAnalyzedModules = Object.create(null);
|
|
609
644
|
|
|
610
645
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
@@ -612,7 +647,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
612
647
|
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
613
648
|
return knownType;
|
|
614
649
|
}
|
|
615
|
-
fullyAnalyzedModules[id] = true;
|
|
616
650
|
if (isCyclic(id)) {
|
|
617
651
|
return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
|
|
618
652
|
}
|
|
@@ -620,13 +654,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
620
654
|
};
|
|
621
655
|
|
|
622
656
|
const setInitialParentType = (id, initialCommonJSType) => {
|
|
623
|
-
//
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
knownCjsModuleTypes[id] =
|
|
628
|
-
? knownCjsModuleTypes[id]
|
|
629
|
-
: initialCommonJSType;
|
|
657
|
+
// Fully analyzed modules may never change type
|
|
658
|
+
if (fullyAnalyzedModules[id]) {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
knownCjsModuleTypes[id] = initialCommonJSType;
|
|
630
662
|
if (
|
|
631
663
|
detectCyclesAndConditional &&
|
|
632
664
|
knownCjsModuleTypes[id] === true &&
|
|
@@ -637,7 +669,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
637
669
|
}
|
|
638
670
|
};
|
|
639
671
|
|
|
640
|
-
const
|
|
672
|
+
const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
|
|
641
673
|
const childId = resolved.id;
|
|
642
674
|
requiredIds[childId] = true;
|
|
643
675
|
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
@@ -646,41 +678,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
646
678
|
|
|
647
679
|
getDependencies(parentId).add(childId);
|
|
648
680
|
if (!isCyclic(childId)) {
|
|
649
|
-
// This makes sure the current transform handler waits for all direct
|
|
650
|
-
// loaded and transformed and therefore for all
|
|
651
|
-
// loaded as well so that all
|
|
681
|
+
// This makes sure the current transform handler waits for all direct
|
|
682
|
+
// dependencies to be loaded and transformed and therefore for all
|
|
683
|
+
// transitive CommonJS dependencies to be loaded as well so that all
|
|
684
|
+
// cycles have been found and knownCjsModuleTypes is reliable.
|
|
652
685
|
await loadModule(resolved);
|
|
653
686
|
}
|
|
654
687
|
};
|
|
655
688
|
|
|
689
|
+
const getTypeForImportedModule = async (resolved, loadModule) => {
|
|
690
|
+
if (resolved.id in knownCjsModuleTypes) {
|
|
691
|
+
// This handles cyclic ES dependencies
|
|
692
|
+
return knownCjsModuleTypes[resolved.id];
|
|
693
|
+
}
|
|
694
|
+
const {
|
|
695
|
+
meta: { commonjs }
|
|
696
|
+
} = await loadModule(resolved);
|
|
697
|
+
return (commonjs && commonjs.isCommonJS) || false;
|
|
698
|
+
};
|
|
699
|
+
|
|
656
700
|
return {
|
|
657
701
|
getWrappedIds: () =>
|
|
658
702
|
Object.keys(knownCjsModuleTypes).filter(
|
|
659
703
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
660
704
|
),
|
|
661
705
|
isRequiredId: (id) => requiredIds[id],
|
|
662
|
-
async shouldTransformCachedModule({
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
706
|
+
async shouldTransformCachedModule({
|
|
707
|
+
id: parentId,
|
|
708
|
+
resolvedSources,
|
|
709
|
+
meta: { commonjs: parentMeta }
|
|
710
|
+
}) {
|
|
711
|
+
// We explicitly track ES modules to handle circular imports
|
|
712
|
+
if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
|
|
713
|
+
if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
|
|
714
|
+
const parentRequires = parentMeta && parentMeta.requires;
|
|
715
|
+
if (parentRequires) {
|
|
716
|
+
setInitialParentType(parentId, parentMeta.initialCommonJSType);
|
|
717
|
+
await Promise.all(
|
|
718
|
+
parentRequires.map(({ resolved, isConditional }) =>
|
|
719
|
+
analyzeRequiredModule(parentId, resolved, isConditional, this.load)
|
|
720
|
+
)
|
|
721
|
+
);
|
|
722
|
+
if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
|
|
680
723
|
return true;
|
|
681
724
|
}
|
|
725
|
+
for (const {
|
|
726
|
+
resolved: { id }
|
|
727
|
+
} of parentRequires) {
|
|
728
|
+
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
729
|
+
return true;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
// Now that we decided to go with the cached copy, neither the parent
|
|
733
|
+
// module nor any of its children may change types anymore
|
|
734
|
+
fullyAnalyzedModules[parentId] = true;
|
|
735
|
+
for (const {
|
|
736
|
+
resolved: { id }
|
|
737
|
+
} of parentRequires) {
|
|
738
|
+
fullyAnalyzedModules[id] = true;
|
|
739
|
+
}
|
|
682
740
|
}
|
|
683
|
-
|
|
741
|
+
const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
|
|
742
|
+
return (
|
|
743
|
+
await Promise.all(
|
|
744
|
+
Object.keys(resolvedSources)
|
|
745
|
+
.map((source) => resolvedSources[source])
|
|
746
|
+
.filter(({ id, external }) => !(external || parentRequireSet.has(id)))
|
|
747
|
+
.map(async (resolved) => {
|
|
748
|
+
if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
|
|
749
|
+
return (
|
|
750
|
+
(await getTypeForImportedModule(
|
|
751
|
+
(await this.load({ id: resolved.id })).meta.commonjs.resolved,
|
|
752
|
+
this.load
|
|
753
|
+
)) !== IS_WRAPPED_COMMONJS
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
|
|
757
|
+
})
|
|
758
|
+
)
|
|
759
|
+
).some((shouldTransform) => shouldTransform);
|
|
684
760
|
},
|
|
685
761
|
/* eslint-disable no-param-reassign */
|
|
686
762
|
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
@@ -693,16 +769,20 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
693
769
|
parentMeta.requires = [];
|
|
694
770
|
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
695
771
|
setInitialParentType(parentId, isParentCommonJS);
|
|
772
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
|
|
773
|
+
currentlyResolving.set(parentId, currentlyResolvingForParent);
|
|
696
774
|
const requireTargets = await Promise.all(
|
|
697
775
|
sources.map(async ({ source, isConditional }) => {
|
|
698
776
|
// Never analyze or proxy internal modules
|
|
699
777
|
if (source.startsWith('\0')) {
|
|
700
778
|
return { id: source, allowProxy: false };
|
|
701
779
|
}
|
|
780
|
+
currentlyResolvingForParent.add(source);
|
|
702
781
|
const resolved =
|
|
703
782
|
(await rollupContext.resolve(source, parentId, {
|
|
704
783
|
custom: { 'node-resolve': { isRequire: true } }
|
|
705
784
|
})) || resolveExtensions(source, parentId, extensions);
|
|
785
|
+
currentlyResolvingForParent.delete(source);
|
|
706
786
|
if (!resolved) {
|
|
707
787
|
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
708
788
|
}
|
|
@@ -711,16 +791,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
711
791
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
712
792
|
}
|
|
713
793
|
parentMeta.requires.push({ resolved, isConditional });
|
|
714
|
-
await
|
|
794
|
+
await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
|
|
715
795
|
return { id: childId, allowProxy: true };
|
|
716
796
|
})
|
|
717
797
|
);
|
|
718
798
|
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
799
|
+
fullyAnalyzedModules[parentId] = true;
|
|
719
800
|
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
720
801
|
// eslint-disable-next-line no-multi-assign
|
|
721
802
|
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
722
803
|
dependencyId
|
|
723
804
|
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
805
|
+
fullyAnalyzedModules[dependencyId] = true;
|
|
724
806
|
return {
|
|
725
807
|
source: sources[index].source,
|
|
726
808
|
id: allowProxy
|
|
@@ -731,6 +813,10 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
731
813
|
isCommonJS
|
|
732
814
|
};
|
|
733
815
|
});
|
|
816
|
+
},
|
|
817
|
+
isCurrentlyResolving(source, parentId) {
|
|
818
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId);
|
|
819
|
+
return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
|
|
734
820
|
}
|
|
735
821
|
};
|
|
736
822
|
}
|
|
@@ -1174,7 +1260,7 @@ function getRequireStringArg(node) {
|
|
|
1174
1260
|
function getRequireHandlers() {
|
|
1175
1261
|
const requireExpressions = [];
|
|
1176
1262
|
|
|
1177
|
-
function
|
|
1263
|
+
function addRequireExpression(
|
|
1178
1264
|
sourceId,
|
|
1179
1265
|
node,
|
|
1180
1266
|
scope,
|
|
@@ -1254,7 +1340,7 @@ function getRequireHandlers() {
|
|
|
1254
1340
|
}
|
|
1255
1341
|
|
|
1256
1342
|
return {
|
|
1257
|
-
|
|
1343
|
+
addRequireExpression,
|
|
1258
1344
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1259
1345
|
};
|
|
1260
1346
|
}
|
|
@@ -1373,6 +1459,7 @@ async function transformCommonjs(
|
|
|
1373
1459
|
let programDepth = 0;
|
|
1374
1460
|
let currentTryBlockEnd = null;
|
|
1375
1461
|
let shouldWrap = false;
|
|
1462
|
+
let reexports = false;
|
|
1376
1463
|
|
|
1377
1464
|
const globals = new Set();
|
|
1378
1465
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1380,7 +1467,7 @@ async function transformCommonjs(
|
|
|
1380
1467
|
// unconditional require elsewhere.
|
|
1381
1468
|
let currentConditionalNodeEnd = null;
|
|
1382
1469
|
const conditionalNodes = new Set();
|
|
1383
|
-
const {
|
|
1470
|
+
const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1384
1471
|
|
|
1385
1472
|
// See which names are assigned to. This is necessary to prevent
|
|
1386
1473
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
@@ -1397,6 +1484,7 @@ async function transformCommonjs(
|
|
|
1397
1484
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1398
1485
|
const replacedGlobal = [];
|
|
1399
1486
|
const replacedDynamicRequires = [];
|
|
1487
|
+
const importedVariables = new Set();
|
|
1400
1488
|
|
|
1401
1489
|
estreeWalker.walk(ast, {
|
|
1402
1490
|
enter(node, parent) {
|
|
@@ -1452,8 +1540,9 @@ async function transformCommonjs(
|
|
|
1452
1540
|
if (hasDefineEsmProperty(node.right)) {
|
|
1453
1541
|
shouldWrap = true;
|
|
1454
1542
|
}
|
|
1455
|
-
} else if (
|
|
1543
|
+
} else if (isRequireExpression(node.right, scope)) {
|
|
1456
1544
|
shouldWrap = true;
|
|
1545
|
+
reexports = true;
|
|
1457
1546
|
}
|
|
1458
1547
|
}
|
|
1459
1548
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
@@ -1510,6 +1599,11 @@ async function transformCommonjs(
|
|
|
1510
1599
|
}
|
|
1511
1600
|
|
|
1512
1601
|
if (!isRequireExpression(node, scope)) {
|
|
1602
|
+
const keypath = getKeypath(node.callee);
|
|
1603
|
+
if (keypath && importedVariables.has(keypath.name)) {
|
|
1604
|
+
// Heuristic to deoptimize requires after a required function has been called
|
|
1605
|
+
currentConditionalNodeEnd = Infinity;
|
|
1606
|
+
}
|
|
1513
1607
|
return;
|
|
1514
1608
|
}
|
|
1515
1609
|
|
|
@@ -1529,15 +1623,28 @@ async function transformCommonjs(
|
|
|
1529
1623
|
const requireStringArg = getRequireStringArg(node);
|
|
1530
1624
|
if (!ignoreRequire(requireStringArg)) {
|
|
1531
1625
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1532
|
-
|
|
1626
|
+
const toBeRemoved =
|
|
1627
|
+
parent.type === 'ExpressionStatement' &&
|
|
1628
|
+
(!currentConditionalNodeEnd ||
|
|
1629
|
+
// We should completely remove requires directly in a try-catch
|
|
1630
|
+
// so that Rollup can remove up the try-catch
|
|
1631
|
+
(currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
|
|
1632
|
+
? parent
|
|
1633
|
+
: node;
|
|
1634
|
+
addRequireExpression(
|
|
1533
1635
|
requireStringArg,
|
|
1534
1636
|
node,
|
|
1535
1637
|
scope,
|
|
1536
1638
|
usesReturnValue,
|
|
1537
1639
|
currentTryBlockEnd !== null,
|
|
1538
1640
|
currentConditionalNodeEnd !== null,
|
|
1539
|
-
|
|
1641
|
+
toBeRemoved
|
|
1540
1642
|
);
|
|
1643
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
1644
|
+
for (const name of pluginutils.extractAssignedNames(parent.id)) {
|
|
1645
|
+
importedVariables.add(name);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1541
1648
|
}
|
|
1542
1649
|
return;
|
|
1543
1650
|
}
|
|
@@ -1727,12 +1834,15 @@ async function transformCommonjs(
|
|
|
1727
1834
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1728
1835
|
const detectWrappedDefault =
|
|
1729
1836
|
shouldWrap &&
|
|
1730
|
-
(
|
|
1837
|
+
(reexports ||
|
|
1838
|
+
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1839
|
+
code.indexOf('__esModule') >= 0);
|
|
1731
1840
|
|
|
1732
1841
|
if (
|
|
1733
1842
|
!(
|
|
1734
1843
|
shouldWrap ||
|
|
1735
1844
|
isRequired ||
|
|
1845
|
+
needsRequireWrapper ||
|
|
1736
1846
|
uses.module ||
|
|
1737
1847
|
uses.exports ||
|
|
1738
1848
|
uses.require ||
|
|
@@ -1750,7 +1860,9 @@ async function transformCommonjs(
|
|
|
1750
1860
|
magicString.remove(0, commentEnd).trim();
|
|
1751
1861
|
}
|
|
1752
1862
|
|
|
1753
|
-
const exportMode =
|
|
1863
|
+
const exportMode = isEsModule
|
|
1864
|
+
? 'none'
|
|
1865
|
+
: shouldWrap
|
|
1754
1866
|
? uses.module
|
|
1755
1867
|
? 'module'
|
|
1756
1868
|
: 'exports'
|
|
@@ -1843,10 +1955,16 @@ function commonjs(options = {}) {
|
|
|
1843
1955
|
ignoreGlobal,
|
|
1844
1956
|
ignoreDynamicRequires,
|
|
1845
1957
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1958
|
+
defaultIsModuleExports: defaultIsModuleExportsOption,
|
|
1846
1959
|
esmExternals
|
|
1847
1960
|
} = options;
|
|
1848
1961
|
const extensions = options.extensions || ['.js'];
|
|
1849
1962
|
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1963
|
+
const isPossibleCjsId = (id) => {
|
|
1964
|
+
const extName = path.extname(id);
|
|
1965
|
+
return extName === '.cjs' || (extensions.includes(extName) && filter(id));
|
|
1966
|
+
};
|
|
1967
|
+
|
|
1850
1968
|
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
|
|
1851
1969
|
|
|
1852
1970
|
const getRequireReturnsDefault =
|
|
@@ -1862,8 +1980,11 @@ function commonjs(options = {}) {
|
|
|
1862
1980
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1863
1981
|
: () => esmExternals;
|
|
1864
1982
|
|
|
1865
|
-
const
|
|
1866
|
-
typeof
|
|
1983
|
+
const getDefaultIsModuleExports =
|
|
1984
|
+
typeof defaultIsModuleExportsOption === 'function'
|
|
1985
|
+
? defaultIsModuleExportsOption
|
|
1986
|
+
: () =>
|
|
1987
|
+
typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
|
|
1867
1988
|
|
|
1868
1989
|
const dynamicRequireRoot =
|
|
1869
1990
|
typeof options.dynamicRequireRoot === 'string'
|
|
@@ -1898,7 +2019,7 @@ function commonjs(options = {}) {
|
|
|
1898
2019
|
};
|
|
1899
2020
|
};
|
|
1900
2021
|
|
|
1901
|
-
const resolveId = getResolveId(extensions);
|
|
2022
|
+
const { currentlyResolving, resolveId } = getResolveId(extensions, isPossibleCjsId);
|
|
1902
2023
|
|
|
1903
2024
|
const sourceMap = options.sourceMap !== false;
|
|
1904
2025
|
|
|
@@ -1963,7 +2084,7 @@ function commonjs(options = {}) {
|
|
|
1963
2084
|
dynamicRequireModules,
|
|
1964
2085
|
commonDir,
|
|
1965
2086
|
ast,
|
|
1966
|
-
|
|
2087
|
+
getDefaultIsModuleExports(id),
|
|
1967
2088
|
needsRequireWrapper,
|
|
1968
2089
|
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1969
2090
|
requireResolver.isRequiredId(id),
|
|
@@ -2003,7 +2124,11 @@ function commonjs(options = {}) {
|
|
|
2003
2124
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
2004
2125
|
);
|
|
2005
2126
|
}
|
|
2006
|
-
requireResolver = getRequireResolver(
|
|
2127
|
+
requireResolver = getRequireResolver(
|
|
2128
|
+
extensions,
|
|
2129
|
+
detectCyclesAndConditional,
|
|
2130
|
+
currentlyResolving
|
|
2131
|
+
);
|
|
2007
2132
|
},
|
|
2008
2133
|
|
|
2009
2134
|
buildEnd() {
|
|
@@ -2059,11 +2184,13 @@ function commonjs(options = {}) {
|
|
|
2059
2184
|
|
|
2060
2185
|
// entry suffix is just appended to not mess up relative external resolution
|
|
2061
2186
|
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2062
|
-
|
|
2187
|
+
const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
|
|
2188
|
+
return getEntryProxy(acutalId, getDefaultIsModuleExports(acutalId), this.getModuleInfo);
|
|
2063
2189
|
}
|
|
2064
2190
|
|
|
2065
2191
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2066
|
-
|
|
2192
|
+
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2193
|
+
return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
|
|
2067
2194
|
}
|
|
2068
2195
|
|
|
2069
2196
|
if (id === DYNAMIC_MODULES_ID) {
|
|
@@ -2088,10 +2215,7 @@ function commonjs(options = {}) {
|
|
|
2088
2215
|
},
|
|
2089
2216
|
|
|
2090
2217
|
transform(code, id) {
|
|
2091
|
-
|
|
2092
|
-
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
2093
|
-
return null;
|
|
2094
|
-
}
|
|
2218
|
+
if (!isPossibleCjsId(id)) return null;
|
|
2095
2219
|
|
|
2096
2220
|
try {
|
|
2097
2221
|
return transformAndCheckExports.call(this, code, id);
|