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

[core] Add exports field to packages #43521

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1646c6f
Initial commit from POC changes
DiegoAndai Mar 21, 2024
365aaea
Fix @app/next-app build
DiegoAndai Mar 21, 2024
a8a9c75
Refactor exports field flag
DiegoAndai Mar 21, 2024
330c04b
Update mui-material/src/styles/index.d.ts
DiegoAndai Mar 21, 2024
f3f6a46
Refactor build structure check and exports field computation
DiegoAndai Mar 22, 2024
bf84a60
Add `@mui/types` exports field
DiegoAndai Mar 22, 2024
91383ac
Breaking changes guide
DiegoAndai Mar 27, 2024
222eef3
Merge branch 'next' into add-exports-field
DiegoAndai Apr 10, 2024
bee0004
Refactor to add compatibility mode with previous configuration
DiegoAndai Apr 10, 2024
9b37c2a
Add modern export to exports field
DiegoAndai Apr 11, 2024
d7d133d
pnpm dedupe
DiegoAndai Apr 12, 2024
a28200d
pnpm prettier
DiegoAndai Apr 12, 2024
9fe59d8
Update broken link
DiegoAndai Apr 12, 2024
74cb37c
Exclude codemod and docs packages from exports format
DiegoAndai Apr 12, 2024
6bda919
pnpm dedupe
DiegoAndai Apr 12, 2024
6bad76d
Merge branch 'next' into add-exports-field
DiegoAndai Apr 15, 2024
5d0050b
pnpm install / pnpm dedupe
DiegoAndai Apr 15, 2024
35a501e
Merge branch 'next' into add-exports-field
DiegoAndai Apr 15, 2024
90a3325
Change "modern" custom export to "mui-moder"
DiegoAndai Apr 16, 2024
b94b4a6
Make exports field opt-in instead of opt-out
DiegoAndai Apr 16, 2024
6964f8b
Merge branch 'next' into add-exports-field
DiegoAndai Apr 16, 2024
7442410
Remove 'use client' directive from index files
DiegoAndai Apr 18, 2024
fb7a4ff
Exclude material-nextjs from exports format build
DiegoAndai Apr 18, 2024
ca37450
Merge branch 'master' into add-exports-field
DiegoAndai Aug 29, 2024
523b6d3
Remove non-index root file in utils package
DiegoAndai Aug 29, 2024
600f269
Remove unnecessary babel plugin
DiegoAndai Aug 29, 2024
0c07408
pnpm install
DiegoAndai Aug 29, 2024
ea5e991
Comment createModulePackages for testing
DiegoAndai Aug 29, 2024
659a479
Add exports to mui-material-nextjs package
DiegoAndai Aug 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = function getBabelConfig(api) {
// in webpack config:
api.env(['regressions']);

const outFileExtension = '.js';
const outFileExtension = useESModules ? '.mjs' : '.js';

/** @type {babel.PluginItem[]} */
const plugins = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
// import of a small, pure module in a private demo
// bundle size and module duplication is negligible
// eslint-disable-next-line no-restricted-imports
import { convertLength } from '@mui/material/styles/cssUtils';
import { createTheme, responsiveFontSizes } from '@mui/material/styles';
import {
createTheme,
responsiveFontSizes,
unstable_convertLength as convertLength,
} from '@mui/material/styles';
import Box from '@mui/material/Box';
import { LineChart } from '@mui/x-charts';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
This can be used to make separate bundles targeting different browsers.

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)
8 changes: 4 additions & 4 deletions packages/mui-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "pnpm build && pnpm publish",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-joy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "pnpm build && pnpm publish",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-lab/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "pnpm build && pnpm publish",
Expand Down
6 changes: 3 additions & 3 deletions packages/mui-material-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-material/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "pnpm build && pnpm publish",
Expand Down
13 changes: 11 additions & 2 deletions packages/mui-material/src/styles/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export { ComponentsProps, ComponentsPropsList } from './props';
export { ComponentsVariants } from './variants';
export { ComponentsOverrides, ComponentNameToClassKey } from './overrides';
export { Components } from './components';
export { getUnit as unstable_getUnit, toUnitless as unstable_toUnitless } from './cssUtils';
export {
getUnit as unstable_getUnit,
toUnitless as unstable_toUnitless,
convertLength as unstable_convertLength,
} from './cssUtils';

export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>;

Expand All @@ -102,7 +106,12 @@ export { default as makeStyles } from './makeStyles';
export { default as withStyles } from './withStyles';
export { default as withTheme } from './withTheme';

export * from './ThemeProviderWithVars';
export {
useColorScheme,
getInitColorSchemeScript,
CssVarsProvider,
Experimental_CssVarsProvider,
} from './ThemeProviderWithVars';

export { default as extendTheme } from './createThemeWithVars';

