@rollup/plugin-commonjs 22.0.0-2 → 22.0.0-6
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/dist/{index.js → cjs/index.js} +236 -158
- package/dist/cjs/index.js.map +1 -0
- package/dist/{index.es.js → es/index.js} +237 -159
- package/dist/es/index.js.map +1 -0
- package/dist/es/package.json +1 -0
- package/package.json +11 -6
- package/dist/index.es.js.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -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-6";
|
|
11
11
|
var peerDependencies = {
|
|
12
|
-
rollup: "^2.
|
|
12
|
+
rollup: "^2.66.1"
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
function tryParse(parse, code, id) {
|
|
@@ -194,6 +194,9 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
|
|
|
194
194
|
|
|
195
195
|
const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
|
|
196
196
|
|
|
197
|
+
const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
|
|
198
|
+
const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
|
|
199
|
+
|
|
197
200
|
function getDynamicModuleRegistry(
|
|
198
201
|
isDynamicRequireModulesEnabled,
|
|
199
202
|
dynamicRequireModules,
|
|
@@ -201,7 +204,7 @@ function getDynamicModuleRegistry(
|
|
|
201
204
|
ignoreDynamicRequires
|
|
202
205
|
) {
|
|
203
206
|
if (!isDynamicRequireModulesEnabled) {
|
|
204
|
-
return `export function
|
|
207
|
+
return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
|
|
205
208
|
${FAILED_REQUIRE_ERROR}
|
|
206
209
|
}`;
|
|
207
210
|
}
|
|
@@ -231,25 +234,25 @@ ${dynamicModuleProps}
|
|
|
231
234
|
});
|
|
232
235
|
}
|
|
233
236
|
|
|
234
|
-
export function
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
237
|
+
export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
|
|
238
|
+
function handleRequire(path) {
|
|
239
|
+
var resolvedPath = commonjsResolve(path, originalModuleDir);
|
|
240
|
+
if (resolvedPath !== null) {
|
|
241
|
+
return getDynamicModules()[resolvedPath]();
|
|
242
|
+
}
|
|
243
|
+
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
|
|
238
244
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
return resolvedPath;
|
|
245
|
+
handleRequire.resolve = function (path) {
|
|
246
|
+
var resolvedPath = commonjsResolve(path, originalModuleDir);
|
|
247
|
+
if (resolvedPath !== null) {
|
|
248
|
+
return resolvedPath;
|
|
249
|
+
}
|
|
250
|
+
return require.resolve(path);
|
|
246
251
|
}
|
|
247
|
-
return
|
|
252
|
+
return handleRequire;
|
|
248
253
|
}
|
|
249
254
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
function commonjsResolveImpl (path, originalModuleDir) {
|
|
255
|
+
function commonjsResolve (path, originalModuleDir) {
|
|
253
256
|
var shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
254
257
|
path = normalize(path);
|
|
255
258
|
var relPath;
|
|
@@ -325,7 +328,8 @@ const WRAPPED_SUFFIX = '?commonjs-wrapped';
|
|
|
325
328
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
326
329
|
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
327
330
|
const MODULE_SUFFIX = '?commonjs-module';
|
|
328
|
-
const
|
|
331
|
+
const ENTRY_SUFFIX = '?commonjs-entry';
|
|
332
|
+
const ES_IMPORT_SUFFIX = '?commonjs-es-import';
|
|
329
333
|
|
|
330
334
|
const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
|
|
331
335
|
const HELPERS_ID = '\0commonjsHelpers.js';
|
|
@@ -393,21 +397,15 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
|
393
397
|
return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
|
|
394
398
|
}
|
|
395
399
|
|
|
396
|
-
async function getStaticRequireProxy(
|
|
397
|
-
id,
|
|
398
|
-
requireReturnsDefault,
|
|
399
|
-
esModulesWithDefaultExport,
|
|
400
|
-
esModulesWithNamedExports,
|
|
401
|
-
loadModule
|
|
402
|
-
) {
|
|
400
|
+
async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
403
401
|
const name = getName(id);
|
|
404
402
|
const {
|
|
405
403
|
meta: { commonjs: commonjsMeta }
|
|
406
404
|
} = await loadModule({ id });
|
|
407
|
-
if (commonjsMeta
|
|
408
|
-
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
409
|
-
} else if (!commonjsMeta) {
|
|
405
|
+
if (!commonjsMeta) {
|
|
410
406
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
407
|
+
} else if (commonjsMeta.isCommonJS) {
|
|
408
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
411
409
|
} else if (!requireReturnsDefault) {
|
|
412
410
|
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
413
411
|
id
|
|
@@ -415,14 +413,30 @@ async function getStaticRequireProxy(
|
|
|
415
413
|
} else if (
|
|
416
414
|
requireReturnsDefault !== true &&
|
|
417
415
|
(requireReturnsDefault === 'namespace' ||
|
|
418
|
-
!
|
|
419
|
-
(requireReturnsDefault === 'auto' &&
|
|
416
|
+
!commonjsMeta.hasDefaultExport ||
|
|
417
|
+
(requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
|
|
420
418
|
) {
|
|
421
419
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
422
420
|
}
|
|
423
421
|
return `export { default } from ${JSON.stringify(id)};`;
|
|
424
422
|
}
|
|
425
423
|
|
|
424
|
+
async function getEntryProxy(id, defaultIsModuleExports, loadModule) {
|
|
425
|
+
const {
|
|
426
|
+
meta: { commonjs: commonjsMeta },
|
|
427
|
+
hasDefaultExport
|
|
428
|
+
} = await loadModule({ id, moduleSideEffects: true });
|
|
429
|
+
if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
|
|
430
|
+
const stringifiedId = JSON.stringify(id);
|
|
431
|
+
let code = `export * from ${stringifiedId};`;
|
|
432
|
+
if (hasDefaultExport) {
|
|
433
|
+
code += `export { default } from ${stringifiedId};`;
|
|
434
|
+
}
|
|
435
|
+
return code;
|
|
436
|
+
}
|
|
437
|
+
return getEsImportProxy(id, defaultIsModuleExports);
|
|
438
|
+
}
|
|
439
|
+
|
|
426
440
|
function getEsImportProxy(id, defaultIsModuleExports) {
|
|
427
441
|
const name = getName(id);
|
|
428
442
|
const exportsName = `${name}Exports`;
|
|
@@ -491,13 +505,15 @@ function getResolveId(extensions) {
|
|
|
491
505
|
}
|
|
492
506
|
|
|
493
507
|
if (
|
|
494
|
-
|
|
495
|
-
isWrappedId(importee,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
508
|
+
importee.startsWith('\0') &&
|
|
509
|
+
(isWrappedId(importee, MODULE_SUFFIX) ||
|
|
510
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
511
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
512
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
513
|
+
importee.endsWith(ENTRY_SUFFIX) ||
|
|
514
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
515
|
+
importee.startsWith(HELPERS_ID) ||
|
|
516
|
+
importee === DYNAMIC_MODULES_ID)
|
|
501
517
|
) {
|
|
502
518
|
return importee;
|
|
503
519
|
}
|
|
@@ -507,7 +523,8 @@ function getResolveId(extensions) {
|
|
|
507
523
|
importer === DYNAMIC_MODULES_ID ||
|
|
508
524
|
// Proxies are only importing resolved ids, no need to resolve again
|
|
509
525
|
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
510
|
-
isWrappedId(importer, ES_IMPORT_SUFFIX)
|
|
526
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX) ||
|
|
527
|
+
importer.endsWith(ENTRY_SUFFIX)
|
|
511
528
|
) {
|
|
512
529
|
return importee;
|
|
513
530
|
}
|
|
@@ -540,6 +557,10 @@ function getResolveId(extensions) {
|
|
|
540
557
|
if (!resolved || resolved.external) {
|
|
541
558
|
return resolved;
|
|
542
559
|
}
|
|
560
|
+
if (resolveOptions.isEntry) {
|
|
561
|
+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
562
|
+
return resolved.id + ENTRY_SUFFIX;
|
|
563
|
+
}
|
|
543
564
|
const {
|
|
544
565
|
meta: { commonjs: commonjsMeta }
|
|
545
566
|
} = await this.load(resolved);
|
|
@@ -550,7 +571,7 @@ function getResolveId(extensions) {
|
|
|
550
571
|
};
|
|
551
572
|
}
|
|
552
573
|
|
|
553
|
-
function
|
|
574
|
+
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
554
575
|
const knownCjsModuleTypes = Object.create(null);
|
|
555
576
|
const requiredIds = Object.create(null);
|
|
556
577
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -574,11 +595,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
|
|
|
574
595
|
|
|
575
596
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
576
597
|
const knownType = knownCjsModuleTypes[id];
|
|
577
|
-
if (
|
|
578
|
-
knownType === IS_WRAPPED_COMMONJS ||
|
|
579
|
-
!detectCyclesAndConditional ||
|
|
580
|
-
fullyAnalyzedModules[id]
|
|
581
|
-
) {
|
|
598
|
+
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
582
599
|
return knownType;
|
|
583
600
|
}
|
|
584
601
|
fullyAnalyzedModules[id] = true;
|
|
@@ -588,26 +605,80 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
|
|
|
588
605
|
return knownType;
|
|
589
606
|
};
|
|
590
607
|
|
|
608
|
+
const setInitialParentType = (id, initialCommonJSType) => {
|
|
609
|
+
// It is possible a transformed module is already fully analyzed when using
|
|
610
|
+
// the cache and one dependency introduces a new cycle. Then transform is
|
|
611
|
+
// run for a fully analzyed module again. Fully analyzed modules may never
|
|
612
|
+
// change their type as importers already trust their type.
|
|
613
|
+
knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
|
|
614
|
+
? knownCjsModuleTypes[id]
|
|
615
|
+
: initialCommonJSType;
|
|
616
|
+
if (
|
|
617
|
+
detectCyclesAndConditional &&
|
|
618
|
+
knownCjsModuleTypes[id] === true &&
|
|
619
|
+
requiredIds[id] &&
|
|
620
|
+
!unconditionallyRequiredIds[id]
|
|
621
|
+
) {
|
|
622
|
+
knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
|
|
627
|
+
const childId = resolved.id;
|
|
628
|
+
requiredIds[childId] = true;
|
|
629
|
+
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
630
|
+
unconditionallyRequiredIds[childId] = true;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
getDependencies(parentId).add(childId);
|
|
634
|
+
if (!isCyclic(childId)) {
|
|
635
|
+
// This makes sure the current transform handler waits for all direct dependencies to be
|
|
636
|
+
// loaded and transformed and therefore for all transitive CommonJS dependencies to be
|
|
637
|
+
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
|
|
638
|
+
await loadModule(resolved);
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
591
642
|
return {
|
|
592
643
|
getWrappedIds: () =>
|
|
593
644
|
Object.keys(knownCjsModuleTypes).filter(
|
|
594
645
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
595
646
|
),
|
|
596
647
|
isRequiredId: (id) => requiredIds[id],
|
|
597
|
-
|
|
648
|
+
async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
|
|
649
|
+
// Ignore modules that did not pass through the original transformer in a previous build
|
|
650
|
+
if (!(parentMeta && parentMeta.requires)) {
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
setInitialParentType(parentId, parentMeta.initialCommonJSType);
|
|
654
|
+
await Promise.all(
|
|
655
|
+
parentMeta.requires.map(({ resolved, isConditional }) =>
|
|
656
|
+
setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
|
|
657
|
+
)
|
|
658
|
+
);
|
|
659
|
+
if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
|
|
660
|
+
return true;
|
|
661
|
+
}
|
|
662
|
+
for (const {
|
|
663
|
+
resolved: { id }
|
|
664
|
+
} of parentMeta.requires) {
|
|
665
|
+
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
return false;
|
|
670
|
+
},
|
|
671
|
+
/* eslint-disable no-param-reassign */
|
|
672
|
+
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
598
673
|
parentId,
|
|
599
674
|
isParentCommonJS,
|
|
675
|
+
parentMeta,
|
|
600
676
|
sources
|
|
601
677
|
) => {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
requiredIds[parentId] &&
|
|
607
|
-
!unconditionallyRequiredIds[parentId]
|
|
608
|
-
) {
|
|
609
|
-
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
610
|
-
}
|
|
678
|
+
parentMeta.initialCommonJSType = isParentCommonJS;
|
|
679
|
+
parentMeta.requires = [];
|
|
680
|
+
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
681
|
+
setInitialParentType(parentId, isParentCommonJS);
|
|
611
682
|
const requireTargets = await Promise.all(
|
|
612
683
|
sources.map(async ({ source, isConditional }) => {
|
|
613
684
|
// Never analyze or proxy internal modules
|
|
@@ -625,59 +696,58 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
|
|
|
625
696
|
if (resolved.external) {
|
|
626
697
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
627
698
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
unconditionallyRequiredIds[childId] = true;
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
getDependencies(parentId).add(childId);
|
|
634
|
-
if (!isCyclic(childId)) {
|
|
635
|
-
// This makes sure the current transform handler waits for all direct dependencies to be
|
|
636
|
-
// loaded and transformed and therefore for all transitive CommonJS dependencies to be
|
|
637
|
-
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
|
|
638
|
-
await rollupContext.load(resolved);
|
|
639
|
-
} else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
|
|
640
|
-
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
641
|
-
}
|
|
699
|
+
parentMeta.requires.push({ resolved, isConditional });
|
|
700
|
+
await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
|
|
642
701
|
return { id: childId, allowProxy: true };
|
|
643
702
|
})
|
|
644
703
|
);
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
704
|
+
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
705
|
+
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
706
|
+
// eslint-disable-next-line no-multi-assign
|
|
707
|
+
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
708
|
+
dependencyId
|
|
709
|
+
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
710
|
+
return {
|
|
711
|
+
source: sources[index].source,
|
|
712
|
+
id: allowProxy
|
|
713
|
+
? isCommonJS === IS_WRAPPED_COMMONJS
|
|
714
|
+
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
715
|
+
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
716
|
+
: dependencyId,
|
|
717
|
+
isCommonJS
|
|
718
|
+
};
|
|
719
|
+
});
|
|
660
720
|
}
|
|
661
721
|
};
|
|
662
722
|
}
|
|
663
723
|
|
|
664
|
-
function
|
|
665
|
-
const
|
|
666
|
-
const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
|
|
724
|
+
function validateVersion(actualVersion, peerDependencyVersion, name) {
|
|
725
|
+
const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
|
|
667
726
|
let minMajor = Infinity;
|
|
668
727
|
let minMinor = Infinity;
|
|
728
|
+
let minPatch = Infinity;
|
|
669
729
|
let foundVersion;
|
|
670
730
|
// eslint-disable-next-line no-cond-assign
|
|
671
731
|
while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
|
|
672
|
-
const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
|
|
732
|
+
const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
|
|
673
733
|
if (foundMajor < minMajor) {
|
|
674
734
|
minMajor = foundMajor;
|
|
675
735
|
minMinor = foundMinor;
|
|
736
|
+
minPatch = foundPatch;
|
|
676
737
|
}
|
|
677
738
|
}
|
|
678
|
-
if (
|
|
739
|
+
if (!actualVersion) {
|
|
679
740
|
throw new Error(
|
|
680
|
-
`Insufficient
|
|
741
|
+
`Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
const [major, minor, patch] = actualVersion.split('.').map(Number);
|
|
745
|
+
if (
|
|
746
|
+
major < minMajor ||
|
|
747
|
+
(major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
|
|
748
|
+
) {
|
|
749
|
+
throw new Error(
|
|
750
|
+
`Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
|
|
681
751
|
);
|
|
682
752
|
}
|
|
683
753
|
}
|
|
@@ -1120,17 +1190,20 @@ function getRequireHandlers() {
|
|
|
1120
1190
|
exportsName,
|
|
1121
1191
|
id,
|
|
1122
1192
|
exportMode,
|
|
1123
|
-
|
|
1193
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1124
1194
|
needsRequireWrapper,
|
|
1125
1195
|
isEsModule,
|
|
1126
|
-
|
|
1127
|
-
getIgnoreTryCatchRequireStatementMode
|
|
1196
|
+
isDynamicRequireModulesEnabled,
|
|
1197
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1198
|
+
commonjsMeta
|
|
1128
1199
|
) {
|
|
1129
1200
|
const imports = [];
|
|
1130
1201
|
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1131
|
-
if (
|
|
1202
|
+
if (dynamicRequireName) {
|
|
1132
1203
|
imports.push(
|
|
1133
|
-
`import {
|
|
1204
|
+
`import { ${
|
|
1205
|
+
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1206
|
+
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
|
|
1134
1207
|
);
|
|
1135
1208
|
}
|
|
1136
1209
|
if (exportMode === 'module') {
|
|
@@ -1145,9 +1218,10 @@ function getRequireHandlers() {
|
|
|
1145
1218
|
);
|
|
1146
1219
|
}
|
|
1147
1220
|
const requiresBySource = collectSources(requireExpressions);
|
|
1148
|
-
const
|
|
1221
|
+
const requireTargets = await resolveRequireSourcesAndUpdateMeta(
|
|
1149
1222
|
id,
|
|
1150
1223
|
needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
|
|
1224
|
+
commonjsMeta,
|
|
1151
1225
|
Object.keys(requiresBySource).map((source) => {
|
|
1152
1226
|
return {
|
|
1153
1227
|
source,
|
|
@@ -1162,10 +1236,7 @@ function getRequireHandlers() {
|
|
|
1162
1236
|
getIgnoreTryCatchRequireStatementMode,
|
|
1163
1237
|
magicString
|
|
1164
1238
|
);
|
|
1165
|
-
return {
|
|
1166
|
-
importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
|
|
1167
|
-
usesRequireWrapper
|
|
1168
|
-
};
|
|
1239
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1169
1240
|
}
|
|
1170
1241
|
|
|
1171
1242
|
return {
|
|
@@ -1268,9 +1339,10 @@ async function transformCommonjs(
|
|
|
1268
1339
|
astCache,
|
|
1269
1340
|
defaultIsModuleExports,
|
|
1270
1341
|
needsRequireWrapper,
|
|
1271
|
-
|
|
1342
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1272
1343
|
isRequired,
|
|
1273
|
-
checkDynamicRequire
|
|
1344
|
+
checkDynamicRequire,
|
|
1345
|
+
commonjsMeta
|
|
1274
1346
|
) {
|
|
1275
1347
|
const ast = astCache || tryParse(parse, code, id);
|
|
1276
1348
|
const magicString = new MagicString(code);
|
|
@@ -1419,12 +1491,6 @@ async function transformCommonjs(
|
|
|
1419
1491
|
checkDynamicRequire(node.start);
|
|
1420
1492
|
uses.require = true;
|
|
1421
1493
|
const requireNode = node.callee.object;
|
|
1422
|
-
magicString.appendLeft(
|
|
1423
|
-
node.end - 1,
|
|
1424
|
-
`,${JSON.stringify(
|
|
1425
|
-
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1426
|
-
)}`
|
|
1427
|
-
);
|
|
1428
1494
|
replacedDynamicRequires.push(requireNode);
|
|
1429
1495
|
return;
|
|
1430
1496
|
}
|
|
@@ -1439,12 +1505,6 @@ async function transformCommonjs(
|
|
|
1439
1505
|
if (hasDynamicArguments(node)) {
|
|
1440
1506
|
if (isDynamicRequireModulesEnabled) {
|
|
1441
1507
|
checkDynamicRequire(node.start);
|
|
1442
|
-
magicString.appendLeft(
|
|
1443
|
-
node.end - 1,
|
|
1444
|
-
`, ${JSON.stringify(
|
|
1445
|
-
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1446
|
-
)}`
|
|
1447
|
-
);
|
|
1448
1508
|
}
|
|
1449
1509
|
if (!ignoreDynamicRequires) {
|
|
1450
1510
|
replacedDynamicRequires.push(node.callee);
|
|
@@ -1587,9 +1647,10 @@ async function transformCommonjs(
|
|
|
1587
1647
|
if (scope.contains(flattened.name)) return;
|
|
1588
1648
|
|
|
1589
1649
|
if (
|
|
1590
|
-
|
|
1591
|
-
flattened.keypath === 'module' ||
|
|
1592
|
-
|
|
1650
|
+
!isEsModule &&
|
|
1651
|
+
(flattened.keypath === 'module.exports' ||
|
|
1652
|
+
flattened.keypath === 'module' ||
|
|
1653
|
+
flattened.keypath === 'exports')
|
|
1593
1654
|
) {
|
|
1594
1655
|
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1595
1656
|
storeName: false
|
|
@@ -1617,7 +1678,13 @@ async function transformCommonjs(
|
|
|
1617
1678
|
const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
|
|
1618
1679
|
const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
|
|
1619
1680
|
const helpersName = deconflict([scope], globals, 'commonjsHelpers');
|
|
1620
|
-
const dynamicRequireName =
|
|
1681
|
+
const dynamicRequireName =
|
|
1682
|
+
replacedDynamicRequires.length > 0 &&
|
|
1683
|
+
deconflict(
|
|
1684
|
+
[scope],
|
|
1685
|
+
globals,
|
|
1686
|
+
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1687
|
+
);
|
|
1621
1688
|
const deconflictedExportNames = Object.create(null);
|
|
1622
1689
|
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1623
1690
|
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
@@ -1629,10 +1696,17 @@ async function transformCommonjs(
|
|
|
1629
1696
|
});
|
|
1630
1697
|
}
|
|
1631
1698
|
for (const node of replacedDynamicRequires) {
|
|
1632
|
-
magicString.overwrite(
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1699
|
+
magicString.overwrite(
|
|
1700
|
+
node.start,
|
|
1701
|
+
node.end,
|
|
1702
|
+
isDynamicRequireModulesEnabled
|
|
1703
|
+
? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
|
|
1704
|
+
: dynamicRequireName,
|
|
1705
|
+
{
|
|
1706
|
+
contentOnly: true,
|
|
1707
|
+
storeName: true
|
|
1708
|
+
}
|
|
1709
|
+
);
|
|
1636
1710
|
}
|
|
1637
1711
|
|
|
1638
1712
|
// We cannot wrap ES/mixed modules
|
|
@@ -1652,7 +1726,7 @@ async function transformCommonjs(
|
|
|
1652
1726
|
) &&
|
|
1653
1727
|
(ignoreGlobal || !uses.global)
|
|
1654
1728
|
) {
|
|
1655
|
-
return { meta: { commonjs: { isCommonJS: false
|
|
1729
|
+
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1656
1730
|
}
|
|
1657
1731
|
|
|
1658
1732
|
let leadingComment = '';
|
|
@@ -1674,7 +1748,7 @@ async function transformCommonjs(
|
|
|
1674
1748
|
? 'exports'
|
|
1675
1749
|
: 'module';
|
|
1676
1750
|
|
|
1677
|
-
const
|
|
1751
|
+
const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1678
1752
|
magicString,
|
|
1679
1753
|
topLevelDeclarations,
|
|
1680
1754
|
reassignedNames,
|
|
@@ -1684,12 +1758,14 @@ async function transformCommonjs(
|
|
|
1684
1758
|
exportsName,
|
|
1685
1759
|
id,
|
|
1686
1760
|
exportMode,
|
|
1687
|
-
|
|
1761
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1688
1762
|
needsRequireWrapper,
|
|
1689
1763
|
isEsModule,
|
|
1690
|
-
|
|
1691
|
-
getIgnoreTryCatchRequireStatementMode
|
|
1764
|
+
isDynamicRequireModulesEnabled,
|
|
1765
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1766
|
+
commonjsMeta
|
|
1692
1767
|
);
|
|
1768
|
+
const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
|
|
1693
1769
|
const exportBlock = isEsModule
|
|
1694
1770
|
? ''
|
|
1695
1771
|
: rewriteExportsAndGetExportsBlock(
|
|
@@ -1742,12 +1818,7 @@ function ${requireName} () {
|
|
|
1742
1818
|
code: magicString.toString(),
|
|
1743
1819
|
map: sourceMap ? magicString.generateMap() : null,
|
|
1744
1820
|
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
|
|
1745
|
-
meta: {
|
|
1746
|
-
commonjs: {
|
|
1747
|
-
isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
|
|
1748
|
-
isMixedModule: isEsModule
|
|
1749
|
-
}
|
|
1750
|
-
}
|
|
1821
|
+
meta: { commonjs: commonjsMeta }
|
|
1751
1822
|
};
|
|
1752
1823
|
}
|
|
1753
1824
|
|
|
@@ -1778,11 +1849,6 @@ function commonjs(options = {}) {
|
|
|
1778
1849
|
const defaultIsModuleExports =
|
|
1779
1850
|
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1780
1851
|
|
|
1781
|
-
const {
|
|
1782
|
-
resolveRequireSourcesAndGetMeta,
|
|
1783
|
-
getWrappedIds,
|
|
1784
|
-
isRequiredId
|
|
1785
|
-
} = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
|
|
1786
1852
|
const dynamicRequireRoot =
|
|
1787
1853
|
typeof options.dynamicRequireRoot === 'string'
|
|
1788
1854
|
? resolve(options.dynamicRequireRoot)
|
|
@@ -1793,9 +1859,6 @@ function commonjs(options = {}) {
|
|
|
1793
1859
|
);
|
|
1794
1860
|
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
|
|
1795
1861
|
|
|
1796
|
-
const esModulesWithDefaultExport = new Set();
|
|
1797
|
-
const esModulesWithNamedExports = new Set();
|
|
1798
|
-
|
|
1799
1862
|
const ignoreRequire =
|
|
1800
1863
|
typeof options.ignore === 'function'
|
|
1801
1864
|
? options.ignore
|
|
@@ -1823,25 +1886,31 @@ function commonjs(options = {}) {
|
|
|
1823
1886
|
|
|
1824
1887
|
const sourceMap = options.sourceMap !== false;
|
|
1825
1888
|
|
|
1889
|
+
// Initialized in buildStart
|
|
1890
|
+
let requireResolver;
|
|
1891
|
+
|
|
1826
1892
|
function transformAndCheckExports(code, id) {
|
|
1827
1893
|
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
|
|
1828
1894
|
this.parse,
|
|
1829
1895
|
code,
|
|
1830
1896
|
id
|
|
1831
1897
|
);
|
|
1898
|
+
|
|
1899
|
+
const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
|
|
1832
1900
|
if (hasDefaultExport) {
|
|
1833
|
-
|
|
1901
|
+
commonjsMeta.hasDefaultExport = true;
|
|
1834
1902
|
}
|
|
1835
1903
|
if (hasNamedExports) {
|
|
1836
|
-
|
|
1904
|
+
commonjsMeta.hasNamedExports = true;
|
|
1837
1905
|
}
|
|
1838
1906
|
|
|
1839
1907
|
if (
|
|
1840
1908
|
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
|
|
1841
|
-
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
|
|
1909
|
+
(!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
|
|
1842
1910
|
(isEsModule && !options.transformMixedEsModules))
|
|
1843
1911
|
) {
|
|
1844
|
-
|
|
1912
|
+
commonjsMeta.isCommonJS = false;
|
|
1913
|
+
return { meta: { commonjs: commonjsMeta } };
|
|
1845
1914
|
}
|
|
1846
1915
|
|
|
1847
1916
|
const needsRequireWrapper =
|
|
@@ -1880,9 +1949,10 @@ function commonjs(options = {}) {
|
|
|
1880
1949
|
ast,
|
|
1881
1950
|
defaultIsModuleExports,
|
|
1882
1951
|
needsRequireWrapper,
|
|
1883
|
-
|
|
1884
|
-
isRequiredId(id),
|
|
1885
|
-
checkDynamicRequire
|
|
1952
|
+
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1953
|
+
requireResolver.isRequiredId(id),
|
|
1954
|
+
checkDynamicRequire,
|
|
1955
|
+
commonjsMeta
|
|
1886
1956
|
);
|
|
1887
1957
|
}
|
|
1888
1958
|
|
|
@@ -1906,18 +1976,23 @@ function commonjs(options = {}) {
|
|
|
1906
1976
|
return { ...rawOptions, plugins };
|
|
1907
1977
|
},
|
|
1908
1978
|
|
|
1909
|
-
buildStart() {
|
|
1910
|
-
|
|
1979
|
+
buildStart({ plugins }) {
|
|
1980
|
+
validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
|
|
1981
|
+
const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
|
|
1982
|
+
if (nodeResolve) {
|
|
1983
|
+
validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
|
|
1984
|
+
}
|
|
1911
1985
|
if (options.namedExports != null) {
|
|
1912
1986
|
this.warn(
|
|
1913
1987
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
1914
1988
|
);
|
|
1915
1989
|
}
|
|
1990
|
+
requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
|
|
1916
1991
|
},
|
|
1917
1992
|
|
|
1918
1993
|
buildEnd() {
|
|
1919
1994
|
if (options.strictRequires === 'debug') {
|
|
1920
|
-
const wrappedIds = getWrappedIds();
|
|
1995
|
+
const wrappedIds = requireResolver.getWrappedIds();
|
|
1921
1996
|
if (wrappedIds.length) {
|
|
1922
1997
|
this.warn({
|
|
1923
1998
|
code: 'WRAPPED_IDS',
|
|
@@ -1966,6 +2041,11 @@ function commonjs(options = {}) {
|
|
|
1966
2041
|
);
|
|
1967
2042
|
}
|
|
1968
2043
|
|
|
2044
|
+
// entry suffix is just appended to not mess up relative external resolution
|
|
2045
|
+
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2046
|
+
return getEntryProxy(id.slice(0, -ENTRY_SUFFIX.length), defaultIsModuleExports, this.load);
|
|
2047
|
+
}
|
|
2048
|
+
|
|
1969
2049
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
1970
2050
|
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
|
|
1971
2051
|
}
|
|
@@ -1981,18 +2061,16 @@ function commonjs(options = {}) {
|
|
|
1981
2061
|
|
|
1982
2062
|
if (isWrappedId(id, PROXY_SUFFIX)) {
|
|
1983
2063
|
const actualId = unwrapId(id, PROXY_SUFFIX);
|
|
1984
|
-
return getStaticRequireProxy(
|
|
1985
|
-
actualId,
|
|
1986
|
-
getRequireReturnsDefault(actualId),
|
|
1987
|
-
esModulesWithDefaultExport,
|
|
1988
|
-
esModulesWithNamedExports,
|
|
1989
|
-
this.load
|
|
1990
|
-
);
|
|
2064
|
+
return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
|
|
1991
2065
|
}
|
|
1992
2066
|
|
|
1993
2067
|
return null;
|
|
1994
2068
|
},
|
|
1995
2069
|
|
|
2070
|
+
shouldTransformCachedModule(...args) {
|
|
2071
|
+
return requireResolver.shouldTransformCachedModule.call(this, ...args);
|
|
2072
|
+
},
|
|
2073
|
+
|
|
1996
2074
|
transform(code, id) {
|
|
1997
2075
|
const extName = extname(id);
|
|
1998
2076
|
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|
|
@@ -2009,4 +2087,4 @@ function commonjs(options = {}) {
|
|
|
2009
2087
|
}
|
|
2010
2088
|
|
|
2011
2089
|
export { commonjs as default };
|
|
2012
|
-
//# sourceMappingURL=index.
|
|
2090
|
+
//# sourceMappingURL=index.js.map
|