plugins
plugins copied to clipboard
default import from a commonjs package with { __esModule: true } fails in runtime
- Rollup Plugin Name: commonjs
- Rollup Plugin Version: 25.0.7
- Rollup Version: 4.1.4
- Operating System (or Browser): Linux 5.15.123.1-microsoft-standard-WSL2 x86_64
- Node Version: v18.16.0
- Link to reproduction (⚠️ read below): https://stackblitz.com/edit/rollup-repro-wx5pcy
Expected Behavior
Following import:
import userEvent from "@testing-library/user-event";
to be transpiled into:
var userEvent = require('@testing-library/user-event').default;
because '@testing-library/user-event' defines exports["default"] and Object.defineProperty(exports, '__esModule', { value: true });.
From 'plugin-commonjs' docs:
defaultIsModuleExports Default: "auto"
"auto": The value of the default export is exports.default if the CommonJS file has an exports.__esModule === true property; otherwise it's module.exports. This makes it possible to import the default export of ES modules compiled to CommonJS as if they were not compiled.
Actual Behavior
Following import:
import userEvent from "@testing-library/user-event";
to be transpiled into:
var userEvent = require('@testing-library/user-event');
Additional Information
Existing behavior causes compiled code to fail at runtime because it tries to access properties of default export, whereas require result is an object with only 'default' key.
@vskh I've hit this same issue. Did you find a way to work round it?
Just to follow up, I found that the interop option solved this:
{
dir: 'dist/cjs',
format: 'cjs',
sourcemap: true,
+ interop: 'auto',
},
https://rollupjs.org/configuration-options/#output-interop