@rollup/plugin-commonjs 22.0.0-7 → 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 +244 -120
- package/dist/cjs/index.js.map +1 -1
- package/dist/es/index.js +244 -120
- 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) {
|
|
@@ -421,11 +421,11 @@ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
|
421
421
|
return `export { default } from ${JSON.stringify(id)};`;
|
|
422
422
|
}
|
|
423
423
|
|
|
424
|
-
|
|
424
|
+
function getEntryProxy(id, defaultIsModuleExports, getModuleInfo) {
|
|
425
425
|
const {
|
|
426
426
|
meta: { commonjs: commonjsMeta },
|
|
427
427
|
hasDefaultExport
|
|
428
|
-
} =
|
|
428
|
+
} = getModuleInfo(id);
|
|
429
429
|
if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
|
|
430
430
|
const stringifiedId = JSON.stringify(id);
|
|
431
431
|
let code = `export * from ${stringifiedId};`;
|
|
@@ -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,86 +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
|
-
if (
|
|
497
|
-
resolveOptions.custom &&
|
|
498
|
-
resolveOptions.custom['node-resolve'] &&
|
|
499
|
-
resolveOptions.custom['node-resolve'].isRequire
|
|
500
|
-
) {
|
|
501
|
-
return null;
|
|
502
|
-
}
|
|
503
|
-
if (isWrappedId(importee, WRAPPED_SUFFIX)) {
|
|
504
|
-
return unwrapId(importee, WRAPPED_SUFFIX);
|
|
505
|
-
}
|
|
493
|
+
const currentlyResolving = new Map();
|
|
506
494
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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
|
+
}
|
|
519
527
|
|
|
520
|
-
if (importer) {
|
|
521
528
|
if (
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
isWrappedId(
|
|
525
|
-
isWrappedId(
|
|
526
|
-
|
|
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
|
|
527
537
|
) {
|
|
528
538
|
return importee;
|
|
529
539
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
if (
|
|
533
|
-
|
|
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 };
|
|
534
564
|
}
|
|
535
|
-
// For other external imports, we need to make sure they are handled as external
|
|
536
|
-
return { id: importee, external: true };
|
|
537
565
|
}
|
|
538
|
-
}
|
|
539
566
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
// If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
|
|
545
|
-
// if that is the case, we need to add a proxy.
|
|
546
|
-
const customOptions = resolveOptions.custom;
|
|
547
|
-
|
|
548
|
-
// If this is a require, we do not need a proxy
|
|
549
|
-
if (customOptions && customOptions['node-resolve'] && customOptions['node-resolve'].isRequire) {
|
|
550
|
-
return null;
|
|
551
|
-
}
|
|
567
|
+
if (importee.startsWith('\0')) {
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
552
570
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
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
|
+
}
|
|
557
600
|
return resolved;
|
|
558
601
|
}
|
|
559
|
-
if (resolveOptions.isEntry) {
|
|
560
|
-
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
561
|
-
return resolved.id + ENTRY_SUFFIX;
|
|
562
|
-
}
|
|
563
|
-
const {
|
|
564
|
-
meta: { commonjs: commonjsMeta }
|
|
565
|
-
} = await this.load(resolved);
|
|
566
|
-
if (commonjsMeta && commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
567
|
-
return wrapId(resolved.id, ES_IMPORT_SUFFIX);
|
|
568
|
-
}
|
|
569
|
-
return resolved;
|
|
570
602
|
};
|
|
571
603
|
}
|
|
572
604
|
|
|
573
|
-
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
605
|
+
function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
|
|
574
606
|
const knownCjsModuleTypes = Object.create(null);
|
|
575
607
|
const requiredIds = Object.create(null);
|
|
576
608
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -590,6 +622,9 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
590
622
|
return false;
|
|
591
623
|
};
|
|
592
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.
|
|
593
628
|
const fullyAnalyzedModules = Object.create(null);
|
|
594
629
|
|
|
595
630
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
@@ -597,7 +632,6 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
597
632
|
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
598
633
|
return knownType;
|
|
599
634
|
}
|
|
600
|
-
fullyAnalyzedModules[id] = true;
|
|
601
635
|
if (isCyclic(id)) {
|
|
602
636
|
return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
|
|
603
637
|
}
|
|
@@ -605,13 +639,11 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
605
639
|
};
|
|
606
640
|
|
|
607
641
|
const setInitialParentType = (id, initialCommonJSType) => {
|
|
608
|
-
//
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
knownCjsModuleTypes[id] =
|
|
613
|
-
? knownCjsModuleTypes[id]
|
|
614
|
-
: initialCommonJSType;
|
|
642
|
+
// Fully analyzed modules may never change type
|
|
643
|
+
if (fullyAnalyzedModules[id]) {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
knownCjsModuleTypes[id] = initialCommonJSType;
|
|
615
647
|
if (
|
|
616
648
|
detectCyclesAndConditional &&
|
|
617
649
|
knownCjsModuleTypes[id] === true &&
|
|
@@ -622,7 +654,7 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
622
654
|
}
|
|
623
655
|
};
|
|
624
656
|
|
|
625
|
-
const
|
|
657
|
+
const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
|
|
626
658
|
const childId = resolved.id;
|
|
627
659
|
requiredIds[childId] = true;
|
|
628
660
|
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
@@ -631,41 +663,85 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
631
663
|
|
|
632
664
|
getDependencies(parentId).add(childId);
|
|
633
665
|
if (!isCyclic(childId)) {
|
|
634
|
-
// This makes sure the current transform handler waits for all direct
|
|
635
|
-
// loaded and transformed and therefore for all
|
|
636
|
-
// 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.
|
|
637
670
|
await loadModule(resolved);
|
|
638
671
|
}
|
|
639
672
|
};
|
|
640
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
|
+
|
|
641
685
|
return {
|
|
642
686
|
getWrappedIds: () =>
|
|
643
687
|
Object.keys(knownCjsModuleTypes).filter(
|
|
644
688
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
645
689
|
),
|
|
646
690
|
isRequiredId: (id) => requiredIds[id],
|
|
647
|
-
async shouldTransformCachedModule({
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
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) {
|
|
665
708
|
return true;
|
|
666
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
|
+
}
|
|
667
725
|
}
|
|
668
|
-
|
|
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);
|
|
669
745
|
},
|
|
670
746
|
/* eslint-disable no-param-reassign */
|
|
671
747
|
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
@@ -678,16 +754,20 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
678
754
|
parentMeta.requires = [];
|
|
679
755
|
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
680
756
|
setInitialParentType(parentId, isParentCommonJS);
|
|
757
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
|
|
758
|
+
currentlyResolving.set(parentId, currentlyResolvingForParent);
|
|
681
759
|
const requireTargets = await Promise.all(
|
|
682
760
|
sources.map(async ({ source, isConditional }) => {
|
|
683
761
|
// Never analyze or proxy internal modules
|
|
684
762
|
if (source.startsWith('\0')) {
|
|
685
763
|
return { id: source, allowProxy: false };
|
|
686
764
|
}
|
|
765
|
+
currentlyResolvingForParent.add(source);
|
|
687
766
|
const resolved =
|
|
688
767
|
(await rollupContext.resolve(source, parentId, {
|
|
689
768
|
custom: { 'node-resolve': { isRequire: true } }
|
|
690
769
|
})) || resolveExtensions(source, parentId, extensions);
|
|
770
|
+
currentlyResolvingForParent.delete(source);
|
|
691
771
|
if (!resolved) {
|
|
692
772
|
return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
|
|
693
773
|
}
|
|
@@ -696,16 +776,18 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
696
776
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
697
777
|
}
|
|
698
778
|
parentMeta.requires.push({ resolved, isConditional });
|
|
699
|
-
await
|
|
779
|
+
await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
|
|
700
780
|
return { id: childId, allowProxy: true };
|
|
701
781
|
})
|
|
702
782
|
);
|
|
703
783
|
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
784
|
+
fullyAnalyzedModules[parentId] = true;
|
|
704
785
|
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
705
786
|
// eslint-disable-next-line no-multi-assign
|
|
706
787
|
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
707
788
|
dependencyId
|
|
708
789
|
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
790
|
+
fullyAnalyzedModules[dependencyId] = true;
|
|
709
791
|
return {
|
|
710
792
|
source: sources[index].source,
|
|
711
793
|
id: allowProxy
|
|
@@ -716,6 +798,10 @@ function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
|
716
798
|
isCommonJS
|
|
717
799
|
};
|
|
718
800
|
});
|
|
801
|
+
},
|
|
802
|
+
isCurrentlyResolving(source, parentId) {
|
|
803
|
+
const currentlyResolvingForParent = currentlyResolving.get(parentId);
|
|
804
|
+
return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
|
|
719
805
|
}
|
|
720
806
|
};
|
|
721
807
|
}
|
|
@@ -1159,7 +1245,7 @@ function getRequireStringArg(node) {
|
|
|
1159
1245
|
function getRequireHandlers() {
|
|
1160
1246
|
const requireExpressions = [];
|
|
1161
1247
|
|
|
1162
|
-
function
|
|
1248
|
+
function addRequireExpression(
|
|
1163
1249
|
sourceId,
|
|
1164
1250
|
node,
|
|
1165
1251
|
scope,
|
|
@@ -1239,7 +1325,7 @@ function getRequireHandlers() {
|
|
|
1239
1325
|
}
|
|
1240
1326
|
|
|
1241
1327
|
return {
|
|
1242
|
-
|
|
1328
|
+
addRequireExpression,
|
|
1243
1329
|
rewriteRequireExpressionsAndGetImportBlock
|
|
1244
1330
|
};
|
|
1245
1331
|
}
|
|
@@ -1358,6 +1444,7 @@ async function transformCommonjs(
|
|
|
1358
1444
|
let programDepth = 0;
|
|
1359
1445
|
let currentTryBlockEnd = null;
|
|
1360
1446
|
let shouldWrap = false;
|
|
1447
|
+
let reexports = false;
|
|
1361
1448
|
|
|
1362
1449
|
const globals = new Set();
|
|
1363
1450
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1365,7 +1452,7 @@ async function transformCommonjs(
|
|
|
1365
1452
|
// unconditional require elsewhere.
|
|
1366
1453
|
let currentConditionalNodeEnd = null;
|
|
1367
1454
|
const conditionalNodes = new Set();
|
|
1368
|
-
const {
|
|
1455
|
+
const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
|
|
1369
1456
|
|
|
1370
1457
|
// See which names are assigned to. This is necessary to prevent
|
|
1371
1458
|
// illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
|
|
@@ -1382,6 +1469,7 @@ async function transformCommonjs(
|
|
|
1382
1469
|
const topLevelDefineCompiledEsmExpressions = [];
|
|
1383
1470
|
const replacedGlobal = [];
|
|
1384
1471
|
const replacedDynamicRequires = [];
|
|
1472
|
+
const importedVariables = new Set();
|
|
1385
1473
|
|
|
1386
1474
|
walk(ast, {
|
|
1387
1475
|
enter(node, parent) {
|
|
@@ -1437,8 +1525,9 @@ async function transformCommonjs(
|
|
|
1437
1525
|
if (hasDefineEsmProperty(node.right)) {
|
|
1438
1526
|
shouldWrap = true;
|
|
1439
1527
|
}
|
|
1440
|
-
} else if (
|
|
1528
|
+
} else if (isRequireExpression(node.right, scope)) {
|
|
1441
1529
|
shouldWrap = true;
|
|
1530
|
+
reexports = true;
|
|
1442
1531
|
}
|
|
1443
1532
|
}
|
|
1444
1533
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
@@ -1495,6 +1584,11 @@ async function transformCommonjs(
|
|
|
1495
1584
|
}
|
|
1496
1585
|
|
|
1497
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
|
+
}
|
|
1498
1592
|
return;
|
|
1499
1593
|
}
|
|
1500
1594
|
|
|
@@ -1514,15 +1608,28 @@ async function transformCommonjs(
|
|
|
1514
1608
|
const requireStringArg = getRequireStringArg(node);
|
|
1515
1609
|
if (!ignoreRequire(requireStringArg)) {
|
|
1516
1610
|
const usesReturnValue = parent.type !== 'ExpressionStatement';
|
|
1517
|
-
|
|
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(
|
|
1518
1620
|
requireStringArg,
|
|
1519
1621
|
node,
|
|
1520
1622
|
scope,
|
|
1521
1623
|
usesReturnValue,
|
|
1522
1624
|
currentTryBlockEnd !== null,
|
|
1523
1625
|
currentConditionalNodeEnd !== null,
|
|
1524
|
-
|
|
1626
|
+
toBeRemoved
|
|
1525
1627
|
);
|
|
1628
|
+
if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
|
|
1629
|
+
for (const name of extractAssignedNames(parent.id)) {
|
|
1630
|
+
importedVariables.add(name);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1526
1633
|
}
|
|
1527
1634
|
return;
|
|
1528
1635
|
}
|
|
@@ -1712,12 +1819,15 @@ async function transformCommonjs(
|
|
|
1712
1819
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1713
1820
|
const detectWrappedDefault =
|
|
1714
1821
|
shouldWrap &&
|
|
1715
|
-
(
|
|
1822
|
+
(reexports ||
|
|
1823
|
+
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1824
|
+
code.indexOf('__esModule') >= 0);
|
|
1716
1825
|
|
|
1717
1826
|
if (
|
|
1718
1827
|
!(
|
|
1719
1828
|
shouldWrap ||
|
|
1720
1829
|
isRequired ||
|
|
1830
|
+
needsRequireWrapper ||
|
|
1721
1831
|
uses.module ||
|
|
1722
1832
|
uses.exports ||
|
|
1723
1833
|
uses.require ||
|
|
@@ -1735,7 +1845,9 @@ async function transformCommonjs(
|
|
|
1735
1845
|
magicString.remove(0, commentEnd).trim();
|
|
1736
1846
|
}
|
|
1737
1847
|
|
|
1738
|
-
const exportMode =
|
|
1848
|
+
const exportMode = isEsModule
|
|
1849
|
+
? 'none'
|
|
1850
|
+
: shouldWrap
|
|
1739
1851
|
? uses.module
|
|
1740
1852
|
? 'module'
|
|
1741
1853
|
: 'exports'
|
|
@@ -1821,11 +1933,14 @@ function ${requireName} () {
|
|
|
1821
1933
|
};
|
|
1822
1934
|
}
|
|
1823
1935
|
|
|
1936
|
+
const PLUGIN_NAME = 'commonjs';
|
|
1937
|
+
|
|
1824
1938
|
function commonjs(options = {}) {
|
|
1825
1939
|
const {
|
|
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),
|
|
@@ -1956,7 +2074,7 @@ function commonjs(options = {}) {
|
|
|
1956
2074
|
}
|
|
1957
2075
|
|
|
1958
2076
|
return {
|
|
1959
|
-
name:
|
|
2077
|
+
name: PLUGIN_NAME,
|
|
1960
2078
|
|
|
1961
2079
|
version,
|
|
1962
2080
|
|
|
@@ -1964,7 +2082,7 @@ function commonjs(options = {}) {
|
|
|
1964
2082
|
// We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
|
|
1965
2083
|
// do not prevent our plugin from resolving entry points ot proxies.
|
|
1966
2084
|
const plugins = Array.isArray(rawOptions.plugins)
|
|
1967
|
-
? rawOptions.plugins
|
|
2085
|
+
? [...rawOptions.plugins]
|
|
1968
2086
|
: rawOptions.plugins
|
|
1969
2087
|
? [rawOptions.plugins]
|
|
1970
2088
|
: [];
|
|
@@ -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,11 +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
|
-
|
|
2167
|
+
const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
|
|
2168
|
+
return getEntryProxy(acutalId, getDefaultIsModuleExports(acutalId), this.getModuleInfo);
|
|
2046
2169
|
}
|
|
2047
2170
|
|
|
2048
2171
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
2049
|
-
|
|
2172
|
+
const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
|
|
2173
|
+
return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
|
|
2050
2174
|
}
|
|
2051
2175
|
|
|
2052
2176
|
if (id === DYNAMIC_MODULES_ID) {
|