Skip to content

Commit 40c1351

Browse files
authored
fix: better CommonJS transformation (#1660)
1 parent 13b5da3 commit 40c1351

File tree

1 file changed

+6
-5
lines changed
  • packages/repl/src/lib/workers/bundler/plugins

1 file changed

+6
-5
lines changed

packages/repl/src/lib/workers/bundler/plugins/commonjs.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const plugin: Plugin = {
2727
});
2828

2929
const requires: string[] = [];
30-
const exports: string[] = [];
30+
const exports: Set<string> = new Set();
3131

3232
walk(ast as Node, null, {
3333
CallExpression: (node, context) => {
@@ -52,9 +52,7 @@ const plugin: Plugin = {
5252

5353
// Default is a special case (and would result in invalid syntax) and kinda fucked up: https://github.com/evanw/esbuild/issues/1719#issuecomment-953470495
5454
if (node.left.property.name !== 'default') {
55-
exports.push(
56-
`export const ${node.left.property.name} = module.exports.${node.left.property.name};`
57-
);
55+
exports.add(node.left.property.name);
5856
}
5957
}
6058
});
@@ -76,7 +74,10 @@ const plugin: Plugin = {
7674
`const exports = {}; const module = { exports };`,
7775
code,
7876
`export default module.exports;`,
79-
exports.join('\n')
77+
...Array.from(exports).map((name) => {
78+
const alias = `$$module_exports_${name}`;
79+
return `const ${alias} = module.exports.${name}; export { ${alias} as ${name} };`;
80+
})
8081
].join('\n\n');
8182

8283
return {

0 commit comments

Comments
 (0)