@rollup/plugin-commonjs 22.0.0-9 → 22.0.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 +62 -0
- package/README.md +3 -1
- package/dist/cjs/index.js +238 -118
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +238 -118
- package/dist/es/index.js.map +1 -1
- package/package.json +3 -3
- package/types/index.d.ts +8 -0
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.0
|
|
19
|
+
var version = "22.0.0";
|
|
20
20
|
var peerDependencies = {
|
|
21
|
-
rollup: "^2.
|
|
21
|
+
rollup: "^2.68.0"
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
function tryParse(parse, code, id) {
|
|
@@ -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
|
|
|
@@ -500,84 +499,119 @@ function resolveExtensions(importee, importer, extensions) {
|
|
|
500
499
|
}
|
|
501
500
|
|
|
502
501
|
function getResolveId(extensions) {
|
|
503
|
-
|
|
504
|
-
// We assume that all requires are pre-resolved
|
|
505
|
-
const customOptions = resolveOptions.custom;
|
|
506
|
-
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
|
|
507
|
-
return null;
|
|
508
|
-
}
|
|
509
|
-
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
510
|
-
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
511
|
-
}
|
|
502
|
+
const currentlyResolving = new Map();
|
|
512
503
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
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
|
+
}
|
|
525
536
|
|
|
526
|
-
if (importer) {
|
|
527
537
|
if (
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
isWrappedId(
|
|
531
|
-
isWrappedId(
|
|
532
|
-
|
|
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
|
|
533
546
|
) {
|
|
534
547
|
return importee;
|
|
535
548
|
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
if (
|
|
539
|
-
|
|
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 };
|
|
540
573
|
}
|
|
541
|
-
// For other external imports, we need to make sure they are handled as external
|
|
542
|
-
return { id: importee, external: true };
|
|
543
574
|
}
|
|
544
|
-
}
|
|
545
575
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
576
|
+
if (importee.startsWith('\0')) {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
549
579
|
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
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
|
+
) {
|
|
595
|
+
return resolved;
|
|
596
|
+
}
|
|
597
|
+
const moduleInfo = await this.load(resolved);
|
|
598
|
+
if (resolveOptions.isEntry) {
|
|
599
|
+
moduleInfo.moduleSideEffects = true;
|
|
600
|
+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
601
|
+
return resolved.id + ENTRY_SUFFIX;
|
|
602
|
+
}
|
|
603
|
+
const {
|
|
604
|
+
meta: { commonjs: commonjsMeta }
|
|
605
|
+
} = moduleInfo;
|
|
606
|
+
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
607
|
+
return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
|
|
608
|
+
}
|
|
562
609
|
return resolved;
|
|
563
610
|
}
|
|
564
|
-
const moduleInfo = await this.load(resolved);
|
|
565
|
-
if (resolveOptions.isEntry) {
|
|
566
|
-
moduleInfo.moduleSideEffects = true;
|
|
567
|
-
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
568
|
-
return resolved.id + ENTRY_SUFFIX;
|
|
569
|
-
}
|
|
570
|
-
const {
|
|
571
|
-
meta: { commonjs: commonjsMeta }
|
|
572
|
-
} = moduleInfo;
|
|
573
|
-
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
574
|
-
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
575
|
-
}
|
|
576
|
-
return resolved;
|
|
577
611
|
};
|
|
578
612
|
}
|
|
579
613
|
|
|
580
|
-
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
614
|
+
function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
|
|
581
615
|
const knownCjsModuleTypes = Object.create(null);
|
|
582
616
|
const requiredIds = Object.create(null);
|
|
583
617
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -597,6 +631,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
597
631
|
return false;
|
|
598
632
|
};
|
|
599
633
|
|
|
634
|
+
// Once a module is listed here, its type (wrapped or not) is fixed and may
|
|
635
|
+
// not change for the rest of the current build, to not break already
|
|
636
|
+
// transformed modules.
|
|
600
637
|
const fullyAnalyzedModules = Object.create(null);
|
|
601
638
|
|
|
602
639
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
@@ -604,7 +641,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
604
641
|
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
605
642
|
return knownType;
|
|
606
643
|
}
|
|
607
|
-
fullyAnalyzedModules[id] = true;
|
|
608
644
|
if (isCyclic(id)) {
|
|
609
645
|
return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
|
|
610
646
|
}
|
|
@@ -612,13 +648,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
612
648
|
};
|
|
613
649
|
|
|
614
650
|
const setInitialParentType = (id, initialCommonJSType) => {
|
|
615
|
-
//
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
knownCjsModuleTypes[id] =
|
|
620
|
-
? knownCjsModuleTypes[id]
|
|
621
|
-
: initialCommonJSType;
|
|
651
|
+
// Fully analyzed modules may never change type
|
|
652
|
+
if (fullyAnalyzedModules[id]) {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
knownCjsModuleTypes[id] = initialCommonJSType;
|
|
622
656
|
if (
|
|
623
657
|
detectCyclesAndConditional &&
|
|
624
658
|
knownCjsModuleTypes[id] === true &&
|
|
@@ -629,7 +663,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
629
663
|
}
|
|
630
664
|
};
|
|
631
665
|
|
|
632
|
-
const
|
|
666
|
+
const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
|
|
633
667
|
const childId = resolved.id;
|
|
634
668
|
requiredIds[childId] = true;
|
|
635
669
|
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
@@ -638,41 +672,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
638
672
|
|
|
639
673
|
getDependencies(parentId).add(childId);
|
|
640
674
|
if (!isCyclic(childId)) {
|
|
641
|
-
// This makes sure the current transform handler waits for all direct
|
|
642
|
-
// loaded and transformed and therefore for all
|
|
643
|
-
// loaded as well so that all
|
|
675
|
+
// This makes sure the current transform handler waits for all direct
|
|
676
|
+
// dependencies to be loaded and transformed and therefore for all
|
|
677
|
+
// transitive CommonJS dependencies to be loaded as well so that all
|
|
678
|
+
// cycles have been found and knownCjsModuleTypes is reliable.
|
|
644
679
|
await loadModule(resolved);
|
|
645
680
|
}
|
|
646
681
|
};
|
|
647
682
|
|
|
683
|
+
const getTypeForImportedModule = async (resolved, loadModule) => {
|
|
684
|
+
if (resolved.id in knownCjsModuleTypes) {
|
|
685
|
+
// This handles cyclic ES dependencies
|
|
686
|
+
return knownCjsModuleTypes[resolved.id];
|
|
687
|
+
}
|
|
688
|
+
const {
|
|
689
|
+
meta: { commonjs }
|
|
690
|
+
} = await loadModule(resolved);
|
|
691
|
+
return (commonjs && commonjs.isCommonJS) || false;
|
|
692
|
+
};
|
|
693
|
+
|
|
648
694
|
return {
|
|
649
695
|
getWrappedIds: () =>
|
|
650
696
|
Object.keys(knownCjsModuleTypes).filter(
|
|
651
697
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
652
698
|
),
|
|
653
699
|
isRequiredId: (id) => requiredIds[id],
|
|
654
|
-
async shouldTransformCachedModule({
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
700
|
+
async shouldTransformCachedModule({
|
|
701
|
+
id: parentId,
|
|
702
|
+
resolvedSources,
|
|
703
|
+
meta: { commonjs: parentMeta }
|
|
704
|
+
}) {
|
|
705
|
+
// We explicitly track ES modules to handle circular imports
|
|
706
|
+
if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
|
|
707
|
+
if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
|
|
708
|
+
const parentRequires = parentMeta && parentMeta.requires;
|
|
709
|
+
if (parentRequires) {
|
|
710
|
+
setInitialParentType(parentId, parentMeta.initialCommonJSType);
|
|
711
|
+
await Promise.all(
|
|
712
|
+
parentRequires.map(({ resolved, isConditional }) =>
|
|
713
|
+
analyzeRequiredModule(parentId, resolved, isConditional, this.load)
|
|
714
|
+
)
|
|
715
|
+
);
|
|
716
|
+
if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
|
|
672
717
|
return true;
|
|
673
718
|
}
|
|
719
|
+
for (const {
|
|
720
|
+
resolved: { id }
|
|
721
|
+
} of parentRequires) {
|
|
722
|
+
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
723
|
+
return true;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
// Now that we decided to go with the cached copy, neither the parent
|
|
727
|
+
// module nor any of its children may change types anymore
|
|
728
|
+
fullyAnalyzedModules[parentId] = true;
|
|
729
|
+
for (const {
|
|
730
|
+
resolved: { id }
|
|
731
|
+
} of parentRequires) {
|
|
732
|
+
fullyAnalyzedModules[id] = true;
|
|
733
|
+
}
|
|
674
734
|
}
|
|
675
|
-
|
|
735
|
+
const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
|
|
736
|
+
return (
|
|
737
|
+
await Promise.all(
|
|
738
|
+
Object.keys(resolvedSources)
|
|
739
|
+
.map((source) => resolvedSources[source])
|
|
740
|
+
.filter(({ id, external }) => !(external || parentRequireSet.has(id)))
|
|
741
|
+
.map(async (resolved) => {
|
|
742
|
+
if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
|
|
743
|
+
return (
|
|
744
|
+
(await getTypeForImportedModule(
|
|
745
|
+
(await this.load({ id: resolved.id })).meta.commonjs.resolved,
|
|
746
|
+
this.load
|
|
747
|
+
)) !== IS_WRAPPED_COMMONJS
|
|
748
|
+
);
|
|
749
|
+
}
|
|
750
|
+
return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
|
|
751
|
+
})
|
|
752
|
+
)
|
|
753
|
+
).some((shouldTransform) => shouldTransform);
|
|
676
754
|
},
|
|
677
755
|
/* eslint-disable no-param-reassign */
|
|
678
756
|
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
@@ -685,16 +763,20 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
685
763
|
parentMeta.requires = [];
|
|
686
764
|
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
687
765
|
setInitialParentType(parentId, isParentCommonJS);
|
|
766
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
|
|
767
|
+
currentlyResolving.set(parentId, currentlyResolvingForParent);
|
|
688
768
|
const requireTargets = await Promise.all(
|
|
689
769
|
sources.map(async ({ source, isConditional }) => {
|
|
690
770
|
// Never analyze or proxy internal modules
|
|
691
771
|
if (source.startsWith('\0')) {
|
|
692
772
|
return { id: source, allowProxy: false };
|
|
693
773
|
}
|
|
774
|
+
currentlyResolvingForParent.add(source);
|
|
694
775
|
const resolved =
|
|
695
776
|
(await rollupContext.resolve(source, parentId, {
|
|
696
777
|
custom: { 'node-resolve': { isRequire: true } }
|
|
697
778
|
})) || resolveExtensions(source, parentId, extensions);
|
|
779
|
+
currentlyResolvingForParent.delete(source);
|
|
698
780
|
if (!resolved) {
|
|
699
781
|
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
700
782
|
}
|
|
@@ -703,16 +785,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
703
785
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
704
786
|
}
|
|
705
787
|
parentMeta.requires.push({ resolved, isConditional });
|
|
706
|
-
await
|
|
788
|
+
await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
|
|
707
789
|
return { id: childId, allowProxy: true };
|
|
708
790
|
})
|
|
709
791
|
);
|
|
710
792
|
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
793
|
+
fullyAnalyzedModules[parentId] = true;
|
|
711
794
|
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
712
795
|
// eslint-disable-next-line no-multi-assign
|
|
713
796
|
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
714
797
|
dependencyId
|
|
715
798
|
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
799
|
+
fullyAnalyzedModules[dependencyId] = true;
|
|
716
800
|
return {
|
|
717
801
|
source: sources[index].source,
|
|
718
802
|
id: allowProxy
|
|
@@ -723,6 +807,10 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
723
807
|
isCommonJS
|
|
724
808
|
};
|
|
725
809
|
});
|
|
810
|
+
},
|
|
811
|
+
isCurrentlyResolving(source, parentId) {
|
|
812
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId);
|
|
813
|
+
return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
|
|
726
814
|
}
|
|
727
815
|
};
|
|
728
816
|
}
|
|
@@ -1166,7 +1254,7 @@ function getRequireStringArg(node) {
|
|
|
1166
1254
|
function getRequireHandlers() {
|
|
1167
1255
|
const requireExpressions = [];
|
|
1168
1256
|
|
|
1169
|
-
function
|
|
1257
|
+
function addRequireExpression(
|
|
1170
1258
|
sourceId,
|
|
1171
1259
|
node,
|
|
1172
1260
|
scope,
|
|
@@ -1246,7 +1334,7 @@ function getRequireHandlers() {
|
|
|
1246
1334
|
}
|
|
1247
1335
|
|
|
1248
1336
|
return {
|
|
1249
|
-
|
|
1337
|
+
addRequireExpression,
|
|
1250
1338
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1251
1339
|
};
|
|
1252
1340
|
}
|
|
@@ -1365,6 +1453,7 @@ async function transformCommonjs(
|
|
|
1365
1453
|
let programDepth = 0;
|
|
1366
1454
|
let currentTryBlockEnd = null;
|
|
1367
1455
|
let shouldWrap = false;
|
|
1456
|
+
let reexports = false;
|
|
1368
1457
|
|
|
1369
1458
|
const globals = new Set();
|
|
1370
1459
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1372,7 +1461,7 @@ async function transformCommonjs(
|
|
|
1372
1461
|
// unconditional require elsewhere.
|
|
1373
1462
|
let currentConditionalNodeEnd = null;
|
|
1374
1463
|
const conditionalNodes = new Set();
|
|
1375
|
-
const {
|
|
1464
|
+
const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1376
1465
|
|
|
1377
1466
|
// See which names are assigned to. This is necessary to prevent
|
|
1378
1467
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
@@ -1389,6 +1478,7 @@ async function transformCommonjs(
|
|
|
1389
1478
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1390
1479
|
const replacedGlobal = [];
|
|
1391
1480
|
const replacedDynamicRequires = [];
|
|
1481
|
+
const importedVariables = new Set();
|
|
1392
1482
|
|
|
1393
1483
|
estreeWalker.walk(ast, {
|
|
1394
1484
|
enter(node, parent) {
|
|
@@ -1444,8 +1534,9 @@ async function transformCommonjs(
|
|
|
1444
1534
|
if (hasDefineEsmProperty(node.right)) {
|
|
1445
1535
|
shouldWrap = true;
|
|
1446
1536
|
}
|
|
1447
|
-
} else if (
|
|
1537
|
+
} else if (isRequireExpression(node.right, scope)) {
|
|
1448
1538
|
shouldWrap = true;
|
|
1539
|
+
reexports = true;
|
|
1449
1540
|
}
|
|
1450
1541
|
}
|
|
1451
1542
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
@@ -1502,6 +1593,11 @@ async function transformCommonjs(
|
|
|
1502
1593
|
}
|
|
1503
1594
|
|
|
1504
1595
|
if (!isRequireExpression(node, scope)) {
|
|
1596
|
+
const keypath = getKeypath(node.callee);
|
|
1597
|
+
if (keypath && importedVariables.has(keypath.name)) {
|
|
1598
|
+
// Heuristic to deoptimize requires after a required function has been called
|
|
1599
|
+
currentConditionalNodeEnd = Infinity;
|
|
1600
|
+
}
|
|
1505
1601
|
return;
|
|
1506
1602
|
}
|
|
1507
1603
|
|
|
@@ -1521,15 +1617,28 @@ async function transformCommonjs(
|
|
|
1521
1617
|
const requireStringArg = getRequireStringArg(node);
|
|
1522
1618
|
if (!ignoreRequire(requireStringArg)) {
|
|
1523
1619
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1524
|
-
|
|
1620
|
+
const toBeRemoved =
|
|
1621
|
+
parent.type === 'ExpressionStatement' &&
|
|
1622
|
+
(!currentConditionalNodeEnd ||
|
|
1623
|
+
// We should completely remove requires directly in a try-catch
|
|
1624
|
+
// so that Rollup can remove up the try-catch
|
|
1625
|
+
(currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
|
|
1626
|
+
? parent
|
|
1627
|
+
: node;
|
|
1628
|
+
addRequireExpression(
|
|
1525
1629
|
requireStringArg,
|
|
1526
1630
|
node,
|
|
1527
1631
|
scope,
|
|
1528
1632
|
usesReturnValue,
|
|
1529
1633
|
currentTryBlockEnd !== null,
|
|
1530
1634
|
currentConditionalNodeEnd !== null,
|
|
1531
|
-
|
|
1635
|
+
toBeRemoved
|
|
1532
1636
|
);
|
|
1637
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
1638
|
+
for (const name of pluginutils.extractAssignedNames(parent.id)) {
|
|
1639
|
+
importedVariables.add(name);
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1533
1642
|
}
|
|
1534
1643
|
return;
|
|
1535
1644
|
}
|
|
@@ -1719,12 +1828,15 @@ async function transformCommonjs(
|
|
|
1719
1828
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1720
1829
|
const detectWrappedDefault =
|
|
1721
1830
|
shouldWrap &&
|
|
1722
|
-
(
|
|
1831
|
+
(reexports ||
|
|
1832
|
+
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1833
|
+
code.indexOf('__esModule') >= 0);
|
|
1723
1834
|
|
|
1724
1835
|
if (
|
|
1725
1836
|
!(
|
|
1726
1837
|
shouldWrap ||
|
|
1727
1838
|
isRequired ||
|
|
1839
|
+
needsRequireWrapper ||
|
|
1728
1840
|
uses.module ||
|
|
1729
1841
|
uses.exports ||
|
|
1730
1842
|
uses.require ||
|
|
@@ -1742,7 +1854,9 @@ async function transformCommonjs(
|
|
|
1742
1854
|
magicString.remove(0, commentEnd).trim();
|
|
1743
1855
|
}
|
|
1744
1856
|
|
|
1745
|
-
const exportMode =
|
|
1857
|
+
const exportMode = isEsModule
|
|
1858
|
+
? 'none'
|
|
1859
|
+
: shouldWrap
|
|
1746
1860
|
? uses.module
|
|
1747
1861
|
? 'module'
|
|
1748
1862
|
: 'exports'
|
|
@@ -1835,6 +1949,7 @@ function commonjs(options = {}) {
|
|
|
1835
1949
|
ignoreGlobal,
|
|
1836
1950
|
ignoreDynamicRequires,
|
|
1837
1951
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1952
|
+
defaultIsModuleExports: defaultIsModuleExportsOption,
|
|
1838
1953
|
esmExternals
|
|
1839
1954
|
} = options;
|
|
1840
1955
|
const extensions = options.extensions || ['.js'];
|
|
@@ -1854,8 +1969,11 @@ function commonjs(options = {}) {
|
|
|
1854
1969
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1855
1970
|
: () => esmExternals;
|
|
1856
1971
|
|
|
1857
|
-
const
|
|
1858
|
-
typeof
|
|
1972
|
+
const getDefaultIsModuleExports =
|
|
1973
|
+
typeof defaultIsModuleExportsOption === 'function'
|
|
1974
|
+
? defaultIsModuleExportsOption
|
|
1975
|
+
: () =>
|
|
1976
|
+
typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
|
|
1859
1977
|
|
|
1860
1978
|
const dynamicRequireRoot =
|
|
1861
1979
|
typeof options.dynamicRequireRoot === 'string'
|
|
@@ -1890,7 +2008,7 @@ function commonjs(options = {}) {
|
|
|
1890
2008
|
};
|
|
1891
2009
|
};
|
|
1892
2010
|
|
|
1893
|
-
const resolveId = getResolveId(extensions);
|
|
2011
|
+
const { currentlyResolving, resolveId } = getResolveId(extensions);
|
|
1894
2012
|
|
|
1895
2013
|
const sourceMap = options.sourceMap !== false;
|
|
1896
2014
|
|
|
@@ -1955,7 +2073,7 @@ function commonjs(options = {}) {
|
|
|
1955
2073
|
dynamicRequireModules,
|
|
1956
2074
|
commonDir,
|
|
1957
2075
|
ast,
|
|
1958
|
-
|
|
2076
|
+
getDefaultIsModuleExports(id),
|
|
1959
2077
|
needsRequireWrapper,
|
|
1960
2078
|
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1961
2079
|
requireResolver.isRequiredId(id),
|
|
@@ -1995,7 +2113,11 @@ function commonjs(options = {}) {
|
|
|
1995
2113
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
1996
2114
|
);
|
|
1997
2115
|
}
|
|
1998
|
-
requireResolver = getRequireResolver(
|
|
2116
|
+
requireResolver = getRequireResolver(
|
|
2117
|
+
extensions,
|
|
2118
|
+
detectCyclesAndConditional,
|
|
2119
|
+
currentlyResolving
|
|
2120
|
+
);
|
|
1999
2121
|
},
|
|
2000
2122
|
|
|
2001
2123
|
buildEnd() {
|
|
@@ -2051,15 +2173,13 @@ function commonjs(options = {}) {
|
|
|
2051
2173
|
|
|
2052
2174
|
// entry suffix is just appended to not mess up relative external resolution
|
|
2053
2175
|
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
defaultIsModuleExports,
|
|
2057
|
-
this.getModuleInfo
|
|
2058
|
-
);
|
|
2176
|
+
const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
|
|
2177
|
+
return getEntryProxy(acutalId, getDefaultIsModuleExports(acutalId), this.getModuleInfo);
|
|
2059
2178
|
}
|
|
2060
2179
|
|
|
2061
2180
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2062
|
-
|
|
2181
|
+
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2182
|
+
return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
|
|
2063
2183
|
}
|
|
2064
2184
|
|
|
2065
2185
|
if (id === DYNAMIC_MODULES_ID) {
|