rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.64k stars 591 forks source link

default import from a commonjs package with { __esModule: true } fails in runtime #1616

Open vskh opened 1 year ago

vskh commented 1 year ago

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.

simonsmith commented 10 months ago

@vskh I've hit this same issue. Did you find a way to work round it?

simonsmith commented 10 months ago

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