@rollup/plugin-commonjs 22.0.0-9 → 22.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/README.md +3 -1
- package/dist/cjs/index.js +263 -127
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +263 -127
- 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/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.2";
|
|
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
|
|
|
@@ -499,85 +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
|
-
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
|
-
}
|
|
501
|
+
function getResolveId(extensions, isPossibleCjsId) {
|
|
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
|
+
!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
|
+
}
|
|
562
615
|
return resolved;
|
|
563
616
|
}
|
|
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
617
|
};
|
|
578
618
|
}
|
|
579
619
|
|
|
580
|
-
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
620
|
+
function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
|
|
581
621
|
const knownCjsModuleTypes = Object.create(null);
|
|
582
622
|
const requiredIds = Object.create(null);
|
|
583
623
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -597,6 +637,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
597
637
|
return false;
|
|
598
638
|
};
|
|
599
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.
|
|
600
643
|
const fullyAnalyzedModules = Object.create(null);
|
|
601
644
|
|
|
602
645
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
@@ -604,7 +647,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
604
647
|
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
605
648
|
return knownType;
|
|
606
649
|
}
|
|
607
|
-
fullyAnalyzedModules[id] = true;
|
|
608
650
|
if (isCyclic(id)) {
|
|
609
651
|
return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
|
|
610
652
|
}
|
|
@@ -612,13 +654,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
612
654
|
};
|
|
613
655
|
|
|
614
656
|
const setInitialParentType = (id, initialCommonJSType) => {
|
|
615
|
-
//
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
knownCjsModuleTypes[id] =
|
|
620
|
-
? knownCjsModuleTypes[id]
|
|
621
|
-
: initialCommonJSType;
|
|
657
|
+
// Fully analyzed modules may never change type
|
|
658
|
+
if (fullyAnalyzedModules[id]) {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
knownCjsModuleTypes[id] = initialCommonJSType;
|
|
622
662
|
if (
|
|
623
663
|
detectCyclesAndConditional &&
|
|
624
664
|
knownCjsModuleTypes[id] === true &&
|
|
@@ -629,7 +669,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
629
669
|
}
|
|
630
670
|
};
|
|
631
671
|
|
|
632
|
-
const
|
|
672
|
+
const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
|
|
633
673
|
const childId = resolved.id;
|
|
634
674
|
requiredIds[childId] = true;
|
|
635
675
|
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
@@ -638,41 +678,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
638
678
|
|
|
639
679
|
getDependencies(parentId).add(childId);
|
|
640
680
|
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
|
|
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.
|
|
644
685
|
await loadModule(resolved);
|
|
645
686
|
}
|
|
646
687
|
};
|
|
647
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
|
+
|
|
648
700
|
return {
|
|
649
701
|
getWrappedIds: () =>
|
|
650
702
|
Object.keys(knownCjsModuleTypes).filter(
|
|
651
703
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
652
704
|
),
|
|
653
705
|
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]) {
|
|
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) {
|
|
672
723
|
return true;
|
|
673
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
|
+
}
|
|
674
740
|
}
|
|
675
|
-
|
|
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);
|
|
676
760
|
},
|
|
677
761
|
/* eslint-disable no-param-reassign */
|
|
678
762
|
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
@@ -685,16 +769,20 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
685
769
|
parentMeta.requires = [];
|
|
686
770
|
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
687
771
|
setInitialParentType(parentId, isParentCommonJS);
|
|
772
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
|
|
773
|
+
currentlyResolving.set(parentId, currentlyResolvingForParent);
|
|
688
774
|
const requireTargets = await Promise.all(
|
|
689
775
|
sources.map(async ({ source, isConditional }) => {
|
|
690
776
|
// Never analyze or proxy internal modules
|
|
691
777
|
if (source.startsWith('\0')) {
|
|
692
778
|
return { id: source, allowProxy: false };
|
|
693
779
|
}
|
|
780
|
+
currentlyResolvingForParent.add(source);
|
|
694
781
|
const resolved =
|
|
695
782
|
(await rollupContext.resolve(source, parentId, {
|
|
696
783
|
custom: { 'node-resolve': { isRequire: true } }
|
|
697
784
|
})) || resolveExtensions(source, parentId, extensions);
|
|
785
|
+
currentlyResolvingForParent.delete(source);
|
|
698
786
|
if (!resolved) {
|
|
699
787
|
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
700
788
|
}
|
|
@@ -703,16 +791,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
703
791
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
704
792
|
}
|
|
705
793
|
parentMeta.requires.push({ resolved, isConditional });
|
|
706
|
-
await
|
|
794
|
+
await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
|
|
707
795
|
return { id: childId, allowProxy: true };
|
|
708
796
|
})
|
|
709
797
|
);
|
|
710
798
|
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
799
|
+
fullyAnalyzedModules[parentId] = true;
|
|
711
800
|
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
712
801
|
// eslint-disable-next-line no-multi-assign
|
|
713
802
|
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
714
803
|
dependencyId
|
|
715
804
|
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
805
|
+
fullyAnalyzedModules[dependencyId] = true;
|
|
716
806
|
return {
|
|
717
807
|
source: sources[index].source,
|
|
718
808
|
id: allowProxy
|
|
@@ -723,6 +813,10 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
723
813
|
isCommonJS
|
|
724
814
|
};
|
|
725
815
|
});
|
|
816
|
+
},
|
|
817
|
+
isCurrentlyResolving(source, parentId) {
|
|
818
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId);
|
|
819
|
+
return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
|
|
726
820
|
}
|
|
727
821
|
};
|
|
728
822
|
}
|
|
@@ -880,7 +974,7 @@ function hasDefineEsmProperty(node) {
|
|
|
880
974
|
});
|
|
881
975
|
}
|
|
882
976
|
|
|
883
|
-
function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
977
|
+
function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
|
|
884
978
|
const args = [];
|
|
885
979
|
const passedArgs = [];
|
|
886
980
|
if (uses.module) {
|
|
@@ -893,7 +987,7 @@ function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
|
893
987
|
}
|
|
894
988
|
magicString
|
|
895
989
|
.trim()
|
|
896
|
-
.indent('\t')
|
|
990
|
+
.indent('\t', { exclude: indentExclusionRanges })
|
|
897
991
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
898
992
|
.append(`\n} (${passedArgs.join(', ')}));`);
|
|
899
993
|
}
|
|
@@ -1166,7 +1260,7 @@ function getRequireStringArg(node) {
|
|
|
1166
1260
|
function getRequireHandlers() {
|
|
1167
1261
|
const requireExpressions = [];
|
|
1168
1262
|
|
|
1169
|
-
function
|
|
1263
|
+
function addRequireExpression(
|
|
1170
1264
|
sourceId,
|
|
1171
1265
|
node,
|
|
1172
1266
|
scope,
|
|
@@ -1246,7 +1340,7 @@ function getRequireHandlers() {
|
|
|
1246
1340
|
}
|
|
1247
1341
|
|
|
1248
1342
|
return {
|
|
1249
|
-
|
|
1343
|
+
addRequireExpression,
|
|
1250
1344
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1251
1345
|
};
|
|
1252
1346
|
}
|
|
@@ -1365,6 +1459,7 @@ async function transformCommonjs(
|
|
|
1365
1459
|
let programDepth = 0;
|
|
1366
1460
|
let currentTryBlockEnd = null;
|
|
1367
1461
|
let shouldWrap = false;
|
|
1462
|
+
let reexports = false;
|
|
1368
1463
|
|
|
1369
1464
|
const globals = new Set();
|
|
1370
1465
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1372,7 +1467,7 @@ async function transformCommonjs(
|
|
|
1372
1467
|
// unconditional require elsewhere.
|
|
1373
1468
|
let currentConditionalNodeEnd = null;
|
|
1374
1469
|
const conditionalNodes = new Set();
|
|
1375
|
-
const {
|
|
1470
|
+
const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1376
1471
|
|
|
1377
1472
|
// See which names are assigned to. This is necessary to prevent
|
|
1378
1473
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
@@ -1389,6 +1484,8 @@ async function transformCommonjs(
|
|
|
1389
1484
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1390
1485
|
const replacedGlobal = [];
|
|
1391
1486
|
const replacedDynamicRequires = [];
|
|
1487
|
+
const importedVariables = new Set();
|
|
1488
|
+
const indentExclusionRanges = [];
|
|
1392
1489
|
|
|
1393
1490
|
estreeWalker.walk(ast, {
|
|
1394
1491
|
enter(node, parent) {
|
|
@@ -1444,8 +1541,9 @@ async function transformCommonjs(
|
|
|
1444
1541
|
if (hasDefineEsmProperty(node.right)) {
|
|
1445
1542
|
shouldWrap = true;
|
|
1446
1543
|
}
|
|
1447
|
-
} else if (
|
|
1544
|
+
} else if (isRequireExpression(node.right, scope)) {
|
|
1448
1545
|
shouldWrap = true;
|
|
1546
|
+
reexports = true;
|
|
1449
1547
|
}
|
|
1450
1548
|
}
|
|
1451
1549
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
@@ -1502,6 +1600,11 @@ async function transformCommonjs(
|
|
|
1502
1600
|
}
|
|
1503
1601
|
|
|
1504
1602
|
if (!isRequireExpression(node, scope)) {
|
|
1603
|
+
const keypath = getKeypath(node.callee);
|
|
1604
|
+
if (keypath && importedVariables.has(keypath.name)) {
|
|
1605
|
+
// Heuristic to deoptimize requires after a required function has been called
|
|
1606
|
+
currentConditionalNodeEnd = Infinity;
|
|
1607
|
+
}
|
|
1505
1608
|
return;
|
|
1506
1609
|
}
|
|
1507
1610
|
|
|
@@ -1521,15 +1624,28 @@ async function transformCommonjs(
|
|
|
1521
1624
|
const requireStringArg = getRequireStringArg(node);
|
|
1522
1625
|
if (!ignoreRequire(requireStringArg)) {
|
|
1523
1626
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1524
|
-
|
|
1627
|
+
const toBeRemoved =
|
|
1628
|
+
parent.type === 'ExpressionStatement' &&
|
|
1629
|
+
(!currentConditionalNodeEnd ||
|
|
1630
|
+
// We should completely remove requires directly in a try-catch
|
|
1631
|
+
// so that Rollup can remove up the try-catch
|
|
1632
|
+
(currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
|
|
1633
|
+
? parent
|
|
1634
|
+
: node;
|
|
1635
|
+
addRequireExpression(
|
|
1525
1636
|
requireStringArg,
|
|
1526
1637
|
node,
|
|
1527
1638
|
scope,
|
|
1528
1639
|
usesReturnValue,
|
|
1529
1640
|
currentTryBlockEnd !== null,
|
|
1530
1641
|
currentConditionalNodeEnd !== null,
|
|
1531
|
-
|
|
1642
|
+
toBeRemoved
|
|
1532
1643
|
);
|
|
1644
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
1645
|
+
for (const name of pluginutils.extractAssignedNames(parent.id)) {
|
|
1646
|
+
importedVariables.add(name);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1533
1649
|
}
|
|
1534
1650
|
return;
|
|
1535
1651
|
}
|
|
@@ -1668,6 +1784,11 @@ async function transformCommonjs(
|
|
|
1668
1784
|
if (!scope.parent) {
|
|
1669
1785
|
topLevelDeclarations.push(node);
|
|
1670
1786
|
}
|
|
1787
|
+
return;
|
|
1788
|
+
case 'TemplateElement':
|
|
1789
|
+
if (node.value.raw.includes('\n')) {
|
|
1790
|
+
indentExclusionRanges.push([node.start, node.end]);
|
|
1791
|
+
}
|
|
1671
1792
|
}
|
|
1672
1793
|
},
|
|
1673
1794
|
|
|
@@ -1719,12 +1840,15 @@ async function transformCommonjs(
|
|
|
1719
1840
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1720
1841
|
const detectWrappedDefault =
|
|
1721
1842
|
shouldWrap &&
|
|
1722
|
-
(
|
|
1843
|
+
(reexports ||
|
|
1844
|
+
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1845
|
+
code.indexOf('__esModule') >= 0);
|
|
1723
1846
|
|
|
1724
1847
|
if (
|
|
1725
1848
|
!(
|
|
1726
1849
|
shouldWrap ||
|
|
1727
1850
|
isRequired ||
|
|
1851
|
+
needsRequireWrapper ||
|
|
1728
1852
|
uses.module ||
|
|
1729
1853
|
uses.exports ||
|
|
1730
1854
|
uses.require ||
|
|
@@ -1742,7 +1866,9 @@ async function transformCommonjs(
|
|
|
1742
1866
|
magicString.remove(0, commentEnd).trim();
|
|
1743
1867
|
}
|
|
1744
1868
|
|
|
1745
|
-
const exportMode =
|
|
1869
|
+
const exportMode = isEsModule
|
|
1870
|
+
? 'none'
|
|
1871
|
+
: shouldWrap
|
|
1746
1872
|
? uses.module
|
|
1747
1873
|
? 'module'
|
|
1748
1874
|
: 'exports'
|
|
@@ -1795,11 +1921,13 @@ async function transformCommonjs(
|
|
|
1795
1921
|
);
|
|
1796
1922
|
|
|
1797
1923
|
if (shouldWrap) {
|
|
1798
|
-
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1924
|
+
wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges);
|
|
1799
1925
|
}
|
|
1800
1926
|
|
|
1801
1927
|
if (usesRequireWrapper) {
|
|
1802
|
-
magicString.trim().indent('\t'
|
|
1928
|
+
magicString.trim().indent('\t', {
|
|
1929
|
+
exclude: indentExclusionRanges
|
|
1930
|
+
});
|
|
1803
1931
|
magicString.prepend(
|
|
1804
1932
|
`var ${isRequiredName};
|
|
1805
1933
|
|
|
@@ -1835,10 +1963,16 @@ function commonjs(options = {}) {
|
|
|
1835
1963
|
ignoreGlobal,
|
|
1836
1964
|
ignoreDynamicRequires,
|
|
1837
1965
|
requireReturnsDefault: requireReturnsDefaultOption,
|
|
1966
|
+
defaultIsModuleExports: defaultIsModuleExportsOption,
|
|
1838
1967
|
esmExternals
|
|
1839
1968
|
} = options;
|
|
1840
1969
|
const extensions = options.extensions || ['.js'];
|
|
1841
1970
|
const filter = pluginutils.createFilter(options.include, options.exclude);
|
|
1971
|
+
const isPossibleCjsId = (id) => {
|
|
1972
|
+
const extName = path.extname(id);
|
|
1973
|
+
return extName === '.cjs' || (extensions.includes(extName) && filter(id));
|
|
1974
|
+
};
|
|
1975
|
+
|
|
1842
1976
|
const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
|
|
1843
1977
|
|
|
1844
1978
|
const getRequireReturnsDefault =
|
|
@@ -1854,8 +1988,11 @@ function commonjs(options = {}) {
|
|
|
1854
1988
|
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
|
|
1855
1989
|
: () => esmExternals;
|
|
1856
1990
|
|
|
1857
|
-
const
|
|
1858
|
-
typeof
|
|
1991
|
+
const getDefaultIsModuleExports =
|
|
1992
|
+
typeof defaultIsModuleExportsOption === 'function'
|
|
1993
|
+
? defaultIsModuleExportsOption
|
|
1994
|
+
: () =>
|
|
1995
|
+
typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
|
|
1859
1996
|
|
|
1860
1997
|
const dynamicRequireRoot =
|
|
1861
1998
|
typeof options.dynamicRequireRoot === 'string'
|
|
@@ -1890,7 +2027,7 @@ function commonjs(options = {}) {
|
|
|
1890
2027
|
};
|
|
1891
2028
|
};
|
|
1892
2029
|
|
|
1893
|
-
const resolveId = getResolveId(extensions);
|
|
2030
|
+
const { currentlyResolving, resolveId } = getResolveId(extensions, isPossibleCjsId);
|
|
1894
2031
|
|
|
1895
2032
|
const sourceMap = options.sourceMap !== false;
|
|
1896
2033
|
|
|
@@ -1955,7 +2092,7 @@ function commonjs(options = {}) {
|
|
|
1955
2092
|
dynamicRequireModules,
|
|
1956
2093
|
commonDir,
|
|
1957
2094
|
ast,
|
|
1958
|
-
|
|
2095
|
+
getDefaultIsModuleExports(id),
|
|
1959
2096
|
needsRequireWrapper,
|
|
1960
2097
|
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1961
2098
|
requireResolver.isRequiredId(id),
|
|
@@ -1995,7 +2132,11 @@ function commonjs(options = {}) {
|
|
|
1995
2132
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
1996
2133
|
);
|
|
1997
2134
|
}
|
|
1998
|
-
requireResolver = getRequireResolver(
|
|
2135
|
+
requireResolver = getRequireResolver(
|
|
2136
|
+
extensions,
|
|
2137
|
+
detectCyclesAndConditional,
|
|
2138
|
+
currentlyResolving
|
|
2139
|
+
);
|
|
1999
2140
|
},
|
|
2000
2141
|
|
|
2001
2142
|
buildEnd() {
|
|
@@ -2051,15 +2192,13 @@ function commonjs(options = {}) {
|
|
|
2051
2192
|
|
|
2052
2193
|
// entry suffix is just appended to not mess up relative external resolution
|
|
2053
2194
|
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
defaultIsModuleExports,
|
|
2057
|
-
this.getModuleInfo
|
|
2058
|
-
);
|
|
2195
|
+
const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
|
|
2196
|
+
return getEntryProxy(acutalId, getDefaultIsModuleExports(acutalId), this.getModuleInfo);
|
|
2059
2197
|
}
|
|
2060
2198
|
|
|
2061
2199
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2062
|
-
|
|
2200
|
+
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2201
|
+
return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
|
|
2063
2202
|
}
|
|
2064
2203
|
|
|
2065
2204
|
if (id === DYNAMIC_MODULES_ID) {
|
|
@@ -2084,10 +2223,7 @@ function commonjs(options = {}) {
|
|
|
2084
2223
|
},
|
|
2085
2224
|
|
|
2086
2225
|
transform(code, id) {
|
|
2087
|
-
|
|
2088
|
-
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
2089
|
-
return null;
|
|
2090
|
-
}
|
|
2226
|
+
if (!isPossibleCjsId(id)) return null;
|
|
2091
2227
|
|
|
2092
2228
|
try {
|
|
2093
2229
|
return transformAndCheckExports.call(this, code, id);
|