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 4c34dd5 commit 4a02a94
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'creates the correct exports from CJS module re-exporting a transpiled ES module',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep from './proxy';

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

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

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

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

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

t.is(named, 'named');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dep');
Loading

0 comments on commit 4a02a94

Please sign in to comment.