plugins icon indicating copy to clipboard operation
plugins copied to clipboard

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

Open vskh opened this issue 2 years ago • 2 comments

  • 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 avatar Oct 20 '23 21:10 vskh

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

simonsmith avatar Jan 09 '24 12:01 simonsmith

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

simonsmith avatar Jan 09 '24 16:01 simonsmith