Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using React Material-UI and Emotion in a Mono-Repo (PNPM) Emotion is included two times #33

Closed
7 tasks done
swernerx opened this issue Aug 16, 2022 · 5 comments
Closed
7 tasks done
Labels
bug Something isn't working has workaround

Comments

@swernerx
Copy link

swernerx commented Aug 16, 2022

Describe the bug

In my set-up two variants of the same @emotion/react (identical version, different module format) library are sent over to the browser.

I created a small showcase for the issue here: https://github.com/swernerx/emotion-react-vite-issue

Seems to be related to the situation that Emotion is being used as a direct and indirect dependency inside a (pnpm-powered) mono-repository set-up. In the case of @emotion/react either both the CJS and the ESM version or two ESM bundles are sent to the browser. The library contains a check to warn about a duplicate import situation, which made us aware of the problem. There might be other libraries/use-cases where the same issue applies.

Reproduction

https://github.com/swernerx/emotion-react-vite-issue

System Info

System:
    OS: macOS 12.5
    CPU: (20) arm64 Apple M1 Ultra
    Memory: 2.30 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.16.0 - /opt/homebrew/opt/node@16/bin/node
    npm: 8.11.0 - /opt/homebrew/opt/node@16/bin/npm
  Browsers:
    Brave Browser: 103.1.40.105
    Chrome: 104.0.5112.79
    Firefox: 98.0.2
    Safari: 15.6

Used Package Manager

pnpm

Logs

emotion-react.browser.esm.js:398 You are loading @emotion/react when it is already loaded. Running multiple instances may cause problems. This can happen if multiple versions are used, or if multiple builds of the same version are used.

Validations

@swernerx
Copy link
Author

swernerx commented Aug 16, 2022

When adding a debug console.log to the bundles of @emotion/react it seems that the ESM bundle is loaded two times:

@emotion_react.js?v=f88aff20:340 Emotion Browser-ESM
@mui_material.js?v=e7f1b82a:1653 Emotion Browser-ESM
@mui_material.js?v=e7f1b82a:1655 You are loading @emotion/react when it is already loaded. Running multiple instances may cause problems. This can happen if multiple versions are used, or if multiple builds of the same version are used.
  1. standalone as it is used by the actual application
  2. pre-bundled as it is used by a dependency of @mui/material

@swernerx
Copy link
Author

The issue seems to be related to dev-mode only. In the production build + vite preview everything works fine.

@hengwangsay
Copy link

same issue for me

@patak-dev patak-dev transferred this issue from vitejs/vite Dec 3, 2022
@ArnaudBarre ArnaudBarre added bug Something isn't working and removed pending triage labels Jan 22, 2023
@ArnaudBarre
Copy link
Member

This bug still happen with the latest version of Vite. @patak-dev I think this bug should go back to Vite core repo, it seems to be an issue with how pre-bundling works.

Another workaround I found is to make the main app explicitly depend on @mui/material and add this to the config:
include: ['@mui/material'],

@ArnaudBarre
Copy link
Member

Hi! Bumping all dependencies to latest and restating the lockfile from scratch solve the issue. The bug is really sensitive to the fact that you can have a mismatch of emotion versions between the uikit and your app. Maybe having only one version in the workspace could make this more stable.

If you still find yourself having issues with this, I suggest you open create a new issue into the core repo

@github-actions github-actions bot locked and limited conversation to collaborators Jun 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has workaround
Projects
None yet
Development

No branches or pull requests

4 participants