@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/es/index.js
CHANGED
|
@@ -7,9 +7,9 @@ import { walk } from 'estree-walker';
|
|
|
7
7
|
import MagicString from 'magic-string';
|
|
8
8
|
import isReference from 'is-reference';
|
|
9
9
|
|
|
10
|
-
var version = "22.0.0
|
|
10
|
+
var version = "22.0.0";
|
|
11
11
|
var peerDependencies = {
|
|
12
|
-
rollup: "^2.
|
|
12
|
+
rollup: "^2.68.0"
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
function tryParse(parse, code, id) {
|
|
@@ -453,8 +453,7 @@ function getEsImportProxy(id, defaultIsModuleExports) {
|
|
|
453
453
|
}
|
|
454
454
|
return {
|
|
455
455
|
code,
|
|
456
|
-
syntheticNamedExports: '__moduleExports'
|
|
457
|
-
meta: { commonjs: { isCommonJS: false } }
|
|
456
|
+
syntheticNamedExports: '__moduleExports'
|
|
458
457
|
};
|
|
459
458
|
}
|
|
460
459
|
|
|
@@ -491,84 +490,119 @@ function resolveExtensions(importee, importer, extensions) {
|
|
|
491
490
|
}
|
|
492
491
|
|
|
493
492
|
function getResolveId(extensions) {
|
|
494
|
-
|
|
495
|
-
// We assume that all requires are pre-resolved
|
|
496
|
-
const customOptions = resolveOptions.custom;
|
|
497
|
-
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
|
|
498
|
-
return null;
|
|
499
|
-
}
|
|
500
|
-
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
501
|
-
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
502
|
-
}
|
|
493
|
+
const currentlyResolving = new Map();
|
|
503
494
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
495
|
+
return {
|
|
496
|
+
/**
|
|
497
|
+
* This is a Maps of importers to Sets of require sources being resolved at
|
|
498
|
+
* the moment by resolveRequireSourcesAndUpdateMeta
|
|
499
|
+
*/
|
|
500
|
+
currentlyResolving,
|
|
501
|
+
async resolveId(importee, importer, resolveOptions) {
|
|
502
|
+
const customOptions = resolveOptions.custom;
|
|
503
|
+
// All logic below is specific to ES imports.
|
|
504
|
+
// Also, if we do not skip this logic for requires that are resolved while
|
|
505
|
+
// transforming a commonjs file, it can easily lead to deadlocks.
|
|
506
|
+
if (
|
|
507
|
+
customOptions &&
|
|
508
|
+
customOptions['node-resolve'] &&
|
|
509
|
+
customOptions['node-resolve'].isRequire
|
|
510
|
+
) {
|
|
511
|
+
return null;
|
|
512
|
+
}
|
|
513
|
+
const currentlyResolvingForParent = currentlyResolving.get(importer);
|
|
514
|
+
if (currentlyResolvingForParent && currentlyResolvingForParent.has(importee)) {
|
|
515
|
+
this.warn({
|
|
516
|
+
code: 'THIS_RESOLVE_WITHOUT_OPTIONS',
|
|
517
|
+
message:
|
|
518
|
+
'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.',
|
|
519
|
+
url: 'https://rollupjs.org/guide/en/#resolveid'
|
|
520
|
+
});
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
525
|
+
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
526
|
+
}
|
|
516
527
|
|
|
517
|
-
if (importer) {
|
|
518
528
|
if (
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
isWrappedId(
|
|
522
|
-
isWrappedId(
|
|
523
|
-
|
|
529
|
+
importee.endsWith(ENTRY_SUFFIX) ||
|
|
530
|
+
isWrappedId(importee, MODULE_SUFFIX) ||
|
|
531
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
532
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
533
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
534
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
535
|
+
importee.startsWith(HELPERS_ID) ||
|
|
536
|
+
importee === DYNAMIC_MODULES_ID
|
|
524
537
|
) {
|
|
525
538
|
return importee;
|
|
526
539
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
if (
|
|
530
|
-
|
|
540
|
+
|
|
541
|
+
if (importer) {
|
|
542
|
+
if (
|
|
543
|
+
importer === DYNAMIC_MODULES_ID ||
|
|
544
|
+
// Proxies are only importing resolved ids, no need to resolve again
|
|
545
|
+
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
546
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX) ||
|
|
547
|
+
importer.endsWith(ENTRY_SUFFIX)
|
|
548
|
+
) {
|
|
549
|
+
return importee;
|
|
550
|
+
}
|
|
551
|
+
if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
|
|
552
|
+
// We need to return null for unresolved imports so that the proper warning is shown
|
|
553
|
+
if (
|
|
554
|
+
!(await this.resolve(
|
|
555
|
+
importee,
|
|
556
|
+
importer,
|
|
557
|
+
Object.assign({ skipSelf: true }, resolveOptions)
|
|
558
|
+
))
|
|
559
|
+
) {
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
// For other external imports, we need to make sure they are handled as external
|
|
563
|
+
return { id: importee, external: true };
|
|
531
564
|
}
|
|
532
|
-
// For other external imports, we need to make sure they are handled as external
|
|
533
|
-
return { id: importee, external: true };
|
|
534
565
|
}
|
|
535
|
-
}
|
|
536
566
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
567
|
+
if (importee.startsWith('\0')) {
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
540
570
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
571
|
+
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
572
|
+
// if that is the case, we need to add a proxy.
|
|
573
|
+
const resolved =
|
|
574
|
+
(await this.resolve(
|
|
575
|
+
importee,
|
|
576
|
+
importer,
|
|
577
|
+
Object.assign({ skipSelf: true }, resolveOptions)
|
|
578
|
+
)) || resolveExtensions(importee, importer, extensions);
|
|
579
|
+
// Make sure that even if other plugins resolve again, we ignore our own proxies
|
|
580
|
+
if (
|
|
581
|
+
!resolved ||
|
|
582
|
+
resolved.external ||
|
|
583
|
+
resolved.id.endsWith(ENTRY_SUFFIX) ||
|
|
584
|
+
isWrappedId(resolved.id, ES_IMPORT_SUFFIX)
|
|
585
|
+
) {
|
|
586
|
+
return resolved;
|
|
587
|
+
}
|
|
588
|
+
const moduleInfo = await this.load(resolved);
|
|
589
|
+
if (resolveOptions.isEntry) {
|
|
590
|
+
moduleInfo.moduleSideEffects = true;
|
|
591
|
+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
592
|
+
return resolved.id + ENTRY_SUFFIX;
|
|
593
|
+
}
|
|
594
|
+
const {
|
|
595
|
+
meta: { commonjs: commonjsMeta }
|
|
596
|
+
} = moduleInfo;
|
|
597
|
+
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
598
|
+
return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
|
|
599
|
+
}
|
|
553
600
|
return resolved;
|
|
554
601
|
}
|
|
555
|
-
const moduleInfo = await this.load(resolved);
|
|
556
|
-
if (resolveOptions.isEntry) {
|
|
557
|
-
moduleInfo.moduleSideEffects = true;
|
|
558
|
-
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
559
|
-
return resolved.id + ENTRY_SUFFIX;
|
|
560
|
-
}
|
|
561
|
-
const {
|
|
562
|
-
meta: { commonjs: commonjsMeta }
|
|
563
|
-
} = moduleInfo;
|
|
564
|
-
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
565
|
-
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
566
|
-
}
|
|
567
|
-
return resolved;
|
|
568
602
|
};
|
|
569
603
|
}
|
|
570
604
|
|
|
571
|
-
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
605
|
+
function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
|
|
572
606
|
const knownCjsModuleTypes = Object.create(null);
|
|
573
607
|
const requiredIds = Object.create(null);
|
|
574
608
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -588,6 +622,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
588
622
|
return false;
|
|
589
623
|
};
|
|
590
624
|
|
|
625
|
+
// Once a module is listed here, its type (wrapped or not) is fixed and may
|
|
626
|
+
// not change for the rest of the current build, to not break already
|
|
627
|
+
// transformed modules.
|
|
591
628
|
const fullyAnalyzedModules = Object.create(null);
|
|
592
629
|
|
|
593
630
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
@@ -595,7 +632,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
595
632
|
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
596
633
|
return knownType;
|
|
597
634
|
}
|
|
598
|
-
fullyAnalyzedModules[id] = true;
|
|
599
635
|
if (isCyclic(id)) {
|
|
600
636
|
return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
|
|
601
637
|
}
|
|
@@ -603,13 +639,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
603
639
|
};
|
|
604
640
|
|
|
605
641
|
const setInitialParentType = (id, initialCommonJSType) => {
|
|
606
|
-
//
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
knownCjsModuleTypes[id] =
|
|
611
|
-
? knownCjsModuleTypes[id]
|
|
612
|
-
: initialCommonJSType;
|
|
642
|
+
// Fully analyzed modules may never change type
|
|
643
|
+
if (fullyAnalyzedModules[id]) {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
knownCjsModuleTypes[id] = initialCommonJSType;
|
|
613
647
|
if (
|
|
614
648
|
detectCyclesAndConditional &&
|
|
615
649
|
knownCjsModuleTypes[id] === true &&
|
|
@@ -620,7 +654,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
620
654
|
}
|
|
621
655
|
};
|
|
622
656
|
|
|
623
|
-
const
|
|
657
|
+
const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
|
|
624
658
|
const childId = resolved.id;
|
|
625
659
|
requiredIds[childId] = true;
|
|
626
660
|
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
@@ -629,41 +663,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
629
663
|
|
|
630
664
|
getDependencies(parentId).add(childId);
|
|
631
665
|
if (!isCyclic(childId)) {
|
|
632
|
-
// This makes sure the current transform handler waits for all direct
|
|
633
|
-
// loaded and transformed and therefore for all
|
|
634
|
-
// loaded as well so that all
|
|
666
|
+
// This makes sure the current transform handler waits for all direct
|
|
667
|
+
// dependencies to be loaded and transformed and therefore for all
|
|
668
|
+
// transitive CommonJS dependencies to be loaded as well so that all
|
|
669
|
+
// cycles have been found and knownCjsModuleTypes is reliable.
|
|
635
670
|
await loadModule(resolved);
|
|
636
671
|
}
|
|
637
672
|
};
|
|
638
673
|
|
|
674
|
+
const getTypeForImportedModule = async (resolved, loadModule) => {
|
|
675
|
+
if (resolved.id in knownCjsModuleTypes) {
|
|
676
|
+
// This handles cyclic ES dependencies
|
|
677
|
+
return knownCjsModuleTypes[resolved.id];
|
|
678
|
+
}
|
|
679
|
+
const {
|
|
680
|
+
meta: { commonjs }
|
|
681
|
+
} = await loadModule(resolved);
|
|
682
|
+
return (commonjs && commonjs.isCommonJS) || false;
|
|
683
|
+
};
|
|
684
|
+
|
|
639
685
|
return {
|
|
640
686
|
getWrappedIds: () =>
|
|
641
687
|
Object.keys(knownCjsModuleTypes).filter(
|
|
642
688
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
643
689
|
),
|
|
644
690
|
isRequiredId: (id) => requiredIds[id],
|
|
645
|
-
async shouldTransformCachedModule({
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
691
|
+
async shouldTransformCachedModule({
|
|
692
|
+
id: parentId,
|
|
693
|
+
resolvedSources,
|
|
694
|
+
meta: { commonjs: parentMeta }
|
|
695
|
+
}) {
|
|
696
|
+
// We explicitly track ES modules to handle circular imports
|
|
697
|
+
if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
|
|
698
|
+
if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
|
|
699
|
+
const parentRequires = parentMeta && parentMeta.requires;
|
|
700
|
+
if (parentRequires) {
|
|
701
|
+
setInitialParentType(parentId, parentMeta.initialCommonJSType);
|
|
702
|
+
await Promise.all(
|
|
703
|
+
parentRequires.map(({ resolved, isConditional }) =>
|
|
704
|
+
analyzeRequiredModule(parentId, resolved, isConditional, this.load)
|
|
705
|
+
)
|
|
706
|
+
);
|
|
707
|
+
if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
|
|
663
708
|
return true;
|
|
664
709
|
}
|
|
710
|
+
for (const {
|
|
711
|
+
resolved: { id }
|
|
712
|
+
} of parentRequires) {
|
|
713
|
+
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
// Now that we decided to go with the cached copy, neither the parent
|
|
718
|
+
// module nor any of its children may change types anymore
|
|
719
|
+
fullyAnalyzedModules[parentId] = true;
|
|
720
|
+
for (const {
|
|
721
|
+
resolved: { id }
|
|
722
|
+
} of parentRequires) {
|
|
723
|
+
fullyAnalyzedModules[id] = true;
|
|
724
|
+
}
|
|
665
725
|
}
|
|
666
|
-
|
|
726
|
+
const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
|
|
727
|
+
return (
|
|
728
|
+
await Promise.all(
|
|
729
|
+
Object.keys(resolvedSources)
|
|
730
|
+
.map((source) => resolvedSources[source])
|
|
731
|
+
.filter(({ id, external }) => !(external || parentRequireSet.has(id)))
|
|
732
|
+
.map(async (resolved) => {
|
|
733
|
+
if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
|
|
734
|
+
return (
|
|
735
|
+
(await getTypeForImportedModule(
|
|
736
|
+
(await this.load({ id: resolved.id })).meta.commonjs.resolved,
|
|
737
|
+
this.load
|
|
738
|
+
)) !== IS_WRAPPED_COMMONJS
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
|
|
742
|
+
})
|
|
743
|
+
)
|
|
744
|
+
).some((shouldTransform) => shouldTransform);
|
|
667
745
|
},
|
|
668
746
|
/* eslint-disable no-param-reassign */
|
|
669
747
|
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
@@ -676,16 +754,20 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
676
754
|
parentMeta.requires = [];
|
|
677
755
|
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
678
756
|
setInitialParentType(parentId, isParentCommonJS);
|
|
757
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
|
|
758
|
+
currentlyResolving.set(parentId, currentlyResolvingForParent);
|
|
679
759
|
const requireTargets = await Promise.all(
|
|
680
760
|
sources.map(async ({ source, isConditional }) => {
|
|
681
761
|
// Never analyze or proxy internal modules
|
|
682
762
|
if (source.startsWith('\0')) {
|
|
683
763
|
return { id: source, allowProxy: false };
|
|
684
764
|
}
|
|
765
|
+
currentlyResolvingForParent.add(source);
|
|
685
766
|
const resolved =
|
|
686
767
|
(await rollupContext.resolve(source, parentId, {
|
|
687
768
|
custom: { 'node-resolve': { isRequire: true } }
|
|
688
769
|
})) || resolveExtensions(source, parentId, extensions);
|
|
770
|
+
currentlyResolvingForParent.delete(source);
|
|
689
771
|
if (!resolved) {
|
|
690
772
|
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
691
773
|
}
|
|
@@ -694,16 +776,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
694
776
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
695
777
|
}
|
|
696
778
|
parentMeta.requires.push({ resolved, isConditional });
|
|
697
|
-
await
|
|
779
|
+
await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
|
|
698
780
|
return { id: childId, allowProxy: true };
|
|
699
781
|
})
|
|
700
782
|
);
|
|
701
783
|
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
784
|
+
fullyAnalyzedModules[parentId] = true;
|
|
702
785
|
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
703
786
|
// eslint-disable-next-line no-multi-assign
|
|
704
787
|
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
705
788
|
dependencyId
|
|
706
789
|
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
790
|
+
fullyAnalyzedModules[dependencyId] = true;
|
|
707
791
|
return {
|
|
708
792
|
source: sources[index].source,
|
|
709
793
|
id: allowProxy
|
|
@@ -714,6 +798,10 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
714
798
|
isCommonJS
|
|
715
799
|
};
|
|
716
800
|
});
|
|
801
|
+
},
|
|
802
|
+
isCurrentlyResolving(source, parentId) {
|
|
803
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId);
|
|
804
|
+
return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
|
|
717
805
|
}
|
|
718
806
|
};
|
|
719
807
|
}
|
|
@@ -1157,7 +1245,7 @@ function getRequireStringArg(node) {
|
|
|
1157
1245
|
function getRequireHandlers() {
|
|
1158
1246
|
const requireExpressions = [];
|
|
1159
1247
|
|
|
1160
|
-
function
|
|
1248
|
+
function addRequireExpression(
|
|
1161
1249
|
sourceId,
|
|
1162
1250
|
node,
|
|
1163
1251
|
scope,
|
|
@@ -1237,7 +1325,7 @@ function getRequireHandlers() {
|
|
|
1237
1325
|
}
|
|
1238
1326
|
|
|
1239
1327
|
return {
|
|
1240
|
-
|
|
1328
|
+
addRequireExpression,
|
|
1241
1329
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1242
1330
|
};
|
|
1243
1331
|
}
|
|
@@ -1356,6 +1444,7 @@ async function transformCommonjs(
|
|
|
1356
1444
|
let programDepth = 0;
|
|
1357
1445
|
let currentTryBlockEnd = null;
|
|
1358
1446
|
let shouldWrap = false;
|
|
1447
|
+
let reexports = false;
|
|
1359
1448
|
|
|
1360
1449
|
const globals = new Set();
|
|
1361
1450
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1363,7 +1452,7 @@ async function transformCommonjs(
|
|
|
1363
1452
|
// unconditional require elsewhere.
|
|
1364
1453
|
let currentConditionalNodeEnd = null;
|
|
1365
1454
|
const conditionalNodes = new Set();
|
|
1366
|
-
const {
|
|
1455
|
+
const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1367
1456
|
|
|
1368
1457
|
// See which names are assigned to. This is necessary to prevent
|
|
1369
1458
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
@@ -1380,6 +1469,7 @@ async function transformCommonjs(
|
|
|
1380
1469
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1381
1470
|
const replacedGlobal = [];
|
|
1382
1471
|
const replacedDynamicRequires = [];
|
|
1472
|
+
const importedVariables = new Set();
|
|
1383
1473
|
|
|
1384
1474
|
walk(ast, {
|
|
1385
1475
|
enter(node, parent) {
|
|
@@ -1435,8 +1525,9 @@ async function transformCommonjs(
|
|
|
1435
1525
|
if (hasDefineEsmProperty(node.right)) {
|
|
1436
1526
|
shouldWrap = true;
|
|
1437
1527
|
}
|
|
1438
|
-
} else if (
|
|
1528
|
+
} else if (isRequireExpression(node.right, scope)) {
|
|
1439
1529
|
shouldWrap = true;
|
|
1530
|
+
reexports = true;
|
|
1440
1531
|
}
|
|
1441
1532
|
}
|
|
1442
1533
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
@@ -1493,6 +1584,11 @@ async function transformCommonjs(
|
|
|
1493
1584
|
}
|
|
1494
1585
|
|
|
1495
1586
|
if (!isRequireExpression(node, scope)) {
|
|
1587
|
+
const keypath = getKeypath(node.callee);
|
|
1588
|
+
if (keypath && importedVariables.has(keypath.name)) {
|
|
1589
|
+
// Heuristic to deoptimize requires after a required function has been called
|
|
1590
|
+
currentConditionalNodeEnd = Infinity;
|
|
1591
|
+
}
|
|
1496
1592
|
return;
|
|
1497
1593
|
}
|
|
1498
1594
|
|
|
@@ -1512,15 +1608,28 @@ async function transformCommonjs(
|
|
|
1512
1608
|
const requireStringArg = getRequireStringArg(node);
|
|
1513
1609
|
if (!ignoreRequire(requireStringArg)) {
|
|
1514
1610
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1515
|
-
|
|
1611
|
+
const toBeRemoved =
|
|
1612
|
+
parent.type === 'ExpressionStatement' &&
|
|
1613
|
+
(!currentConditionalNodeEnd ||
|
|
1614
|
+
// We should completely remove requires directly in a try-catch
|
|
1615
|
+
// so that Rollup can remove up the try-catch
|
|
1616
|
+
(currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
|
|
1617
|
+
? parent
|
|
1618
|
+
: node;
|
|
1619
|
+
addRequireExpression(
|
|
1516
1620
|
requireStringArg,
|
|
1517
1621
|
node,
|
|
1518
1622
|
scope,
|
|
1519
1623
|
usesReturnValue,
|
|
1520
1624
|
currentTryBlockEnd !== null,
|
|
1521
1625
|
currentConditionalNodeEnd !== null,
|
|
1522
|
-
|
|
1626
|
+
toBeRemoved
|
|
1523
1627
|
);
|
|
1628
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
1629
|
+
for (const name of extractAssignedNames(parent.id)) {
|
|
1630
|
+
importedVariables.add(name);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1524
1633
|
}
|
|
1525
1634
|
return;
|
|
1526
1635
|
}
|
|
@@ -1710,12 +1819,15 @@ async function transformCommonjs(
|
|
|
1710
1819
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1711
1820
|
const detectWrappedDefault =
|
|
1712
1821
|
shouldWrap &&
|
|
1713
|
-
(
|
|
1822
|
+
(reexports ||
|
|
1823
|
+
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1824
|
+
code.indexOf('__esModule') >= 0);
|
|
1714
1825
|
|
|
1715
1826
|
if (
|
|
1716
1827
|
!(
|
|
1717
1828
|
shouldWrap ||
|
|
1718
1829
|
isRequired ||
|
|
1830
|
+
needsRequireWrapper ||
|
|
1719
1831
|
uses.module ||
|
|
1720
1832
|
uses.exports ||
|
|
1721
1833
|
uses.require ||
|
|
@@ -1733,7 +1845,9 @@ async function transformCommonjs(
|
|
|
1733
1845
|
magicString.remove(0, commentEnd).trim();
|
|
1734
1846
|
}
|
|
1735
1847
|
|
|
1736
|
-
const exportMode =
|
|
1848
|
+
const exportMode = isEsModule
|
|
1849
|
+
? 'none'
|
|
1850
|
+
: shouldWrap
|
|
1737
1851
|
? uses.module
|
|
1738
1852
|
? 'module'
|
|
1739
1853
|
: 'exports'
|
|
@@ -1826,6 +1940,7 @@ function commonjs(options = {}) {
|
|
|
1826
1940
|
ignoreGlobal,
|
|
1827
1941
|
ignoreDynamicRequires,
|
|
1828
1942
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1943
|
+
defaultIsModuleExports: defaultIsModuleExportsOption,
|
|
1829
1944
|
esmExternals
|
|
1830
1945
|
} = options;
|
|
1831
1946
|
const extensions = options.extensions || ['.js'];
|
|
@@ -1845,8 +1960,11 @@ function commonjs(options = {}) {
|
|
|
1845
1960
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1846
1961
|
: () => esmExternals;
|
|
1847
1962
|
|
|
1848
|
-
const
|
|
1849
|
-
typeof
|
|
1963
|
+
const getDefaultIsModuleExports =
|
|
1964
|
+
typeof defaultIsModuleExportsOption === 'function'
|
|
1965
|
+
? defaultIsModuleExportsOption
|
|
1966
|
+
: () =>
|
|
1967
|
+
typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
|
|
1850
1968
|
|
|
1851
1969
|
const dynamicRequireRoot =
|
|
1852
1970
|
typeof options.dynamicRequireRoot === 'string'
|
|
@@ -1881,7 +1999,7 @@ function commonjs(options = {}) {
|
|
|
1881
1999
|
};
|
|
1882
2000
|
};
|
|
1883
2001
|
|
|
1884
|
-
const resolveId = getResolveId(extensions);
|
|
2002
|
+
const { currentlyResolving, resolveId } = getResolveId(extensions);
|
|
1885
2003
|
|
|
1886
2004
|
const sourceMap = options.sourceMap !== false;
|
|
1887
2005
|
|
|
@@ -1946,7 +2064,7 @@ function commonjs(options = {}) {
|
|
|
1946
2064
|
dynamicRequireModules,
|
|
1947
2065
|
commonDir,
|
|
1948
2066
|
ast,
|
|
1949
|
-
|
|
2067
|
+
getDefaultIsModuleExports(id),
|
|
1950
2068
|
needsRequireWrapper,
|
|
1951
2069
|
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1952
2070
|
requireResolver.isRequiredId(id),
|
|
@@ -1986,7 +2104,11 @@ function commonjs(options = {}) {
|
|
|
1986
2104
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
1987
2105
|
);
|
|
1988
2106
|
}
|
|
1989
|
-
requireResolver = getRequireResolver(
|
|
2107
|
+
requireResolver = getRequireResolver(
|
|
2108
|
+
extensions,
|
|
2109
|
+
detectCyclesAndConditional,
|
|
2110
|
+
currentlyResolving
|
|
2111
|
+
);
|
|
1990
2112
|
},
|
|
1991
2113
|
|
|
1992
2114
|
buildEnd() {
|
|
@@ -2042,15 +2164,13 @@ function commonjs(options = {}) {
|
|
|
2042
2164
|
|
|
2043
2165
|
// entry suffix is just appended to not mess up relative external resolution
|
|
2044
2166
|
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
defaultIsModuleExports,
|
|
2048
|
-
this.getModuleInfo
|
|
2049
|
-
);
|
|
2167
|
+
const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
|
|
2168
|
+
return getEntryProxy(acutalId, getDefaultIsModuleExports(acutalId), this.getModuleInfo);
|
|
2050
2169
|
}
|
|
2051
2170
|
|
|
2052
2171
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2053
|
-
|
|
2172
|
+
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2173
|
+
return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
|
|
2054
2174
|
}
|
|
2055
2175
|
|
|
2056
2176
|
if (id === DYNAMIC_MODULES_ID) {
|