plugins icon indicating copy to clipboard operation
plugins copied to clipboard

[@rollup/plugin-commonjs] commonjs does not recognize named cjs exports

Open GauBen opened this issue 1 year ago • 1 comments

  • Rollup Plugin Name: @rollup/plugin-commonjs
  • Rollup Plugin Version: latest (^25.0.7)
  • Rollup Version: latest (^4.10.0)
  • Operating System (or Browser):
  • Node Version: 20
  • Link to reproduction: https://stackblitz.com/edit/rollup-repro-axmnsg?file=src%2Fmain.cjs

I'm trying to transpile CJS to ESM until https://github.com/prisma/prisma/issues/5030 is addressed. I'm having a hard time doing so as I can't quite get the pieces together.

Expected Behavior

The commonjs plugin properly detects all named exports

Actual Behavior

No named exports are found

Additional Information

There is a node-backed package that does a pretty solid named exports analysis, which works for my specific use case. I've added a npm run cjs-lex command in the repro to show how it works

GauBen avatar Feb 12 '24 15:02 GauBen

I think my use case is related. I noticed that Object.defineProperty(exports, ...) syntax is also detected by cjs-module-lexer.

My input file input.js:

import {foo} from "foo"
console.log(foo)

The foo package is a pure CJS package and has the following content:

// foo/index.js
Object.defineProperty(exports, "foo", { enumerable: true, get: function () { return 123; } });

A bundle produced by Rollup outputs undefined when executed, while it should output value of foo - 123.

bhovhannes avatar Mar 25 '24 19:03 bhovhannes