Skip to content

Commit

Permalink
refactor(commonjs): Inline __esModule handling into export generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 24, 2020
1 parent f6fb5be commit e8948a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
14 changes: 13 additions & 1 deletion packages/commonjs/src/generate-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function rewriteExportsAndGetExportsBlock(
topLevelExportsAssignmentsByName,
defineCompiledEsmExpressions,
deconflict,
isRestorableCompiledEsm,
code,
uses,
HELPERS_NAME,
Expand Down Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -374,7 +359,6 @@ export default function transformCommonjs(
topLevelExportsAssignmentsByName,
defineCompiledEsmExpressions,
(name) => deconflict(scope, globals, name),
isRestorableCompiledEsm,
code,
uses,
HELPERS_NAME,
Expand Down

0 comments on commit e8948a0

Please sign in to comment.