Expand Down
13 changes: 11 additions & 2 deletions packages/mui-material/src/styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export function experimental_sx() {
export { default as createTheme, createMuiTheme } from './createTheme';
export { default as unstable_createMuiStrictModeTheme } from './createMuiStrictModeTheme';
export { default as createStyles } from './createStyles';
export { getUnit as unstable_getUnit, toUnitless as unstable_toUnitless } from './cssUtils';
export {
getUnit as unstable_getUnit,
toUnitless as unstable_toUnitless,
convertLength as unstable_convertLength,
} from './cssUtils';
export { default as responsiveFontSizes } from './responsiveFontSizes';
export { default as createTransitions, duration, easing } from './createTransitions';
export { default as createColorScheme } from './createColorScheme';
Expand All @@ -45,7 +49,12 @@ export { default as makeStyles } from './makeStyles';
export { default as withStyles } from './withStyles';
export { default as withTheme } from './withTheme';

export * from './ThemeProviderWithVars';
export {
useColorScheme,
getInitColorSchemeScript,
CssVarsProvider,
Experimental_CssVarsProvider,
} from './ThemeProviderWithVars';
export { default as extendTheme } from './createThemeWithVars';
export { default as experimental_extendTheme } from './experimental_extendTheme'; // TODO: Remove in v7
export { default as getOverlayAlpha } from './getOverlayAlpha';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use client';
/* eslint-disable no-constant-condition */
import * as React from 'react';
import {
Expand Down
1 change: 0 additions & 1 deletion packages/mui-material/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use client';
import { unstable_ClassNameGenerator as ClassNameGenerator } from '@mui/utils';

export { default as capitalize } from './capitalize';
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-private-theming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"prebuild": "rimraf build",
"release": "pnpm build && pnpm publish",
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/mui-private-theming/**/*.test.{js,ts,tsx}'",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-styled-engine-sc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"prebuild": "rimraf build",
"release": "pnpm build && pnpm publish",
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/mui-styled-engine-sc/**/*.test.{js,ts,tsx}'",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-styled-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"prebuild": "rimraf build",
"release": "pnpm build && pnpm publish",
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/mui-styled-engine/**/*.test.{js,ts,tsx}'",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-styles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"prebuild": "rimraf build",
"release": "pnpm build && pnpm publish",
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/mui-styles/**/*.test.{js,ts,tsx}'",
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "pnpm build && pnpm publish",
Expand Down
5 changes: 5 additions & 0 deletions packages/mui-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@
"@types/react": {
"optional": true
}
},
"exports": {
".": {
"types": "./index.d.ts"
}
}
}
8 changes: 4 additions & 4 deletions packages/mui-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
},
"scripts": {
"build": "pnpm build:modern && pnpm build:node && pnpm build:stable && pnpm build:types && pnpm build:copy-files",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"build:modern": "node ../../scripts/build.mjs modern --exportFormat exports",
"build:node": "node ../../scripts/build.mjs node --exportFormat exports",
"build:stable": "node ../../scripts/build.mjs stable --exportFormat exports",
"build:copy-files": "node ../../scripts/copyFiles.mjs --exportFormat exports",
"build:types": "node ../../scripts/buildTypes.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "pnpm build && pnpm publish",
Expand Down
1 change: 1 addition & 0 deletions packages/mui-utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types';
File renamed without changes.
13 changes: 12 additions & 1 deletion scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const validBundles = [
];

async function run(argv) {
const { bundle, largeFiles, outDir: relativeOutDir, verbose } = argv;
const { bundle, exportFormat, largeFiles, outDir: relativeOutDir, verbose } = argv;

if (validBundles.indexOf(bundle) === -1) {
throw new TypeError(
Expand All @@ -29,6 +29,7 @@ async function run(argv) {
NODE_ENV: 'production',
BABEL_ENV: bundle,
MUI_BUILD_VERBOSE: verbose,
MUI_ADD_IMPORT_EXTENSIONS: exportFormat === 'exports',
...(await getVersionEnvVariables()),
};

Expand Down Expand Up @@ -84,6 +85,10 @@ async function run(argv) {
babelArgs.push('--compact false');
}

if (exportFormat === 'exports' && bundle !== 'node') {
babelArgs.push('--out-file-extension', '.mjs');
}

const command = ['pnpm babel', ...babelArgs].join(' ');

if (verbose) {
Expand Down Expand Up @@ -112,6 +117,12 @@ yargs(process.argv.slice(2))
description: `Valid bundles: "${validBundles.join('" | "')}"`,
type: 'string',
})
.option('exportFormat', {
type: 'string',
options: ['exports', 'legacy'],
default: 'legacy',
describe: 'Set to `exports` to build the package with the `exports` field.',
})
.option('largeFiles', {
type: 'boolean',
default: false,
Expand Down
Loading
Loading