@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.js
CHANGED
|
@@ -18,7 +18,7 @@ var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
|
18
18
|
var isReference__default = /*#__PURE__*/_interopDefaultLegacy(isReference);
|
|
19
19
|
|
|
20
20
|
var peerDependencies = {
|
|
21
|
-
rollup: "^2.
|
|
21
|
+
rollup: "^2.38.3"
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
function tryParse(parse, code, id) {
|
|
@@ -92,6 +92,8 @@ const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
|
|
|
92
92
|
const PROXY_SUFFIX = '?commonjs-proxy';
|
|
93
93
|
const REQUIRE_SUFFIX = '?commonjs-require';
|
|
94
94
|
const EXTERNAL_SUFFIX = '?commonjs-external';
|
|
95
|
+
const EXPORTS_SUFFIX = '?commonjs-exports';
|
|
96
|
+
const MODULE_SUFFIX = '?commonjs-module';
|
|
95
97
|
|
|
96
98
|
const DYNAMIC_REGISTER_SUFFIX = '?commonjs-dynamic-register';
|
|
97
99
|
const DYNAMIC_JSON_PREFIX = '\0commonjs-dynamic-json:';
|
|
@@ -138,25 +140,20 @@ export function getAugmentedNamespace(n) {
|
|
|
138
140
|
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.');`;
|
|
139
141
|
|
|
140
142
|
const HELPER_NON_DYNAMIC = `
|
|
141
|
-
export function createCommonjsModule(fn) {
|
|
142
|
-
var module = { exports: {} }
|
|
143
|
-
return fn(module, module.exports), module.exports;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
143
|
export function commonjsRequire (path) {
|
|
147
144
|
${FAILED_REQUIRE_ERROR}
|
|
148
145
|
}
|
|
149
146
|
`;
|
|
150
147
|
|
|
151
148
|
const getDynamicHelpers = (ignoreDynamicRequires) => `
|
|
152
|
-
export function
|
|
153
|
-
return
|
|
154
|
-
path:
|
|
149
|
+
export function createModule(modulePath) {
|
|
150
|
+
return {
|
|
151
|
+
path: modulePath,
|
|
155
152
|
exports: {},
|
|
156
153
|
require: function (path, base) {
|
|
157
|
-
return commonjsRequire(path,
|
|
154
|
+
return commonjsRequire(path, base == null ? modulePath : base);
|
|
158
155
|
}
|
|
159
|
-
}
|
|
156
|
+
};
|
|
160
157
|
}
|
|
161
158
|
|
|
162
159
|
export function commonjsRegister (path, loader) {
|
|
@@ -335,16 +332,20 @@ function getHelpersModule(isDynamicRequireModulesEnabled, ignoreDynamicRequires)
|
|
|
335
332
|
|
|
336
333
|
/* eslint-disable import/prefer-default-export */
|
|
337
334
|
|
|
338
|
-
function deconflict(
|
|
335
|
+
function deconflict(scopes, globals, identifier) {
|
|
339
336
|
let i = 1;
|
|
340
337
|
let deconflicted = pluginutils.makeLegalIdentifier(identifier);
|
|
338
|
+
const hasConflicts = () =>
|
|
339
|
+
scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
|
|
341
340
|
|
|
342
|
-
while (
|
|
341
|
+
while (hasConflicts()) {
|
|
343
342
|
deconflicted = pluginutils.makeLegalIdentifier(`${identifier}_${i}`);
|
|
344
343
|
i += 1;
|
|
345
344
|
}
|
|
346
|
-
|
|
347
|
-
scope
|
|
345
|
+
|
|
346
|
+
for (const scope of scopes) {
|
|
347
|
+
scope.declarations[deconflicted] = true;
|
|
348
|
+
}
|
|
348
349
|
|
|
349
350
|
return deconflicted;
|
|
350
351
|
}
|
|
@@ -406,28 +407,18 @@ function getDynamicPackagesEntryIntro(
|
|
|
406
407
|
) {
|
|
407
408
|
let dynamicImports = Array.from(
|
|
408
409
|
dynamicRequireModuleSet,
|
|
409
|
-
(dynamicId) => `require(${JSON.stringify(
|
|
410
|
+
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
|
|
410
411
|
).join('\n');
|
|
411
412
|
|
|
412
413
|
if (dynamicRequireModuleDirPaths.length) {
|
|
413
|
-
dynamicImports += `require(${JSON.stringify(
|
|
414
|
+
dynamicImports += `require(${JSON.stringify(
|
|
415
|
+
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
|
|
416
|
+
)});`;
|
|
414
417
|
}
|
|
415
418
|
|
|
416
419
|
return dynamicImports;
|
|
417
420
|
}
|
|
418
421
|
|
|
419
|
-
function wrapModuleRegisterProxy(id) {
|
|
420
|
-
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
function unwrapModuleRegisterProxy(id) {
|
|
424
|
-
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
function isModuleRegisterProxy(id) {
|
|
428
|
-
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
422
|
function isDynamicModuleImport(id, dynamicRequireModuleSet) {
|
|
432
423
|
const normalizedPath = normalizePathSlashes(id);
|
|
433
424
|
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
|
|
@@ -460,37 +451,37 @@ function getDynamicRequirePaths(patterns) {
|
|
|
460
451
|
return { dynamicRequireModuleSet, dynamicRequireModuleDirPaths };
|
|
461
452
|
}
|
|
462
453
|
|
|
463
|
-
function
|
|
464
|
-
let
|
|
465
|
-
if (
|
|
454
|
+
function getCommonJSMetaPromise(commonJSMetaPromises, id) {
|
|
455
|
+
let commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
456
|
+
if (commonJSMetaPromise) return commonJSMetaPromise.promise;
|
|
466
457
|
|
|
467
458
|
const promise = new Promise((resolve) => {
|
|
468
|
-
|
|
459
|
+
commonJSMetaPromise = {
|
|
469
460
|
resolve,
|
|
470
461
|
promise: null
|
|
471
462
|
};
|
|
472
|
-
|
|
463
|
+
commonJSMetaPromises.set(id, commonJSMetaPromise);
|
|
473
464
|
});
|
|
474
|
-
|
|
465
|
+
commonJSMetaPromise.promise = promise;
|
|
475
466
|
|
|
476
467
|
return promise;
|
|
477
468
|
}
|
|
478
469
|
|
|
479
|
-
function
|
|
480
|
-
const
|
|
481
|
-
if (
|
|
482
|
-
if (
|
|
483
|
-
|
|
484
|
-
|
|
470
|
+
function setCommonJSMetaPromise(commonJSMetaPromises, id, commonjsMeta) {
|
|
471
|
+
const commonJSMetaPromise = commonJSMetaPromises.get(id);
|
|
472
|
+
if (commonJSMetaPromise) {
|
|
473
|
+
if (commonJSMetaPromise.resolve) {
|
|
474
|
+
commonJSMetaPromise.resolve(commonjsMeta);
|
|
475
|
+
commonJSMetaPromise.resolve = null;
|
|
485
476
|
}
|
|
486
477
|
} else {
|
|
487
|
-
|
|
478
|
+
commonJSMetaPromises.set(id, { promise: Promise.resolve(commonjsMeta), resolve: null });
|
|
488
479
|
}
|
|
489
480
|
}
|
|
490
481
|
|
|
491
482
|
// e.g. id === "commonjsHelpers?commonjsRegister"
|
|
492
483
|
function getSpecificHelperProxy(id) {
|
|
493
|
-
return `export {${id.split('?')[1]} as default} from
|
|
484
|
+
return `export {${id.split('?')[1]} as default} from "${HELPERS_ID}";`;
|
|
494
485
|
}
|
|
495
486
|
|
|
496
487
|
function getUnknownRequireProxy(id, requireReturnsDefault) {
|
|
@@ -531,16 +522,16 @@ async function getStaticRequireProxy(
|
|
|
531
522
|
requireReturnsDefault,
|
|
532
523
|
esModulesWithDefaultExport,
|
|
533
524
|
esModulesWithNamedExports,
|
|
534
|
-
|
|
525
|
+
commonJsMetaPromises
|
|
535
526
|
) {
|
|
536
527
|
const name = getName(id);
|
|
537
|
-
const
|
|
538
|
-
if (
|
|
539
|
-
return `
|
|
540
|
-
} else if (
|
|
528
|
+
const commonjsMeta = await getCommonJSMetaPromise(commonJsMetaPromises, id);
|
|
529
|
+
if (commonjsMeta && commonjsMeta.isCommonJS) {
|
|
530
|
+
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
531
|
+
} else if (commonjsMeta === null) {
|
|
541
532
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
542
533
|
} else if (!requireReturnsDefault) {
|
|
543
|
-
return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
534
|
+
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
544
535
|
id
|
|
545
536
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
546
537
|
} else if (
|
|
@@ -551,7 +542,7 @@ async function getStaticRequireProxy(
|
|
|
551
542
|
) {
|
|
552
543
|
return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
|
|
553
544
|
}
|
|
554
|
-
return `export {default} from ${JSON.stringify(id)};`;
|
|
545
|
+
return `export { default } from ${JSON.stringify(id)};`;
|
|
555
546
|
}
|
|
556
547
|
|
|
557
548
|
/* eslint-disable no-param-reassign, no-undefined */
|
|
@@ -588,12 +579,17 @@ function getResolveId(extensions) {
|
|
|
588
579
|
}
|
|
589
580
|
|
|
590
581
|
return function resolveId(importee, rawImporter) {
|
|
582
|
+
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
|
|
583
|
+
return importee;
|
|
584
|
+
}
|
|
585
|
+
|
|
591
586
|
const importer =
|
|
592
|
-
rawImporter &&
|
|
593
|
-
?
|
|
587
|
+
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
588
|
+
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
|
|
594
589
|
: rawImporter;
|
|
595
590
|
|
|
596
|
-
//
|
|
591
|
+
// Except for exports, proxies are only importing resolved ids,
|
|
592
|
+
// no need to resolve again
|
|
597
593
|
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
|
|
598
594
|
return importee;
|
|
599
595
|
}
|
|
@@ -607,9 +603,9 @@ function getResolveId(extensions) {
|
|
|
607
603
|
} else if (isRequiredModule) {
|
|
608
604
|
importee = unwrapId(importee, REQUIRE_SUFFIX);
|
|
609
605
|
|
|
610
|
-
isModuleRegistration =
|
|
606
|
+
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
611
607
|
if (isModuleRegistration) {
|
|
612
|
-
importee =
|
|
608
|
+
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
|
|
613
609
|
}
|
|
614
610
|
}
|
|
615
611
|
|
|
@@ -636,7 +632,7 @@ function getResolveId(extensions) {
|
|
|
636
632
|
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
|
|
637
633
|
resolved.external = false;
|
|
638
634
|
} else if (resolved && isModuleRegistration) {
|
|
639
|
-
resolved.id =
|
|
635
|
+
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
|
|
640
636
|
} else if (!resolved && (isProxyModule || isRequiredModule)) {
|
|
641
637
|
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
|
|
642
638
|
}
|
|
@@ -736,8 +732,6 @@ function isDefineCompiledEsm(node) {
|
|
|
736
732
|
}
|
|
737
733
|
|
|
738
734
|
function getDefinePropertyCallName(node, targetName) {
|
|
739
|
-
const targetNames = targetName.split('.');
|
|
740
|
-
|
|
741
735
|
const {
|
|
742
736
|
callee: { object, property }
|
|
743
737
|
} = node;
|
|
@@ -745,6 +739,7 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
745
739
|
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
|
|
746
740
|
if (node.arguments.length !== 3) return;
|
|
747
741
|
|
|
742
|
+
const targetNames = targetName.split('.');
|
|
748
743
|
const [target, key, value] = node.arguments;
|
|
749
744
|
if (targetNames.length === 1) {
|
|
750
745
|
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
|
|
@@ -771,128 +766,185 @@ function getDefinePropertyCallName(node, targetName) {
|
|
|
771
766
|
return { key: key.value, value: valueProperty.value };
|
|
772
767
|
}
|
|
773
768
|
|
|
774
|
-
function isLocallyShadowed(name, scope) {
|
|
775
|
-
while (scope.parent) {
|
|
776
|
-
if (scope.declarations[name]) {
|
|
777
|
-
return true;
|
|
778
|
-
}
|
|
779
|
-
// eslint-disable-next-line no-param-reassign
|
|
780
|
-
scope = scope.parent;
|
|
781
|
-
}
|
|
782
|
-
return false;
|
|
783
|
-
}
|
|
784
|
-
|
|
785
769
|
function isShorthandProperty(parent) {
|
|
786
770
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
787
771
|
}
|
|
788
772
|
|
|
789
|
-
function wrapCode(magicString, uses, moduleName,
|
|
790
|
-
const args =
|
|
791
|
-
|
|
773
|
+
function wrapCode(magicString, uses, moduleName, exportsName) {
|
|
774
|
+
const args = [];
|
|
775
|
+
const passedArgs = [];
|
|
776
|
+
if (uses.module) {
|
|
777
|
+
args.push('module');
|
|
778
|
+
passedArgs.push(moduleName);
|
|
779
|
+
}
|
|
780
|
+
if (uses.exports) {
|
|
781
|
+
args.push('exports');
|
|
782
|
+
passedArgs.push(exportsName);
|
|
783
|
+
}
|
|
792
784
|
magicString
|
|
793
785
|
.trim()
|
|
794
|
-
.prepend(`
|
|
795
|
-
.append(
|
|
796
|
-
`\n}${virtualDynamicRequirePath ? `, ${JSON.stringify(virtualDynamicRequirePath)}` : ''});`
|
|
797
|
-
);
|
|
786
|
+
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
787
|
+
.append(`\n}(${passedArgs.join(', ')}));`);
|
|
798
788
|
}
|
|
799
789
|
|
|
800
790
|
function rewriteExportsAndGetExportsBlock(
|
|
801
791
|
magicString,
|
|
802
792
|
moduleName,
|
|
793
|
+
exportsName,
|
|
803
794
|
wrapped,
|
|
804
|
-
|
|
805
|
-
|
|
795
|
+
moduleExportsAssignments,
|
|
796
|
+
firstTopLevelModuleExportsAssignment,
|
|
797
|
+
exportsAssignmentsByName,
|
|
798
|
+
topLevelAssignments,
|
|
806
799
|
defineCompiledEsmExpressions,
|
|
807
|
-
|
|
808
|
-
isRestorableCompiledEsm,
|
|
800
|
+
deconflictedExportNames,
|
|
809
801
|
code,
|
|
810
|
-
uses,
|
|
811
802
|
HELPERS_NAME,
|
|
803
|
+
exportMode,
|
|
804
|
+
detectWrappedDefault,
|
|
812
805
|
defaultIsModuleExports
|
|
813
806
|
) {
|
|
814
|
-
const
|
|
815
|
-
const
|
|
816
|
-
|
|
807
|
+
const exports = [];
|
|
808
|
+
const exportDeclarations = [];
|
|
809
|
+
|
|
810
|
+
if (exportMode === 'replace') {
|
|
811
|
+
getExportsForReplacedModuleExports(
|
|
812
|
+
magicString,
|
|
813
|
+
exports,
|
|
814
|
+
exportDeclarations,
|
|
815
|
+
moduleExportsAssignments,
|
|
816
|
+
firstTopLevelModuleExportsAssignment,
|
|
817
|
+
exportsName
|
|
818
|
+
);
|
|
819
|
+
} else {
|
|
820
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
821
|
+
if (wrapped) {
|
|
822
|
+
getExportsWhenWrapping(
|
|
823
|
+
exportDeclarations,
|
|
824
|
+
exportsName,
|
|
825
|
+
detectWrappedDefault,
|
|
826
|
+
HELPERS_NAME,
|
|
827
|
+
defaultIsModuleExports
|
|
828
|
+
);
|
|
829
|
+
} else {
|
|
830
|
+
getExports(
|
|
831
|
+
magicString,
|
|
832
|
+
exports,
|
|
833
|
+
exportDeclarations,
|
|
834
|
+
moduleExportsAssignments,
|
|
835
|
+
exportsAssignmentsByName,
|
|
836
|
+
deconflictedExportNames,
|
|
837
|
+
topLevelAssignments,
|
|
838
|
+
moduleName,
|
|
839
|
+
exportsName,
|
|
840
|
+
defineCompiledEsmExpressions,
|
|
841
|
+
HELPERS_NAME,
|
|
842
|
+
defaultIsModuleExports
|
|
843
|
+
);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (exports.length) {
|
|
847
|
+
exportDeclarations.push(`export { ${exports.join(', ')} };`);
|
|
848
|
+
}
|
|
817
849
|
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
const namedExportProperties = [];
|
|
850
|
+
return `\n\n${exportDeclarations.join('\n')}`;
|
|
851
|
+
}
|
|
821
852
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
853
|
+
function getExportsForReplacedModuleExports(
|
|
854
|
+
magicString,
|
|
855
|
+
exports,
|
|
856
|
+
exportDeclarations,
|
|
857
|
+
moduleExportsAssignments,
|
|
858
|
+
firstTopLevelModuleExportsAssignment,
|
|
859
|
+
exportsName
|
|
860
|
+
) {
|
|
861
|
+
for (const { left } of moduleExportsAssignments) {
|
|
862
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
863
|
+
}
|
|
864
|
+
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
865
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
866
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
867
|
+
}
|
|
827
868
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
869
|
+
function getExportsWhenWrapping(
|
|
870
|
+
exportDeclarations,
|
|
871
|
+
exportsName,
|
|
872
|
+
detectWrappedDefault,
|
|
873
|
+
HELPERS_NAME,
|
|
874
|
+
defaultIsModuleExports
|
|
875
|
+
) {
|
|
876
|
+
exportDeclarations.push(
|
|
877
|
+
`export default ${
|
|
878
|
+
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
879
|
+
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
880
|
+
: defaultIsModuleExports === false
|
|
881
|
+
? `${exportsName}.default`
|
|
882
|
+
: exportsName
|
|
883
|
+
};`
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
function getExports(
|
|
888
|
+
magicString,
|
|
889
|
+
exports,
|
|
890
|
+
exportDeclarations,
|
|
891
|
+
moduleExportsAssignments,
|
|
892
|
+
exportsAssignmentsByName,
|
|
893
|
+
deconflictedExportNames,
|
|
894
|
+
topLevelAssignments,
|
|
895
|
+
moduleName,
|
|
896
|
+
exportsName,
|
|
897
|
+
defineCompiledEsmExpressions,
|
|
898
|
+
HELPERS_NAME,
|
|
899
|
+
defaultIsModuleExports
|
|
900
|
+
) {
|
|
901
|
+
let deconflictedDefaultExportName;
|
|
902
|
+
// Collect and rewrite module.exports assignments
|
|
903
|
+
for (const { left } of moduleExportsAssignments) {
|
|
904
|
+
magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
|
|
905
|
+
}
|
|
842
906
|
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
907
|
+
// Collect and rewrite named exports
|
|
908
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
909
|
+
const deconflicted = deconflictedExportNames[exportName];
|
|
910
|
+
let needsDeclaration = true;
|
|
911
|
+
for (const node of nodes) {
|
|
912
|
+
let replacement = `${deconflicted} = ${exportsName}.${exportName}`;
|
|
913
|
+
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
914
|
+
replacement = `var ${replacement}`;
|
|
915
|
+
needsDeclaration = false;
|
|
847
916
|
}
|
|
917
|
+
magicString.overwrite(node.start, node.left.end, replacement);
|
|
848
918
|
}
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
if (!hasModuleExportsAssignment) {
|
|
852
|
-
const moduleExports = `{\n${namedExportProperties.join(',\n')}\n}`;
|
|
853
|
-
magicString
|
|
854
|
-
.trim()
|
|
855
|
-
.append(
|
|
856
|
-
`\n\nvar ${moduleName} = ${
|
|
857
|
-
isRestorableCompiledEsm
|
|
858
|
-
? `/*#__PURE__*/Object.defineProperty(${moduleExports}, '__esModule', {value: true})`
|
|
859
|
-
: moduleExports
|
|
860
|
-
};`
|
|
861
|
-
);
|
|
919
|
+
if (needsDeclaration) {
|
|
920
|
+
magicString.prepend(`var ${deconflicted};\n`);
|
|
862
921
|
}
|
|
863
|
-
}
|
|
864
922
|
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
if (defaultIsModuleExports === 'auto') {
|
|
868
|
-
if (isRestorableCompiledEsm) {
|
|
869
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
|
|
870
|
-
} else if (
|
|
871
|
-
(wrapped || deconflictedDefaultExportName) &&
|
|
872
|
-
(defineCompiledEsmExpressions.length > 0 || code.includes('__esModule'))
|
|
873
|
-
) {
|
|
874
|
-
// eslint-disable-next-line no-param-reassign
|
|
875
|
-
uses.commonjsHelpers = true;
|
|
876
|
-
defaultExport.push(
|
|
877
|
-
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
|
|
878
|
-
);
|
|
923
|
+
if (exportName === 'default') {
|
|
924
|
+
deconflictedDefaultExportName = deconflicted;
|
|
879
925
|
} else {
|
|
880
|
-
|
|
881
|
-
}
|
|
882
|
-
} else if (defaultIsModuleExports === true) {
|
|
883
|
-
defaultExport.push(`export default ${moduleName};`);
|
|
884
|
-
} else if (defaultIsModuleExports === false) {
|
|
885
|
-
if (deconflictedDefaultExportName) {
|
|
886
|
-
defaultExport.push(`export default ${deconflictedDefaultExportName};`);
|
|
887
|
-
} else {
|
|
888
|
-
defaultExport.push(`export default ${moduleName}.default;`);
|
|
926
|
+
exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
|
|
889
927
|
}
|
|
890
928
|
}
|
|
891
929
|
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
930
|
+
// Collect and rewrite exports.__esModule assignments
|
|
931
|
+
let isRestorableCompiledEsm = false;
|
|
932
|
+
for (const expression of defineCompiledEsmExpressions) {
|
|
933
|
+
isRestorableCompiledEsm = true;
|
|
934
|
+
const moduleExportsExpression =
|
|
935
|
+
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
936
|
+
magicString.overwrite(moduleExportsExpression.start, moduleExportsExpression.end, exportsName);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
if (!isRestorableCompiledEsm || defaultIsModuleExports === true) {
|
|
940
|
+
exportDeclarations.push(`export default ${exportsName};`);
|
|
941
|
+
} else if (moduleExportsAssignments.length === 0 || defaultIsModuleExports === false) {
|
|
942
|
+
exports.push(`${deconflictedDefaultExportName || exportsName} as default`);
|
|
943
|
+
} else {
|
|
944
|
+
exportDeclarations.push(
|
|
945
|
+
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName});`
|
|
946
|
+
);
|
|
947
|
+
}
|
|
896
948
|
}
|
|
897
949
|
|
|
898
950
|
function isRequireStatement(node, scope) {
|
|
@@ -1010,44 +1062,46 @@ function getRequireHandlers() {
|
|
|
1010
1062
|
topLevelDeclarations,
|
|
1011
1063
|
topLevelRequireDeclarators,
|
|
1012
1064
|
reassignedNames,
|
|
1013
|
-
|
|
1014
|
-
dynamicRegisterSources
|
|
1065
|
+
helpersName,
|
|
1066
|
+
dynamicRegisterSources,
|
|
1067
|
+
moduleName,
|
|
1068
|
+
exportsName,
|
|
1069
|
+
id,
|
|
1070
|
+
exportMode
|
|
1015
1071
|
) {
|
|
1016
|
-
const removedDeclarators = getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1017
|
-
topLevelRequireDeclarators,
|
|
1018
|
-
requiredByNode,
|
|
1019
|
-
reassignedNames
|
|
1020
|
-
);
|
|
1021
1072
|
setRemainingImportNamesAndRewriteRequires(
|
|
1022
1073
|
requireExpressionsWithUsedReturnValue,
|
|
1023
1074
|
requiredByNode,
|
|
1024
1075
|
magicString
|
|
1025
1076
|
);
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1077
|
+
const imports = [];
|
|
1078
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}";`);
|
|
1079
|
+
if (exportMode === 'module') {
|
|
1080
|
+
imports.push(
|
|
1081
|
+
`import { __module as ${moduleName}, exports as ${exportsName} } from ${JSON.stringify(
|
|
1082
|
+
wrapId(id, MODULE_SUFFIX)
|
|
1083
|
+
)}`
|
|
1084
|
+
);
|
|
1085
|
+
} else if (exportMode === 'exports') {
|
|
1086
|
+
imports.push(
|
|
1087
|
+
`import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
|
|
1088
|
+
);
|
|
1089
|
+
}
|
|
1090
|
+
for (const source of dynamicRegisterSources) {
|
|
1091
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1092
|
+
}
|
|
1093
|
+
for (const source of requiredSources) {
|
|
1094
|
+
if (!source.startsWith('\0')) {
|
|
1095
|
+
imports.push(`import ${JSON.stringify(wrapId(source, REQUIRE_SUFFIX))};`);
|
|
1096
|
+
}
|
|
1097
|
+
const { name, nodesUsingRequired } = requiredBySource[source];
|
|
1098
|
+
imports.push(
|
|
1099
|
+
`import ${nodesUsingRequired.length ? `${name} from ` : ''}${JSON.stringify(
|
|
1100
|
+
source.startsWith('\0') ? source : wrapId(source, PROXY_SUFFIX)
|
|
1101
|
+
)};`
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
return imports.length ? `${imports.join('\n')}\n\n` : '';
|
|
1051
1105
|
}
|
|
1052
1106
|
|
|
1053
1107
|
return {
|
|
@@ -1057,30 +1111,6 @@ function getRequireHandlers() {
|
|
|
1057
1111
|
};
|
|
1058
1112
|
}
|
|
1059
1113
|
|
|
1060
|
-
function getDeclaratorsReplacedByImportsAndSetImportNames(
|
|
1061
|
-
topLevelRequireDeclarators,
|
|
1062
|
-
requiredByNode,
|
|
1063
|
-
reassignedNames
|
|
1064
|
-
) {
|
|
1065
|
-
const removedDeclarators = new Set();
|
|
1066
|
-
for (const declarator of topLevelRequireDeclarators) {
|
|
1067
|
-
const { required } = requiredByNode.get(declarator.init);
|
|
1068
|
-
if (!required.name) {
|
|
1069
|
-
const potentialName = declarator.id.name;
|
|
1070
|
-
if (
|
|
1071
|
-
!reassignedNames.has(potentialName) &&
|
|
1072
|
-
!required.nodesUsingRequired.some((node) =>
|
|
1073
|
-
isLocallyShadowed(potentialName, requiredByNode.get(node).scope)
|
|
1074
|
-
)
|
|
1075
|
-
) {
|
|
1076
|
-
required.name = potentialName;
|
|
1077
|
-
removedDeclarators.add(declarator);
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
return removedDeclarators;
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
1114
|
function setRemainingImportNamesAndRewriteRequires(
|
|
1085
1115
|
requireExpressionsWithUsedReturnValue,
|
|
1086
1116
|
requiredByNode,
|
|
@@ -1102,25 +1132,6 @@ function setRemainingImportNamesAndRewriteRequires(
|
|
|
1102
1132
|
}
|
|
1103
1133
|
}
|
|
1104
1134
|
|
|
1105
|
-
function removeDeclaratorsFromDeclarations(topLevelDeclarations, removedDeclarators, magicString) {
|
|
1106
|
-
for (const declaration of topLevelDeclarations) {
|
|
1107
|
-
let keepDeclaration = false;
|
|
1108
|
-
let [{ start }] = declaration.declarations;
|
|
1109
|
-
for (const declarator of declaration.declarations) {
|
|
1110
|
-
if (removedDeclarators.has(declarator)) {
|
|
1111
|
-
magicString.remove(start, declarator.end);
|
|
1112
|
-
} else if (!keepDeclaration) {
|
|
1113
|
-
magicString.remove(start, declarator.start);
|
|
1114
|
-
keepDeclaration = true;
|
|
1115
|
-
}
|
|
1116
|
-
start = declarator.end;
|
|
1117
|
-
}
|
|
1118
|
-
if (!keepDeclaration) {
|
|
1119
|
-
magicString.remove(declaration.start, declaration.end);
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
1135
|
/* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
|
|
1125
1136
|
|
|
1126
1137
|
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
|
|
@@ -1150,9 +1161,9 @@ function transformCommonjs(
|
|
|
1150
1161
|
module: false,
|
|
1151
1162
|
exports: false,
|
|
1152
1163
|
global: false,
|
|
1153
|
-
require: false
|
|
1154
|
-
commonjsHelpers: false
|
|
1164
|
+
require: false
|
|
1155
1165
|
};
|
|
1166
|
+
let usesDynamicRequire = false;
|
|
1156
1167
|
const virtualDynamicRequirePath =
|
|
1157
1168
|
isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(path.dirname(id), commonDir);
|
|
1158
1169
|
let scope = pluginutils.attachScopes(ast, 'scope');
|
|
@@ -1160,13 +1171,11 @@ function transformCommonjs(
|
|
|
1160
1171
|
let programDepth = 0;
|
|
1161
1172
|
let currentTryBlockEnd = null;
|
|
1162
1173
|
let shouldWrap = false;
|
|
1163
|
-
const defineCompiledEsmExpressions = [];
|
|
1164
1174
|
|
|
1165
1175
|
const globals = new Set();
|
|
1166
1176
|
|
|
1167
1177
|
// TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
|
|
1168
|
-
const HELPERS_NAME = deconflict(scope, globals, 'commonjsHelpers');
|
|
1169
|
-
const namedExports = {};
|
|
1178
|
+
const HELPERS_NAME = deconflict([scope], globals, 'commonjsHelpers');
|
|
1170
1179
|
const dynamicRegisterSources = new Set();
|
|
1171
1180
|
let hasRemovedRequire = false;
|
|
1172
1181
|
|
|
@@ -1183,8 +1192,13 @@ function transformCommonjs(
|
|
|
1183
1192
|
const topLevelDeclarations = [];
|
|
1184
1193
|
const topLevelRequireDeclarators = new Set();
|
|
1185
1194
|
const skippedNodes = new Set();
|
|
1186
|
-
const
|
|
1187
|
-
const
|
|
1195
|
+
const moduleAccessScopes = new Set([scope]);
|
|
1196
|
+
const exportsAccessScopes = new Set([scope]);
|
|
1197
|
+
const moduleExportsAssignments = [];
|
|
1198
|
+
let firstTopLevelModuleExportsAssignment = null;
|
|
1199
|
+
const exportsAssignmentsByName = new Map();
|
|
1200
|
+
const topLevelAssignments = new Set();
|
|
1201
|
+
const topLevelDefineCompiledEsmExpressions = [];
|
|
1188
1202
|
|
|
1189
1203
|
estreeWalker.walk(ast, {
|
|
1190
1204
|
enter(node, parent) {
|
|
@@ -1224,30 +1238,34 @@ function transformCommonjs(
|
|
|
1224
1238
|
uses[flattened.name] = true;
|
|
1225
1239
|
|
|
1226
1240
|
// we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
|
|
1227
|
-
if (
|
|
1228
|
-
|
|
1241
|
+
if (flattened.keypath === 'module.exports') {
|
|
1242
|
+
moduleExportsAssignments.push(node);
|
|
1243
|
+
if (programDepth > 3) {
|
|
1244
|
+
moduleAccessScopes.add(scope);
|
|
1245
|
+
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1246
|
+
firstTopLevelModuleExportsAssignment = node;
|
|
1247
|
+
}
|
|
1229
1248
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1249
|
+
if (programDepth > 3) {
|
|
1250
|
+
shouldWrap = true;
|
|
1251
|
+
} else {
|
|
1252
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1253
|
+
}
|
|
1235
1254
|
} else {
|
|
1236
|
-
|
|
1255
|
+
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1256
|
+
nodes: [],
|
|
1257
|
+
scopes: new Set()
|
|
1258
|
+
};
|
|
1259
|
+
exportsAssignments.nodes.push(node);
|
|
1260
|
+
exportsAssignments.scopes.add(scope);
|
|
1261
|
+
exportsAccessScopes.add(scope);
|
|
1262
|
+
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
1263
|
+
if (programDepth <= 3) {
|
|
1264
|
+
topLevelAssignments.add(node);
|
|
1265
|
+
}
|
|
1237
1266
|
}
|
|
1238
1267
|
|
|
1239
1268
|
skippedNodes.add(node.left);
|
|
1240
|
-
|
|
1241
|
-
if (flattened.keypath === 'module.exports' && node.right.type === 'ObjectExpression') {
|
|
1242
|
-
node.right.properties.forEach((prop) => {
|
|
1243
|
-
if (prop.computed || !('key' in prop) || prop.key.type !== 'Identifier') return;
|
|
1244
|
-
const { name } = prop.key;
|
|
1245
|
-
if (name === pluginutils.makeLegalIdentifier(name)) namedExports[name] = true;
|
|
1246
|
-
});
|
|
1247
|
-
return;
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
if (exportsPatternMatch[1]) namedExports[exportsPatternMatch[1]] = true;
|
|
1251
1269
|
} else {
|
|
1252
1270
|
for (const name of pluginutils.extractAssignedNames(node.left)) {
|
|
1253
1271
|
reassignedNames.add(name);
|
|
@@ -1259,7 +1277,7 @@ function transformCommonjs(
|
|
|
1259
1277
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1260
1278
|
// skip special handling for [module.]exports until we know we render this
|
|
1261
1279
|
skippedNodes.add(node.arguments[0]);
|
|
1262
|
-
|
|
1280
|
+
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1263
1281
|
} else {
|
|
1264
1282
|
shouldWrap = true;
|
|
1265
1283
|
}
|
|
@@ -1287,7 +1305,6 @@ function transformCommonjs(
|
|
|
1287
1305
|
storeName: true
|
|
1288
1306
|
}
|
|
1289
1307
|
);
|
|
1290
|
-
uses.commonjsHelpers = true;
|
|
1291
1308
|
return;
|
|
1292
1309
|
}
|
|
1293
1310
|
|
|
@@ -1314,13 +1331,13 @@ function transformCommonjs(
|
|
|
1314
1331
|
}
|
|
1315
1332
|
|
|
1316
1333
|
let sourceId = getRequireStringArg(node);
|
|
1317
|
-
const isDynamicRegister =
|
|
1334
|
+
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1318
1335
|
if (isDynamicRegister) {
|
|
1319
|
-
sourceId =
|
|
1336
|
+
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
|
|
1320
1337
|
if (sourceId.endsWith('.json')) {
|
|
1321
1338
|
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
|
|
1322
1339
|
}
|
|
1323
|
-
dynamicRegisterSources.add(
|
|
1340
|
+
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
|
|
1324
1341
|
} else {
|
|
1325
1342
|
if (
|
|
1326
1343
|
!sourceId.endsWith('.json') &&
|
|
@@ -1338,7 +1355,7 @@ function transformCommonjs(
|
|
|
1338
1355
|
path.dirname(id) === '.' ? null /* default behavior */ : virtualDynamicRequirePath
|
|
1339
1356
|
)})`
|
|
1340
1357
|
);
|
|
1341
|
-
|
|
1358
|
+
usesDynamicRequire = true;
|
|
1342
1359
|
}
|
|
1343
1360
|
return;
|
|
1344
1361
|
}
|
|
@@ -1395,7 +1412,6 @@ function transformCommonjs(
|
|
|
1395
1412
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1396
1413
|
storeName: true
|
|
1397
1414
|
});
|
|
1398
|
-
uses.commonjsHelpers = true;
|
|
1399
1415
|
}
|
|
1400
1416
|
}
|
|
1401
1417
|
|
|
@@ -1419,8 +1435,7 @@ function transformCommonjs(
|
|
|
1419
1435
|
});
|
|
1420
1436
|
}
|
|
1421
1437
|
}
|
|
1422
|
-
|
|
1423
|
-
uses.commonjsHelpers = true;
|
|
1438
|
+
usesDynamicRequire = true;
|
|
1424
1439
|
return;
|
|
1425
1440
|
case 'module':
|
|
1426
1441
|
case 'exports':
|
|
@@ -1433,11 +1448,12 @@ function transformCommonjs(
|
|
|
1433
1448
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1434
1449
|
storeName: true
|
|
1435
1450
|
});
|
|
1436
|
-
uses.commonjsHelpers = true;
|
|
1437
1451
|
}
|
|
1438
1452
|
return;
|
|
1439
1453
|
case 'define':
|
|
1440
|
-
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1454
|
+
magicString.overwrite(node.start, node.end, 'undefined', {
|
|
1455
|
+
storeName: true
|
|
1456
|
+
});
|
|
1441
1457
|
return;
|
|
1442
1458
|
default:
|
|
1443
1459
|
globals.add(name);
|
|
@@ -1449,7 +1465,6 @@ function transformCommonjs(
|
|
|
1449
1465
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, {
|
|
1450
1466
|
storeName: true
|
|
1451
1467
|
});
|
|
1452
|
-
uses.commonjsHelpers = true;
|
|
1453
1468
|
skippedNodes.add(node.object);
|
|
1454
1469
|
skippedNodes.add(node.property);
|
|
1455
1470
|
}
|
|
@@ -1468,7 +1483,6 @@ function transformCommonjs(
|
|
|
1468
1483
|
magicString.overwrite(node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, {
|
|
1469
1484
|
storeName: true
|
|
1470
1485
|
});
|
|
1471
|
-
uses.commonjsHelpers = true;
|
|
1472
1486
|
}
|
|
1473
1487
|
}
|
|
1474
1488
|
return;
|
|
@@ -1485,7 +1499,9 @@ function transformCommonjs(
|
|
|
1485
1499
|
flattened.keypath === 'module' ||
|
|
1486
1500
|
flattened.keypath === 'exports'
|
|
1487
1501
|
) {
|
|
1488
|
-
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1502
|
+
magicString.overwrite(node.start, node.end, `'object'`, {
|
|
1503
|
+
storeName: false
|
|
1504
|
+
});
|
|
1489
1505
|
}
|
|
1490
1506
|
}
|
|
1491
1507
|
return;
|
|
@@ -1503,23 +1519,22 @@ function transformCommonjs(
|
|
|
1503
1519
|
}
|
|
1504
1520
|
});
|
|
1505
1521
|
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
defineCompiledEsmExpressions[0].end
|
|
1513
|
-
);
|
|
1514
|
-
} else {
|
|
1515
|
-
shouldWrap = true;
|
|
1516
|
-
uses.exports = true;
|
|
1517
|
-
}
|
|
1522
|
+
const nameBase = getName(id);
|
|
1523
|
+
const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
|
|
1524
|
+
const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
|
|
1525
|
+
const deconflictedExportNames = Object.create(null);
|
|
1526
|
+
for (const [exportName, { scopes }] of exportsAssignmentsByName) {
|
|
1527
|
+
deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
|
|
1518
1528
|
}
|
|
1519
1529
|
|
|
1520
1530
|
// We cannot wrap ES/mixed modules
|
|
1521
|
-
shouldWrap =
|
|
1522
|
-
|
|
1531
|
+
shouldWrap =
|
|
1532
|
+
!isEsModule &&
|
|
1533
|
+
!disableWrap &&
|
|
1534
|
+
(shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1535
|
+
const detectWrappedDefault =
|
|
1536
|
+
shouldWrap &&
|
|
1537
|
+
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
|
|
1523
1538
|
|
|
1524
1539
|
if (
|
|
1525
1540
|
!(
|
|
@@ -1528,17 +1543,15 @@ function transformCommonjs(
|
|
|
1528
1543
|
uses.module ||
|
|
1529
1544
|
uses.exports ||
|
|
1530
1545
|
uses.require ||
|
|
1531
|
-
|
|
1546
|
+
usesDynamicRequire ||
|
|
1532
1547
|
hasRemovedRequire ||
|
|
1533
|
-
|
|
1548
|
+
topLevelDefineCompiledEsmExpressions.length > 0
|
|
1534
1549
|
) &&
|
|
1535
1550
|
(ignoreGlobal || !uses.global)
|
|
1536
1551
|
) {
|
|
1537
1552
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1538
1553
|
}
|
|
1539
1554
|
|
|
1540
|
-
const moduleName = deconflict(scope, globals, getName(id));
|
|
1541
|
-
|
|
1542
1555
|
let leadingComment = '';
|
|
1543
1556
|
if (code.startsWith('/*')) {
|
|
1544
1557
|
const commentEnd = code.indexOf('*/', 2) + 2;
|
|
@@ -1546,34 +1559,53 @@ function transformCommonjs(
|
|
|
1546
1559
|
magicString.remove(0, commentEnd).trim();
|
|
1547
1560
|
}
|
|
1548
1561
|
|
|
1562
|
+
const exportMode = shouldWrap
|
|
1563
|
+
? uses.module
|
|
1564
|
+
? 'module'
|
|
1565
|
+
: 'exports'
|
|
1566
|
+
: firstTopLevelModuleExportsAssignment
|
|
1567
|
+
? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
|
|
1568
|
+
? 'replace'
|
|
1569
|
+
: 'module'
|
|
1570
|
+
: moduleExportsAssignments.length === 0
|
|
1571
|
+
? 'exports'
|
|
1572
|
+
: 'module';
|
|
1573
|
+
|
|
1574
|
+
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1575
|
+
magicString,
|
|
1576
|
+
topLevelDeclarations,
|
|
1577
|
+
topLevelRequireDeclarators,
|
|
1578
|
+
reassignedNames,
|
|
1579
|
+
HELPERS_NAME,
|
|
1580
|
+
dynamicRegisterSources,
|
|
1581
|
+
moduleName,
|
|
1582
|
+
exportsName,
|
|
1583
|
+
id,
|
|
1584
|
+
exportMode
|
|
1585
|
+
);
|
|
1586
|
+
|
|
1549
1587
|
const exportBlock = isEsModule
|
|
1550
1588
|
? ''
|
|
1551
1589
|
: rewriteExportsAndGetExportsBlock(
|
|
1552
1590
|
magicString,
|
|
1553
1591
|
moduleName,
|
|
1592
|
+
exportsName,
|
|
1554
1593
|
shouldWrap,
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1594
|
+
moduleExportsAssignments,
|
|
1595
|
+
firstTopLevelModuleExportsAssignment,
|
|
1596
|
+
exportsAssignmentsByName,
|
|
1597
|
+
topLevelAssignments,
|
|
1598
|
+
topLevelDefineCompiledEsmExpressions,
|
|
1599
|
+
deconflictedExportNames,
|
|
1560
1600
|
code,
|
|
1561
|
-
uses,
|
|
1562
1601
|
HELPERS_NAME,
|
|
1602
|
+
exportMode,
|
|
1603
|
+
detectWrappedDefault,
|
|
1563
1604
|
defaultIsModuleExports
|
|
1564
1605
|
);
|
|
1565
1606
|
|
|
1566
|
-
const importBlock = rewriteRequireExpressionsAndGetImportBlock(
|
|
1567
|
-
magicString,
|
|
1568
|
-
topLevelDeclarations,
|
|
1569
|
-
topLevelRequireDeclarators,
|
|
1570
|
-
reassignedNames,
|
|
1571
|
-
uses.commonjsHelpers && HELPERS_NAME,
|
|
1572
|
-
dynamicRegisterSources
|
|
1573
|
-
);
|
|
1574
|
-
|
|
1575
1607
|
if (shouldWrap) {
|
|
1576
|
-
wrapCode(magicString, uses, moduleName,
|
|
1608
|
+
wrapCode(magicString, uses, moduleName, exportsName);
|
|
1577
1609
|
}
|
|
1578
1610
|
|
|
1579
1611
|
magicString
|
|
@@ -1622,7 +1654,7 @@ function commonjs(options = {}) {
|
|
|
1622
1654
|
|
|
1623
1655
|
const esModulesWithDefaultExport = new Set();
|
|
1624
1656
|
const esModulesWithNamedExports = new Set();
|
|
1625
|
-
const
|
|
1657
|
+
const commonJsMetaPromises = new Map();
|
|
1626
1658
|
|
|
1627
1659
|
const ignoreRequire =
|
|
1628
1660
|
typeof options.ignore === 'function'
|
|
@@ -1675,13 +1707,11 @@ function commonjs(options = {}) {
|
|
|
1675
1707
|
return { meta: { commonjs: { isCommonJS: false } } };
|
|
1676
1708
|
}
|
|
1677
1709
|
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
if (isModuleRegisterProxy(id)) {
|
|
1682
|
-
disableWrap = true;
|
|
1710
|
+
// avoid wrapping as this is a commonjsRegister call
|
|
1711
|
+
const disableWrap = isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1712
|
+
if (disableWrap) {
|
|
1683
1713
|
// eslint-disable-next-line no-param-reassign
|
|
1684
|
-
id =
|
|
1714
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1685
1715
|
}
|
|
1686
1716
|
|
|
1687
1717
|
return transformCommonjs(
|
|
@@ -1726,6 +1756,39 @@ function commonjs(options = {}) {
|
|
|
1726
1756
|
return getSpecificHelperProxy(id);
|
|
1727
1757
|
}
|
|
1728
1758
|
|
|
1759
|
+
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
1760
|
+
const actualId = unwrapId(id, MODULE_SUFFIX);
|
|
1761
|
+
let name = getName(actualId);
|
|
1762
|
+
let code;
|
|
1763
|
+
if (isDynamicRequireModulesEnabled) {
|
|
1764
|
+
if (['modulePath', 'commonjsRequire', 'createModule'].includes(name)) {
|
|
1765
|
+
name = `${name}_`;
|
|
1766
|
+
}
|
|
1767
|
+
code =
|
|
1768
|
+
`import {commonjsRequire, createModule} from "${HELPERS_ID}";\n` +
|
|
1769
|
+
`var ${name} = createModule(${JSON.stringify(
|
|
1770
|
+
getVirtualPathForDynamicRequirePath(path.dirname(actualId), commonDir)
|
|
1771
|
+
)});\n` +
|
|
1772
|
+
`export {${name} as __module}`;
|
|
1773
|
+
} else {
|
|
1774
|
+
code = `var ${name} = {exports: {}}; export {${name} as __module}`;
|
|
1775
|
+
}
|
|
1776
|
+
return {
|
|
1777
|
+
code,
|
|
1778
|
+
syntheticNamedExports: '__module',
|
|
1779
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
if (isWrappedId(id, EXPORTS_SUFFIX)) {
|
|
1784
|
+
const actualId = unwrapId(id, EXPORTS_SUFFIX);
|
|
1785
|
+
const name = getName(actualId);
|
|
1786
|
+
return {
|
|
1787
|
+
code: `var ${name} = {}; export {${name} as __exports}`,
|
|
1788
|
+
meta: { commonjs: { isCommonJS: false } }
|
|
1789
|
+
};
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1729
1792
|
if (isWrappedId(id, EXTERNAL_SUFFIX)) {
|
|
1730
1793
|
const actualId = unwrapId(id, EXTERNAL_SUFFIX);
|
|
1731
1794
|
return getUnknownRequireProxy(
|
|
@@ -1746,9 +1809,9 @@ function commonjs(options = {}) {
|
|
|
1746
1809
|
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
|
|
1747
1810
|
}
|
|
1748
1811
|
|
|
1749
|
-
if (
|
|
1812
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1750
1813
|
return getDynamicRequireProxy(
|
|
1751
|
-
normalizePathSlashes(
|
|
1814
|
+
normalizePathSlashes(unwrapId(id, DYNAMIC_REGISTER_SUFFIX)),
|
|
1752
1815
|
commonDir
|
|
1753
1816
|
);
|
|
1754
1817
|
}
|
|
@@ -1760,7 +1823,7 @@ function commonjs(options = {}) {
|
|
|
1760
1823
|
getRequireReturnsDefault(actualId),
|
|
1761
1824
|
esModulesWithDefaultExport,
|
|
1762
1825
|
esModulesWithNamedExports,
|
|
1763
|
-
|
|
1826
|
+
commonJsMetaPromises
|
|
1764
1827
|
);
|
|
1765
1828
|
}
|
|
1766
1829
|
|
|
@@ -1770,8 +1833,8 @@ function commonjs(options = {}) {
|
|
|
1770
1833
|
transform(code, rawId) {
|
|
1771
1834
|
let id = rawId;
|
|
1772
1835
|
|
|
1773
|
-
if (
|
|
1774
|
-
id =
|
|
1836
|
+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
|
|
1837
|
+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
|
|
1775
1838
|
}
|
|
1776
1839
|
|
|
1777
1840
|
const extName = path.extname(id);
|
|
@@ -1791,16 +1854,12 @@ function commonjs(options = {}) {
|
|
|
1791
1854
|
}
|
|
1792
1855
|
},
|
|
1793
1856
|
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
if (isCjs != null) {
|
|
1799
|
-
setIsCjsPromise(isCjsPromises, id, isCjs);
|
|
1800
|
-
return;
|
|
1801
|
-
}
|
|
1857
|
+
moduleParsed({ id, meta: { commonjs: commonjsMeta } }) {
|
|
1858
|
+
if (commonjsMeta && commonjsMeta.isCommonJS != null) {
|
|
1859
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, commonjsMeta);
|
|
1860
|
+
return;
|
|
1802
1861
|
}
|
|
1803
|
-
|
|
1862
|
+
setCommonJSMetaPromise(commonJsMetaPromises, id, null);
|
|
1804
1863
|
}
|
|
1805
1864
|
};
|
|
1806
1865
|
}
|