-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Build warnings with MUI - barrel_optimize #55663
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
I downgraded the NEXT version to 13.4.19 and the warnings stopped. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
It's not necessary to comment "same issue" without providing more context/a public, minimal reproduction. Please upvote the issue instead 👍, or subscribe to notifications. Thanks |
This seem to be a bug in the MUI package. It is only triggered when importing
As a workaround, I recommend importing { styled } from The error has vanished from my styled Custom MUI component for now. 👍 |
Changing the import of |
Might be multiple places in the project you import either Not sure if this should be handled by Vercel or MUI however. FYI: I have a greenfield project with one component, so I've not had the time yet to import much MUI components all over the place. |
Actually, it seems mostly related to Next.js. We encountered this issue with MUI, but I don't think it's specific to MUI. Also, I haven't downgraded my Next.js version. It's not affecting my build; it's just giving warnings (suppressing warnings is my passion 😄), and my production doesn't seem to be affected by anything. For clarification, in our project, I've used nearly every MUI component in our app, and there has been no negative user feedback regarding these warnings. I can humbly say these warnings are ignorable right now.🤡 |
Ran into the same thing: #55834 |
Found a Fix after bashing my head against it for 2 days Chaning from: to
It is not every component you have to do it. For what i have tested it is only some of the MUI components(not sure witch) and if you import children into parents both both components must be changed. Server Components must also change. this fixed, for me atleast all bugs and warnings and render errors related to: Slugs, Parallel route and possible also all other Next Routing that uses MUI must also be changed to get rid of the bug/warning |
I use the "eslint-plugin-mui-path-imports" dependency that does this automatic import work (I even recommend it), but the warnings continue to be displayed. |
I solved my problem importing the functions from MUI in a separated import. In the warning:
the problem is the import of useTheme.
I solved importing like this:
I think this is due to the new feature "Optimized Package Imports". Hope it helps. |
Not to repeat others above but my solution was converting to:
Hope it helps. |
In fact, the solutions from @yelodevopsi and @ElectricCodeGuy works well here. Doing what both said in all imports, the warnings disappear. Thank you guys. |
The "fixes" that are being posted aren't really fixes but workarounds. I appreciate the cooperation and sharing so we can get it to work in the meantime! However, since it worked before Next version 13.5, I believe its on Next to fix not MUI. Isn't this a tree shaking issue that should be supported by Next? https://mui.com/material-ui/guides/minimizing-bundle-size/#when-and-how-to-use-tree-shaking |
I can add that
|
for me the error stopped showing up after changing |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I have written a codemod to change the import of mui and separate components from @mui/material and styles @mui/material/styles.
to
Create a file named
code
I also wrote a codemod to change imports for
codeHowever, I still had to add some code manually because props(GridProps, SxProps) and classes (badgeClasses, outlinedInputClasses) were also imported in the code. Use it at your own risk, and I'd appreciate it if you could help enhance this code.
|
There is a potential workaround on Material UI's side: diff --git a/packages/mui-material/src/index.js b/packages/mui-material/src/index.js
index bcfd9bdcfe..2a6c5934f2 100644
--- a/packages/mui-material/src/index.js
+++ b/packages/mui-material/src/index.js
@@ -3,7 +3,6 @@
import * as colors from './colors';
export { colors };
-export * from './styles';
export * from './utils';
@@ -409,6 +408,7 @@ export { default as useAutocomplete } from './useAutocomplete';
export { default as GlobalStyles } from './GlobalStyles';
export * from './GlobalStyles';
-export { StyledEngineProvider } from './styles';
+export { StyledEngineProvider, useTheme, … } from './styles';
export { unstable_composeClasses } from '@mui/base/composeClasses'; removing the barrel export. But it feels odd that we would need to change this. I'm not even sure it would cover all the cases. We effectively couldn't use barrel imports. |
this is working for me, next version 13.5 makes that warning shows up |
As mentioned above already, may read the changes in the NextJS update, it's explained there why it "suddenly" shows the warning. Just sort the imports properly and use the right place to import the required modules, like the useTheme and useStyles for example has to come from the mui/styles Most components from the mui/material. alpha/darken for example comes from mui/material/styles I added some Eslint rules to my repo to ensure my co-workers to follow the path. The warnings are telling you which import is wrong, so may just read them. |
An official answer would be great. Is this going to be fixed in an upcoming release or is nextjs now also very opinionated how dependencies should manage their exports aka expects every dependency to fall in line and do it the nextjs way? FYI the exports from mui are 100% valid. |
I certainly don’t want to start drama on the internet, but there is no wrong way to import these utils and components if the way is documented by the mentioned library maintainers. It’s disappointing that Vercel have not started to address this, even with a similar “we are looking at it”. I understand they are extremely busy with many issues to get too, but many of these comments are repeating the same incorrect statements due to an issue with something that cannot be turned off. If the fix is going to be challenging, at the very least allow us to opt out, and if we already can, make the documentation very clear. |
This was exactly the solution for me. Looks like ThemeProvider has moved?! It's not inside @mui/material anymore but inside @mui/material/styles. |
Hey everyone! 🙌 In my project, I found myself needing to refactor imports across more than 400 files due to the changes in MUI v5. Manually modifying each file was simply not viable, so I created a small TypeScript script to automate the process. I figured that this might be helpful to others facing the same challenge, so I wanted to share it. The script takes in your old import statements and outputs the new format. It even handles some special cases, such as Special Imports Handled:
Here's how you can use the script:
function transformImportStatement(input: string): string {
const importRegex = /import\s*{([\s\S]*?)}\s*from\s*['"]([^'"]+)['"]\s*;?/;
const match = input.match(importRegex);
if (!match) {
return input; // Return the input line if it doesn't match the expected import pattern
}
const components = match[1]
.split(',')
.map(s => s.trim())
.filter(s => s.length > 0)
.sort();
const modulePath = match[2];
const specialImports: Record<string, string> = {
'ThemeProvider': '@mui/material/styles/ThemeProvider',
'styled': '@mui/material/styles',
'useTheme': '@mui/material/styles'
};
const standardImports: string[] = [];
const specialImportLines: Record<string, string[]> = {};
components.forEach(component => {
if (specialImports[component]) {
if (!specialImportLines[specialImports[component]]) {
specialImportLines[specialImports[component]] = [];
}
specialImportLines[specialImports[component]].push(component);
} else {
standardImports.push(`import ${component} from '${modulePath}/${component}'`);
}
});
const specialImportStatements = Object.entries(specialImportLines).map(([path, components]) => {
return `import { ${components.join(', ')} } from '${path}'`;
});
return [...standardImports, ...specialImportStatements].join('\n');
}
const input = `
import {
ThemeProvider,
CssBaseline,
IconButton,
Button,
Typography,
Badge,
} from "@mui/material";
`;
const result = transformImportStatement(input);
console.log(result);
Example:Input: transformImportStatement(`import { ThemeProvider, CssBaseline, IconButton, Button, Typography, Badge } from "@mui/material"`);
// OR
transformImportStatement(`
import {
ThemeProvider,
CssBaseline
IconButton,
Button,
Typography,
Badge
} from "@mui/material";
`); Output: import Badge from '@mui/material/Badge'
import Button from '@mui/material/Button'
import IconButton from '@mui/material/IconButton'
import Typography from '@mui/material/Typography' I hope this helps speed up the migration process for some of you. If you find any issues or have suggestions for improvement, please let me know! Happy coding! 💻 |
Downgrade Next to resolve build warnings caused by a MUI/Next issue documented at vercel/next.js#55663
* Install Tailwind CSS and update Next * Configure Tailwind CSS * Use Tailwind to style the term detail sections * Downgrade Next Downgrade Next to resolve build warnings caused by a MUI/Next issue documented at vercel/next.js#55663 * Downgrade Next again Downgrade Next to 13.2.4 in an attempt to fix a build issue in the CI pipeline. * Fix the package-lock.json file
I upgraded to the new npm version of MUI but still have same issue
|
Has a conclusion been reached regarding this issue? I believe the core issue is not about changing the way of importing, but the fact that the optimization settings are hard-coded and cannot be modified. While I appreciate that Next.js supports the optimization of other styling libraries, given the issues that have arisen, wouldn't it be better to allow users to specify as before, or making it possible to override it in next.config.js? |
I think it will be stable in the next release v13.5.5, because in v13.5.5-canary.4, there are no more warning states
|
We were getting a lot of: The requested module '__barrel_optimize__?names=createTheme&wildcard!=!./generateUtilityClass' contains conflicting star exports for the name '__esModule' with the previous requested module '__barrel_optimize__?names=createTheme&wildcard!=!./utils' Changing this appears to fix it. See: vercel/next.js#55663 (comment)
This comment has been minimized.
This comment has been minimized.
@shuding Great, thanks! |
appears true for me as well. Thanks all! |
Will this be sorted in the next release? |
I had a line that was auto-imported thanks to VSCode like this import { PaletteMode, createTheme } from "@mui/material": I had to keep the Hovever, it did not like the import of Here is what did the trick. import { PaletteMode } from "@mui/material";
import createTheme from "@mui/material/styles/createTheme"; Now I have no warning and I haven't downgraded my Next.js version either. |
@aminnairi as mentioned earlier, your imports before were perfectly valid and you shouldn't need to change them. I can also confirm that this was fixed in v13.5.5-canary.4, specifically in #56489. |
update to 13.5.5 since it resolves barrel optimize error ref: vercel/next.js#55663
works for me @aminnairi ty! |
Thanks @yelodevopsi this approach worked. |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
https://github.com/muhammedogz/nextjs-mui-build-warnings
To Reproduce
npm i && npm run build
--
Also you can create the warning from MUI template.
index.tsx
const theme = useTheme();
npm run build
Current vs. Expected behavior
Build should be completed without warnings.
Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #1 SMP Fri Jan 27 02:56:13 UTC 2023 Binaries: Node: 20.7.0 npm: 10.1.0 Yarn: N/A pnpm: N/A Relevant Packages: next: 13.5.2-canary.1 eslint-config-next: 13.5.1 react: 18.2.0 react-dom: 18.2.0 typescript: 5.2.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Not sure
Additional context
Maybe this PR could introduces this warning.
Warrning messages from build.
NEXT-1644
The text was updated successfully, but these errors were encountered: