镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

transformMixedEsModules shouldn't replace typeof exports #1014

Closed

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)

Activity

  1. deleted a comment from fi3ework on Nov 17, 2021
  2. yunsii commented on Dec 8, 2021

    @yunsii

    Any progress? lodash-es cause the same error, when I use Vite with transformMixedEsModules=true.

  3. added a commit that references this issue on Dec 10, 2021
    e366573
  4. lukastaegert commented on Dec 10, 2021

    @lukastaegert
    Member

    I added a fix to #1038

  5. added 2 commits that reference this issue on Dec 11, 2021
    a60d0ac
    d121e16
  6. yunsii commented on Dec 17, 2021

    @yunsii

    I added a fix to #1038

    Hello, when will it be released?

  7. shellscape commented on Dec 17, 2021

    @shellscape
    Collaborator

    @theprimone we don't have a release schedule. please exercise patience.

  8. yunsii commented on Dec 17, 2021

    @yunsii

    @theprimone we don't have a release schedule. please exercise patience.

    Well,how can I make a workaround?It causes error after bundled, though I try to configure exclude.

  9. yunsii commented on Dec 19, 2021

    @yunsii

    It looks like version @rollup/plugin-commonjs@22.0.0-4 fixes the problem for now, detail: https://unpkg.com/browse/@rollup/plugin-commonjs@22.0.0-4/dist/index.es.js

  10. added a commit that references this issue on Jan 14, 2022
    d0e7390
  11. added a commit that references this issue on Jan 27, 2022
    181a556
  12. added a commit that references this issue on Feb 3, 2022
    1c6837d
  13. webhao commented on Feb 10, 2022

    @webhao

    @yunsii I didn't solve it.update@rollup/plugin-commonjs@22.0.0-9,use commonjsOptions: { transformMixedEsModules: true, strictRequires: true, }

  14. yunsii commented on Feb 10, 2022

    @yunsii

    @yunsii I didn't solve it.update@rollup/plugin-commonjs@22.0.0-9,use commonjsOptions: { transformMixedEsModules: true, strictRequires: true, }

    The same error: exports is not defined? I replace require with import ... from ... and remove commonjs plugin finnally 😂

  15. added a commit that references this issue on Feb 13, 2022
    fe66824
  16. added a commit that references this issue on Feb 22, 2022
    beac91e
  17. mccxiv commented on Feb 24, 2022

    @mccxiv

    Workaround while waiting for the fix to release:

    Add window.exports = undefined before anything else executes.
    For me, I had to do it in a separate script tag above the main file:

    <script>
      // Workaround for https://github.com/rollup/plugins/issues/1014
      window.exports = undefined
    </script>
    
  18. added a commit that references this issue on Feb 24, 2022
    92bab9e
  19. superchangme commented on Mar 30, 2022

    @superchangme

    help me vite 2.6.8 still has this problem Uncaught ReferenceError: exports is not defined

  20. added a commit that references this issue on Apr 3, 2022
    46776c6
  21. added 5 commits that reference this issue on Apr 18, 2022
    8bff5ec
    1e66132
    756a83b
    6ea05f9
    a66d253
  22. added a commit that references this issue on Apr 24, 2022
    df01795
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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