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

[docs] Modularize Imports for Nextjs #35457

Closed
wants to merge 3 commits into from

Conversation

dev-ahmedhany
Copy link

@dev-ahmedhany dev-ahmedhany commented Dec 13, 2022

Closes #35450

Signed-off-by: Ahmed Hany 39633124+dev-ahmedhany@users.noreply.github.com

mui#35450



Signed-off-by: Ahmed Hany <39633124+dev-ahmedhany@users.noreply.github.com>
@mui-bot
Copy link

mui-bot commented Dec 13, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-35457--material-ui.netlify.app/

No bundle size changes

Generated by 🚫 dangerJS against 4ddd6de

@zannager zannager added the docs Improvements or additions to the documentation label Dec 13, 2022
@Janpot
Copy link
Member

Janpot commented Dec 13, 2022

Gave this a quick try on Toolpad and this breaks on imports like:

import { Box, styled } from '@mui/material'

As there is no @mui/material/styled. Perhaps it should only be done for @mui/icons-material?

@dev-ahmedhany
Copy link
Author

@Janpot
but @mui/material is also at the docs

'babel-plugin-direct-import',
      { modules: ['@mui/material', '@mui/icons-material'] },

@michaldudak
Copy link
Member

Could you add it as Option 3, after a Babel plugin?

…ndle-size.md

Co-authored-by: Michał Dudak <michal.dudak@gmail.com>
Signed-off-by: Ahmed Hany <39633124+dev-ahmedhany@users.noreply.github.com>
@michaldudak
Copy link
Member

michaldudak commented Dec 21, 2022

Good catch, @Janpot. I see that the Babel plugins also fail to import styled from @mui/material. I suppose your suggestion to apply this only to @mui/icons-material makes the most sense. CC @mui/core

/** @type {import('next').NextConfig} */
const nextConfig = {
...
experimental: {
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't seems to be under experimental in https://nextjs.org/docs/advanced-features/compiler#modularize-imports

Copy link
Member

Choose a reason for hiding this comment

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

It's recently promoted

Comment on lines 209 to 211
'@mui/material': {
transform: '@mui/material/{{member}}'
},
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't do this, this is probably overkill. There are also cases where it doesn't work because the code is not consistent enough.

@@ -194,6 +194,31 @@ Modify your `package.json` commands:
}
```

If you are using Next.js >= v12.1.1, you can make use of their [Modularize Imports](https://nextjs.org/docs/advanced-features/compiler#modularize-imports) feature.
Copy link
Member

Choose a reason for hiding this comment

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

This section of the docs starts to be quite long. I think that it could be good to change the headers to help navigate the content.

@oliviertassinari
Copy link
Member

I think that it would be great to update the /examples so that developers get this optimization by default.

@michaldudak
Copy link
Member

@dev-ahmedhany, to sum up - if you'd like to continue with this PR, please remove the instructions for @mui/material imports (only leave @mui/icons-material). Also, remove the experimental field and update the Next version to 13.1.

@dev-ahmedhany dev-ahmedhany requested review from Janpot and michaldudak and removed request for michaldudak and Janpot January 10, 2023 11:32
@dev-ahmedhany
Copy link
Author

@michaldudak done

@michaldudak
Copy link
Member

Thanks. Please merge in the latest master, this should resolve the failing checks. Also, as @oliviertassinari pointed out, it would be great to update the examples to use this feature (/examples/nextjs and /examples/nextjs-with-typescript).

@zigang93
Copy link

zigang93 commented Mar 3, 2023

How about @mui/x-data-grid example ? hit some error with the config below:

modularizeImports: {
    '@mui/x-data-grid': {
      transform: '@mui/x-data-grid/{{member}}',
    },
    '@mui/x-date-pickers': {
      transform: '@mui/x-date-pickers/{{member}}',
    },
  },

@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 5, 2023

How about @mui/x-data-grid example

@zigang93 These are already one npm package per "component" (not in the React definition, from the perspective of end-users), there isn't as much value to modularizing imports as with icons.

In any case, we also need to modularize all the internal imports: #35840.

@cherniavskii
Copy link
Member

bundlephobia.com/package/@mui/x-data-grid@6.0.0 the main component is 70% of the bundle size. I think that it makes more sense to look into tree-shaking. Right now, modularizing imports makes little sense.

@oliviertassinari Yeah, I think most of the data grid's code is imported anyway, there are only a few components/hooks that are optional (like GridToolbar).
Regarding the tree-shaking, do you have any specific issues in mind?

@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 22, 2023

Regarding the tree-shaking, do you have any specific issues in mind?

@cherniavskii I'm thinking of mui/mui-x#7358, and #35840 for the data grid.

@oliviertassinari
Copy link
Member

I wonder what we should do with this PR considering vercel/next.js#50900.

ijjk added a commit to vercel/next.js that referenced this pull request Jun 29, 2023
Fixes #51872. We were exploring in
mui/material-ui#35457 the option to move the
`modularizeImports` config to our Next.js examples to fix
mui/material-ui#35450 however, we never got to
complete the work.

We are not yet in a position where we can apply modularizeImports to
`@mui/material`. We still have folders to create to make it work.

Maybe we should close mui/material-ui#35457?

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@michaldudak
Copy link
Member

If I understand it correctly, this is applied by default by Next.js and doesn't require additional configuration. I believe we can close this PR.

@oliviertassinari
Copy link
Member

I reverted one of those: vercel/next.js#51953

@michaldudak
Copy link
Member

This PR is related only to icons-material, so the code you reverted doesn't affect it, does it?

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 13, 2023

This PR is related only to icons-material, so the code you reverted doesn't affect it, does it?

@michaldudak Agree. I also agree that we can close this PR.

However, the opportunity I see is we could open a new issue so we can scale this to all the MUI npm packages, beyond @mui/icons-material. It could be done with special rules, like in https://github.com/vercel/next.js/blob/261db496f7cf04b70f078a6eae0c8baf6fe5c238/packages/next/src/server/config.ts#L738. I didn't have time in vercel/next.js#51953 to do it right, there was an urgency to fix it before it was rolled from their canary release to stable. Then, the question is is the Next.js job to keep this up to date? Is there a similar opportunity with Vite, should we update the Vite example as well? At the end of the day, it's about improving the DX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation external dependency Blocked by external dependency, we can’t do anything about it nextjs performance
Projects
None yet
9 participants