diff --git a/packages/commonjs/src/generate-exports.js b/packages/commonjs/src/generate-exports.js index 992b6b1bc..52bcde142 100644 --- a/packages/commonjs/src/generate-exports.js +++ b/packages/commonjs/src/generate-exports.js @@ -18,7 +18,6 @@ export function rewriteExportsAndGetExportsBlock( topLevelExportsAssignmentsByName, defineCompiledEsmExpressions, deconflict, - isRestorableCompiledEsm, code, uses, HELPERS_NAME, @@ -68,6 +67,19 @@ export function rewriteExportsAndGetExportsBlock( ); } + // Collect and rewrite exports.__esModule assignments + let isRestorableCompiledEsm = false; + for (const expression of defineCompiledEsmExpressions) { + isRestorableCompiledEsm = true; + const moduleExportsExpression = + expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object; + magicString.overwrite( + moduleExportsExpression.start, + moduleExportsExpression.end, + `${moduleName}.exports` + ); + } + if (isRestorableCompiledEsm) { exportDeclarations.push( deconflictedDefaultExportName diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index da73403fb..069cf3a34 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -324,21 +324,6 @@ export default function transformCommonjs( // TODO Lukas we do not need this for ES modules const moduleName = deconflict(scope, globals, getName(id)); - // TODO Lukas inline this into exports block - let isRestorableCompiledEsm = false; - if (!shouldWrap) { - for (const expression of defineCompiledEsmExpressions) { - isRestorableCompiledEsm = true; - const moduleExportsExpression = - expression.type === 'CallExpression' ? expression.arguments[0] : expression.left.object; - magicString.overwrite( - moduleExportsExpression.start, - moduleExportsExpression.end, - `${moduleName}.exports` - ); - } - } - // We cannot wrap ES/mixed modules shouldWrap = shouldWrap && !disableWrap && !isEsModule; uses.commonjsHelpers = uses.commonjsHelpers || shouldWrap; @@ -374,7 +359,6 @@ export default function transformCommonjs( topLevelExportsAssignmentsByName, defineCompiledEsmExpressions, (name) => deconflict(scope, globals, name), - isRestorableCompiledEsm, code, uses, HELPERS_NAME,