Skip to content

Commit 4a02a94

Browse files
fwoutslukastaegert
authored andcommitted
fix(commonjs): support CJS modules re-exporting transpiled ESM modules (#1165)
1 parent 4c34dd5 commit 4a02a94

File tree

29 files changed

+462
-132
lines changed

29 files changed

+462
-132
lines changed

packages/commonjs/src/transform-commonjs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default async function transformCommonjs(
7171
let programDepth = 0;
7272
let currentTryBlockEnd = null;
7373
let shouldWrap = false;
74+
let reexports = false;
7475

7576
const globals = new Set();
7677
// A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
@@ -151,8 +152,9 @@ export default async function transformCommonjs(
151152
if (hasDefineEsmProperty(node.right)) {
152153
shouldWrap = true;
153154
}
154-
} else if (defaultIsModuleExports === false) {
155+
} else if (isRequireExpression(node.right, scope)) {
155156
shouldWrap = true;
157+
reexports = true;
156158
}
157159
}
158160
} else if (exportName === KEY_COMPILED_ESM) {
@@ -444,7 +446,9 @@ export default async function transformCommonjs(
444446
shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
445447
const detectWrappedDefault =
446448
shouldWrap &&
447-
(topLevelDefineCompiledEsmExpressions.length > 0 || code.indexOf('__esModule') >= 0);
449+
(reexports ||
450+
topLevelDefineCompiledEsmExpressions.length > 0 ||
451+
code.indexOf('__esModule') >= 0);
448452

449453
if (
450454
!(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
3+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
exports.default = 'default';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import dep from './proxy';
2+
3+
t.is(dep, 'default');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dep');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
3+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
exports.default = 'default';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as entry from './proxy';
2+
3+
t.deepEqual(entry, { default: 'default' });
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./entry');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
exports.default = 'default';
3+
exports.named = 'named';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as entry from './proxy';
2+
3+
t.deepEqual(entry, { default: 'default', named: 'named' });
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./entry');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
3+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
exports.named = 'named';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as entry from './proxy';
2+
3+
t.deepEqual(entry, {
4+
default: {
5+
named: 'named',
6+
},
7+
named: 'named'
8+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./entry');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
exports.named = 'named';
3+
exports.default = 'default';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import dep, { named } from './proxy';
2+
3+
t.is(dep, 'default');
4+
t.is(named, 'named');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dep');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
3+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
exports.named = 'named';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { named } from './proxy';
2+
3+
t.is(named, 'named');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dep');

0 commit comments

Comments
 (0)