question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Information: solving jss classnames production build conflict with external MUI dependent component

See original GitHub issue

So I spend the all afternoon reading the related issues and debugging my code so I am posting this here in case it helps someone.
I can make a PR for the doc if you want but don’t know where it should go as it is not directly a MUI issue.

Use case:

  • I have an external component “mui-tree-list” which use mui v1.
  • The lib use rollup to package the component.
  • in the rollup config I put external: [ 'react', '@material-ui/core', '@material-ui/icons', 'react-dom' ], WRONG ! ;p

If you do this, you’ll end up having jss classname conflicts in the production build of you final app.

why ?

cause @material-ui/core does not avoid @material-ui/core/List for instance to be included in your dist package.

and @material-ui/core/List will include withStyles which will cause jss class naming conflict in your final package !

So the solution is, you should put this in your rollup config:
external: id => /react|react-dom|material-ui\/.*/.test(id),

this way all material-ui components will be excluded from your build… lost some hair with this one :p .

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
marton-levaycommented, Jul 12, 2018

Ohai

If anybody is still looking for a solution with webpack (for us it was an ejected create-react-app with material-ui elements, shared as a library, and then used in a similar cra app with material elements): You have to externalize your whole material-ui dependency using regex in webpack config, otherwise various material scripts - withStyles, withTheme, etc… - will load twice (or more if you have more libs) which will cause the screen to fall apart.

https://webpack.js.org/guides/author-libraries/#external-limitations So in webpack.config.prod.js add something similar:

externals: [
    'react',
    'react-dom',
    //this will externalize everything which start with '@material-ui'
    /^@material-ui\/.+$/,
    'prop-types',
    'react-router-dom'
  ],

Of course this means you want to indicate in your /build/package json that these are peerDependecies. You have to have them in the app where you wish to use this lib.

1reaction
oliviertassinaricommented, Jun 2, 2018

You should have a warning in the console next time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

solving jss classnames production build conflict with external ...
Use case: I have an external component "mui-tree-list" which use mui v1. The lib use rollup to package the component.
Read more >
JSS integration with React
Use namespaced themes so that a set of UI components gets no conflicts with another set of UI components from a different library...
Read more >
React + Material-UI style classes from different components ...
The issue is related to using two different versions of a class name generator. Many ways to do this; in my case I...
Read more >
Style library interoperability - Material UI - MUI
This guide aims to document the most popular alternatives, but you should find that the principles applied here can be adapted to other...
Read more >
Frequently Asked Questions - Material UI - MUI
The #1 reason this happens is likely due to class name conflicts once your code is in a production bundle. For MUI to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found