-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[core] Add exports field to packages #41596
Changes from all commits
1646c6f
365aaea
a8a9c75
330c04b
f3f6a46
bf84a60
91383ac
222eef3
bee0004
9b37c2a
d7d133d
a28200d
9fe59d8
74cb37c
6bda919
6bad76d
5d0050b
35a501e
90a3325
b94b4a6
6964f8b
7442410
fb7a4ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -214,50 +214,25 @@ It will perform the following diffs: | |||||
|
||||||
The packages published on npm are **transpiled** with [Babel](https://github.com/babel/babel), optimized for performance with the [supported platforms](/material-ui/getting-started/supported-platforms/). | ||||||
|
||||||
A [modern bundle](#modern-bundle) is also available. | ||||||
|
||||||
### How to use custom bundles? | ||||||
|
||||||
:::error | ||||||
You are strongly discouraged to: | ||||||
|
||||||
- Import from any of the custom bundles directly. Do not do this: | ||||||
|
||||||
```js | ||||||
import { Button } from '@mui/material/legacy'; | ||||||
``` | ||||||
|
||||||
You have no guarantee that the dependencies also use legacy or modern bundles, leading to module duplication in your JavaScript files. | ||||||
|
||||||
- Import from any of the undocumented files or folders. Do not do this: | ||||||
|
||||||
```js | ||||||
import { Button } from '@mui/material/esm'; | ||||||
``` | ||||||
|
||||||
You have no guarantee that these imports will continue to work from one version to the next. | ||||||
### Modern bundle | ||||||
|
||||||
::: | ||||||
The modern bundle targets the latest released versions of evergreen browsers (Chrome, Firefox, Safari, Edge). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
This can be used to make separate bundles targeting different browsers. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
A great way to use these bundles is to configure bundler aliases, for example with [Webpack's `resolve.alias`](https://webpack.js.org/configuration/resolve/#resolvealias): | ||||||
To use it, configure your bundler's resolve condition names. | ||||||
For example, with [Webpack's `resolve.conditionNames`](https://webpack.js.org/configuration/resolve/#resolveconditionnames): | ||||||
|
||||||
```js | ||||||
```js title="webpack.config.js" | ||||||
{ | ||||||
// ... | ||||||
resolve: { | ||||||
alias: { | ||||||
'@mui/material': '@mui/material/legacy', | ||||||
'@mui/styled-engine': '@mui/styled-engine/legacy', | ||||||
'@mui/system': '@mui/system/legacy', | ||||||
'@mui/base': '@mui/base/legacy', | ||||||
'@mui/utils': '@mui/utils/legacy', | ||||||
'@mui/lab': '@mui/lab/legacy', | ||||||
} | ||||||
conditionNames: ['mui-modern', 'import', 'default']; | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
### Modern bundle | ||||||
Here's the documentation for common bundlers: | ||||||
|
||||||
The modern bundle can be found under the [`/modern` folder](https://unpkg.com/@mui/material/modern/). | ||||||
It targets the latest released versions of evergreen browsers (Chrome, Firefox, Safari, Edge). | ||||||
This can be used to make separate bundles targeting different browsers. | ||||||
- [Webpack](https://webpack.js.org/configuration/resolve/#resolveconditionnames) | ||||||
- [Vite](https://vitejs.dev/config/shared-options.html#resolve-conditions) | ||||||
- [ESBuild](https://esbuild.github.io/api/#conditions) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,3 +23,73 @@ The steps you need to take to migrate from Material UI v5 to v6 are described | |||||
This list is a work in progress. | ||||||
Expect updates as new breaking changes are introduced. | ||||||
::: | ||||||
|
||||||
### Added exports field to package.json | ||||||
|
||||||
The `exports` field has been added to the `@mui/material/package.json` file to improve the ESM and CJS builds split: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```json title="@mui/material/package.json" | ||||||
// ... | ||||||
"exports": { | ||||||
".": { | ||||||
"types": "./index.d.ts", | ||||||
"mui-modern": "./modern/index.mjs", | ||||||
"import": "./index.mjs", | ||||||
"default": "./node/index.js" | ||||||
}, | ||||||
"./*": { | ||||||
"types": "./*/index.d.ts", | ||||||
"mui-modern": "./modern/index.mjs", | ||||||
"import": "./*/index.mjs", | ||||||
"default": "./node/*/index.js" | ||||||
} | ||||||
} | ||||||
// ... | ||||||
``` | ||||||
|
||||||
Read more about the `exports` field in the [Node.js documentation](https://nodejs.org/api/packages.html#exports). | ||||||
|
||||||
This change limits the exported modules to the root import and one level deep imports. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
If you were importing from deeper levels, you will need to update your imports: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```diff title="index.mjs" | ||||||
- import buttonClasses from '@mui/material/Button/buttonClasses'; | ||||||
+ import { buttonClasses } from '@mui/material/Button'; | ||||||
``` | ||||||
|
||||||
```diff title="index.cjs" | ||||||
- const { default: Button } = require('@mui/material/node/Button'); | ||||||
+ const { default: Button } = require('@mui/material/Button'); | ||||||
``` | ||||||
|
||||||
You might have to update your bundler configuration to support the new structure. | ||||||
Following are some common use cases that require changes: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#### Importing CJS | ||||||
|
||||||
If you were importing from `/node` as a workaround, this is no longer necessary as the `exports` field maps CJS to the correct files. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#### Using the modern bundle | ||||||
|
||||||
The way the modern bundle should be imported has changed. | ||||||
Previously, you would alias `@mui/material` to `@mui/material/modern` in your bundler configuration. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Now, you should configure your bundler's resolve conditions to use the `"mui-modern"` condition name. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Here's the updated Webpack example that was previously documented: | ||||||
|
||||||
```diff title="webpack.config.js" | ||||||
module.exports = { | ||||||
//... | ||||||
resolve: { | ||||||
- alias: { | ||||||
- '@mui/material': '@mui/material/modern', | ||||||
- } | ||||||
+ conditionNames: ['mui-modern', 'import', 'default'], | ||||||
}, | ||||||
}; | ||||||
``` | ||||||
|
||||||
Documentation: [resolve.conditionNames](https://webpack.js.org/configuration/resolve/#resolveconditionnames) | ||||||
|
||||||
:::info | ||||||
For guidance on other bundlers, refer to the [Modern bundle documentation](/material-ui/guides/minimizing-bundle-size/#modern-bundle). | ||||||
::: |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,16 +24,45 @@ This list is a work in progress. | |||||
Expect updates as new breaking changes are introduced. | ||||||
::: | ||||||
|
||||||
### Root code is now ESM | ||||||
### Added exports field to package.json | ||||||
|
||||||
The ESM code, previously under the `esm/` build, has been moved to the root of the package. | ||||||
The CommonJS code, previously on the root, has been moved to the `node/` build. | ||||||
The `exports` field has been added to the `@mui/system/package.json` file to improve the ESM and CJS builds split: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
:::info | ||||||
This is an intermediate step to prepare for adding the `exports` field to the `package.json` file. | ||||||
If you have trouble using this new structure, please wait for the future update which adds the `exports` field. | ||||||
You can follow progress on https://github.com/mui/material-ui/issues/30671. | ||||||
::: | ||||||
```json title="@mui/system/package.json" | ||||||
// ... | ||||||
"exports": { | ||||||
".": { | ||||||
"types": "./index.d.ts", | ||||||
"mui-modern": "./modern/index.mjs", | ||||||
"import": "./index.mjs", | ||||||
"default": "./node/index.js" | ||||||
}, | ||||||
"./*": { | ||||||
"types": "./*/index.d.ts", | ||||||
"mui-modern": "./modern/index.mjs", | ||||||
"import": "./*/index.mjs", | ||||||
"default": "./node/*/index.js" | ||||||
} | ||||||
} | ||||||
// ... | ||||||
``` | ||||||
|
||||||
Read more about the `exports` field in the [Node.js documentation](https://nodejs.org/api/packages.html#exports). | ||||||
|
||||||
This change limits the exported modules to the root import and one level deep imports. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the suggestions in the other doc above |
||||||
If you were importing from deeper levels, you will need to update your imports: | ||||||
|
||||||
```diff | ||||||
- import styled from '@mui/system/esm/styled'; | ||||||
+ import styled from '@mui/system/styled'; | ||||||
``` | ||||||
|
||||||
You might have to update your bundler configuration to support the new structure. | ||||||
Following are some common use cases that require changes: | ||||||
|
||||||
#### Importing ESM | ||||||
|
||||||
If you were importing from `/esm` as a workaround, this is no longer necessary as the `exports` field maps ESM to the correct files. | ||||||
|
||||||
### GridProps type | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Badge } from './Badge'; | ||
export * from './Badge.types'; | ||
export * from './badgeClasses'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Button } from './Button'; | ||
|
||
export * from './buttonClasses'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Input } from './Input'; | ||
|
||
export * from './Input.types'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { MenuButton } from './MenuButton'; | ||
export * from './MenuButton.types'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export * from './MenuItem'; | ||
export * from './MenuItem.types'; | ||
export * from './menuItemClasses'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
'use client'; | ||
export { MultiSelect } from './MultiSelect'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export { NoSsr } from './NoSsr'; | ||
export * from './NoSsr.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export * from './Option'; | ||
export * from './Option.types'; | ||
export * from './optionClasses'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { OptionGroup } from './OptionGroup'; | ||
|
||
export * from './OptionGroup.types'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Popper } from './Popper'; | ||
export type { | ||
PopperPlacementType, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export { Portal } from './Portal'; | ||
export * from './Portal.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Select } from './Select'; | ||
|
||
export * from './selectClasses'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Slider } from './Slider'; | ||
export * from './Slider.types'; | ||
export * from './sliderClasses'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Snackbar } from './Snackbar'; | ||
|
||
export * from './Snackbar.types'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Switch } from './Switch'; | ||
export * from './Switch.types'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Tab } from './Tab'; | ||
export * from './Tab.types'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { TabPanel } from './TabPanel'; | ||
export * from './TabPanel.types'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { TablePagination } from './TablePagination'; | ||
export * from './TablePagination.types'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { Tabs } from './Tabs'; | ||
export * from './TabsContext'; | ||
export * from './tabsClasses'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { TabsList } from './TabsList'; | ||
export * from './TabsList.types'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export { TextareaAutosize } from './TextareaAutosize'; | ||
export * from './TextareaAutosize.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { NumberInput as Unstable_NumberInput } from './NumberInput'; | ||
export * from './numberInputClasses'; | ||
export * from './NumberInput.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use client'; | ||
export { useModal as unstable_useModal } from './useModal'; | ||
export * from './useModal.types'; | ||
export * from './ModalManager'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export { useNumberInput as unstable_useNumberInput } from './useNumberInput'; | ||
export * from './useNumberInput.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export { useBadge } from './useBadge'; | ||
export * from './useBadge.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export { useButton } from './useButton'; | ||
export * from './useButton.types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
'use client'; | ||
export * from './useCompoundParent'; | ||
export * from './useCompoundItem'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to not always add the extensions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
regressions
androllup
(umd) builds use this config file as well, and those break if we add the extensions.When we remove the umd build, I could test if the issue with the
regressions
build is fixable, and we can remove this check. Does that make sense?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally al tests use builds that are as close to the real build as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, I'll look into adapting the
regressions
builds as well 👍🏼There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Janpot, I investigated the issue a bit. The
regressions
build usesbabel.config.js
to compile the packages as well as other files, like the docs data files and the regressions tests files. It also points to thesrc
of the packages instead of thebuild
(see the regressions webpack config and the base webpack config).I was able to use the packages builds instead of the
src
code by doingBut even after doing that I still had to filter out some files and not add the file extensions to them:
This is because the files in those folders do not have extensions, as we're not adding them. We could add them, but I think this is outside of this PR's scope.
In conclusion, I would do
MUI_ADD_IMPORT_EXTENSIONS
flag for this PR, adding the extensions only when building usingbuild.mjs
src
for the regression tests. In that issue's PR, we could revisit removingMUI_ADD_IMPORT_EXTENSIONS
and modifying the regression infrastructure accordingly.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for me