@rollup/plugin-commonjs 18.1.0 → 19.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 +8 -0
- package/dist/index.es.js +380 -321
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +380 -321
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.es.js
CHANGED
|
@@ -9,7 +9,7 @@ import isReference from 'is-reference';
|
|
|
9
9
|
import { sync } from 'resolve';
|
|
10
10
|
|
|
11
11
|
var peerDependencies = {
|
|
12
|
-
rollup: "^2.
|
|
12
|
+
rollup: "^2.38.3"
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
function tryParse(parse, code, id) {
|
|
@@ -83,6 +83,8 @@ const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
|
83
83
|
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
84
84
|
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
85
85
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
86
|
+
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
87
|
+
const MODULE_SUFFIX = '?commonjs-module';
|
|
86
88
|
|
|
87
89
|
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
88
90
|
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
@@ -129,25 +131,20 @@ export function getAugmentedNamespace(n) {
|
|
|
129
131
|
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.');`;
|
|
130
132
|
|
|
131
133
|
const HELPER_NON_DYNAMIC = `
|
|
132
|
-
export function createCommonjsModule(fn) {
|
|
133
|
-
var module = { exports: {} }
|
|
134
|
-
return fn(module, module.exports), module.exports;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
134
|
export function commonjsRequire (path) {
|
|
138
135
|
${FAILED_REQUIRE_ERROR}
|
|
139
136
|
}
|
|
140
137
|
`;
|
|
141
138
|
|
|
142
139
|
const getDynamicHelpers = (ignoreDynamicRequires) => `
|
|
143
|
-
export function
|
|
144
|
-
return
|
|
145
|
-
path:
|
|
140
|
+
export function createModule(modulePath) {
|
|
141
|
+
return {
|
|
142
|
+
path: modulePath,
|
|
146
143
|
exports: {},
|
|
147
144
|
require: function (path, base) {
|
|
148
|
-
return commonjsRequire(path,
|
|
145
|
+
return commonjsRequire(path, base == null ? modulePath : base);
|
|
149
146
|
}
|
|
150
|
-
}
|
|
147
|
+
};
|
|
151
148
|
}
|
|
152
149
|
|
|
153
150
|
export function commonjsRegister (path, loader) {
|
|
@@ -326,16 +323,20 @@ function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires)
|
|
|
326
323
|
|
|
327
324
|
/* eslint-disable import/prefer-default-export */
|
|
328
325
|
|
|
329
|
-
function deconflict(
|
|
326
|
+
function deconflict(scopes, globals, identifier) {
|
|
330
327
|
let i = 1;
|
|
331
328
|
let deconflicted = makeLegalIdentifier(identifier);
|
|
329
|
+
const hasConflicts = () =>
|
|
330
|
+
scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
|
|
332
331
|
|
|
333
|
-
while (
|
|
332
|
+
while (hasConflicts()) {
|
|
334
333
|
deconflicted = makeLegalIdentifier(`${identifier}_${i}`);
|
|
335
334
|
i += 1;
|
|
336
335
|
}
|
|
337
|
-
|
|
338
|
-
scope
|
|
336
|
+
|
|
337
|
+
for (const scope of scopes) {
|
|
338
|
+
scope.declarations[deconflicted] = true;
|
|
339
|
+
}
|
|
339
340
|
|
|
340
341
|
return deconflicted;
|
|
341
342
|
}
|
|
@@ -397,28 +398,18 @@ function getDynamicPackagesEntryIntro(
|
|
|
397
398
|
) {
|
|
398
399
|
let dynamicImports = Array.from(
|
|
399
400
|
dynamicRequireModuleSet,
|
|
400
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
401
|
+
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
|
|
401
402
|
).join('\n');
|
|
402
403
|
|
|
403
404
|
if (dynamicRequireModuleDirPaths.length) {
|
|
404
|
-
dynamicImports += `require(${JSON.stringify(
|
|
405
|
+
dynamicImports += `require(${JSON.stringify(
|
|
406
|
+
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
|
|
407
|
+
)});`;
|
|
405
408
|
}
|
|
406
409
|
|
|
407
410
|
return dynamicImports;
|
|
408
411
|
}
|
|
409
412
|
|
|
410
|
-
function wrapModuleRegisterProxy(id) {
|
|
411
|
-
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
function unwrapModuleRegisterProxy(id) {
|
|
415
|
-
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
function isModuleRegisterProxy(id) {
|
|
419
|
-
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
413
|
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
423
414
|
const normalizedPath = normalizePathSlashes(id);
|
|
424
415
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
@@ -451,37 +442,37 @@ function getDynamicRequirePaths(patterns) {
|
|
|
451
442
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
452
443
|
}
|
|
453
444
|
|
|
454
|
-
function
|
|
455
|
-
let
|
|
456
|
-
if (
|
|
445
|
+
function getCommonJSMetaPromise(commonJSMetaPromises, id) {
|
|
446
|
+
let commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
447
|
+
if (commonJSMetaPromise) return commonJSMetaPromise.promise;
|
|
457
448
|
|
|
458
449
|
const promise = new Promise((resolve) => {
|
|
459
|
-
|
|
450
|
+
commonJSMetaPromise = {
|
|
460
451
|
resolve,
|
|
461
452
|
promise: null
|
|
462
453
|
};
|
|
463
|
-
|
|
454
|
+
commonJSMetaPromises.set(id, commonJSMetaPromise);
|
|
464
455
|
});
|
|
465
|
-
|
|
456
|
+
commonJSMetaPromise.promise = promise;
|
|
466
457
|
|
|
467
458
|
return promise;
|
|
468
459
|
}
|
|
469
460
|
|
|
470
|
-
function
|
|
471
|
-
const
|
|
472
|
-
if (
|
|
473
|
-
if (
|
|
474
|
-
|
|
475
|
-
|
|
461
|
+
function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
|
|
462
|
+
const commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
463
|
+
if (commonJSMetaPromise) {
|
|
464
|
+
if (commonJSMetaPromise.resolve) {
|
|
465
|
+
commonJSMetaPromise.resolve(commonjsMeta);
|
|
466
|
+
commonJSMetaPromise.resolve = null;
|
|
476
467
|
}
|
|
477
468
|
} else {
|
|
478
|
-
|
|
469
|
+
commonJSMetaPromises.set(id, { promise: Promise.resolve(commonjsMeta), resolve: null });
|
|
479
470
|
}
|
|
480
471
|
}
|
|
481
472
|
|
|
482
473
|
// e.g. id === "commonjsHelpers?commonjsRegister"
|
|
483
474
|
function getSpecificHelperProxy(id) {
|
|
484
|
-
return `export {${id.split('?')[1]} as default} from
|
|
475
|
+
return `export {${id.split('?')[1]} as default} from "${HELPERS_ID}";`;
|
|
485
476
|
}
|
|
486
477
|
|
|
487
478
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
@@ -522,16 +513,16 @@ async function getStaticRequireProxy(
|
|
|
522
513
|
requireReturnsDefault,
|
|
523
514
|
esModulesWithDefaultExport,
|
|
524
515
|
esModulesWithNamedExports,
|
|
525
|
-
|
|
516
|
+
commonJsMetaPromises
|
|
526
517
|
) {
|
|
527
518
|
const name = getName(id);
|
|
528
|
-
const
|
|
529
|
-
if (
|
|
530
|
-
return `
|
|
531
|
-
} else if (
|
|
519
|
+
const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
|
|
520
|
+
if (commonjsMeta && commonjsMeta.isCommonJS) {
|
|
521
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
522
|
+
} else if (commonjsMeta === null) {
|
|
532
523
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
533
524
|
} else if (!requireReturnsDefault) {
|
|
534
|
-
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
525
|
+
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
535
526
|
id
|
|
536
527
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
537
528
|
} else if (
|
|
@@ -542,7 +533,7 @@ async function getStaticRequireProxy(
|
|
|
542
533
|
) {
|
|
543
534
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
544
535
|
}
|
|
545
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
536
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
546
537
|
}
|
|
547
538
|
|
|
548
539
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
@@ -579,12 +570,17 @@ function getResolveId(extensions) {
|
|
|
579
570
|
}
|
|
580
571
|
|
|
581
572
|
return function resolveId(importee, rawImporter) {
|
|
573
|
+
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
|
|
574
|
+
return importee;
|
|
575
|
+
}
|
|
576
|
+
|
|
582
577
|
const importer =
|
|
583
|
-
rawImporter &&
|
|
584
|
-
?
|
|
578
|
+
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
579
|
+
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
585
580
|
: rawImporter;
|
|
586
581
|
|
|
587
|
-
//
|
|
582
|
+
// Except for exports, proxies are only importing resolved ids,
|
|
583
|
+
// no need to resolve again
|
|
588
584
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
589
585
|
return importee;
|
|
590
586
|
}
|
|
@@ -598,9 +594,9 @@ function getResolveId(extensions) {
|
|
|
598
594
|
} else if (isRequiredModule) {
|
|
599
595
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
600
596
|
|
|
601
|
-
isModuleRegistration =
|
|
597
|
+
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
602
598
|
if (isModuleRegistration) {
|
|
603
|
-
importee =
|
|
599
|
+
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
604
600
|
}
|
|
605
601
|
}
|
|
606
602
|
|
|
@@ -627,7 +623,7 @@ function getResolveId(extensions) {
|
|
|
627
623
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
628
624
|
resolved.external = false;
|
|
629
625
|
} else if (resolved && isModuleRegistration) {
|
|
630
|
-
resolved.id =
|
|
626
|
+
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
631
627
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
632
628
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
633
629
|
}
|
|
@@ -727,8 +723,6 @@ function isDefineCompiledEsm(node) {
|
|
|
727
723
|
}
|
|
728
724
|
|
|
729
725
|
function getDefinePropertyCallName(node, targetName) {
|
|
730
|
-
const targetNames = targetName.split('.');
|
|
731
|
-
|
|
732
726
|
const {
|
|
733
727
|
callee: { object, property }
|
|
734
728
|
} = node;
|
|
@@ -736,6 +730,7 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
736
730
|
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
|
|
737
731
|
if (node.arguments.length !== 3) return;
|
|
738
732
|
|
|
733
|
+
const targetNames = targetName.split('.');
|
|
739
734
|
const [target, key, value] = node.arguments;
|
|
740
735
|
if (targetNames.length === 1) {
|
|
741
736
|
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
|
|
@@ -762,128 +757,185 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
762
757
|
return { key: key.value, value: valueProperty.value };
|
|
763
758
|
}
|
|
764
759
|
|
|
765
|
-
function isLocallyShadowed(name, scope) {
|
|
766
|
-
while (scope.parent) {
|
|
767
|
-
if (scope.declarations[name]) {
|
|
768
|
-
return true;
|
|
769
|
-
}
|
|
770
|
-
// eslint-disable-next-line no-param-reassign
|
|
771
|
-
scope = scope.parent;
|
|
772
|
-
}
|
|
773
|
-
return false;
|
|
774
|
-
}
|
|
775
|
-
|
|
776
760
|
function isShorthandProperty(parent) {
|
|
777
761
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
778
762
|
}
|
|
779
763
|
|
|
780
|
-
function wrapCode(magicString, uses, moduleName,
|
|
781
|
-
const args =
|
|
782
|
-
|
|
764
|
+
function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
765
|
+
const args = [];
|
|
766
|
+
const passedArgs = [];
|
|
767
|
+
if (uses.module) {
|
|
768
|
+
args.push('module');
|
|
769
|
+
passedArgs.push(moduleName);
|
|
770
|
+
}
|
|
771
|
+
if (uses.exports) {
|
|
772
|
+
args.push('exports');
|
|
773
|
+
passedArgs.push(exportsName);
|
|
774
|
+
}
|
|
783
775
|
magicString
|
|
784
776
|
.trim()
|
|
785
|
-
.prepend(`
|
|
786
|
-
.append(
|
|
787
|
-
`\n}${virtualDynamicRequirePath ? `, ${JSON.stringify(virtualDynamicRequirePath)}` : ''});`
|
|
788
|
-
);
|
|
777
|
+
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
778
|
+
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
789
779
|
}
|
|
790
780
|
|
|
791
781
|
function rewriteExportsAndGetExportsBlock(
|
|
792
782
|
magicString,
|
|
793
783
|
moduleName,
|
|
784
|
+
exportsName,
|
|
794
785
|
wrapped,
|
|
795
|
-
|
|
796
|
-
|
|
786
|
+
moduleExportsAssignments,
|
|
787
|
+
firstTopLevelModuleExportsAssignment,
|
|
788
|
+
exportsAssignmentsByName,
|
|
789
|
+
topLevelAssignments,
|
|
797
790
|
defineCompiledEsmExpressions,
|
|
798
|
-
|
|
799
|
-
isRestorableCompiledEsm,
|
|
791
|
+
deconflictedExportNames,
|
|
800
792
|
code,
|
|
801
|
-
uses,
|
|
802
793
|
HELPERS_NAME,
|
|
794
|
+
exportMode,
|
|
795
|
+
detectWrappedDefault,
|
|
803
796
|
defaultIsModuleExports
|
|
804
797
|
) {
|
|
805
|
-
const
|
|
806
|
-
const
|
|
807
|
-
|
|
798
|
+
const exports = [];
|
|
799
|
+
const exportDeclarations = [];
|
|
800
|
+
|
|
801
|
+
if (exportMode === 'replace') {
|
|
802
|
+
getExportsForReplacedModuleExports(
|
|
803
|
+
magicString,
|
|
804
|
+
exports,
|
|
805
|
+
exportDeclarations,
|
|
806
|
+
moduleExportsAssignments,
|
|
807
|
+
firstTopLevelModuleExportsAssignment,
|
|
808
|
+
exportsName
|
|
809
|
+
);
|
|
810
|
+
} else {
|
|
811
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
812
|
+
if (wrapped) {
|
|
813
|
+
getExportsWhenWrapping(
|
|
814
|
+
exportDeclarations,
|
|
815
|
+
exportsName,
|
|
816
|
+
detectWrappedDefault,
|
|
817
|
+
HELPERS_NAME,
|
|
818
|
+
defaultIsModuleExports
|
|
819
|
+
);
|
|
820
|
+
} else {
|
|
821
|
+
getExports(
|
|
822
|
+
magicString,
|
|
823
|
+
exports,
|
|
824
|
+
exportDeclarations,
|
|
825
|
+
moduleExportsAssignments,
|
|
826
|
+
exportsAssignmentsByName,
|
|
827
|
+
deconflictedExportNames,
|
|
828
|
+
topLevelAssignments,
|
|
829
|
+
moduleName,
|
|
830
|
+
exportsName,
|
|
831
|
+
defineCompiledEsmExpressions,
|
|
832
|
+
HELPERS_NAME,
|
|
833
|
+
defaultIsModuleExports
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
if (exports.length) {
|
|
838
|
+
exportDeclarations.push(`export { ${exports.join(', ')} };`);
|
|
839
|
+
}
|
|
808
840
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
const namedExportProperties = [];
|
|
841
|
+
return `\n\n${exportDeclarations.join('\n')}`;
|
|
842
|
+
}
|
|
812
843
|
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
844
|
+
function getExportsForReplacedModuleExports(
|
|
845
|
+
magicString,
|
|
846
|
+
exports,
|
|
847
|
+
exportDeclarations,
|
|
848
|
+
moduleExportsAssignments,
|
|
849
|
+
firstTopLevelModuleExportsAssignment,
|
|
850
|
+
exportsName
|
|
851
|
+
) {
|
|
852
|
+
for (const { left } of moduleExportsAssignments) {
|
|
853
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
854
|
+
}
|
|
855
|
+
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
856
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
857
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
858
|
+
}
|
|
818
859
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
860
|
+
function getExportsWhenWrapping(
|
|
861
|
+
exportDeclarations,
|
|
862
|
+
exportsName,
|
|
863
|
+
detectWrappedDefault,
|
|
864
|
+
HELPERS_NAME,
|
|
865
|
+
defaultIsModuleExports
|
|
866
|
+
) {
|
|
867
|
+
exportDeclarations.push(
|
|
868
|
+
`export default ${
|
|
869
|
+
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
870
|
+
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
871
|
+
: defaultIsModuleExports === false
|
|
872
|
+
? `${exportsName}.default`
|
|
873
|
+
: exportsName
|
|
874
|
+
};`
|
|
875
|
+
);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function getExports(
|
|
879
|
+
magicString,
|
|
880
|
+
exports,
|
|
881
|
+
exportDeclarations,
|
|
882
|
+
moduleExportsAssignments,
|
|
883
|
+
exportsAssignmentsByName,
|
|
884
|
+
deconflictedExportNames,
|
|
885
|
+
topLevelAssignments,
|
|
886
|
+
moduleName,
|
|
887
|
+
exportsName,
|
|
888
|
+
defineCompiledEsmExpressions,
|
|
889
|
+
HELPERS_NAME,
|
|
890
|
+
defaultIsModuleExports
|
|
891
|
+
) {
|
|
892
|
+
let deconflictedDefaultExportName;
|
|
893
|
+
// Collect and rewrite module.exports assignments
|
|
894
|
+
for (const { left } of moduleExportsAssignments) {
|
|
895
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
896
|
+
}
|
|
833
897
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
898
|
+
// Collect and rewrite named exports
|
|
899
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
900
|
+
const deconflicted = deconflictedExportNames[exportName];
|
|
901
|
+
let needsDeclaration = true;
|
|
902
|
+
for (const node of nodes) {
|
|
903
|
+
let replacement = `${deconflicted} = ${exportsName}.${exportName}`;
|
|
904
|
+
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
905
|
+
replacement = `var ${replacement}`;
|
|
906
|
+
needsDeclaration = false;
|
|
838
907
|
}
|
|
908
|
+
magicString.overwrite(node.start, node.left.end, replacement);
|
|
839
909
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
if (!hasModuleExportsAssignment) {
|
|
843
|
-
const moduleExports = `{\n${namedExportProperties.join(',\n')}\n}`;
|
|
844
|
-
magicString
|
|
845
|
-
.trim()
|
|
846
|
-
.append(
|
|
847
|
-
`\n\nvar ${moduleName} = ${
|
|
848
|
-
isRestorableCompiledEsm
|
|
849
|
-
? `/*#__PURE__*/Object.defineProperty(${moduleExports}, '__esModule', {value: true})`
|
|
850
|
-
: moduleExports
|
|
851
|
-
};`
|
|
852
|
-
);
|
|
910
|
+
if (needsDeclaration) {
|
|
911
|
+
magicString.prepend(`var ${deconflicted};\n`);
|
|
853
912
|
}
|
|
854
|
-
}
|
|
855
913
|
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
if (defaultIsModuleExports === 'auto') {
|
|
859
|
-
if (isRestorableCompiledEsm) {
|
|
860
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
|
|
861
|
-
} else if (
|
|
862
|
-
(wrapped || deconflictedDefaultExportName) &&
|
|
863
|
-
(defineCompiledEsmExpressions.length > 0 || code.includes('__esModule'))
|
|
864
|
-
) {
|
|
865
|
-
// eslint-disable-next-line no-param-reassign
|
|
866
|
-
uses.commonjsHelpers = true;
|
|
867
|
-
defaultExport.push(
|
|
868
|
-
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
|
|
869
|
-
);
|
|
914
|
+
if (exportName === 'default') {
|
|
915
|
+
deconflictedDefaultExportName = deconflicted;
|
|
870
916
|
} else {
|
|
871
|
-
|
|
872
|
-
}
|
|
873
|
-
} else if (defaultIsModuleExports === true) {
|
|
874
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
875
|
-
} else if (defaultIsModuleExports === false) {
|
|
876
|
-
if (deconflictedDefaultExportName) {
|
|
877
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName};`);
|
|
878
|
-
} else {
|
|
879
|
-
defaultExport.push(`export default ${moduleName}.default;`);
|
|
917
|
+
exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
|
|
880
918
|
}
|
|
881
919
|
}
|
|
882
920
|
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
921
|
+
// Collect and rewrite exports.__esModule assignments
|
|
922
|
+
let isRestorableCompiledEsm = false;
|
|
923
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
924
|
+
isRestorableCompiledEsm = true;
|
|
925
|
+
const moduleExportsExpression =
|
|
926
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
927
|
+
magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
931
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
932
|
+
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
933
|
+
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
934
|
+
} else {
|
|
935
|
+
exportDeclarations.push(
|
|
936
|
+
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
|
|
937
|
+
);
|
|
938
|
+
}
|
|
887
939
|
}
|
|
888
940
|
|
|
889
941
|
function isRequireStatement(node, scope) {
|
|
@@ -1001,44 +1053,46 @@ function getRequireHandlers() {
|
|
|
1001
1053
|
topLevelDeclarations,
|
|
1002
1054
|
topLevelRequireDeclarators,
|
|
1003
1055
|
reassignedNames,
|
|
1004
|
-
|
|
1005
|
-
dynamicRegisterSources
|
|
1056
|
+
helpersName,
|
|
1057
|
+
dynamicRegisterSources,
|
|
1058
|
+
moduleName,
|
|
1059
|
+
exportsName,
|
|
1060
|
+
id,
|
|
1061
|
+
exportMode
|
|
1006
1062
|
) {
|
|
1007
|
-
const removedDeclarators = getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1008
|
-
topLevelRequireDeclarators,
|
|
1009
|
-
requiredByNode,
|
|
1010
|
-
reassignedNames
|
|
1011
|
-
);
|
|
1012
1063
|
setRemainingImportNamesAndRewriteRequires(
|
|
1013
1064
|
requireExpressionsWithUsedReturnValue,
|
|
1014
1065
|
requiredByNode,
|
|
1015
1066
|
magicString
|
|
1016
1067
|
);
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1068
|
+
const imports = [];
|
|
1069
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1070
|
+
if (exportMode === 'module') {
|
|
1071
|
+
imports.push(
|
|
1072
|
+
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
1073
|
+
wrapId(id, MODULE_SUFFIX)
|
|
1074
|
+
)}`
|
|
1075
|
+
);
|
|
1076
|
+
} else if (exportMode === 'exports') {
|
|
1077
|
+
imports.push(
|
|
1078
|
+
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
for (const source of dynamicRegisterSources) {
|
|
1082
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1083
|
+
}
|
|
1084
|
+
for (const source of requiredSources) {
|
|
1085
|
+
if (!source.startsWith('\0')) {
|
|
1086
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1087
|
+
}
|
|
1088
|
+
const { name, nodesUsingRequired } = requiredBySource[source];
|
|
1089
|
+
imports.push(
|
|
1090
|
+
`import ${nodesUsingRequired.length ? `${name} from ` : ''}${JSON.stringify(
|
|
1091
|
+
source.startsWith('\0') ? source : wrapId(source, PROXY_SUFFIX)
|
|
1092
|
+
)};`
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1042
1096
|
}
|
|
1043
1097
|
|
|
1044
1098
|
return {
|
|
@@ -1048,30 +1102,6 @@ function getRequireHandlers() {
|
|
|
1048
1102
|
};
|
|
1049
1103
|
}
|
|
1050
1104
|
|
|
1051
|
-
function getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1052
|
-
topLevelRequireDeclarators,
|
|
1053
|
-
requiredByNode,
|
|
1054
|
-
reassignedNames
|
|
1055
|
-
) {
|
|
1056
|
-
const removedDeclarators = new Set();
|
|
1057
|
-
for (const declarator of topLevelRequireDeclarators) {
|
|
1058
|
-
const { required } = requiredByNode.get(declarator.init);
|
|
1059
|
-
if (!required.name) {
|
|
1060
|
-
const potentialName = declarator.id.name;
|
|
1061
|
-
if (
|
|
1062
|
-
!reassignedNames.has(potentialName) &&
|
|
1063
|
-
!required.nodesUsingRequired.some((node) =>
|
|
1064
|
-
isLocallyShadowed(potentialName, requiredByNode.get(node).scope)
|
|
1065
|
-
)
|
|
1066
|
-
) {
|
|
1067
|
-
required.name = potentialName;
|
|
1068
|
-
removedDeclarators.add(declarator);
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
return removedDeclarators;
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
1105
|
function setRemainingImportNamesAndRewriteRequires(
|
|
1076
1106
|
requireExpressionsWithUsedReturnValue,
|
|
1077
1107
|
requiredByNode,
|
|
@@ -1093,25 +1123,6 @@ function setRemainingImportNamesAndRewriteRequires(
|
|
|
1093
1123
|
}
|
|
1094
1124
|
}
|
|
1095
1125
|
|
|
1096
|
-
function removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString) {
|
|
1097
|
-
for (const declaration of topLevelDeclarations) {
|
|
1098
|
-
let keepDeclaration = false;
|
|
1099
|
-
let [{ start }] = declaration.declarations;
|
|
1100
|
-
for (const declarator of declaration.declarations) {
|
|
1101
|
-
if (removedDeclarators.has(declarator)) {
|
|
1102
|
-
magicString.remove(start, declarator.end);
|
|
1103
|
-
} else if (!keepDeclaration) {
|
|
1104
|
-
magicString.remove(start, declarator.start);
|
|
1105
|
-
keepDeclaration = true;
|
|
1106
|
-
}
|
|
1107
|
-
start = declarator.end;
|
|
1108
|
-
}
|
|
1109
|
-
if (!keepDeclaration) {
|
|
1110
|
-
magicString.remove(declaration.start, declaration.end);
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
1126
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1116
1127
|
|
|
1117
1128
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
@@ -1141,9 +1152,9 @@ function transformCommonjs(
|
|
|
1141
1152
|
module: false,
|
|
1142
1153
|
exports: false,
|
|
1143
1154
|
global: false,
|
|
1144
|
-
require: false
|
|
1145
|
-
commonjsHelpers: false
|
|
1155
|
+
require: false
|
|
1146
1156
|
};
|
|
1157
|
+
let usesDynamicRequire = false;
|
|
1147
1158
|
const virtualDynamicRequirePath =
|
|
1148
1159
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname(id), commonDir);
|
|
1149
1160
|
let scope = attachScopes(ast, 'scope');
|
|
@@ -1151,13 +1162,11 @@ function transformCommonjs(
|
|
|
1151
1162
|
let programDepth = 0;
|
|
1152
1163
|
let currentTryBlockEnd = null;
|
|
1153
1164
|
let shouldWrap = false;
|
|
1154
|
-
const defineCompiledEsmExpressions = [];
|
|
1155
1165
|
|
|
1156
1166
|
const globals = new Set();
|
|
1157
1167
|
|
|
1158
1168
|
// TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
|
|
1159
|
-
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1160
|
-
const namedExports = {};
|
|
1169
|
+
const HELPERS_NAME = deconflict([scope], globals, 'commonjsHelpers');
|
|
1161
1170
|
const dynamicRegisterSources = new Set();
|
|
1162
1171
|
let hasRemovedRequire = false;
|
|
1163
1172
|
|
|
@@ -1174,8 +1183,13 @@ function transformCommonjs(
|
|
|
1174
1183
|
const topLevelDeclarations = [];
|
|
1175
1184
|
const topLevelRequireDeclarators = new Set();
|
|
1176
1185
|
const skippedNodes = new Set();
|
|
1177
|
-
const
|
|
1178
|
-
const
|
|
1186
|
+
const moduleAccessScopes = new Set([scope]);
|
|
1187
|
+
const exportsAccessScopes = new Set([scope]);
|
|
1188
|
+
const moduleExportsAssignments = [];
|
|
1189
|
+
let firstTopLevelModuleExportsAssignment = null;
|
|
1190
|
+
const exportsAssignmentsByName = new Map();
|
|
1191
|
+
const topLevelAssignments = new Set();
|
|
1192
|
+
const topLevelDefineCompiledEsmExpressions = [];
|
|
1179
1193
|
|
|
1180
1194
|
walk(ast, {
|
|
1181
1195
|
enter(node, parent) {
|
|
@@ -1215,30 +1229,34 @@ function transformCommonjs(
|
|
|
1215
1229
|
uses[flattened.name] = true;
|
|
1216
1230
|
|
|
1217
1231
|
// we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
|
|
1218
|
-
if (
|
|
1219
|
-
|
|
1232
|
+
if (flattened.keypath === 'module.exports') {
|
|
1233
|
+
moduleExportsAssignments.push(node);
|
|
1234
|
+
if (programDepth > 3) {
|
|
1235
|
+
moduleAccessScopes.add(scope);
|
|
1236
|
+
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1237
|
+
firstTopLevelModuleExportsAssignment = node;
|
|
1238
|
+
}
|
|
1220
1239
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1240
|
+
if (programDepth > 3) {
|
|
1241
|
+
shouldWrap = true;
|
|
1242
|
+
} else {
|
|
1243
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1244
|
+
}
|
|
1226
1245
|
} else {
|
|
1227
|
-
|
|
1246
|
+
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1247
|
+
nodes: [],
|
|
1248
|
+
scopes: new Set()
|
|
1249
|
+
};
|
|
1250
|
+
exportsAssignments.nodes.push(node);
|
|
1251
|
+
exportsAssignments.scopes.add(scope);
|
|
1252
|
+
exportsAccessScopes.add(scope);
|
|
1253
|
+
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
1254
|
+
if (programDepth <= 3) {
|
|
1255
|
+
topLevelAssignments.add(node);
|
|
1256
|
+
}
|
|
1228
1257
|
}
|
|
1229
1258
|
|
|
1230
1259
|
skippedNodes.add(node.left);
|
|
1231
|
-
|
|
1232
|
-
if (flattened.keypath === 'module.exports' && node.right.type === 'ObjectExpression') {
|
|
1233
|
-
node.right.properties.forEach((prop) => {
|
|
1234
|
-
if (prop.computed || !('key' in prop) || prop.key.type !== 'Identifier') return;
|
|
1235
|
-
const { name } = prop.key;
|
|
1236
|
-
if (name === makeLegalIdentifier(name)) namedExports[name] = true;
|
|
1237
|
-
});
|
|
1238
|
-
return;
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
if (exportsPatternMatch[1]) namedExports[exportsPatternMatch[1]] = true;
|
|
1242
1260
|
} else {
|
|
1243
1261
|
for (const name of extractAssignedNames(node.left)) {
|
|
1244
1262
|
reassignedNames.add(name);
|
|
@@ -1250,7 +1268,7 @@ function transformCommonjs(
|
|
|
1250
1268
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1251
1269
|
// skip special handling for [module.]exports until we know we render this
|
|
1252
1270
|
skippedNodes.add(node.arguments[0]);
|
|
1253
|
-
|
|
1271
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1254
1272
|
} else {
|
|
1255
1273
|
shouldWrap = true;
|
|
1256
1274
|
}
|
|
@@ -1278,7 +1296,6 @@ function transformCommonjs(
|
|
|
1278
1296
|
storeName: true
|
|
1279
1297
|
}
|
|
1280
1298
|
);
|
|
1281
|
-
uses.commonjsHelpers = true;
|
|
1282
1299
|
return;
|
|
1283
1300
|
}
|
|
1284
1301
|
|
|
@@ -1305,13 +1322,13 @@ function transformCommonjs(
|
|
|
1305
1322
|
}
|
|
1306
1323
|
|
|
1307
1324
|
let sourceId = getRequireStringArg(node);
|
|
1308
|
-
const isDynamicRegister =
|
|
1325
|
+
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1309
1326
|
if (isDynamicRegister) {
|
|
1310
|
-
sourceId =
|
|
1327
|
+
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1311
1328
|
if (sourceId.endsWith('.json')) {
|
|
1312
1329
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1313
1330
|
}
|
|
1314
|
-
dynamicRegisterSources.add(
|
|
1331
|
+
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1315
1332
|
} else {
|
|
1316
1333
|
if (
|
|
1317
1334
|
!sourceId.endsWith('.json') &&
|
|
@@ -1329,7 +1346,7 @@ function transformCommonjs(
|
|
|
1329
1346
|
dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1330
1347
|
)})`
|
|
1331
1348
|
);
|
|
1332
|
-
|
|
1349
|
+
usesDynamicRequire = true;
|
|
1333
1350
|
}
|
|
1334
1351
|
return;
|
|
1335
1352
|
}
|
|
@@ -1386,7 +1403,6 @@ function transformCommonjs(
|
|
|
1386
1403
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1387
1404
|
storeName: true
|
|
1388
1405
|
});
|
|
1389
|
-
uses.commonjsHelpers = true;
|
|
1390
1406
|
}
|
|
1391
1407
|
}
|
|
1392
1408
|
|
|
@@ -1410,8 +1426,7 @@ function transformCommonjs(
|
|
|
1410
1426
|
});
|
|
1411
1427
|
}
|
|
1412
1428
|
}
|
|
1413
|
-
|
|
1414
|
-
uses.commonjsHelpers = true;
|
|
1429
|
+
usesDynamicRequire = true;
|
|
1415
1430
|
return;
|
|
1416
1431
|
case 'module':
|
|
1417
1432
|
case 'exports':
|
|
@@ -1424,11 +1439,12 @@ function transformCommonjs(
|
|
|
1424
1439
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1425
1440
|
storeName: true
|
|
1426
1441
|
});
|
|
1427
|
-
uses.commonjsHelpers = true;
|
|
1428
1442
|
}
|
|
1429
1443
|
return;
|
|
1430
1444
|
case 'define':
|
|
1431
|
-
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1445
|
+
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1446
|
+
storeName: true
|
|
1447
|
+
});
|
|
1432
1448
|
return;
|
|
1433
1449
|
default:
|
|
1434
1450
|
globals.add(name);
|
|
@@ -1440,7 +1456,6 @@ function transformCommonjs(
|
|
|
1440
1456
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1441
1457
|
storeName: true
|
|
1442
1458
|
});
|
|
1443
|
-
uses.commonjsHelpers = true;
|
|
1444
1459
|
skippedNodes.add(node.object);
|
|
1445
1460
|
skippedNodes.add(node.property);
|
|
1446
1461
|
}
|
|
@@ -1459,7 +1474,6 @@ function transformCommonjs(
|
|
|
1459
1474
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1460
1475
|
storeName: true
|
|
1461
1476
|
});
|
|
1462
|
-
uses.commonjsHelpers = true;
|
|
1463
1477
|
}
|
|
1464
1478
|
}
|
|
1465
1479
|
return;
|
|
@@ -1476,7 +1490,9 @@ function transformCommonjs(
|
|
|
1476
1490
|
flattened.keypath === 'module' ||
|
|
1477
1491
|
flattened.keypath === 'exports'
|
|
1478
1492
|
) {
|
|
1479
|
-
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1493
|
+
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1494
|
+
storeName: false
|
|
1495
|
+
});
|
|
1480
1496
|
}
|
|
1481
1497
|
}
|
|
1482
1498
|
return;
|
|
@@ -1494,23 +1510,22 @@ function transformCommonjs(
|
|
|
1494
1510
|
}
|
|
1495
1511
|
});
|
|
1496
1512
|
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
defineCompiledEsmExpressions[0].end
|
|
1504
|
-
);
|
|
1505
|
-
} else {
|
|
1506
|
-
shouldWrap = true;
|
|
1507
|
-
uses.exports = true;
|
|
1508
|
-
}
|
|
1513
|
+
const nameBase = getName(id);
|
|
1514
|
+
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1515
|
+
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1516
|
+
const deconflictedExportNames = Object.create(null);
|
|
1517
|
+
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1518
|
+
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1509
1519
|
}
|
|
1510
1520
|
|
|
1511
1521
|
// We cannot wrap ES/mixed modules
|
|
1512
|
-
shouldWrap =
|
|
1513
|
-
|
|
1522
|
+
shouldWrap =
|
|
1523
|
+
!isEsModule &&
|
|
1524
|
+
!disableWrap &&
|
|
1525
|
+
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1526
|
+
const detectWrappedDefault =
|
|
1527
|
+
shouldWrap &&
|
|
1528
|
+
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1514
1529
|
|
|
1515
1530
|
if (
|
|
1516
1531
|
!(
|
|
@@ -1519,17 +1534,15 @@ function transformCommonjs(
|
|
|
1519
1534
|
uses.module ||
|
|
1520
1535
|
uses.exports ||
|
|
1521
1536
|
uses.require ||
|
|
1522
|
-
|
|
1537
|
+
usesDynamicRequire ||
|
|
1523
1538
|
hasRemovedRequire ||
|
|
1524
|
-
|
|
1539
|
+
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1525
1540
|
) &&
|
|
1526
1541
|
(ignoreGlobal || !uses.global)
|
|
1527
1542
|
) {
|
|
1528
1543
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1529
1544
|
}
|
|
1530
1545
|
|
|
1531
|
-
const moduleName = deconflict(scope, globals, getName(id));
|
|
1532
|
-
|
|
1533
1546
|
let leadingComment = '';
|
|
1534
1547
|
if (code.startsWith('/*')) {
|
|
1535
1548
|
const commentEnd = code.indexOf('*/', 2) + 2;
|
|
@@ -1537,34 +1550,53 @@ function transformCommonjs(
|
|
|
1537
1550
|
magicString.remove(0, commentEnd).trim();
|
|
1538
1551
|
}
|
|
1539
1552
|
|
|
1553
|
+
const exportMode = shouldWrap
|
|
1554
|
+
? uses.module
|
|
1555
|
+
? 'module'
|
|
1556
|
+
: 'exports'
|
|
1557
|
+
: firstTopLevelModuleExportsAssignment
|
|
1558
|
+
? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
|
|
1559
|
+
? 'replace'
|
|
1560
|
+
: 'module'
|
|
1561
|
+
: moduleExportsAssignments.length === 0
|
|
1562
|
+
? 'exports'
|
|
1563
|
+
: 'module';
|
|
1564
|
+
|
|
1565
|
+
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1566
|
+
magicString,
|
|
1567
|
+
topLevelDeclarations,
|
|
1568
|
+
topLevelRequireDeclarators,
|
|
1569
|
+
reassignedNames,
|
|
1570
|
+
HELPERS_NAME,
|
|
1571
|
+
dynamicRegisterSources,
|
|
1572
|
+
moduleName,
|
|
1573
|
+
exportsName,
|
|
1574
|
+
id,
|
|
1575
|
+
exportMode
|
|
1576
|
+
);
|
|
1577
|
+
|
|
1540
1578
|
const exportBlock = isEsModule
|
|
1541
1579
|
? ''
|
|
1542
1580
|
: rewriteExportsAndGetExportsBlock(
|
|
1543
1581
|
magicString,
|
|
1544
1582
|
moduleName,
|
|
1583
|
+
exportsName,
|
|
1545
1584
|
shouldWrap,
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1585
|
+
moduleExportsAssignments,
|
|
1586
|
+
firstTopLevelModuleExportsAssignment,
|
|
1587
|
+
exportsAssignmentsByName,
|
|
1588
|
+
topLevelAssignments,
|
|
1589
|
+
topLevelDefineCompiledEsmExpressions,
|
|
1590
|
+
deconflictedExportNames,
|
|
1551
1591
|
code,
|
|
1552
|
-
uses,
|
|
1553
1592
|
HELPERS_NAME,
|
|
1593
|
+
exportMode,
|
|
1594
|
+
detectWrappedDefault,
|
|
1554
1595
|
defaultIsModuleExports
|
|
1555
1596
|
);
|
|
1556
1597
|
|
|
1557
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1558
|
-
magicString,
|
|
1559
|
-
topLevelDeclarations,
|
|
1560
|
-
topLevelRequireDeclarators,
|
|
1561
|
-
reassignedNames,
|
|
1562
|
-
uses.commonjsHelpers && HELPERS_NAME,
|
|
1563
|
-
dynamicRegisterSources
|
|
1564
|
-
);
|
|
1565
|
-
|
|
1566
1598
|
if (shouldWrap) {
|
|
1567
|
-
wrapCode(magicString, uses, moduleName,
|
|
1599
|
+
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1568
1600
|
}
|
|
1569
1601
|
|
|
1570
1602
|
magicString
|
|
@@ -1613,7 +1645,7 @@ function commonjs(options = {}) {
|
|
|
1613
1645
|
|
|
1614
1646
|
const esModulesWithDefaultExport = new Set();
|
|
1615
1647
|
const esModulesWithNamedExports = new Set();
|
|
1616
|
-
const
|
|
1648
|
+
const commonJsMetaPromises = new Map();
|
|
1617
1649
|
|
|
1618
1650
|
const ignoreRequire =
|
|
1619
1651
|
typeof options.ignore === 'function'
|
|
@@ -1666,13 +1698,11 @@ function commonjs(options = {}) {
|
|
|
1666
1698
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1667
1699
|
}
|
|
1668
1700
|
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
if (isModuleRegisterProxy(id)) {
|
|
1673
|
-
disableWrap = true;
|
|
1701
|
+
// avoid wrapping as this is a commonjsRegister call
|
|
1702
|
+
const disableWrap = isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1703
|
+
if (disableWrap) {
|
|
1674
1704
|
// eslint-disable-next-line no-param-reassign
|
|
1675
|
-
id =
|
|
1705
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1676
1706
|
}
|
|
1677
1707
|
|
|
1678
1708
|
return transformCommonjs(
|
|
@@ -1717,6 +1747,39 @@ function commonjs(options = {}) {
|
|
|
1717
1747
|
return getSpecificHelperProxy(id);
|
|
1718
1748
|
}
|
|
1719
1749
|
|
|
1750
|
+
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1751
|
+
const actualId = unwrapId(id, MODULE_SUFFIX);
|
|
1752
|
+
let name = getName(actualId);
|
|
1753
|
+
let code;
|
|
1754
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1755
|
+
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1756
|
+
name = `${name}_`;
|
|
1757
|
+
}
|
|
1758
|
+
code =
|
|
1759
|
+
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1760
|
+
`var ${name} = createModule(${JSON.stringify(
|
|
1761
|
+
getVirtualPathForDynamicRequirePath(dirname(actualId), commonDir)
|
|
1762
|
+
)});\n` +
|
|
1763
|
+
`export {${name} as __module}`;
|
|
1764
|
+
} else {
|
|
1765
|
+
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1766
|
+
}
|
|
1767
|
+
return {
|
|
1768
|
+
code,
|
|
1769
|
+
syntheticNamedExports: '__module',
|
|
1770
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1771
|
+
};
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1775
|
+
const actualId = unwrapId(id, EXPORTS_SUFFIX);
|
|
1776
|
+
const name = getName(actualId);
|
|
1777
|
+
return {
|
|
1778
|
+
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1779
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1720
1783
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
1721
1784
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
1722
1785
|
return getUnknownRequireProxy(
|
|
@@ -1737,9 +1800,9 @@ function commonjs(options = {}) {
|
|
|
1737
1800
|
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1738
1801
|
}
|
|
1739
1802
|
|
|
1740
|
-
if (
|
|
1803
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1741
1804
|
return getDynamicRequireProxy(
|
|
1742
|
-
normalizePathSlashes(
|
|
1805
|
+
normalizePathSlashes(unwrapId(id, DYNAMIC_REGISTER_SUFFIX)),
|
|
1743
1806
|
commonDir
|
|
1744
1807
|
);
|
|
1745
1808
|
}
|
|
@@ -1751,7 +1814,7 @@ function commonjs(options = {}) {
|
|
|
1751
1814
|
getRequireReturnsDefault(actualId),
|
|
1752
1815
|
esModulesWithDefaultExport,
|
|
1753
1816
|
esModulesWithNamedExports,
|
|
1754
|
-
|
|
1817
|
+
commonJsMetaPromises
|
|
1755
1818
|
);
|
|
1756
1819
|
}
|
|
1757
1820
|
|
|
@@ -1761,8 +1824,8 @@ function commonjs(options = {}) {
|
|
|
1761
1824
|
transform(code, rawId) {
|
|
1762
1825
|
let id = rawId;
|
|
1763
1826
|
|
|
1764
|
-
if (
|
|
1765
|
-
id =
|
|
1827
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1828
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1766
1829
|
}
|
|
1767
1830
|
|
|
1768
1831
|
const extName = extname(id);
|
|
@@ -1782,16 +1845,12 @@ function commonjs(options = {}) {
|
|
|
1782
1845
|
}
|
|
1783
1846
|
},
|
|
1784
1847
|
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
if (isCjs != null) {
|
|
1790
|
-
setIsCjsPromise(isCjsPromises, id, isCjs);
|
|
1791
|
-
return;
|
|
1792
|
-
}
|
|
1848
|
+
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1849
|
+
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1850
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1851
|
+
return;
|
|
1793
1852
|
}
|
|
1794
|
-
|
|
1853
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1795
1854
|
}
|
|
1796
1855
|
};
|
|
1797
1856
|
}
|