@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
|
@@ -16,9 +16,9 @@ var glob__default = /*#__PURE__*/_interopDefaultLegacy(glob);
|
|
|
16
16
|
var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
17
17
|
var isReference__default = /*#__PURE__*/_interopDefaultLegacy(isReference);
|
|
18
18
|
|
|
19
|
-
var version = "22.0.0-
|
|
19
|
+
var version = "22.0.0-6";
|
|
20
20
|
var peerDependencies = {
|
|
21
|
-
rollup: "^2.
|
|
21
|
+
rollup: "^2.66.1"
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
function tryParse(parse, code, id) {
|
|
@@ -203,6 +203,9 @@ function getDynamicRequireModules(patterns, dynamicRequireRoot) {
|
|
|
203
203
|
|
|
204
204
|
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.');`;
|
|
205
205
|
|
|
206
|
+
const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
|
|
207
|
+
const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
|
|
208
|
+
|
|
206
209
|
function getDynamicModuleRegistry(
|
|
207
210
|
isDynamicRequireModulesEnabled,
|
|
208
211
|
dynamicRequireModules,
|
|
@@ -210,7 +213,7 @@ function getDynamicModuleRegistry(
|
|
|
210
213
|
ignoreDynamicRequires
|
|
211
214
|
) {
|
|
212
215
|
if (!isDynamicRequireModulesEnabled) {
|
|
213
|
-
return `export function
|
|
216
|
+
return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
|
|
214
217
|
${FAILED_REQUIRE_ERROR}
|
|
215
218
|
}`;
|
|
216
219
|
}
|
|
@@ -240,25 +243,25 @@ ${dynamicModuleProps}
|
|
|
240
243
|
});
|
|
241
244
|
}
|
|
242
245
|
|
|
243
|
-
export function
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
246
|
+
export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
|
|
247
|
+
function handleRequire(path) {
|
|
248
|
+
var resolvedPath = commonjsResolve(path, originalModuleDir);
|
|
249
|
+
if (resolvedPath !== null) {
|
|
250
|
+
return getDynamicModules()[resolvedPath]();
|
|
251
|
+
}
|
|
252
|
+
${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
|
|
247
253
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
return resolvedPath;
|
|
254
|
+
handleRequire.resolve = function (path) {
|
|
255
|
+
var resolvedPath = commonjsResolve(path, originalModuleDir);
|
|
256
|
+
if (resolvedPath !== null) {
|
|
257
|
+
return resolvedPath;
|
|
258
|
+
}
|
|
259
|
+
return require.resolve(path);
|
|
255
260
|
}
|
|
256
|
-
return
|
|
261
|
+
return handleRequire;
|
|
257
262
|
}
|
|
258
263
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
function commonjsResolveImpl (path, originalModuleDir) {
|
|
264
|
+
function commonjsResolve (path, originalModuleDir) {
|
|
262
265
|
var shouldTryNodeModules = isPossibleNodeModulesPath(path);
|
|
263
266
|
path = normalize(path);
|
|
264
267
|
var relPath;
|
|
@@ -334,7 +337,8 @@ const WRAPPED_SUFFIX = '?commonjs-wrapped';
|
|
|
334
337
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
335
338
|
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
336
339
|
const MODULE_SUFFIX = '?commonjs-module';
|
|
337
|
-
const
|
|
340
|
+
const ENTRY_SUFFIX = '?commonjs-entry';
|
|
341
|
+
const ES_IMPORT_SUFFIX = '?commonjs-es-import';
|
|
338
342
|
|
|
339
343
|
const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
|
|
340
344
|
const HELPERS_ID = '\0commonjsHelpers.js';
|
|
@@ -402,21 +406,15 @@ function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
|
402
406
|
return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
|
|
403
407
|
}
|
|
404
408
|
|
|
405
|
-
async function getStaticRequireProxy(
|
|
406
|
-
id,
|
|
407
|
-
requireReturnsDefault,
|
|
408
|
-
esModulesWithDefaultExport,
|
|
409
|
-
esModulesWithNamedExports,
|
|
410
|
-
loadModule
|
|
411
|
-
) {
|
|
409
|
+
async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
412
410
|
const name = getName(id);
|
|
413
411
|
const {
|
|
414
412
|
meta: { commonjs: commonjsMeta }
|
|
415
413
|
} = await loadModule({ id });
|
|
416
|
-
if (commonjsMeta
|
|
417
|
-
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
418
|
-
} else if (!commonjsMeta) {
|
|
414
|
+
if (!commonjsMeta) {
|
|
419
415
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
416
|
+
} else if (commonjsMeta.isCommonJS) {
|
|
417
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
420
418
|
} else if (!requireReturnsDefault) {
|
|
421
419
|
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
422
420
|
id
|
|
@@ -424,14 +422,30 @@ async function getStaticRequireProxy(
|
|
|
424
422
|
} else if (
|
|
425
423
|
requireReturnsDefault !== true &&
|
|
426
424
|
(requireReturnsDefault === 'namespace' ||
|
|
427
|
-
!
|
|
428
|
-
(requireReturnsDefault === 'auto' &&
|
|
425
|
+
!commonjsMeta.hasDefaultExport ||
|
|
426
|
+
(requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
|
|
429
427
|
) {
|
|
430
428
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
431
429
|
}
|
|
432
430
|
return `export { default } from ${JSON.stringify(id)};`;
|
|
433
431
|
}
|
|
434
432
|
|
|
433
|
+
async function getEntryProxy(id, defaultIsModuleExports, loadModule) {
|
|
434
|
+
const {
|
|
435
|
+
meta: { commonjs: commonjsMeta },
|
|
436
|
+
hasDefaultExport
|
|
437
|
+
} = await loadModule({ id, moduleSideEffects: true });
|
|
438
|
+
if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
|
|
439
|
+
const stringifiedId = JSON.stringify(id);
|
|
440
|
+
let code = `export * from ${stringifiedId};`;
|
|
441
|
+
if (hasDefaultExport) {
|
|
442
|
+
code += `export { default } from ${stringifiedId};`;
|
|
443
|
+
}
|
|
444
|
+
return code;
|
|
445
|
+
}
|
|
446
|
+
return getEsImportProxy(id, defaultIsModuleExports);
|
|
447
|
+
}
|
|
448
|
+
|
|
435
449
|
function getEsImportProxy(id, defaultIsModuleExports) {
|
|
436
450
|
const name = getName(id);
|
|
437
451
|
const exportsName = `${name}Exports`;
|
|
@@ -500,13 +514,15 @@ function getResolveId(extensions) {
|
|
|
500
514
|
}
|
|
501
515
|
|
|
502
516
|
if (
|
|
503
|
-
|
|
504
|
-
isWrappedId(importee,
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
517
|
+
importee.startsWith('\0') &&
|
|
518
|
+
(isWrappedId(importee, MODULE_SUFFIX) ||
|
|
519
|
+
isWrappedId(importee, EXPORTS_SUFFIX) ||
|
|
520
|
+
isWrappedId(importee, PROXY_SUFFIX) ||
|
|
521
|
+
isWrappedId(importee, ES_IMPORT_SUFFIX) ||
|
|
522
|
+
importee.endsWith(ENTRY_SUFFIX) ||
|
|
523
|
+
isWrappedId(importee, EXTERNAL_SUFFIX) ||
|
|
524
|
+
importee.startsWith(HELPERS_ID) ||
|
|
525
|
+
importee === DYNAMIC_MODULES_ID)
|
|
510
526
|
) {
|
|
511
527
|
return importee;
|
|
512
528
|
}
|
|
@@ -516,7 +532,8 @@ function getResolveId(extensions) {
|
|
|
516
532
|
importer === DYNAMIC_MODULES_ID ||
|
|
517
533
|
// Proxies are only importing resolved ids, no need to resolve again
|
|
518
534
|
isWrappedId(importer, PROXY_SUFFIX) ||
|
|
519
|
-
isWrappedId(importer, ES_IMPORT_SUFFIX)
|
|
535
|
+
isWrappedId(importer, ES_IMPORT_SUFFIX) ||
|
|
536
|
+
importer.endsWith(ENTRY_SUFFIX)
|
|
520
537
|
) {
|
|
521
538
|
return importee;
|
|
522
539
|
}
|
|
@@ -549,6 +566,10 @@ function getResolveId(extensions) {
|
|
|
549
566
|
if (!resolved || resolved.external) {
|
|
550
567
|
return resolved;
|
|
551
568
|
}
|
|
569
|
+
if (resolveOptions.isEntry) {
|
|
570
|
+
// We must not precede entry proxies with a `\0` as that will mess up relative external resolution
|
|
571
|
+
return resolved.id + ENTRY_SUFFIX;
|
|
572
|
+
}
|
|
552
573
|
const {
|
|
553
574
|
meta: { commonjs: commonjsMeta }
|
|
554
575
|
} = await this.load(resolved);
|
|
@@ -559,7 +580,7 @@ function getResolveId(extensions) {
|
|
|
559
580
|
};
|
|
560
581
|
}
|
|
561
582
|
|
|
562
|
-
function
|
|
583
|
+
function getRequireResolver(extensions, detectCyclesAndConditional) {
|
|
563
584
|
const knownCjsModuleTypes = Object.create(null);
|
|
564
585
|
const requiredIds = Object.create(null);
|
|
565
586
|
const unconditionallyRequiredIds = Object.create(null);
|
|
@@ -583,11 +604,7 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
|
|
|
583
604
|
|
|
584
605
|
const getTypeForFullyAnalyzedModule = (id) => {
|
|
585
606
|
const knownType = knownCjsModuleTypes[id];
|
|
586
|
-
if (
|
|
587
|
-
knownType === IS_WRAPPED_COMMONJS ||
|
|
588
|
-
!detectCyclesAndConditional ||
|
|
589
|
-
fullyAnalyzedModules[id]
|
|
590
|
-
) {
|
|
607
|
+
if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
|
|
591
608
|
return knownType;
|
|
592
609
|
}
|
|
593
610
|
fullyAnalyzedModules[id] = true;
|
|
@@ -597,26 +614,80 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
|
|
|
597
614
|
return knownType;
|
|
598
615
|
};
|
|
599
616
|
|
|
617
|
+
const setInitialParentType = (id, initialCommonJSType) => {
|
|
618
|
+
// It is possible a transformed module is already fully analyzed when using
|
|
619
|
+
// the cache and one dependency introduces a new cycle. Then transform is
|
|
620
|
+
// run for a fully analzyed module again. Fully analyzed modules may never
|
|
621
|
+
// change their type as importers already trust their type.
|
|
622
|
+
knownCjsModuleTypes[id] = fullyAnalyzedModules[id]
|
|
623
|
+
? knownCjsModuleTypes[id]
|
|
624
|
+
: initialCommonJSType;
|
|
625
|
+
if (
|
|
626
|
+
detectCyclesAndConditional &&
|
|
627
|
+
knownCjsModuleTypes[id] === true &&
|
|
628
|
+
requiredIds[id] &&
|
|
629
|
+
!unconditionallyRequiredIds[id]
|
|
630
|
+
) {
|
|
631
|
+
knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
const setTypesForRequiredModules = async (parentId, resolved, isConditional, loadModule) => {
|
|
636
|
+
const childId = resolved.id;
|
|
637
|
+
requiredIds[childId] = true;
|
|
638
|
+
if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
|
|
639
|
+
unconditionallyRequiredIds[childId] = true;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
getDependencies(parentId).add(childId);
|
|
643
|
+
if (!isCyclic(childId)) {
|
|
644
|
+
// This makes sure the current transform handler waits for all direct dependencies to be
|
|
645
|
+
// loaded and transformed and therefore for all transitive CommonJS dependencies to be
|
|
646
|
+
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
|
|
647
|
+
await loadModule(resolved);
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
|
|
600
651
|
return {
|
|
601
652
|
getWrappedIds: () =>
|
|
602
653
|
Object.keys(knownCjsModuleTypes).filter(
|
|
603
654
|
(id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
|
|
604
655
|
),
|
|
605
656
|
isRequiredId: (id) => requiredIds[id],
|
|
606
|
-
|
|
657
|
+
async shouldTransformCachedModule({ id: parentId, meta: { commonjs: parentMeta } }) {
|
|
658
|
+
// Ignore modules that did not pass through the original transformer in a previous build
|
|
659
|
+
if (!(parentMeta && parentMeta.requires)) {
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
setInitialParentType(parentId, parentMeta.initialCommonJSType);
|
|
663
|
+
await Promise.all(
|
|
664
|
+
parentMeta.requires.map(({ resolved, isConditional }) =>
|
|
665
|
+
setTypesForRequiredModules(parentId, resolved, isConditional, this.load)
|
|
666
|
+
)
|
|
667
|
+
);
|
|
668
|
+
if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
|
|
669
|
+
return true;
|
|
670
|
+
}
|
|
671
|
+
for (const {
|
|
672
|
+
resolved: { id }
|
|
673
|
+
} of parentMeta.requires) {
|
|
674
|
+
if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
|
|
675
|
+
return true;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
return false;
|
|
679
|
+
},
|
|
680
|
+
/* eslint-disable no-param-reassign */
|
|
681
|
+
resolveRequireSourcesAndUpdateMeta: (rollupContext) => async (
|
|
607
682
|
parentId,
|
|
608
683
|
isParentCommonJS,
|
|
684
|
+
parentMeta,
|
|
609
685
|
sources
|
|
610
686
|
) => {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
requiredIds[parentId] &&
|
|
616
|
-
!unconditionallyRequiredIds[parentId]
|
|
617
|
-
) {
|
|
618
|
-
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
619
|
-
}
|
|
687
|
+
parentMeta.initialCommonJSType = isParentCommonJS;
|
|
688
|
+
parentMeta.requires = [];
|
|
689
|
+
parentMeta.isRequiredCommonJS = Object.create(null);
|
|
690
|
+
setInitialParentType(parentId, isParentCommonJS);
|
|
620
691
|
const requireTargets = await Promise.all(
|
|
621
692
|
sources.map(async ({ source, isConditional }) => {
|
|
622
693
|
// Never analyze or proxy internal modules
|
|
@@ -634,59 +705,58 @@ function getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndCondition
|
|
|
634
705
|
if (resolved.external) {
|
|
635
706
|
return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
|
|
636
707
|
}
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
unconditionallyRequiredIds[childId] = true;
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
getDependencies(parentId).add(childId);
|
|
643
|
-
if (!isCyclic(childId)) {
|
|
644
|
-
// This makes sure the current transform handler waits for all direct dependencies to be
|
|
645
|
-
// loaded and transformed and therefore for all transitive CommonJS dependencies to be
|
|
646
|
-
// loaded as well so that all cycles have been found and knownCjsModuleTypes is reliable.
|
|
647
|
-
await rollupContext.load(resolved);
|
|
648
|
-
} else if (detectCyclesAndConditional && knownCjsModuleTypes[parentId]) {
|
|
649
|
-
knownCjsModuleTypes[parentId] = IS_WRAPPED_COMMONJS;
|
|
650
|
-
}
|
|
708
|
+
parentMeta.requires.push({ resolved, isConditional });
|
|
709
|
+
await setTypesForRequiredModules(parentId, resolved, isConditional, rollupContext.load);
|
|
651
710
|
return { id: childId, allowProxy: true };
|
|
652
711
|
})
|
|
653
712
|
);
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
713
|
+
parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
|
|
714
|
+
return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
|
|
715
|
+
// eslint-disable-next-line no-multi-assign
|
|
716
|
+
const isCommonJS = (parentMeta.isRequiredCommonJS[
|
|
717
|
+
dependencyId
|
|
718
|
+
] = getTypeForFullyAnalyzedModule(dependencyId));
|
|
719
|
+
return {
|
|
720
|
+
source: sources[index].source,
|
|
721
|
+
id: allowProxy
|
|
722
|
+
? isCommonJS === IS_WRAPPED_COMMONJS
|
|
723
|
+
? wrapId(dependencyId, WRAPPED_SUFFIX)
|
|
724
|
+
: wrapId(dependencyId, PROXY_SUFFIX)
|
|
725
|
+
: dependencyId,
|
|
726
|
+
isCommonJS
|
|
727
|
+
};
|
|
728
|
+
});
|
|
669
729
|
}
|
|
670
730
|
};
|
|
671
731
|
}
|
|
672
732
|
|
|
673
|
-
function
|
|
674
|
-
const
|
|
675
|
-
const versionRegexp = /\^(\d+\.\d+)\.\d+/g;
|
|
733
|
+
function validateVersion(actualVersion, peerDependencyVersion, name) {
|
|
734
|
+
const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
|
|
676
735
|
let minMajor = Infinity;
|
|
677
736
|
let minMinor = Infinity;
|
|
737
|
+
let minPatch = Infinity;
|
|
678
738
|
let foundVersion;
|
|
679
739
|
// eslint-disable-next-line no-cond-assign
|
|
680
740
|
while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
|
|
681
|
-
const [foundMajor, foundMinor] = foundVersion[1].split('.').map(Number);
|
|
741
|
+
const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
|
|
682
742
|
if (foundMajor < minMajor) {
|
|
683
743
|
minMajor = foundMajor;
|
|
684
744
|
minMinor = foundMinor;
|
|
745
|
+
minPatch = foundPatch;
|
|
685
746
|
}
|
|
686
747
|
}
|
|
687
|
-
if (
|
|
748
|
+
if (!actualVersion) {
|
|
688
749
|
throw new Error(
|
|
689
|
-
`Insufficient
|
|
750
|
+
`Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
const [major, minor, patch] = actualVersion.split('.').map(Number);
|
|
754
|
+
if (
|
|
755
|
+
major < minMajor ||
|
|
756
|
+
(major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
|
|
757
|
+
) {
|
|
758
|
+
throw new Error(
|
|
759
|
+
`Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
|
|
690
760
|
);
|
|
691
761
|
}
|
|
692
762
|
}
|
|
@@ -1129,17 +1199,20 @@ function getRequireHandlers() {
|
|
|
1129
1199
|
exportsName,
|
|
1130
1200
|
id,
|
|
1131
1201
|
exportMode,
|
|
1132
|
-
|
|
1202
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1133
1203
|
needsRequireWrapper,
|
|
1134
1204
|
isEsModule,
|
|
1135
|
-
|
|
1136
|
-
getIgnoreTryCatchRequireStatementMode
|
|
1205
|
+
isDynamicRequireModulesEnabled,
|
|
1206
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1207
|
+
commonjsMeta
|
|
1137
1208
|
) {
|
|
1138
1209
|
const imports = [];
|
|
1139
1210
|
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1140
|
-
if (
|
|
1211
|
+
if (dynamicRequireName) {
|
|
1141
1212
|
imports.push(
|
|
1142
|
-
`import {
|
|
1213
|
+
`import { ${
|
|
1214
|
+
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1215
|
+
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}";`
|
|
1143
1216
|
);
|
|
1144
1217
|
}
|
|
1145
1218
|
if (exportMode === 'module') {
|
|
@@ -1154,9 +1227,10 @@ function getRequireHandlers() {
|
|
|
1154
1227
|
);
|
|
1155
1228
|
}
|
|
1156
1229
|
const requiresBySource = collectSources(requireExpressions);
|
|
1157
|
-
const
|
|
1230
|
+
const requireTargets = await resolveRequireSourcesAndUpdateMeta(
|
|
1158
1231
|
id,
|
|
1159
1232
|
needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
|
|
1233
|
+
commonjsMeta,
|
|
1160
1234
|
Object.keys(requiresBySource).map((source) => {
|
|
1161
1235
|
return {
|
|
1162
1236
|
source,
|
|
@@ -1171,10 +1245,7 @@ function getRequireHandlers() {
|
|
|
1171
1245
|
getIgnoreTryCatchRequireStatementMode,
|
|
1172
1246
|
magicString
|
|
1173
1247
|
);
|
|
1174
|
-
return {
|
|
1175
|
-
importBlock: imports.length ? `${imports.join('\n')}\n\n` : '',
|
|
1176
|
-
usesRequireWrapper
|
|
1177
|
-
};
|
|
1248
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1178
1249
|
}
|
|
1179
1250
|
|
|
1180
1251
|
return {
|
|
@@ -1277,9 +1348,10 @@ async function transformCommonjs(
|
|
|
1277
1348
|
astCache,
|
|
1278
1349
|
defaultIsModuleExports,
|
|
1279
1350
|
needsRequireWrapper,
|
|
1280
|
-
|
|
1351
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1281
1352
|
isRequired,
|
|
1282
|
-
checkDynamicRequire
|
|
1353
|
+
checkDynamicRequire,
|
|
1354
|
+
commonjsMeta
|
|
1283
1355
|
) {
|
|
1284
1356
|
const ast = astCache || tryParse(parse, code, id);
|
|
1285
1357
|
const magicString = new MagicString__default["default"](code);
|
|
@@ -1428,12 +1500,6 @@ async function transformCommonjs(
|
|
|
1428
1500
|
checkDynamicRequire(node.start);
|
|
1429
1501
|
uses.require = true;
|
|
1430
1502
|
const requireNode = node.callee.object;
|
|
1431
|
-
magicString.appendLeft(
|
|
1432
|
-
node.end - 1,
|
|
1433
|
-
`,${JSON.stringify(
|
|
1434
|
-
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1435
|
-
)}`
|
|
1436
|
-
);
|
|
1437
1503
|
replacedDynamicRequires.push(requireNode);
|
|
1438
1504
|
return;
|
|
1439
1505
|
}
|
|
@@ -1448,12 +1514,6 @@ async function transformCommonjs(
|
|
|
1448
1514
|
if (hasDynamicArguments(node)) {
|
|
1449
1515
|
if (isDynamicRequireModulesEnabled) {
|
|
1450
1516
|
checkDynamicRequire(node.start);
|
|
1451
|
-
magicString.appendLeft(
|
|
1452
|
-
node.end - 1,
|
|
1453
|
-
`, ${JSON.stringify(
|
|
1454
|
-
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1455
|
-
)}`
|
|
1456
|
-
);
|
|
1457
1517
|
}
|
|
1458
1518
|
if (!ignoreDynamicRequires) {
|
|
1459
1519
|
replacedDynamicRequires.push(node.callee);
|
|
@@ -1596,9 +1656,10 @@ async function transformCommonjs(
|
|
|
1596
1656
|
if (scope.contains(flattened.name)) return;
|
|
1597
1657
|
|
|
1598
1658
|
if (
|
|
1599
|
-
|
|
1600
|
-
flattened.keypath === 'module' ||
|
|
1601
|
-
|
|
1659
|
+
!isEsModule &&
|
|
1660
|
+
(flattened.keypath === 'module.exports' ||
|
|
1661
|
+
flattened.keypath === 'module' ||
|
|
1662
|
+
flattened.keypath === 'exports')
|
|
1602
1663
|
) {
|
|
1603
1664
|
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1604
1665
|
storeName: false
|
|
@@ -1626,7 +1687,13 @@ async function transformCommonjs(
|
|
|
1626
1687
|
const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
|
|
1627
1688
|
const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
|
|
1628
1689
|
const helpersName = deconflict([scope], globals, 'commonjsHelpers');
|
|
1629
|
-
const dynamicRequireName =
|
|
1690
|
+
const dynamicRequireName =
|
|
1691
|
+
replacedDynamicRequires.length > 0 &&
|
|
1692
|
+
deconflict(
|
|
1693
|
+
[scope],
|
|
1694
|
+
globals,
|
|
1695
|
+
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1696
|
+
);
|
|
1630
1697
|
const deconflictedExportNames = Object.create(null);
|
|
1631
1698
|
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1632
1699
|
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
@@ -1638,10 +1705,17 @@ async function transformCommonjs(
|
|
|
1638
1705
|
});
|
|
1639
1706
|
}
|
|
1640
1707
|
for (const node of replacedDynamicRequires) {
|
|
1641
|
-
magicString.overwrite(
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1708
|
+
magicString.overwrite(
|
|
1709
|
+
node.start,
|
|
1710
|
+
node.end,
|
|
1711
|
+
isDynamicRequireModulesEnabled
|
|
1712
|
+
? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
|
|
1713
|
+
: dynamicRequireName,
|
|
1714
|
+
{
|
|
1715
|
+
contentOnly: true,
|
|
1716
|
+
storeName: true
|
|
1717
|
+
}
|
|
1718
|
+
);
|
|
1645
1719
|
}
|
|
1646
1720
|
|
|
1647
1721
|
// We cannot wrap ES/mixed modules
|
|
@@ -1661,7 +1735,7 @@ async function transformCommonjs(
|
|
|
1661
1735
|
) &&
|
|
1662
1736
|
(ignoreGlobal || !uses.global)
|
|
1663
1737
|
) {
|
|
1664
|
-
return { meta: { commonjs: { isCommonJS: false
|
|
1738
|
+
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1665
1739
|
}
|
|
1666
1740
|
|
|
1667
1741
|
let leadingComment = '';
|
|
@@ -1683,7 +1757,7 @@ async function transformCommonjs(
|
|
|
1683
1757
|
? 'exports'
|
|
1684
1758
|
: 'module';
|
|
1685
1759
|
|
|
1686
|
-
const
|
|
1760
|
+
const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1687
1761
|
magicString,
|
|
1688
1762
|
topLevelDeclarations,
|
|
1689
1763
|
reassignedNames,
|
|
@@ -1693,12 +1767,14 @@ async function transformCommonjs(
|
|
|
1693
1767
|
exportsName,
|
|
1694
1768
|
id,
|
|
1695
1769
|
exportMode,
|
|
1696
|
-
|
|
1770
|
+
resolveRequireSourcesAndUpdateMeta,
|
|
1697
1771
|
needsRequireWrapper,
|
|
1698
1772
|
isEsModule,
|
|
1699
|
-
|
|
1700
|
-
getIgnoreTryCatchRequireStatementMode
|
|
1773
|
+
isDynamicRequireModulesEnabled,
|
|
1774
|
+
getIgnoreTryCatchRequireStatementMode,
|
|
1775
|
+
commonjsMeta
|
|
1701
1776
|
);
|
|
1777
|
+
const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
|
|
1702
1778
|
const exportBlock = isEsModule
|
|
1703
1779
|
? ''
|
|
1704
1780
|
: rewriteExportsAndGetExportsBlock(
|
|
@@ -1751,12 +1827,7 @@ function ${requireName} () {
|
|
|
1751
1827
|
code: magicString.toString(),
|
|
1752
1828
|
map: sourceMap ? magicString.generateMap() : null,
|
|
1753
1829
|
syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
|
|
1754
|
-
meta: {
|
|
1755
|
-
commonjs: {
|
|
1756
|
-
isCommonJS: !isEsModule && (usesRequireWrapper ? IS_WRAPPED_COMMONJS : true),
|
|
1757
|
-
isMixedModule: isEsModule
|
|
1758
|
-
}
|
|
1759
|
-
}
|
|
1830
|
+
meta: { commonjs: commonjsMeta }
|
|
1760
1831
|
};
|
|
1761
1832
|
}
|
|
1762
1833
|
|
|
@@ -1787,11 +1858,6 @@ function commonjs(options = {}) {
|
|
|
1787
1858
|
const defaultIsModuleExports =
|
|
1788
1859
|
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
|
|
1789
1860
|
|
|
1790
|
-
const {
|
|
1791
|
-
resolveRequireSourcesAndGetMeta,
|
|
1792
|
-
getWrappedIds,
|
|
1793
|
-
isRequiredId
|
|
1794
|
-
} = getResolveRequireSourcesAndGetMeta(extensions, detectCyclesAndConditional);
|
|
1795
1861
|
const dynamicRequireRoot =
|
|
1796
1862
|
typeof options.dynamicRequireRoot === 'string'
|
|
1797
1863
|
? path.resolve(options.dynamicRequireRoot)
|
|
@@ -1802,9 +1868,6 @@ function commonjs(options = {}) {
|
|
|
1802
1868
|
);
|
|
1803
1869
|
const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
|
|
1804
1870
|
|
|
1805
|
-
const esModulesWithDefaultExport = new Set();
|
|
1806
|
-
const esModulesWithNamedExports = new Set();
|
|
1807
|
-
|
|
1808
1871
|
const ignoreRequire =
|
|
1809
1872
|
typeof options.ignore === 'function'
|
|
1810
1873
|
? options.ignore
|
|
@@ -1832,25 +1895,31 @@ function commonjs(options = {}) {
|
|
|
1832
1895
|
|
|
1833
1896
|
const sourceMap = options.sourceMap !== false;
|
|
1834
1897
|
|
|
1898
|
+
// Initialized in buildStart
|
|
1899
|
+
let requireResolver;
|
|
1900
|
+
|
|
1835
1901
|
function transformAndCheckExports(code, id) {
|
|
1836
1902
|
const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
|
|
1837
1903
|
this.parse,
|
|
1838
1904
|
code,
|
|
1839
1905
|
id
|
|
1840
1906
|
);
|
|
1907
|
+
|
|
1908
|
+
const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
|
|
1841
1909
|
if (hasDefaultExport) {
|
|
1842
|
-
|
|
1910
|
+
commonjsMeta.hasDefaultExport = true;
|
|
1843
1911
|
}
|
|
1844
1912
|
if (hasNamedExports) {
|
|
1845
|
-
|
|
1913
|
+
commonjsMeta.hasNamedExports = true;
|
|
1846
1914
|
}
|
|
1847
1915
|
|
|
1848
1916
|
if (
|
|
1849
1917
|
!dynamicRequireModules.has(normalizePathSlashes(id)) &&
|
|
1850
|
-
(!(hasCjsKeywords(code, ignoreGlobal) || isRequiredId(id)) ||
|
|
1918
|
+
(!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
|
|
1851
1919
|
(isEsModule && !options.transformMixedEsModules))
|
|
1852
1920
|
) {
|
|
1853
|
-
|
|
1921
|
+
commonjsMeta.isCommonJS = false;
|
|
1922
|
+
return { meta: { commonjs: commonjsMeta } };
|
|
1854
1923
|
}
|
|
1855
1924
|
|
|
1856
1925
|
const needsRequireWrapper =
|
|
@@ -1889,9 +1958,10 @@ function commonjs(options = {}) {
|
|
|
1889
1958
|
ast,
|
|
1890
1959
|
defaultIsModuleExports,
|
|
1891
1960
|
needsRequireWrapper,
|
|
1892
|
-
|
|
1893
|
-
isRequiredId(id),
|
|
1894
|
-
checkDynamicRequire
|
|
1961
|
+
requireResolver.resolveRequireSourcesAndUpdateMeta(this),
|
|
1962
|
+
requireResolver.isRequiredId(id),
|
|
1963
|
+
checkDynamicRequire,
|
|
1964
|
+
commonjsMeta
|
|
1895
1965
|
);
|
|
1896
1966
|
}
|
|
1897
1967
|
|
|
@@ -1915,18 +1985,23 @@ function commonjs(options = {}) {
|
|
|
1915
1985
|
return { ...rawOptions, plugins };
|
|
1916
1986
|
},
|
|
1917
1987
|
|
|
1918
|
-
buildStart() {
|
|
1919
|
-
|
|
1988
|
+
buildStart({ plugins }) {
|
|
1989
|
+
validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
|
|
1990
|
+
const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
|
|
1991
|
+
if (nodeResolve) {
|
|
1992
|
+
validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
|
|
1993
|
+
}
|
|
1920
1994
|
if (options.namedExports != null) {
|
|
1921
1995
|
this.warn(
|
|
1922
1996
|
'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
|
|
1923
1997
|
);
|
|
1924
1998
|
}
|
|
1999
|
+
requireResolver = getRequireResolver(extensions, detectCyclesAndConditional);
|
|
1925
2000
|
},
|
|
1926
2001
|
|
|
1927
2002
|
buildEnd() {
|
|
1928
2003
|
if (options.strictRequires === 'debug') {
|
|
1929
|
-
const wrappedIds = getWrappedIds();
|
|
2004
|
+
const wrappedIds = requireResolver.getWrappedIds();
|
|
1930
2005
|
if (wrappedIds.length) {
|
|
1931
2006
|
this.warn({
|
|
1932
2007
|
code: 'WRAPPED_IDS',
|
|
@@ -1975,6 +2050,11 @@ function commonjs(options = {}) {
|
|
|
1975
2050
|
);
|
|
1976
2051
|
}
|
|
1977
2052
|
|
|
2053
|
+
// entry suffix is just appended to not mess up relative external resolution
|
|
2054
|
+
if (id.endsWith(ENTRY_SUFFIX)) {
|
|
2055
|
+
return getEntryProxy(id.slice(0, -ENTRY_SUFFIX.length), defaultIsModuleExports, this.load);
|
|
2056
|
+
}
|
|
2057
|
+
|
|
1978
2058
|
if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
|
|
1979
2059
|
return getEsImportProxy(unwrapId(id, ES_IMPORT_SUFFIX), defaultIsModuleExports);
|
|
1980
2060
|
}
|
|
@@ -1990,18 +2070,16 @@ function commonjs(options = {}) {
|
|
|
1990
2070
|
|
|
1991
2071
|
if (isWrappedId(id, PROXY_SUFFIX)) {
|
|
1992
2072
|
const actualId = unwrapId(id, PROXY_SUFFIX);
|
|
1993
|
-
return getStaticRequireProxy(
|
|
1994
|
-
actualId,
|
|
1995
|
-
getRequireReturnsDefault(actualId),
|
|
1996
|
-
esModulesWithDefaultExport,
|
|
1997
|
-
esModulesWithNamedExports,
|
|
1998
|
-
this.load
|
|
1999
|
-
);
|
|
2073
|
+
return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
|
|
2000
2074
|
}
|
|
2001
2075
|
|
|
2002
2076
|
return null;
|
|
2003
2077
|
},
|
|
2004
2078
|
|
|
2079
|
+
shouldTransformCachedModule(...args) {
|
|
2080
|
+
return requireResolver.shouldTransformCachedModule.call(this, ...args);
|
|
2081
|
+
},
|
|
2082
|
+
|
|
2005
2083
|
transform(code, id) {
|
|
2006
2084
|
const extName = path.extname(id);
|
|
2007
2085
|
if (extName !== '.cjs' && (!filter(id) || !extensions.includes(extName))) {
|