-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[ESM] @mui/icons-material move to pure ESM #35233
Comments
We plan to properly support ES modules (file extensions in imports, package.json |
@michaldudak What about adding a {
"type": "module"
} I've done this for a package and I was able to make it work without using the |
I suppose we could do this. @macrozone, would this alone help in your case? cc @mui/code-infra for thoughts. |
I may be mistaken, but I believe Next.js during SSR would try to load it untranspiled as ESM on node.js and fail on the missing file extensions and missing package.json exports. I think this would break a lot of existing code. I also remember this breaking with React 17. (It's been a while since I've looked into #30671 and my memory is a bit cloudy, so treat this comment mainly as potential scenarios to test.)
@arobert93 I can't seem to load |
@Janpot For some reason you need to run |
@arobert93 Still can't load it when imported as import Activity from '@untitled-ui/icons-react/buid/esm/Activity'; https://stackblitz.com/edit/nextjs-n1kkod?file=pages%2Findex.js I believe the reason is that |
@Janpot there's a typo in your import path: Replace: import Activity from '@untitled-ui/icons-react/buid/esm/Activity'; With: import Activity from '@untitled-ui/icons-react/build/esm/Activity'; |
🤷 I copied that straight from the README edit:Ok, looking over the docs again
It seems I was conflating not having |
Mmm, we have this specific issue, and because the v6 with ESM support is TBA, we will have to leave MUI for our project. Bummer. |
@Janpot I'm curious how you solved this. My issue is: either it works in Next.js or in Jest. I can't have both working at a time. My stack involves Next.js 13, typescript, ts-jest and jest. Commonjs modules vs ES6 is a challenging thing to setup with this stack man. |
Not sure whether it will help you, but in resolve: {
alias: [
{
// FIXME(https://github.com/mui/material-ui/issues/35233)
find: /^@mui\/icons-material\/(?!esm\/)([^/]*)/,
replacement: '@mui/icons-material/esm/$1',
},
],
}, Which forces the bundler to import the ESM target for edit: I changed the regex from |
Here is a workaround for esbuild based on @Janpot's answer: // FIXME(https://github.com/mui/material-ui/issues/35233)
const fixMuiIconsMaterialImports = {
name: 'fix @mui/icons-material imports',
setup(build) {
build.onResolve(
{ filter: /^@mui\/icons-material\/([^/]*)$/ },
async args => {
const name = args.path.split('/').pop()
const result = await build.resolve(
`@mui/icons-material/esm/${name}.js`,
{
kind: 'import-statement',
resolveDir: args.resolveDir,
},
)
if (result.errors.length > 0) {
return { errors: result.errors }
}
return { path: result.path }
},
)
},
} Just add it to your build's {
...
plugins: [fixMuiIconsMaterialImports],
...
} |
We ran into the following issue in Toolpad a while ago:
Investigated a bit and I found that the issue disappears when I turn the "type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./esm/index.js",
"require": "./index.js"
},
"./*": {
"types": "./*.d.ts",
"import": "./esm/*.js",
"require": "./*.js"
}
},
|
I'm facing a similar issue, I summarised it here: #31835 (comment) |
Moving to v7's milestone as per: #30671 (comment) |
The above esbuild workaround didn't work for me, but I found this esbuild plugin (in the popular https://github.com/refinedev/refine project, where they use it with tsup) https://github.com/refinedev/refine/blob/4c34444633a252fd830fc3ddbc5963e112269e2f/packages/shared/mui-icons-material-esm-replace-plugin.ts which does the trick. |
Both `@mui/icons-material` (mui/material-ui#35233) and `lodash` (lodash/lodash#5107) have problems being imported in a consuming package when using ESM. The workarounds attempted in #258 almost seemed to work (didn't break a downstream bundled package using Vite), but still caused problems for the original example node application https://codesandbox.io/p/devbox/pensive-volhard-hyhtls, with errors like: ``` Error: Cannot find module '/path/to/mui-tiptap-in-node/node_modules/@mui/icons-material/esm/FormatColorFill ``` This approach is inspired by what tss-react does https://github.com/garronej/tss-react/blob/f5351e42e33f35f18415cfc1ffc6b08eb8ce4d25/package.json (e.g. see here garronej/tss-react@4699702 and garronej/tss-react#164).
Should resolve #256 Both `@mui/icons-material` (mui/material-ui#35233) and `lodash` (lodash/lodash#5107) have problems being imported in a consuming package when using ESM. The workarounds attempted in #258 almost seemed to work (didn't break a downstream bundled package using Vite), but still caused problems for the original example node application https://codesandbox.io/p/devbox/pensive-volhard-hyhtls, with errors like: ``` Error: Cannot find module '/path/to/mui-tiptap-in-node/node_modules/@mui/icons-material/esm/FormatColorFill ``` This approach is inspired by what tss-react does https://github.com/garronej/tss-react/blob/f5351e42e33f35f18415cfc1ffc6b08eb8ce4d25/package.json (e.g. see here garronej/tss-react@4699702 and garronej/tss-react#164). With this change, this code now works in the node context (though slightly odd): ``` import pkg from "mui-tiptap"; const { FontSize, HeadingWithAnchor, ResizableImage, TableImproved } = pkg; ```
Should resolve #256. Both `@mui/icons-material` (mui/material-ui#35233) and `lodash` (lodash/lodash#5107) have problems being imported in a consuming package when using ESM. The workarounds attempted in #258 almost seemed to work (didn't break a downstream bundled package using Vite), but still caused problems for the original example node application https://codesandbox.io/p/devbox/pensive-volhard-hyhtls, with errors like: ``` Error: Cannot find module '/path/to/mui-tiptap-in-node/node_modules/@mui/icons-material/esm/FormatColorFill ``` This approach is inspired by what tss-react does https://github.com/garronej/tss-react/blob/f5351e42e33f35f18415cfc1ffc6b08eb8ce4d25/package.json (e.g. see here garronej/tss-react@4699702 and garronej/tss-react#164). With this change, this code now works in the node context (though slightly odd): ``` import pkg from "mui-tiptap"; const { FontSize, HeadingWithAnchor, ResizableImage, TableImproved } = pkg; ```
Should resolve #256. Both `@mui/icons-material` (mui/material-ui#35233) and `lodash` (lodash/lodash#5107) have problems being imported in a consuming package when using ESM. The workarounds attempted in #258 almost seemed to work (didn't break a downstream bundled package using Vite), but still caused problems for the original example node application https://codesandbox.io/p/devbox/pensive-volhard-hyhtls, with errors like: ``` Error: Cannot find module '/path/to/mui-tiptap-in-node/node_modules/@mui/icons-material/esm/FormatColorFill ``` This approach is inspired by what tss-react does https://github.com/garronej/tss-react/blob/f5351e42e33f35f18415cfc1ffc6b08eb8ce4d25/package.json (e.g. see here garronej/tss-react@4699702 and garronej/tss-react#164). With this change, this code now works in the node context (though slightly odd): ``` import pkg from "mui-tiptap"; const { FontSize, HeadingWithAnchor, ResizableImage, TableImproved } = pkg; ```
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note We value your feedback @macrozone! How was your experience with our support team? |
Steps to reproduce 🕹
I try to convert a NPM module to pure ESM (with typescript).
It occasionally uses icons from
@mui/icons-material
We usually had imports like this:
this no longer works, DeleteIcon is not an Component, but an object with the
default
property.this however works:
and this as well (probably the recommended way anyway)
Current behavior 😯
DeleteIcon is not an Component, but an object with the
default
property.Expected behavior 🤔
DeleteIcon is a Component and works out of the box
Context 🔦
I maintain https://github.com/react-page/react-page and have a problem there currently with latest nextjs in combination with some libraries and therefore try to move everything to pure ESM.
the ESM transition is pretty rough and combined with typescript its surprisingly complicated. Its unclear whether the above problem is related to the config or to @mui/icons-material, or both.
I see that not all npm packages have this problem though.
Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: