Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ export default async function transformCommonjs(
// We cannot wrap ES/mixed modules
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));

// Top-level `Object.defineProperty([module.]exports, '__esModule', ...)` calls
// were skipped above, so they did not mark `exports` or `module` as used. A
// wrapped module keeps them as they are, so the wrapper must provide that name.
if (shouldWrap) {
for (const { type } of topLevelDefineCompiledEsmExpressions) {
uses[type] = true;
Comment on lines +480 to +481

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Local names break builds

This loop also marks locally declared names as wrapper arguments. For example, const exports = {}; Object.defineProperty(exports, '__esModule', { value: true }); module.exports = exports; previously got a wrapper with only a module parameter. The new flag adds an exports parameter alongside const exports, so the transformed file fails to parse.

The call recorder does not check whether the target is locally declared. Exclude those targets before marking uses[type], and add a regression test.

}
}

if (
!(
shouldWrap ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
description:
'passes exports to the wrapper when a wrapped module only uses it to define __esModule'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });
module.exports.type = typeof module.exports;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const dep = require('./dep.js');

t.is(dep.__esModule, true);
t.is(dep.type, 'object');
45 changes: 45 additions & 0 deletions packages/commonjs/test/snapshots/function.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10974,3 +10974,48 @@ module.exports = main;
",
}
`;

exports[`wrapped-define-compiled-esm 1`] = `
{
"main.js": "'use strict';

function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}

var main$1 = {};

var dep = {exports: {}};

var hasRequiredDep;

function requireDep () {
if (hasRequiredDep) return dep.exports;
hasRequiredDep = 1;
(function (module, exports) {

Object.defineProperty(exports, '__esModule', { value: true });
module.exports.type = 'object';
} (dep, dep.exports));
return dep.exports;
}

var hasRequiredMain;

function requireMain () {
if (hasRequiredMain) return main$1;
hasRequiredMain = 1;
const dep = requireDep();

t.is(dep.__esModule, true);
t.is(dep.type, 'object');
return main$1;
}

var mainExports = requireMain();
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);

module.exports = main;
",
}
`;