Skip to content

transformMixedEsModules shouldn't replace typeof exports #1014

Closed
@gluck

Description

@gluck
  • Rollup Plugin Name: @rollup/plugin-commonjs
  • Rollup Plugin Version: 21.0.0

Expected Behavior / Situation

If an ESM file is transformed with transformMixedEsModules=true (for require support), it shouldn't replace typeof exports by 'object' and leave it as-is (or replace by 'undefined' ?)

Actual Behavior / Situation

Right now the bundle fails with exports is not defined because of code in ESM such as lodash-es:
https://github.com/lodash/lodash/blob/es/isBuffer.js#L5

var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;
// becomes:
var freeExports = exports && !exports.nodeType && exports;

(lodash-es itself doesn't require transformMixedEsModules, but other parts of the bundle do)

Modification Proposal

If esModule=true do not transform typeof module/exports/module.exports into 'object', as they'll likely open invalid code paths in an ESM.

(I can PR with tests if agreed)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions