Skip to content

Commit

Permalink
fix(commonjs): support CJS modules re-exporting transpiled ESM modules (
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts authored and lukastaegert committed Apr 24, 2022
1 parent e2f26a4 commit 5b3bd05
Show file tree
Hide file tree
Showing 29 changed files with 462 additions and 132 deletions.
8 changes: 6 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Expand Up @@ -71,6 +71,7 @@ export default async function transformCommonjs(
let programDepth = 0;
let currentTryBlockEnd = null;
let shouldWrap = false;
let reexports = false;

const globals = new Set();
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
Expand Down Expand Up @@ -151,8 +152,9 @@ export default async function transformCommonjs(
if (hasDefineEsmProperty(node.right)) {
shouldWrap = true;
}
} else if (defaultIsModuleExports === false) {
} else if (isRequireExpression(node.right, scope)) {
shouldWrap = true;
reexports = true;
}
}
} else if (exportName === KEY_COMPILED_ESM) {
Expand Down Expand Up @@ -444,7 +446,9 @@ export default async function transformCommonjs(
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
const detectWrappedDefault =
shouldWrap &&
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
(reexports ||
topLevelDefineCompiledEsmExpressions.length > 0 ||
code.indexOf('__esModule') >= 0);

if (
!(
Expand Down
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
@@ -0,0 +1,3 @@
import dep from './proxy';

t.is(dep, 'default');
@@ -0,0 +1 @@
module.exports = require('./dep');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
@@ -0,0 +1,3 @@
import * as entry from './proxy';

t.deepEqual(entry, { default: 'default' });
@@ -0,0 +1 @@
module.exports = require('./entry');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,3 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
exports.named = 'named';
@@ -0,0 +1,3 @@
import * as entry from './proxy';

t.deepEqual(entry, { default: 'default', named: 'named' });
@@ -0,0 +1 @@
module.exports = require('./entry');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.named = 'named';
@@ -0,0 +1,8 @@
import * as entry from './proxy';

t.deepEqual(entry, {
default: {
named: 'named',
},
named: 'named'
});
@@ -0,0 +1 @@
module.exports = require('./entry');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,3 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.named = 'named';
exports.default = 'default';
@@ -0,0 +1,4 @@
import dep, { named } from './proxy';

t.is(dep, 'default');
t.is(named, 'named');
@@ -0,0 +1 @@
module.exports = require('./dep');
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.named = 'named';
@@ -0,0 +1,3 @@
import { named } from './proxy';

t.is(named, 'named');
@@ -0,0 +1 @@
module.exports = require('./dep');

0 comments on commit 5b3bd05

Please sign in to comment.