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

[icons] Add exports field to package.json #43624

Merged
merged 8 commits into from
Sep 10, 2024
Merged

[icons] Add exports field to package.json #43624

merged 8 commits into from
Sep 10, 2024

Conversation

Janpot
Copy link
Member

@Janpot Janpot commented Sep 5, 2024

Currently esm requests for subpath module specifiers always resolve to the commonjs file. This PR adds an exports field to the package.json to correct the import. We don't change package layout, so anyone using a bundler that doesn't support the exports field should be unaffected.

Adding backwards compatible exports for specific setups we recommended in the past:

Closes #35233

Verified that it fixes the reproduction of #35233 (comment)

May also fully or partly fix:

In the Next major version we can consider following changes:

Opened a ticket here: #43629

@Janpot Janpot added the package: icons Specific to @mui/icons label Sep 5, 2024
@mui-bot
Copy link

mui-bot commented Sep 5, 2024

Netlify deploy preview

https://deploy-preview-43624--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 05b31b2

@Janpot Janpot marked this pull request as ready for review September 5, 2024 17:53
@oliviertassinari oliviertassinari added the bug 🐛 Something doesn't work label Sep 5, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 5, 2024

In the Next major version we can consider following changes:

Should we open an issue for those? Add the "breaking change" label, so in the next major, we can easily review those.

@DiegoAndai
Copy link
Member

@Janpot, it seems like you've covered all bases regarding the risks of releasing this in a minor version 🙌🏼 , so I'm on board with releasing it 👌🏼.

Just to make sure, did you test that we won't encounter the same issue that made us revert #26656 in the past? Do our tests cover this case now?

Finally, what do you think about waiting a couple of weeks before releasing this? Right now we're receiving a lot of feedback from the v6 stable, so I'm thinking we could hold this until the end of September. This way we avoid overloading the team in case we need to guide people through this change. Does this make sense to you?

@Janpot
Copy link
Member Author

Janpot commented Sep 9, 2024

Just to make sure, did you test that we won't encounter the #26310 (comment) that made us revert #26656 in the past?

That PR changed the icons package to an ESM-only package which wasn't supported at the time by Next.js. This PR doesn't remove commonjs from the package, even though Next.js now supports ESM-only packages. I've smoke tested this PR against latest stable Next.js and I didn't see problems. The node conditional specifically instructs to use commonjs on the server.

Do our tests cover this case now?

We have some manual bundling tests to test module resolution, but it looks like they're broken. Will look into them when I can.

Finally, what do you think about waiting a couple of weeks before releasing this? Right now we're receiving a lot of feedback from the v6 stable, so I'm thinking we could hold this until the end of September. This way we avoid overloading the team in case we need to guide people through this change. Does this make sense to you?

It's up to the team. I'm happy to do it either way.

"import": "./esm/*.js",
"require": "./*.js"
},
"./esm/*": "./esm/*.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why has this been added? For backward compatibility ?

Copy link
Member Author

@Janpot Janpot Sep 9, 2024

Choose a reason for hiding this comment

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

Exactly, someone might have added

  resolve: {
    alias: [
      {
        find: /^@mui\/icons-material\/(.*)/,
        replacement: '@mui/icons-material/esm/$1',
      },
    ],
  },

to their vite config. It means under the hood their bundler is changing @mui/icons-material/Add to @mui/icons-material/esm/Add before the package.json resolution algorithm has even kicked in. If we don't add an export for this, that module request will follow the ./* => ./esm/*.js export and try to access ./esm/esm/Add.js (* == esm/Add). When we add this export, we special case the ./esm path to not add an extra esm segment (* == Add). We can remove this in a major version.

Same problem for users of https://github.com/avocadowastaken/babel-plugin-direct-import?tab=readme-ov-file#example

edit: you can test this with the pigment vite app. removing the export and putting back the vite config should break its build

Copy link
Member

@DiegoAndai DiegoAndai left a comment

Choose a reason for hiding this comment

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

🚀

@Janpot
Copy link
Member Author

Janpot commented Sep 10, 2024

I'm removing the node condition again. It's currently erroring when importing natively as ESM on Node.js. We can keep this unsupported until we have true ESM files (v7).

@cwagner22
Copy link

It causes this issue with server side rendering:

Directory import 'node_modules/@mui/material/utils' is not supported resolving ES modules imported from node_modules/@mui/icons-material/esm/utils/createSvgIcon.js
Did you mean to import "@mui/material/node/utils/index.js"?

See mahmoudmoravej/remix-mui#5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work package: icons Specific to @mui/icons
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ESM] @mui/icons-material move to pure ESM
6 participants