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

[RFR] Fix import errors when tree-shaking @material-ui #4983

Merged
merged 1 commit into from
Jul 7, 2020

Conversation

rassie
Copy link
Contributor

@rassie rassie commented Jun 26, 2020

Material-UI documents some tree-shaking performance optimizations at https://material-ui.com/guides/minimizing-bundle-size/. Any downstream project using both Material-UI and react-admin implementing these recommendations will encounter errors like

Cannot find module: '@material-ui/core/esm/createMuiTheme'. Make sure this package is installed.

This is apparently due to the way Material-UI re-exports styling exports via @material-ui/core. The official recommendation is to import via @material-ui/core/styles, which react-admin is not doing throughout. This PR is making sure makeStyles and createMuiTheme are imported correctly. This is obviously incomplete: any re-exported import from @material-ui/core/styles would probably be a potential problem, however, just including makeStyles and createMuiTheme has been sufficient to make a minimal example project with the tree-shaking optimization include compilable.

Also included: ESLint configuration forbidding these two imports.

Copy link
Collaborator

@djhi djhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure updating the test imports will bring any benefit

@@ -2,7 +2,8 @@ import { render, cleanup } from '@testing-library/react';
import * as React from 'react';
import expect from 'expect';
import { TestContext } from 'ra-core';
import { createMuiTheme, ThemeProvider } from '@material-ui/core';
import { ThemeProvider } from '@material-ui/core';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be imported from @material-ui/core/styles as well? Same for all other ThemeProvider imports

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting case: ThemeProvider normally lives in @material-ui/styles, which means it's re-exported from @material-ui/core and is in theory subject to the same problem we are talking about here. However, I've failed to confirm this so far, still looking into the source code. It doesn't seem to hurt compilation anyway, at least not for a minimal project, but I'll keep digging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, found it. @material-ui/core/styles exports a couple of exports from @material-ui/styles, while @material-ui/core re-exports everything from @material-ui/core/styles. Now to find out why this works and e.g. makeStyles doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I get it fully, but it seems that ThemeProvider is resolved correctly with the babel rules because at the end of the import chain it's not a part of @material-ui/core but of @material-ui/styles, which is not getting transformed by babel. In theory, everything under core/styles should probably be part of the rule:

colorManipulator.js
createBreakpoints.js
createMixins.js
createMuiStrictModeTheme.js
createMuiTheme.js
createPalette.js
createSpacing.js
createStyles.js
createTypography.js
cssUtils.js
defaultTheme.js
makeStyles.js
MuiThemeProvider.js
responsiveFontSizes.js
shadows.js
shape.js
styled.js
transitions.js
useTheme.js
withStyles.js
withTheme.js
zIndex.js

But the question is probably how to keep up with changes. I think createMuiTheme and makeStyles could be enough for starters and we'll have to see what happens with MUI v5, this problem might disappear in the process.

@rassie
Copy link
Contributor Author

rassie commented Jun 29, 2020

I'm not sure updating the test imports will bring any benefit

Technically, you are right, however I think making an exception in the ESLint rule will probably be more complicated than using the right imports in the tests.

@djhi
Copy link
Collaborator

djhi commented Jul 1, 2020

Sorry we merged something else. Do you mind rebasing?

@djhi djhi added this to the 3.6.3 milestone Jul 1, 2020
@rassie
Copy link
Contributor Author

rassie commented Jul 1, 2020

@djhi done, thanks!

@rassie
Copy link
Contributor Author

rassie commented Jul 6, 2020

@djhi sorry for nagging, but it seems this PR is the last one for 3.6.3, is there anything blocking this from being merged, can I help with that in any way?

@fzaninotto
Copy link
Member

Sorry for the delay.

There were significant changes in the next branch that we don't want to rebase because of this PR. So we will publish 3.6.3 without these changes, then merge next into master, then see if this PR can be merged without conflicts.

@fzaninotto fzaninotto modified the milestones: 3.6.3, 3.7.0 Jul 6, 2020
@rassie
Copy link
Contributor Author

rassie commented Jul 6, 2020

Thanks for the update, I'll keep an eye on releases and will rebase when 3.6.3 is done.

@rassie
Copy link
Contributor Author

rassie commented Jul 6, 2020

Or should I just rebase on top of next?

@fzaninotto fzaninotto modified the milestones: 3.7.0, 3.7.1 Jul 6, 2020
@rassie
Copy link
Contributor Author

rassie commented Jul 6, 2020

I've rebased this PR on master after the 3.7.0 release. Finger crossed the build succeeds :)

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build passes \o/

I have a couple minor comments though, see below.

examples/simple/src/tags/TagList.js Outdated Show resolved Hide resolved
packages/ra-ui-materialui/src/auth/Logout.tsx Show resolved Hide resolved
@material-ui documents some tree-shaking performance optimizations at
https://material-ui.com/guides/minimizing-bundle-size/. Any downstream
project using both Material-UI and react-admin implementing this
recommendations will encounter errors like

Cannot find module: '@material-ui/core/esm/createMuiTheme'. Make sure
this package is installed.

This is apparently due to the way @material-ui re-exports styling
exports via @material-ui/core. The official recommendation is to import
via @material-ui/core/styles, which is what this commit is doing for
makeStyles and createMuiTheme.

Also included: ESLint configuration forbidding these two imports.
@fzaninotto fzaninotto merged commit 14d7f0d into marmelab:master Jul 7, 2020
@fzaninotto
Copy link
Member

Thanks!

@fzaninotto fzaninotto changed the title [RFR] Avoid import errors when tree-shaking @material-ui [RFR] Fix import errors when tree-shaking @material-ui Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants