@rollup/plugin-commonjs 24.0.1 → 24.1.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/dist/cjs/index.js +159 -135
- package/dist/es/index.js +159 -135
- package/package.json +2 -2
package/dist/cjs/index.js
CHANGED
|
@@ -11,7 +11,7 @@ var estreeWalker = require('estree-walker');
|
|
|
11
11
|
var MagicString = require('magic-string');
|
|
12
12
|
var isReference = require('is-reference');
|
|
13
13
|
|
|
14
|
-
var version = "24.0
|
|
14
|
+
var version = "24.1.0-0";
|
|
15
15
|
var peerDependencies = {
|
|
16
16
|
rollup: "^2.68.0||^3.0.0"
|
|
17
17
|
};
|
|
@@ -415,13 +415,16 @@ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
|
415
415
|
} = await loadModule({ id });
|
|
416
416
|
if (!commonjsMeta) {
|
|
417
417
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
418
|
-
}
|
|
418
|
+
}
|
|
419
|
+
if (commonjsMeta.isCommonJS) {
|
|
419
420
|
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
420
|
-
}
|
|
421
|
+
}
|
|
422
|
+
if (!requireReturnsDefault) {
|
|
421
423
|
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
422
424
|
id
|
|
423
425
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
424
|
-
}
|
|
426
|
+
}
|
|
427
|
+
if (
|
|
425
428
|
requireReturnsDefault !== true &&
|
|
426
429
|
(requireReturnsDefault === 'namespace' ||
|
|
427
430
|
!commonjsMeta.hasDefaultExport ||
|
|
@@ -911,11 +914,16 @@ function getKeypath(node) {
|
|
|
911
914
|
|
|
912
915
|
const KEY_COMPILED_ESM = '__esModule';
|
|
913
916
|
|
|
914
|
-
function
|
|
917
|
+
function getDefineCompiledEsmType(node) {
|
|
918
|
+
const definedPropertyWithExports = getDefinePropertyCallName(node, 'exports');
|
|
915
919
|
const definedProperty =
|
|
916
|
-
|
|
920
|
+
definedPropertyWithExports || getDefinePropertyCallName(node, 'module.exports');
|
|
917
921
|
if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
|
|
918
|
-
return isTruthy(definedProperty.value)
|
|
922
|
+
return isTruthy(definedProperty.value)
|
|
923
|
+
? definedPropertyWithExports
|
|
924
|
+
? 'exports'
|
|
925
|
+
: 'module'
|
|
926
|
+
: false;
|
|
919
927
|
}
|
|
920
928
|
return false;
|
|
921
929
|
}
|
|
@@ -959,20 +967,6 @@ function isShorthandProperty(parent) {
|
|
|
959
967
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
960
968
|
}
|
|
961
969
|
|
|
962
|
-
function hasDefineEsmProperty(node) {
|
|
963
|
-
return node.properties.some((property) => {
|
|
964
|
-
if (
|
|
965
|
-
property.type === 'Property' &&
|
|
966
|
-
property.key.type === 'Identifier' &&
|
|
967
|
-
property.key.name === '__esModule' &&
|
|
968
|
-
isTruthy(property.value)
|
|
969
|
-
) {
|
|
970
|
-
return true;
|
|
971
|
-
}
|
|
972
|
-
return false;
|
|
973
|
-
});
|
|
974
|
-
}
|
|
975
|
-
|
|
976
970
|
function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
|
|
977
971
|
const args = [];
|
|
978
972
|
const passedArgs = [];
|
|
@@ -982,19 +976,22 @@ function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRan
|
|
|
982
976
|
}
|
|
983
977
|
if (uses.exports) {
|
|
984
978
|
args.push('exports');
|
|
985
|
-
passedArgs.push(exportsName);
|
|
979
|
+
passedArgs.push(uses.module ? `${moduleName}.exports` : exportsName);
|
|
986
980
|
}
|
|
987
981
|
magicString
|
|
988
982
|
.trim()
|
|
989
983
|
.indent('\t', { exclude: indentExclusionRanges })
|
|
990
984
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
991
|
-
|
|
985
|
+
// For some reason, this line is only indented correctly when using a
|
|
986
|
+
// require-wrapper if we have this leading space
|
|
987
|
+
.append(` \n} (${passedArgs.join(', ')}));`);
|
|
992
988
|
}
|
|
993
989
|
|
|
994
990
|
function rewriteExportsAndGetExportsBlock(
|
|
995
991
|
magicString,
|
|
996
992
|
moduleName,
|
|
997
993
|
exportsName,
|
|
994
|
+
exportedExportsName,
|
|
998
995
|
wrapped,
|
|
999
996
|
moduleExportsAssignments,
|
|
1000
997
|
firstTopLevelModuleExportsAssignment,
|
|
@@ -1005,7 +1002,6 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
1005
1002
|
code,
|
|
1006
1003
|
HELPERS_NAME,
|
|
1007
1004
|
exportMode,
|
|
1008
|
-
detectWrappedDefault,
|
|
1009
1005
|
defaultIsModuleExports,
|
|
1010
1006
|
usesRequireWrapper,
|
|
1011
1007
|
requireName
|
|
@@ -1033,17 +1029,20 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
1033
1029
|
exportDeclarations,
|
|
1034
1030
|
moduleExportsAssignments,
|
|
1035
1031
|
firstTopLevelModuleExportsAssignment,
|
|
1036
|
-
exportsName
|
|
1032
|
+
exportsName,
|
|
1033
|
+
defaultIsModuleExports,
|
|
1034
|
+
HELPERS_NAME
|
|
1037
1035
|
);
|
|
1038
1036
|
} else {
|
|
1039
|
-
|
|
1037
|
+
if (exportMode === 'module') {
|
|
1038
|
+
exportDeclarations.push(`var ${exportedExportsName} = ${moduleName}.exports`);
|
|
1039
|
+
exports.push(`${exportedExportsName} as __moduleExports`);
|
|
1040
|
+
} else {
|
|
1041
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
1042
|
+
}
|
|
1040
1043
|
if (wrapped) {
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
exportsName,
|
|
1044
|
-
detectWrappedDefault,
|
|
1045
|
-
HELPERS_NAME,
|
|
1046
|
-
defaultIsModuleExports
|
|
1044
|
+
exportDeclarations.push(
|
|
1045
|
+
getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
|
|
1047
1046
|
);
|
|
1048
1047
|
} else {
|
|
1049
1048
|
getExports(
|
|
@@ -1056,17 +1055,19 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
1056
1055
|
topLevelAssignments,
|
|
1057
1056
|
moduleName,
|
|
1058
1057
|
exportsName,
|
|
1058
|
+
exportedExportsName,
|
|
1059
1059
|
defineCompiledEsmExpressions,
|
|
1060
1060
|
HELPERS_NAME,
|
|
1061
|
-
defaultIsModuleExports
|
|
1061
|
+
defaultIsModuleExports,
|
|
1062
|
+
exportMode
|
|
1062
1063
|
);
|
|
1063
1064
|
}
|
|
1064
1065
|
}
|
|
1065
1066
|
if (exports.length) {
|
|
1066
|
-
exportDeclarations.push(`export { ${exports.join(', ')} }
|
|
1067
|
+
exportDeclarations.push(`export { ${exports.join(', ')} }`);
|
|
1067
1068
|
}
|
|
1068
1069
|
|
|
1069
|
-
return `\n\n${exportDeclarations.join('
|
|
1070
|
+
return `\n\n${exportDeclarations.join(';\n')};`;
|
|
1070
1071
|
}
|
|
1071
1072
|
|
|
1072
1073
|
function getExportsWhenUsingRequireWrapper(
|
|
@@ -1081,35 +1082,32 @@ function getExportsWhenUsingRequireWrapper(
|
|
|
1081
1082
|
requireName,
|
|
1082
1083
|
defineCompiledEsmExpressions
|
|
1083
1084
|
) {
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
}
|
|
1094
|
-
// Collect and rewrite named exports
|
|
1095
|
-
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1096
|
-
for (const node of nodes) {
|
|
1097
|
-
magicString.overwrite(node.start, node.left.end, `${exportsName}.${exportName}`);
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
// Collect and rewrite exports.__esModule assignments
|
|
1101
|
-
for (const expression of defineCompiledEsmExpressions) {
|
|
1102
|
-
const moduleExportsExpression =
|
|
1103
|
-
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
1085
|
+
exports.push(`${requireName} as __require`);
|
|
1086
|
+
if (wrapped) return;
|
|
1087
|
+
if (exportMode === 'replace') {
|
|
1088
|
+
rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName);
|
|
1089
|
+
} else {
|
|
1090
|
+
rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, `${moduleName}.exports`);
|
|
1091
|
+
// Collect and rewrite named exports
|
|
1092
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1093
|
+
for (const { node, type } of nodes) {
|
|
1104
1094
|
magicString.overwrite(
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1095
|
+
node.start,
|
|
1096
|
+
node.left.end,
|
|
1097
|
+
`${
|
|
1098
|
+
exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
|
|
1099
|
+
}.${exportName}`
|
|
1108
1100
|
);
|
|
1109
1101
|
}
|
|
1110
1102
|
}
|
|
1103
|
+
replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
|
|
1104
|
+
defineCompiledEsmExpressions,
|
|
1105
|
+
magicString,
|
|
1106
|
+
exportMode,
|
|
1107
|
+
moduleName,
|
|
1108
|
+
exportsName
|
|
1109
|
+
);
|
|
1111
1110
|
}
|
|
1112
|
-
exports.push(`${requireName} as __require`);
|
|
1113
1111
|
}
|
|
1114
1112
|
|
|
1115
1113
|
function getExportsForReplacedModuleExports(
|
|
@@ -1118,34 +1116,30 @@ function getExportsForReplacedModuleExports(
|
|
|
1118
1116
|
exportDeclarations,
|
|
1119
1117
|
moduleExportsAssignments,
|
|
1120
1118
|
firstTopLevelModuleExportsAssignment,
|
|
1121
|
-
exportsName
|
|
1119
|
+
exportsName,
|
|
1120
|
+
defaultIsModuleExports,
|
|
1121
|
+
HELPERS_NAME
|
|
1122
1122
|
) {
|
|
1123
1123
|
for (const { left } of moduleExportsAssignments) {
|
|
1124
1124
|
magicString.overwrite(left.start, left.end, exportsName);
|
|
1125
1125
|
}
|
|
1126
1126
|
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
1127
1127
|
exports.push(`${exportsName} as __moduleExports`);
|
|
1128
|
-
exportDeclarations.push(`export default ${exportsName};`);
|
|
1129
|
-
}
|
|
1130
|
-
|
|
1131
|
-
function getExportsWhenWrapping(
|
|
1132
|
-
exportDeclarations,
|
|
1133
|
-
exportsName,
|
|
1134
|
-
detectWrappedDefault,
|
|
1135
|
-
HELPERS_NAME,
|
|
1136
|
-
defaultIsModuleExports
|
|
1137
|
-
) {
|
|
1138
1128
|
exportDeclarations.push(
|
|
1139
|
-
|
|
1140
|
-
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
1141
|
-
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
1142
|
-
: defaultIsModuleExports === false
|
|
1143
|
-
? `${exportsName}.default`
|
|
1144
|
-
: exportsName
|
|
1145
|
-
};`
|
|
1129
|
+
getDefaultExportDeclaration(exportsName, defaultIsModuleExports, HELPERS_NAME)
|
|
1146
1130
|
);
|
|
1147
1131
|
}
|
|
1148
1132
|
|
|
1133
|
+
function getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME) {
|
|
1134
|
+
return `export default ${
|
|
1135
|
+
defaultIsModuleExports === true
|
|
1136
|
+
? exportedExportsName
|
|
1137
|
+
: defaultIsModuleExports === false
|
|
1138
|
+
? `${exportedExportsName}.default`
|
|
1139
|
+
: `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportedExportsName})`
|
|
1140
|
+
}`;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1149
1143
|
function getExports(
|
|
1150
1144
|
magicString,
|
|
1151
1145
|
exports,
|
|
@@ -1156,9 +1150,11 @@ function getExports(
|
|
|
1156
1150
|
topLevelAssignments,
|
|
1157
1151
|
moduleName,
|
|
1158
1152
|
exportsName,
|
|
1153
|
+
exportedExportsName,
|
|
1159
1154
|
defineCompiledEsmExpressions,
|
|
1160
1155
|
HELPERS_NAME,
|
|
1161
|
-
defaultIsModuleExports
|
|
1156
|
+
defaultIsModuleExports,
|
|
1157
|
+
exportMode
|
|
1162
1158
|
) {
|
|
1163
1159
|
let deconflictedDefaultExportName;
|
|
1164
1160
|
// Collect and rewrite module.exports assignments
|
|
@@ -1170,8 +1166,10 @@ function getExports(
|
|
|
1170
1166
|
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1171
1167
|
const deconflicted = deconflictedExportNames[exportName];
|
|
1172
1168
|
let needsDeclaration = true;
|
|
1173
|
-
for (const node of nodes) {
|
|
1174
|
-
let replacement = `${deconflicted} = ${
|
|
1169
|
+
for (const { node, type } of nodes) {
|
|
1170
|
+
let replacement = `${deconflicted} = ${
|
|
1171
|
+
exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
|
|
1172
|
+
}.${exportName}`;
|
|
1175
1173
|
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
1176
1174
|
replacement = `var ${replacement}`;
|
|
1177
1175
|
needsDeclaration = false;
|
|
@@ -1189,24 +1187,60 @@ function getExports(
|
|
|
1189
1187
|
}
|
|
1190
1188
|
}
|
|
1191
1189
|
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
}
|
|
1190
|
+
const isRestorableCompiledEsm = replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
|
|
1191
|
+
defineCompiledEsmExpressions,
|
|
1192
|
+
magicString,
|
|
1193
|
+
exportMode,
|
|
1194
|
+
moduleName,
|
|
1195
|
+
exportsName
|
|
1196
|
+
);
|
|
1200
1197
|
|
|
1201
|
-
if (
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1198
|
+
if (
|
|
1199
|
+
defaultIsModuleExports === false ||
|
|
1200
|
+
(defaultIsModuleExports === 'auto' &&
|
|
1201
|
+
isRestorableCompiledEsm &&
|
|
1202
|
+
moduleExportsAssignments.length === 0)
|
|
1203
|
+
) {
|
|
1204
|
+
// If there is no deconflictedDefaultExportName, then we use the namespace as
|
|
1205
|
+
// fallback because there can be no "default" property on the namespace
|
|
1206
|
+
exports.push(`${deconflictedDefaultExportName || exportedExportsName} as default`);
|
|
1207
|
+
} else if (
|
|
1208
|
+
defaultIsModuleExports === true ||
|
|
1209
|
+
(!isRestorableCompiledEsm && moduleExportsAssignments.length === 0)
|
|
1210
|
+
) {
|
|
1211
|
+
exports.push(`${exportedExportsName} as default`);
|
|
1205
1212
|
} else {
|
|
1206
1213
|
exportDeclarations.push(
|
|
1207
|
-
|
|
1214
|
+
getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName) {
|
|
1220
|
+
for (const { left } of moduleExportsAssignments) {
|
|
1221
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
function replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
|
|
1226
|
+
defineCompiledEsmExpressions,
|
|
1227
|
+
magicString,
|
|
1228
|
+
exportMode,
|
|
1229
|
+
moduleName,
|
|
1230
|
+
exportsName
|
|
1231
|
+
) {
|
|
1232
|
+
let isRestorableCompiledEsm = false;
|
|
1233
|
+
for (const { node, type } of defineCompiledEsmExpressions) {
|
|
1234
|
+
isRestorableCompiledEsm = true;
|
|
1235
|
+
const moduleExportsExpression =
|
|
1236
|
+
node.type === 'CallExpression' ? node.arguments[0] : node.left.object;
|
|
1237
|
+
magicString.overwrite(
|
|
1238
|
+
moduleExportsExpression.start,
|
|
1239
|
+
moduleExportsExpression.end,
|
|
1240
|
+
exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
|
|
1208
1241
|
);
|
|
1209
1242
|
}
|
|
1243
|
+
return isRestorableCompiledEsm;
|
|
1210
1244
|
}
|
|
1211
1245
|
|
|
1212
1246
|
function isRequireExpression(node, scope) {
|
|
@@ -1297,19 +1331,18 @@ function getRequireHandlers() {
|
|
|
1297
1331
|
commonjsMeta
|
|
1298
1332
|
) {
|
|
1299
1333
|
const imports = [];
|
|
1300
|
-
imports.push(`import * as ${helpersName} from "${HELPERS_ID}"
|
|
1334
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}"`);
|
|
1301
1335
|
if (dynamicRequireName) {
|
|
1302
1336
|
imports.push(
|
|
1303
1337
|
`import { ${
|
|
1304
1338
|
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1305
|
-
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"
|
|
1339
|
+
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"`
|
|
1306
1340
|
);
|
|
1307
1341
|
}
|
|
1308
1342
|
if (exportMode === 'module') {
|
|
1309
1343
|
imports.push(
|
|
1310
|
-
`import { __module as ${moduleName}
|
|
1311
|
-
|
|
1312
|
-
)}`
|
|
1344
|
+
`import { __module as ${moduleName} } from ${JSON.stringify(wrapId(id, MODULE_SUFFIX))}`,
|
|
1345
|
+
`var ${exportsName} = ${moduleName}.exports`
|
|
1313
1346
|
);
|
|
1314
1347
|
} else if (exportMode === 'exports') {
|
|
1315
1348
|
imports.push(
|
|
@@ -1335,7 +1368,7 @@ function getRequireHandlers() {
|
|
|
1335
1368
|
getIgnoreTryCatchRequireStatementMode,
|
|
1336
1369
|
magicString
|
|
1337
1370
|
);
|
|
1338
|
-
return imports.length ? `${imports.join('
|
|
1371
|
+
return imports.length ? `${imports.join(';\n')};\n\n` : '';
|
|
1339
1372
|
}
|
|
1340
1373
|
|
|
1341
1374
|
return {
|
|
@@ -1395,9 +1428,9 @@ function processRequireExpressions(
|
|
|
1395
1428
|
}
|
|
1396
1429
|
if (needsImport) {
|
|
1397
1430
|
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1398
|
-
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}
|
|
1431
|
+
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}`);
|
|
1399
1432
|
} else {
|
|
1400
|
-
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}
|
|
1433
|
+
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}`);
|
|
1401
1434
|
}
|
|
1402
1435
|
}
|
|
1403
1436
|
}
|
|
@@ -1422,6 +1455,14 @@ const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/
|
|
|
1422
1455
|
|
|
1423
1456
|
const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
|
1424
1457
|
|
|
1458
|
+
// There are three different types of CommonJS modules, described by their
|
|
1459
|
+
// "exportMode":
|
|
1460
|
+
// - exports: Only assignments to (module.)exports properties
|
|
1461
|
+
// - replace: A single assignment to module.exports itself
|
|
1462
|
+
// - module: Anything else
|
|
1463
|
+
// Special cases:
|
|
1464
|
+
// - usesRequireWrapper
|
|
1465
|
+
// - isWrapped
|
|
1425
1466
|
async function transformCommonjs(
|
|
1426
1467
|
parse,
|
|
1427
1468
|
code,
|
|
@@ -1458,7 +1499,6 @@ async function transformCommonjs(
|
|
|
1458
1499
|
let programDepth = 0;
|
|
1459
1500
|
let currentTryBlockEnd = null;
|
|
1460
1501
|
let shouldWrap = false;
|
|
1461
|
-
let reexports = false;
|
|
1462
1502
|
|
|
1463
1503
|
const globals = new Set();
|
|
1464
1504
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1532,31 +1572,20 @@ async function transformCommonjs(
|
|
|
1532
1572
|
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1533
1573
|
firstTopLevelModuleExportsAssignment = node;
|
|
1534
1574
|
}
|
|
1535
|
-
|
|
1536
|
-
if (defaultIsModuleExports === false) {
|
|
1537
|
-
shouldWrap = true;
|
|
1538
|
-
} else if (defaultIsModuleExports === 'auto') {
|
|
1539
|
-
if (node.right.type === 'ObjectExpression') {
|
|
1540
|
-
if (hasDefineEsmProperty(node.right)) {
|
|
1541
|
-
shouldWrap = true;
|
|
1542
|
-
}
|
|
1543
|
-
} else if (isRequireExpression(node.right, scope)) {
|
|
1544
|
-
shouldWrap = true;
|
|
1545
|
-
reexports = true;
|
|
1546
|
-
}
|
|
1547
|
-
}
|
|
1548
1575
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1549
1576
|
if (programDepth > 3) {
|
|
1550
1577
|
shouldWrap = true;
|
|
1551
1578
|
} else {
|
|
1552
|
-
|
|
1579
|
+
// The "type" is either "module" or "exports" to discern
|
|
1580
|
+
// assignments to module.exports vs exports if needed
|
|
1581
|
+
topLevelDefineCompiledEsmExpressions.push({ node, type: flattened.name });
|
|
1553
1582
|
}
|
|
1554
1583
|
} else {
|
|
1555
1584
|
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1556
1585
|
nodes: [],
|
|
1557
1586
|
scopes: new Set()
|
|
1558
1587
|
};
|
|
1559
|
-
exportsAssignments.nodes.push(node);
|
|
1588
|
+
exportsAssignments.nodes.push({ node, type: flattened.name });
|
|
1560
1589
|
exportsAssignments.scopes.add(scope);
|
|
1561
1590
|
exportsAccessScopes.add(scope);
|
|
1562
1591
|
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
@@ -1573,11 +1602,12 @@ async function transformCommonjs(
|
|
|
1573
1602
|
}
|
|
1574
1603
|
return;
|
|
1575
1604
|
case 'CallExpression': {
|
|
1576
|
-
|
|
1605
|
+
const defineCompiledEsmType = getDefineCompiledEsmType(node);
|
|
1606
|
+
if (defineCompiledEsmType) {
|
|
1577
1607
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1578
1608
|
// skip special handling for [module.]exports until we know we render this
|
|
1579
1609
|
skippedNodes.add(node.arguments[0]);
|
|
1580
|
-
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1610
|
+
topLevelDefineCompiledEsmExpressions.push({ node, type: defineCompiledEsmType });
|
|
1581
1611
|
} else {
|
|
1582
1612
|
shouldWrap = true;
|
|
1583
1613
|
}
|
|
@@ -1595,6 +1625,7 @@ async function transformCommonjs(
|
|
|
1595
1625
|
uses.require = true;
|
|
1596
1626
|
const requireNode = node.callee.object;
|
|
1597
1627
|
replacedDynamicRequires.push(requireNode);
|
|
1628
|
+
skippedNodes.add(node.callee);
|
|
1598
1629
|
return;
|
|
1599
1630
|
}
|
|
1600
1631
|
|
|
@@ -1837,11 +1868,6 @@ async function transformCommonjs(
|
|
|
1837
1868
|
|
|
1838
1869
|
// We cannot wrap ES/mixed modules
|
|
1839
1870
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1840
|
-
const detectWrappedDefault =
|
|
1841
|
-
shouldWrap &&
|
|
1842
|
-
(reexports ||
|
|
1843
|
-
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1844
|
-
code.indexOf('__esModule') >= 0);
|
|
1845
1871
|
|
|
1846
1872
|
if (
|
|
1847
1873
|
!(
|
|
@@ -1879,6 +1905,9 @@ async function transformCommonjs(
|
|
|
1879
1905
|
? 'exports'
|
|
1880
1906
|
: 'module';
|
|
1881
1907
|
|
|
1908
|
+
const exportedExportsName =
|
|
1909
|
+
exportMode === 'module' ? deconflict([], globals, `${nameBase}Exports`) : exportsName;
|
|
1910
|
+
|
|
1882
1911
|
const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1883
1912
|
magicString,
|
|
1884
1913
|
topLevelDeclarations,
|
|
@@ -1903,6 +1932,7 @@ async function transformCommonjs(
|
|
|
1903
1932
|
magicString,
|
|
1904
1933
|
moduleName,
|
|
1905
1934
|
exportsName,
|
|
1935
|
+
exportedExportsName,
|
|
1906
1936
|
shouldWrap,
|
|
1907
1937
|
moduleExportsAssignments,
|
|
1908
1938
|
firstTopLevelModuleExportsAssignment,
|
|
@@ -1913,7 +1943,6 @@ async function transformCommonjs(
|
|
|
1913
1943
|
code,
|
|
1914
1944
|
helpersName,
|
|
1915
1945
|
exportMode,
|
|
1916
|
-
detectWrappedDefault,
|
|
1917
1946
|
defaultIsModuleExports,
|
|
1918
1947
|
usesRequireWrapper,
|
|
1919
1948
|
requireName
|
|
@@ -1927,15 +1956,16 @@ async function transformCommonjs(
|
|
|
1927
1956
|
magicString.trim().indent('\t', {
|
|
1928
1957
|
exclude: indentExclusionRanges
|
|
1929
1958
|
});
|
|
1959
|
+
const exported = exportMode === 'module' ? `${moduleName}.exports` : exportsName;
|
|
1930
1960
|
magicString.prepend(
|
|
1931
1961
|
`var ${isRequiredName};
|
|
1932
1962
|
|
|
1933
1963
|
function ${requireName} () {
|
|
1934
|
-
\tif (${isRequiredName}) return ${
|
|
1964
|
+
\tif (${isRequiredName}) return ${exported};
|
|
1935
1965
|
\t${isRequiredName} = 1;
|
|
1936
1966
|
`
|
|
1937
1967
|
).append(`
|
|
1938
|
-
\treturn ${
|
|
1968
|
+
\treturn ${exported};
|
|
1939
1969
|
}`);
|
|
1940
1970
|
if (exportMode === 'replace') {
|
|
1941
1971
|
magicString.prepend(`var ${exportsName};\n`);
|
|
@@ -2165,15 +2195,9 @@ function commonjs(options = {}) {
|
|
|
2165
2195
|
}
|
|
2166
2196
|
|
|
2167
2197
|
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
2168
|
-
const
|
|
2169
|
-
const moduleExports = `${module}Exports`;
|
|
2198
|
+
const name = getName(unwrapId(id, MODULE_SUFFIX));
|
|
2170
2199
|
return {
|
|
2171
|
-
code: `var ${
|
|
2172
|
-
var ${module} = {
|
|
2173
|
-
get exports(){ return ${moduleExports}; },
|
|
2174
|
-
set exports(v){ ${moduleExports} = v; },
|
|
2175
|
-
};
|
|
2176
|
-
export {${module} as __module, ${moduleExports} as exports}`,
|
|
2200
|
+
code: `var ${name} = {exports: {}}; export {${name} as __module}`,
|
|
2177
2201
|
meta: { commonjs: { isCommonJS: false } }
|
|
2178
2202
|
};
|
|
2179
2203
|
}
|
package/dist/es/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { walk } from 'estree-walker';
|
|
|
7
7
|
import MagicString from 'magic-string';
|
|
8
8
|
import isReference from 'is-reference';
|
|
9
9
|
|
|
10
|
-
var version = "24.0
|
|
10
|
+
var version = "24.1.0-0";
|
|
11
11
|
var peerDependencies = {
|
|
12
12
|
rollup: "^2.68.0||^3.0.0"
|
|
13
13
|
};
|
|
@@ -411,13 +411,16 @@ async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
|
|
|
411
411
|
} = await loadModule({ id });
|
|
412
412
|
if (!commonjsMeta) {
|
|
413
413
|
return getUnknownRequireProxy(id, requireReturnsDefault);
|
|
414
|
-
}
|
|
414
|
+
}
|
|
415
|
+
if (commonjsMeta.isCommonJS) {
|
|
415
416
|
return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
|
|
416
|
-
}
|
|
417
|
+
}
|
|
418
|
+
if (!requireReturnsDefault) {
|
|
417
419
|
return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
|
|
418
420
|
id
|
|
419
421
|
)}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
|
|
420
|
-
}
|
|
422
|
+
}
|
|
423
|
+
if (
|
|
421
424
|
requireReturnsDefault !== true &&
|
|
422
425
|
(requireReturnsDefault === 'namespace' ||
|
|
423
426
|
!commonjsMeta.hasDefaultExport ||
|
|
@@ -907,11 +910,16 @@ function getKeypath(node) {
|
|
|
907
910
|
|
|
908
911
|
const KEY_COMPILED_ESM = '__esModule';
|
|
909
912
|
|
|
910
|
-
function
|
|
913
|
+
function getDefineCompiledEsmType(node) {
|
|
914
|
+
const definedPropertyWithExports = getDefinePropertyCallName(node, 'exports');
|
|
911
915
|
const definedProperty =
|
|
912
|
-
|
|
916
|
+
definedPropertyWithExports || getDefinePropertyCallName(node, 'module.exports');
|
|
913
917
|
if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
|
|
914
|
-
return isTruthy(definedProperty.value)
|
|
918
|
+
return isTruthy(definedProperty.value)
|
|
919
|
+
? definedPropertyWithExports
|
|
920
|
+
? 'exports'
|
|
921
|
+
: 'module'
|
|
922
|
+
: false;
|
|
915
923
|
}
|
|
916
924
|
return false;
|
|
917
925
|
}
|
|
@@ -955,20 +963,6 @@ function isShorthandProperty(parent) {
|
|
|
955
963
|
return parent && parent.type === 'Property' && parent.shorthand;
|
|
956
964
|
}
|
|
957
965
|
|
|
958
|
-
function hasDefineEsmProperty(node) {
|
|
959
|
-
return node.properties.some((property) => {
|
|
960
|
-
if (
|
|
961
|
-
property.type === 'Property' &&
|
|
962
|
-
property.key.type === 'Identifier' &&
|
|
963
|
-
property.key.name === '__esModule' &&
|
|
964
|
-
isTruthy(property.value)
|
|
965
|
-
) {
|
|
966
|
-
return true;
|
|
967
|
-
}
|
|
968
|
-
return false;
|
|
969
|
-
});
|
|
970
|
-
}
|
|
971
|
-
|
|
972
966
|
function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
|
|
973
967
|
const args = [];
|
|
974
968
|
const passedArgs = [];
|
|
@@ -978,19 +972,22 @@ function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRan
|
|
|
978
972
|
}
|
|
979
973
|
if (uses.exports) {
|
|
980
974
|
args.push('exports');
|
|
981
|
-
passedArgs.push(exportsName);
|
|
975
|
+
passedArgs.push(uses.module ? `${moduleName}.exports` : exportsName);
|
|
982
976
|
}
|
|
983
977
|
magicString
|
|
984
978
|
.trim()
|
|
985
979
|
.indent('\t', { exclude: indentExclusionRanges })
|
|
986
980
|
.prepend(`(function (${args.join(', ')}) {\n`)
|
|
987
|
-
|
|
981
|
+
// For some reason, this line is only indented correctly when using a
|
|
982
|
+
// require-wrapper if we have this leading space
|
|
983
|
+
.append(` \n} (${passedArgs.join(', ')}));`);
|
|
988
984
|
}
|
|
989
985
|
|
|
990
986
|
function rewriteExportsAndGetExportsBlock(
|
|
991
987
|
magicString,
|
|
992
988
|
moduleName,
|
|
993
989
|
exportsName,
|
|
990
|
+
exportedExportsName,
|
|
994
991
|
wrapped,
|
|
995
992
|
moduleExportsAssignments,
|
|
996
993
|
firstTopLevelModuleExportsAssignment,
|
|
@@ -1001,7 +998,6 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
1001
998
|
code,
|
|
1002
999
|
HELPERS_NAME,
|
|
1003
1000
|
exportMode,
|
|
1004
|
-
detectWrappedDefault,
|
|
1005
1001
|
defaultIsModuleExports,
|
|
1006
1002
|
usesRequireWrapper,
|
|
1007
1003
|
requireName
|
|
@@ -1029,17 +1025,20 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
1029
1025
|
exportDeclarations,
|
|
1030
1026
|
moduleExportsAssignments,
|
|
1031
1027
|
firstTopLevelModuleExportsAssignment,
|
|
1032
|
-
exportsName
|
|
1028
|
+
exportsName,
|
|
1029
|
+
defaultIsModuleExports,
|
|
1030
|
+
HELPERS_NAME
|
|
1033
1031
|
);
|
|
1034
1032
|
} else {
|
|
1035
|
-
|
|
1033
|
+
if (exportMode === 'module') {
|
|
1034
|
+
exportDeclarations.push(`var ${exportedExportsName} = ${moduleName}.exports`);
|
|
1035
|
+
exports.push(`${exportedExportsName} as __moduleExports`);
|
|
1036
|
+
} else {
|
|
1037
|
+
exports.push(`${exportsName} as __moduleExports`);
|
|
1038
|
+
}
|
|
1036
1039
|
if (wrapped) {
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
exportsName,
|
|
1040
|
-
detectWrappedDefault,
|
|
1041
|
-
HELPERS_NAME,
|
|
1042
|
-
defaultIsModuleExports
|
|
1040
|
+
exportDeclarations.push(
|
|
1041
|
+
getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
|
|
1043
1042
|
);
|
|
1044
1043
|
} else {
|
|
1045
1044
|
getExports(
|
|
@@ -1052,17 +1051,19 @@ function rewriteExportsAndGetExportsBlock(
|
|
|
1052
1051
|
topLevelAssignments,
|
|
1053
1052
|
moduleName,
|
|
1054
1053
|
exportsName,
|
|
1054
|
+
exportedExportsName,
|
|
1055
1055
|
defineCompiledEsmExpressions,
|
|
1056
1056
|
HELPERS_NAME,
|
|
1057
|
-
defaultIsModuleExports
|
|
1057
|
+
defaultIsModuleExports,
|
|
1058
|
+
exportMode
|
|
1058
1059
|
);
|
|
1059
1060
|
}
|
|
1060
1061
|
}
|
|
1061
1062
|
if (exports.length) {
|
|
1062
|
-
exportDeclarations.push(`export { ${exports.join(', ')} }
|
|
1063
|
+
exportDeclarations.push(`export { ${exports.join(', ')} }`);
|
|
1063
1064
|
}
|
|
1064
1065
|
|
|
1065
|
-
return `\n\n${exportDeclarations.join('
|
|
1066
|
+
return `\n\n${exportDeclarations.join(';\n')};`;
|
|
1066
1067
|
}
|
|
1067
1068
|
|
|
1068
1069
|
function getExportsWhenUsingRequireWrapper(
|
|
@@ -1077,35 +1078,32 @@ function getExportsWhenUsingRequireWrapper(
|
|
|
1077
1078
|
requireName,
|
|
1078
1079
|
defineCompiledEsmExpressions
|
|
1079
1080
|
) {
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
}
|
|
1090
|
-
// Collect and rewrite named exports
|
|
1091
|
-
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1092
|
-
for (const node of nodes) {
|
|
1093
|
-
magicString.overwrite(node.start, node.left.end, `${exportsName}.${exportName}`);
|
|
1094
|
-
}
|
|
1095
|
-
}
|
|
1096
|
-
// Collect and rewrite exports.__esModule assignments
|
|
1097
|
-
for (const expression of defineCompiledEsmExpressions) {
|
|
1098
|
-
const moduleExportsExpression =
|
|
1099
|
-
expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object;
|
|
1081
|
+
exports.push(`${requireName} as __require`);
|
|
1082
|
+
if (wrapped) return;
|
|
1083
|
+
if (exportMode === 'replace') {
|
|
1084
|
+
rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName);
|
|
1085
|
+
} else {
|
|
1086
|
+
rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, `${moduleName}.exports`);
|
|
1087
|
+
// Collect and rewrite named exports
|
|
1088
|
+
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1089
|
+
for (const { node, type } of nodes) {
|
|
1100
1090
|
magicString.overwrite(
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1091
|
+
node.start,
|
|
1092
|
+
node.left.end,
|
|
1093
|
+
`${
|
|
1094
|
+
exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
|
|
1095
|
+
}.${exportName}`
|
|
1104
1096
|
);
|
|
1105
1097
|
}
|
|
1106
1098
|
}
|
|
1099
|
+
replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
|
|
1100
|
+
defineCompiledEsmExpressions,
|
|
1101
|
+
magicString,
|
|
1102
|
+
exportMode,
|
|
1103
|
+
moduleName,
|
|
1104
|
+
exportsName
|
|
1105
|
+
);
|
|
1107
1106
|
}
|
|
1108
|
-
exports.push(`${requireName} as __require`);
|
|
1109
1107
|
}
|
|
1110
1108
|
|
|
1111
1109
|
function getExportsForReplacedModuleExports(
|
|
@@ -1114,34 +1112,30 @@ function getExportsForReplacedModuleExports(
|
|
|
1114
1112
|
exportDeclarations,
|
|
1115
1113
|
moduleExportsAssignments,
|
|
1116
1114
|
firstTopLevelModuleExportsAssignment,
|
|
1117
|
-
exportsName
|
|
1115
|
+
exportsName,
|
|
1116
|
+
defaultIsModuleExports,
|
|
1117
|
+
HELPERS_NAME
|
|
1118
1118
|
) {
|
|
1119
1119
|
for (const { left } of moduleExportsAssignments) {
|
|
1120
1120
|
magicString.overwrite(left.start, left.end, exportsName);
|
|
1121
1121
|
}
|
|
1122
1122
|
magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
|
|
1123
1123
|
exports.push(`${exportsName} as __moduleExports`);
|
|
1124
|
-
exportDeclarations.push(`export default ${exportsName};`);
|
|
1125
|
-
}
|
|
1126
|
-
|
|
1127
|
-
function getExportsWhenWrapping(
|
|
1128
|
-
exportDeclarations,
|
|
1129
|
-
exportsName,
|
|
1130
|
-
detectWrappedDefault,
|
|
1131
|
-
HELPERS_NAME,
|
|
1132
|
-
defaultIsModuleExports
|
|
1133
|
-
) {
|
|
1134
1124
|
exportDeclarations.push(
|
|
1135
|
-
|
|
1136
|
-
detectWrappedDefault && defaultIsModuleExports === 'auto'
|
|
1137
|
-
? `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportsName})`
|
|
1138
|
-
: defaultIsModuleExports === false
|
|
1139
|
-
? `${exportsName}.default`
|
|
1140
|
-
: exportsName
|
|
1141
|
-
};`
|
|
1125
|
+
getDefaultExportDeclaration(exportsName, defaultIsModuleExports, HELPERS_NAME)
|
|
1142
1126
|
);
|
|
1143
1127
|
}
|
|
1144
1128
|
|
|
1129
|
+
function getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME) {
|
|
1130
|
+
return `export default ${
|
|
1131
|
+
defaultIsModuleExports === true
|
|
1132
|
+
? exportedExportsName
|
|
1133
|
+
: defaultIsModuleExports === false
|
|
1134
|
+
? `${exportedExportsName}.default`
|
|
1135
|
+
: `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportedExportsName})`
|
|
1136
|
+
}`;
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1145
1139
|
function getExports(
|
|
1146
1140
|
magicString,
|
|
1147
1141
|
exports,
|
|
@@ -1152,9 +1146,11 @@ function getExports(
|
|
|
1152
1146
|
topLevelAssignments,
|
|
1153
1147
|
moduleName,
|
|
1154
1148
|
exportsName,
|
|
1149
|
+
exportedExportsName,
|
|
1155
1150
|
defineCompiledEsmExpressions,
|
|
1156
1151
|
HELPERS_NAME,
|
|
1157
|
-
defaultIsModuleExports
|
|
1152
|
+
defaultIsModuleExports,
|
|
1153
|
+
exportMode
|
|
1158
1154
|
) {
|
|
1159
1155
|
let deconflictedDefaultExportName;
|
|
1160
1156
|
// Collect and rewrite module.exports assignments
|
|
@@ -1166,8 +1162,10 @@ function getExports(
|
|
|
1166
1162
|
for (const [exportName, { nodes }] of exportsAssignmentsByName) {
|
|
1167
1163
|
const deconflicted = deconflictedExportNames[exportName];
|
|
1168
1164
|
let needsDeclaration = true;
|
|
1169
|
-
for (const node of nodes) {
|
|
1170
|
-
let replacement = `${deconflicted} = ${
|
|
1165
|
+
for (const { node, type } of nodes) {
|
|
1166
|
+
let replacement = `${deconflicted} = ${
|
|
1167
|
+
exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
|
|
1168
|
+
}.${exportName}`;
|
|
1171
1169
|
if (needsDeclaration && topLevelAssignments.has(node)) {
|
|
1172
1170
|
replacement = `var ${replacement}`;
|
|
1173
1171
|
needsDeclaration = false;
|
|
@@ -1185,24 +1183,60 @@ function getExports(
|
|
|
1185
1183
|
}
|
|
1186
1184
|
}
|
|
1187
1185
|
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
}
|
|
1186
|
+
const isRestorableCompiledEsm = replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
|
|
1187
|
+
defineCompiledEsmExpressions,
|
|
1188
|
+
magicString,
|
|
1189
|
+
exportMode,
|
|
1190
|
+
moduleName,
|
|
1191
|
+
exportsName
|
|
1192
|
+
);
|
|
1196
1193
|
|
|
1197
|
-
if (
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1194
|
+
if (
|
|
1195
|
+
defaultIsModuleExports === false ||
|
|
1196
|
+
(defaultIsModuleExports === 'auto' &&
|
|
1197
|
+
isRestorableCompiledEsm &&
|
|
1198
|
+
moduleExportsAssignments.length === 0)
|
|
1199
|
+
) {
|
|
1200
|
+
// If there is no deconflictedDefaultExportName, then we use the namespace as
|
|
1201
|
+
// fallback because there can be no "default" property on the namespace
|
|
1202
|
+
exports.push(`${deconflictedDefaultExportName || exportedExportsName} as default`);
|
|
1203
|
+
} else if (
|
|
1204
|
+
defaultIsModuleExports === true ||
|
|
1205
|
+
(!isRestorableCompiledEsm && moduleExportsAssignments.length === 0)
|
|
1206
|
+
) {
|
|
1207
|
+
exports.push(`${exportedExportsName} as default`);
|
|
1201
1208
|
} else {
|
|
1202
1209
|
exportDeclarations.push(
|
|
1203
|
-
|
|
1210
|
+
getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
function rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName) {
|
|
1216
|
+
for (const { left } of moduleExportsAssignments) {
|
|
1217
|
+
magicString.overwrite(left.start, left.end, exportsName);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
function replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
|
|
1222
|
+
defineCompiledEsmExpressions,
|
|
1223
|
+
magicString,
|
|
1224
|
+
exportMode,
|
|
1225
|
+
moduleName,
|
|
1226
|
+
exportsName
|
|
1227
|
+
) {
|
|
1228
|
+
let isRestorableCompiledEsm = false;
|
|
1229
|
+
for (const { node, type } of defineCompiledEsmExpressions) {
|
|
1230
|
+
isRestorableCompiledEsm = true;
|
|
1231
|
+
const moduleExportsExpression =
|
|
1232
|
+
node.type === 'CallExpression' ? node.arguments[0] : node.left.object;
|
|
1233
|
+
magicString.overwrite(
|
|
1234
|
+
moduleExportsExpression.start,
|
|
1235
|
+
moduleExportsExpression.end,
|
|
1236
|
+
exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
|
|
1204
1237
|
);
|
|
1205
1238
|
}
|
|
1239
|
+
return isRestorableCompiledEsm;
|
|
1206
1240
|
}
|
|
1207
1241
|
|
|
1208
1242
|
function isRequireExpression(node, scope) {
|
|
@@ -1293,19 +1327,18 @@ function getRequireHandlers() {
|
|
|
1293
1327
|
commonjsMeta
|
|
1294
1328
|
) {
|
|
1295
1329
|
const imports = [];
|
|
1296
|
-
imports.push(`import * as ${helpersName} from "${HELPERS_ID}"
|
|
1330
|
+
imports.push(`import * as ${helpersName} from "${HELPERS_ID}"`);
|
|
1297
1331
|
if (dynamicRequireName) {
|
|
1298
1332
|
imports.push(
|
|
1299
1333
|
`import { ${
|
|
1300
1334
|
isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
|
|
1301
|
-
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"
|
|
1335
|
+
} as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"`
|
|
1302
1336
|
);
|
|
1303
1337
|
}
|
|
1304
1338
|
if (exportMode === 'module') {
|
|
1305
1339
|
imports.push(
|
|
1306
|
-
`import { __module as ${moduleName}
|
|
1307
|
-
|
|
1308
|
-
)}`
|
|
1340
|
+
`import { __module as ${moduleName} } from ${JSON.stringify(wrapId(id, MODULE_SUFFIX))}`,
|
|
1341
|
+
`var ${exportsName} = ${moduleName}.exports`
|
|
1309
1342
|
);
|
|
1310
1343
|
} else if (exportMode === 'exports') {
|
|
1311
1344
|
imports.push(
|
|
@@ -1331,7 +1364,7 @@ function getRequireHandlers() {
|
|
|
1331
1364
|
getIgnoreTryCatchRequireStatementMode,
|
|
1332
1365
|
magicString
|
|
1333
1366
|
);
|
|
1334
|
-
return imports.length ? `${imports.join('
|
|
1367
|
+
return imports.length ? `${imports.join(';\n')};\n\n` : '';
|
|
1335
1368
|
}
|
|
1336
1369
|
|
|
1337
1370
|
return {
|
|
@@ -1391,9 +1424,9 @@ function processRequireExpressions(
|
|
|
1391
1424
|
}
|
|
1392
1425
|
if (needsImport) {
|
|
1393
1426
|
if (isCommonJS === IS_WRAPPED_COMMONJS) {
|
|
1394
|
-
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}
|
|
1427
|
+
imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}`);
|
|
1395
1428
|
} else {
|
|
1396
|
-
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}
|
|
1429
|
+
imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}`);
|
|
1397
1430
|
}
|
|
1398
1431
|
}
|
|
1399
1432
|
}
|
|
@@ -1418,6 +1451,14 @@ const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/
|
|
|
1418
1451
|
|
|
1419
1452
|
const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
|
|
1420
1453
|
|
|
1454
|
+
// There are three different types of CommonJS modules, described by their
|
|
1455
|
+
// "exportMode":
|
|
1456
|
+
// - exports: Only assignments to (module.)exports properties
|
|
1457
|
+
// - replace: A single assignment to module.exports itself
|
|
1458
|
+
// - module: Anything else
|
|
1459
|
+
// Special cases:
|
|
1460
|
+
// - usesRequireWrapper
|
|
1461
|
+
// - isWrapped
|
|
1421
1462
|
async function transformCommonjs(
|
|
1422
1463
|
parse,
|
|
1423
1464
|
code,
|
|
@@ -1454,7 +1495,6 @@ async function transformCommonjs(
|
|
|
1454
1495
|
let programDepth = 0;
|
|
1455
1496
|
let currentTryBlockEnd = null;
|
|
1456
1497
|
let shouldWrap = false;
|
|
1457
|
-
let reexports = false;
|
|
1458
1498
|
|
|
1459
1499
|
const globals = new Set();
|
|
1460
1500
|
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
|
|
@@ -1528,31 +1568,20 @@ async function transformCommonjs(
|
|
|
1528
1568
|
} else if (!firstTopLevelModuleExportsAssignment) {
|
|
1529
1569
|
firstTopLevelModuleExportsAssignment = node;
|
|
1530
1570
|
}
|
|
1531
|
-
|
|
1532
|
-
if (defaultIsModuleExports === false) {
|
|
1533
|
-
shouldWrap = true;
|
|
1534
|
-
} else if (defaultIsModuleExports === 'auto') {
|
|
1535
|
-
if (node.right.type === 'ObjectExpression') {
|
|
1536
|
-
if (hasDefineEsmProperty(node.right)) {
|
|
1537
|
-
shouldWrap = true;
|
|
1538
|
-
}
|
|
1539
|
-
} else if (isRequireExpression(node.right, scope)) {
|
|
1540
|
-
shouldWrap = true;
|
|
1541
|
-
reexports = true;
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
1571
|
} else if (exportName === KEY_COMPILED_ESM) {
|
|
1545
1572
|
if (programDepth > 3) {
|
|
1546
1573
|
shouldWrap = true;
|
|
1547
1574
|
} else {
|
|
1548
|
-
|
|
1575
|
+
// The "type" is either "module" or "exports" to discern
|
|
1576
|
+
// assignments to module.exports vs exports if needed
|
|
1577
|
+
topLevelDefineCompiledEsmExpressions.push({ node, type: flattened.name });
|
|
1549
1578
|
}
|
|
1550
1579
|
} else {
|
|
1551
1580
|
const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
|
|
1552
1581
|
nodes: [],
|
|
1553
1582
|
scopes: new Set()
|
|
1554
1583
|
};
|
|
1555
|
-
exportsAssignments.nodes.push(node);
|
|
1584
|
+
exportsAssignments.nodes.push({ node, type: flattened.name });
|
|
1556
1585
|
exportsAssignments.scopes.add(scope);
|
|
1557
1586
|
exportsAccessScopes.add(scope);
|
|
1558
1587
|
exportsAssignmentsByName.set(exportName, exportsAssignments);
|
|
@@ -1569,11 +1598,12 @@ async function transformCommonjs(
|
|
|
1569
1598
|
}
|
|
1570
1599
|
return;
|
|
1571
1600
|
case 'CallExpression': {
|
|
1572
|
-
|
|
1601
|
+
const defineCompiledEsmType = getDefineCompiledEsmType(node);
|
|
1602
|
+
if (defineCompiledEsmType) {
|
|
1573
1603
|
if (programDepth === 3 && parent.type === 'ExpressionStatement') {
|
|
1574
1604
|
// skip special handling for [module.]exports until we know we render this
|
|
1575
1605
|
skippedNodes.add(node.arguments[0]);
|
|
1576
|
-
topLevelDefineCompiledEsmExpressions.push(node);
|
|
1606
|
+
topLevelDefineCompiledEsmExpressions.push({ node, type: defineCompiledEsmType });
|
|
1577
1607
|
} else {
|
|
1578
1608
|
shouldWrap = true;
|
|
1579
1609
|
}
|
|
@@ -1591,6 +1621,7 @@ async function transformCommonjs(
|
|
|
1591
1621
|
uses.require = true;
|
|
1592
1622
|
const requireNode = node.callee.object;
|
|
1593
1623
|
replacedDynamicRequires.push(requireNode);
|
|
1624
|
+
skippedNodes.add(node.callee);
|
|
1594
1625
|
return;
|
|
1595
1626
|
}
|
|
1596
1627
|
|
|
@@ -1833,11 +1864,6 @@ async function transformCommonjs(
|
|
|
1833
1864
|
|
|
1834
1865
|
// We cannot wrap ES/mixed modules
|
|
1835
1866
|
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
|
|
1836
|
-
const detectWrappedDefault =
|
|
1837
|
-
shouldWrap &&
|
|
1838
|
-
(reexports ||
|
|
1839
|
-
topLevelDefineCompiledEsmExpressions.length > 0 ||
|
|
1840
|
-
code.indexOf('__esModule') >= 0);
|
|
1841
1867
|
|
|
1842
1868
|
if (
|
|
1843
1869
|
!(
|
|
@@ -1875,6 +1901,9 @@ async function transformCommonjs(
|
|
|
1875
1901
|
? 'exports'
|
|
1876
1902
|
: 'module';
|
|
1877
1903
|
|
|
1904
|
+
const exportedExportsName =
|
|
1905
|
+
exportMode === 'module' ? deconflict([], globals, `${nameBase}Exports`) : exportsName;
|
|
1906
|
+
|
|
1878
1907
|
const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
|
|
1879
1908
|
magicString,
|
|
1880
1909
|
topLevelDeclarations,
|
|
@@ -1899,6 +1928,7 @@ async function transformCommonjs(
|
|
|
1899
1928
|
magicString,
|
|
1900
1929
|
moduleName,
|
|
1901
1930
|
exportsName,
|
|
1931
|
+
exportedExportsName,
|
|
1902
1932
|
shouldWrap,
|
|
1903
1933
|
moduleExportsAssignments,
|
|
1904
1934
|
firstTopLevelModuleExportsAssignment,
|
|
@@ -1909,7 +1939,6 @@ async function transformCommonjs(
|
|
|
1909
1939
|
code,
|
|
1910
1940
|
helpersName,
|
|
1911
1941
|
exportMode,
|
|
1912
|
-
detectWrappedDefault,
|
|
1913
1942
|
defaultIsModuleExports,
|
|
1914
1943
|
usesRequireWrapper,
|
|
1915
1944
|
requireName
|
|
@@ -1923,15 +1952,16 @@ async function transformCommonjs(
|
|
|
1923
1952
|
magicString.trim().indent('\t', {
|
|
1924
1953
|
exclude: indentExclusionRanges
|
|
1925
1954
|
});
|
|
1955
|
+
const exported = exportMode === 'module' ? `${moduleName}.exports` : exportsName;
|
|
1926
1956
|
magicString.prepend(
|
|
1927
1957
|
`var ${isRequiredName};
|
|
1928
1958
|
|
|
1929
1959
|
function ${requireName} () {
|
|
1930
|
-
\tif (${isRequiredName}) return ${
|
|
1960
|
+
\tif (${isRequiredName}) return ${exported};
|
|
1931
1961
|
\t${isRequiredName} = 1;
|
|
1932
1962
|
`
|
|
1933
1963
|
).append(`
|
|
1934
|
-
\treturn ${
|
|
1964
|
+
\treturn ${exported};
|
|
1935
1965
|
}`);
|
|
1936
1966
|
if (exportMode === 'replace') {
|
|
1937
1967
|
magicString.prepend(`var ${exportsName};\n`);
|
|
@@ -2161,15 +2191,9 @@ function commonjs(options = {}) {
|
|
|
2161
2191
|
}
|
|
2162
2192
|
|
|
2163
2193
|
if (isWrappedId(id, MODULE_SUFFIX)) {
|
|
2164
|
-
const
|
|
2165
|
-
const moduleExports = `${module}Exports`;
|
|
2194
|
+
const name = getName(unwrapId(id, MODULE_SUFFIX));
|
|
2166
2195
|
return {
|
|
2167
|
-
code: `var ${
|
|
2168
|
-
var ${module} = {
|
|
2169
|
-
get exports(){ return ${moduleExports}; },
|
|
2170
|
-
set exports(v){ ${moduleExports} = v; },
|
|
2171
|
-
};
|
|
2172
|
-
export {${module} as __module, ${moduleExports} as exports}`,
|
|
2196
|
+
code: `var ${name} = {exports: {}}; export {${name} as __module}`,
|
|
2173
2197
|
meta: { commonjs: { isCommonJS: false } }
|
|
2174
2198
|
};
|
|
2175
2199
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rollup/plugin-commonjs",
|
|
3
|
-
"version": "24.0
|
|
3
|
+
"version": "24.1.0-0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
75
75
|
"locate-character": "^2.0.5",
|
|
76
76
|
"require-relative": "^0.8.7",
|
|
77
|
-
"rollup": "3.
|
|
77
|
+
"rollup": "^3.19.0",
|
|
78
78
|
"shx": "^0.3.4",
|
|
79
79
|
"source-map": "^0.7.4",
|
|
80
80
|
"source-map-support": "^0.5.21",
|