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] Only define aliases once for TypeScript and once for Webpack + Babel #35197

Closed
wants to merge 17 commits into from
Closed
40 changes: 7 additions & 33 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
const path = require('path');
const getMuiAliases = require('./scripts/muiAliases');

const errorCodesPath = path.resolve(__dirname, './docs/public/static/error-codes.json');
const missingError = process.env.MUI_EXTRACT_ERROR_CODES === 'true' ? 'write' : 'annotate';

function resolveAliasPath(relativeToBabelConf) {
const resolvedPath = path.relative(process.cwd(), path.resolve(__dirname, relativeToBabelConf));
return `./${resolvedPath.replace('\\', '/')}`;
}

const productionPlugins = [
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
];

module.exports = function getBabelConfig(api) {
const useESModules = api.env(['regressions', 'legacy', 'modern', 'stable', 'rollup']);

const defaultAlias = {
'@mui/material': resolveAliasPath('./packages/mui-material/src'),
'@mui/docs': resolveAliasPath('./packages/mui-docs/src'),
'@mui/icons-material': resolveAliasPath(
`./packages/mui-icons-material/lib${useESModules ? '/esm' : ''}`,
),
'@mui/lab': resolveAliasPath('./packages/mui-lab/src'),
'@mui/markdown': resolveAliasPath('./packages/markdown'),
'@mui/styled-engine': resolveAliasPath('./packages/mui-styled-engine/src'),
'@mui/styled-engine-sc': resolveAliasPath('./packages/mui-styled-engine-sc/src'),
'@mui/styles': resolveAliasPath('./packages/mui-styles/src'),
'@mui/system': resolveAliasPath('./packages/mui-system/src'),
'@mui/private-theming': resolveAliasPath('./packages/mui-private-theming/src'),
'@mui/base': resolveAliasPath('./packages/mui-base/src'),
'@mui/utils': resolveAliasPath('./packages/mui-utils/src'),
'@mui/material-next': resolveAliasPath('./packages/mui-material-next/src'),
'@mui/joy': resolveAliasPath('./packages/mui-joy/src'),
};
const muiAliases = getMuiAliases({ type: 'src', isRelative: true, useESIcons: useESModules });

const presets = [
[
Expand Down Expand Up @@ -94,8 +72,8 @@ module.exports = function getBabelConfig(api) {
plugins.push([
'babel-plugin-module-resolver',
{
alias: defaultAlias,
root: ['./'],
alias: muiAliases,
},
]);
}
Expand All @@ -121,7 +99,7 @@ module.exports = function getBabelConfig(api) {
'babel-plugin-module-resolver',
{
root: ['./'],
alias: defaultAlias,
alias: muiAliases,
},
],
],
Expand All @@ -131,12 +109,8 @@ module.exports = function getBabelConfig(api) {
[
'babel-plugin-module-resolver',
{
alias: {
...defaultAlias,
modules: './modules',
'typescript-to-proptypes': './packages/typescript-to-proptypes/src',
},
root: ['./'],
alias: muiAliases,
},
],
],
Expand All @@ -154,7 +128,7 @@ module.exports = function getBabelConfig(api) {
'babel-plugin-module-resolver',
{
root: ['./'],
alias: defaultAlias,
alias: muiAliases,
},
],
],
Expand All @@ -165,7 +139,7 @@ module.exports = function getBabelConfig(api) {
[
'babel-plugin-module-resolver',
{
alias: defaultAlias,
alias: muiAliases,
},
],
],
Expand Down
28 changes: 0 additions & 28 deletions docs/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@ const fse = require('fs-extra');

const errorCodesPath = path.resolve(__dirname, './public/static/error-codes.json');

const alias = {
'@mui/material': '../packages/mui-material/src',
'@mui/docs': '../packages/mui-docs/src',
'@mui/icons-material': '../packages/mui-icons-material/lib',
'@mui/lab': '../packages/mui-lab/src',
'@mui/styles': '../packages/mui-styles/src',
'@mui/styled-engine-sc': '../packages/mui-styled-engine-sc/src',
// Swap the comments on the next two lines for using the styled-components as style engine
'@mui/styled-engine': '../packages/mui-styled-engine/src',
// '@mui/styled-engine': '../packages/mui-styled-engine-sc/src',
'@mui/system': '../packages/mui-system/src',
'@mui/private-theming': '../packages/mui-private-theming/src',
'@mui/utils': '../packages/mui-utils/src',
'@mui/base': '../packages/mui-base/src',
'@mui/material-next': '../packages/mui-material-next/src',
'@mui/joy': '../packages/mui-joy/src',
docs: './',
modules: '../modules',
pages: './pages',
};

const { version: transformRuntimeVersion } = fse.readJSONSync(
require.resolve('@babel/runtime-corejs2/package.json'),
);
Expand Down Expand Up @@ -55,13 +34,6 @@ module.exports = {
'babel-plugin-optimize-clsx',
// for IE11 support
'@babel/plugin-transform-object-assign',
[
Copy link
Member Author

Choose a reason for hiding this comment

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

'babel-plugin-module-resolver',
{
alias,
transformFunctions: ['require', 'require.context'],
},
],
],
ignore: [/@babel[\\|/]runtime/], // Fix a Windows issue.
env: {
Expand Down
20 changes: 3 additions & 17 deletions docs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const pkg = require('../package.json');
const withDocsInfra = require('./nextConfigDocsInfra');
const { findPages } = require('./src/modules/utils/find');
const getMuiAliases = require('../scripts/muiAliases');
const {
LANGUAGES,
LANGUAGES_SSR,
Expand Down Expand Up @@ -115,7 +116,7 @@ module.exports = withDocsInfra({
},
],
},
// transpile 3rd party packages with dependencies in this repository
// Transpile dependencies outside this repository with dependencies in this repository
{
test: /\.(js|mjs|jsx)$/,
resourceQuery: { not: [/raw/] },
Expand All @@ -131,22 +132,7 @@ module.exports = withDocsInfra({
[
'babel-plugin-module-resolver',
{
alias: {
// all packages in this monorepo
'@mui/material': '../packages/mui-material/src',
'@mui/docs': '../packages/mui-docs/src',
'@mui/icons-material': '../packages/mui-icons-material/lib',
'@mui/lab': '../packages/mui-lab/src',
'@mui/styled-engine': '../packages/mui-styled-engine/src',
'@mui/styles': '../packages/mui-styles/src',
'@mui/system': '../packages/mui-system/src',
'@mui/private-theming': '../packages/mui-private-theming/src',
'@mui/utils': '../packages/mui-utils/src',
'@mui/base': '../packages/mui-base/src',
'@mui/material-next': '../packages/mui-material-next/src',
'@mui/joy': '../packages/mui-joy/src',
},
// transformFunctions: ['require'],
alias: getMuiAliases({ type: 'src' }),
},
],
],
Expand Down
1 change: 0 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@react-spring/web": "^9.7.3",
"autoprefixer": "^10.4.15",
"autosuggest-highlight": "^3.3.4",
"babel-plugin-module-resolver": "^5.0.0",
"babel-plugin-optimize-clsx": "^2.6.2",
"babel-plugin-react-remove-properties": "^0.3.0",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
Expand Down
7 changes: 3 additions & 4 deletions examples/base-ui-cra-ts/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ export default function App() {
<div className="box">
<h1>Base UI + Create React App scaffold (TypeScript)</h1>
<div className="item">
<a href="https://mui.com/base-ui/">Base UI</a> is a library of
unstyled React UI components which includes prebuilt components with production-ready
functionality, along with low-level hooks for transferring that functionality to other
components.
<a href="https://mui.com/base-ui/">Base UI</a> is a library of unstyled React UI components
which includes prebuilt components with production-ready functionality, along with low-level
hooks for transferring that functionality to other components.
</div>
<div className="item">
<a href="https://create-react-app.dev/">Create React App</a> is a framework for quickly
Expand Down
5 changes: 3 additions & 2 deletions packages/mui-joy/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"include": ["./src/**/*.ts*"],
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"],
"references": [
{ "path": "../mui-base/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" }
{ "path": "../mui-utils/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" },
{ "path": "../mui-base/tsconfig.build.json" }
]
}
14 changes: 8 additions & 6 deletions packages/mui-lab/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
// Actual .ts source files are transpiled via babel
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"composite": true,
"declaration": true,
"rootDir": "./src",
"outDir": "./build",
"emitDeclarationOnly": true
"noEmit": false,
"emitDeclarationOnly": true,
"outDir": "build",
"rootDir": "./src"
},
"include": ["src/**/*.ts*"],
"exclude": ["src/**/*.d.ts", "src/**/*.test.*", "./**/*.spec.*"],
"references": [
{ "path": "../mui-material/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" }
{ "path": "../mui-utils/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" },
{ "path": "../mui-material/tsconfig.build.json" }
]
}
4 changes: 2 additions & 2 deletions packages/mui-material-next/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"include": ["./src/**/*.ts*"],
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"],
"references": [
{ "path": "../mui-utils/tsconfig.build.json" },
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to fix the references in all the packages, otherwise TS was not able to resolve correctly the imports.
Not sure why it is impacted by the new structure

{ "path": "../mui-base/tsconfig.build.json" },
{ "path": "../mui-material/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" }
{ "path": "../mui-material/tsconfig.build.json" }
]
}
5 changes: 3 additions & 2 deletions packages/mui-material/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"include": ["./src/**/*.ts*"],
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"],
"references": [
{ "path": "../mui-base/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" }
{ "path": "../mui-utils/tsconfig.build.json" },
{ "path": "../mui-system/tsconfig.build.json" },
{ "path": "../mui-base/tsconfig.build.json" }
]
}
46 changes: 46 additions & 0 deletions scripts/muiAliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const path = require('path');

/**
* @param {object} params The parameters of the function.
* @param {'build' | 'src'} params.type Define if the target should be the built version or the source.
* @param {boolean | undefined} params.isRelative If `true` the path will be relative to the repository root.
* @param {boolean | undefined} params.useESIcons If `true` will use the ES version of `@mui/icons-material`.
*/
function getMuiAliases({ type, isRelative = false, useESIcons = false }) {
const workspaceRoot = path.join(__dirname, '..');

const resolveAliasPath = (aliasPath) => {
const fullPath = path.join(workspaceRoot, aliasPath);

if (!isRelative) {
return fullPath;
}

const resolvedPath = path.relative(process.cwd(), fullPath);
return `./${resolvedPath.replace('\\', '/')}`;
};

return {
'@mui/base': resolveAliasPath(`packages/mui-base/${type}`),
'@mui/docs': resolveAliasPath(`packages/mui-docs/${type}`),
'@mui/icons-material': resolveAliasPath(
`packages/mui-icons-material/lib${useESIcons ? '/esm' : ''}`,
),
'@mui/joy': resolveAliasPath(`packages/mui-joy/${type}`),
'@mui/lab': resolveAliasPath(`packages/mui-lab/${type}`),
'@mui/markdown': resolveAliasPath('packages/markdown'),
'@mui/material': resolveAliasPath(`packages/mui-material/${type}`),
'@mui/material-next': resolveAliasPath(`packages/mui-material-next/${type}`),
'@mui/private-theming': resolveAliasPath(`packages/mui-private-theming/${type}`),
'@mui/styled-engine': resolveAliasPath(`packages/mui-styled-engine/${type}`),
'@mui/styled-engine-sc': resolveAliasPath(`packages/mui-styled-engine-sc/${type}`),
'@mui/styles': resolveAliasPath(`packages/mui-styles/${type}`),
'@mui/system': resolveAliasPath(`packages/mui-system/${type}`),
'@mui/utils': resolveAliasPath(`packages/mui-utils/${type}`),
docs: resolveAliasPath('docs'),
modules: resolveAliasPath('modules'),
'typescript-to-proptypes': resolveAliasPath('packages/typescript-to-proptypes/src'),
};
}

module.exports = getMuiAliases;
17 changes: 3 additions & 14 deletions scripts/sizeSnapshot/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const CompressionPlugin = require('compression-webpack-plugin');
const glob = require('fast-glob');
const TerserPlugin = require('terser-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const getMuiAliases = require('../muiAliases');

const workspaceRoot = path.join(__dirname, '..', '..');

Expand Down Expand Up @@ -175,7 +176,7 @@ function createWebpackConfig(entry, environment) {
const configuration = {
// ideally this would be computed from the bundles peer dependencies
// Ensure that `react` as well as `react/*` are considered externals but not `react*`
externals: /^(date-fns|dayjs|luxon|moment|react|react-dom)(\/.*)?$/,
externals: /^(react|react-dom)(\/.*)?$/,
mode: 'production',
optimization: {
concatenateModules,
Expand Down Expand Up @@ -208,19 +209,7 @@ function createWebpackConfig(entry, environment) {
}),
],
resolve: {
alias: {
'@mui/material': path.join(workspaceRoot, 'packages/mui-material/build'),
'@mui/lab': path.join(workspaceRoot, 'packages/mui-lab/build'),
'@mui/styled-engine': path.join(workspaceRoot, 'packages/mui-styled-engine/build'),
'@mui/styled-engine-sc': path.join(workspaceRoot, 'packages/mui-styles-sc/build'),
'@mui/styles': path.join(workspaceRoot, 'packages/mui-styles/build'),
'@mui/system': path.join(workspaceRoot, 'packages/mui-system/build'),
'@mui/private-theming': path.join(workspaceRoot, 'packages/mui-private-theming/build'),
'@mui/utils': path.join(workspaceRoot, 'packages/mui-utils/build'),
'@mui/base': path.join(workspaceRoot, 'packages/mui-base/build'),
'@mui/material-next': path.join(workspaceRoot, 'packages/mui-material-next/build'),
'@mui/joy': path.join(workspaceRoot, 'packages/mui-joy/build'),
},
alias: getMuiAliases({ type: 'build' }),
},
entry: { [entry.id]: path.join(workspaceRoot, entry.path) },
// TODO: 'browserslist:modern'
Expand Down
27 changes: 7 additions & 20 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const playwright = require('playwright');
const webpack = require('webpack');
const getMuiAliases = require('../scripts/muiAliases');

const CI = Boolean(process.env.CI);
// renovate PRs are based off of upstream branches.
Expand Down Expand Up @@ -133,36 +134,22 @@ module.exports = function setKarmaConfig(config) {
envName: 'stable',
},
},
// transpile 3rd party packages with dependencies in this repository
Copy link
Member Author

Choose a reason for hiding this comment

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

The tests are passing without it
I think it is not needed since we don't have pickers tests on the core anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

We need it again for the tree view 😆

// Transpile dependencies outside this repository with dependencies in this repository
// TODO: Remove when the lab will stop exporting components from `@mui/x-tree-view`
{
test: /\.(js|mjs|jsx)$/,
include:
/node_modules(\/|\\)(notistack|@mui(\/|\\)x-data-grid|@mui(\/|\\)x-data-grid-pro|@mui(\/|\\)x-license-pro|@mui(\/|\\)x-data-grid-generator|@mui(\/|\\)x-date-pickers-pro|@mui(\/|\\)x-date-pickers|@mui(\/|\\)x-tree-view)/,
include: /node_modules(\/|\\)@mui(\/|\\)x-tree-view/,
use: {
Copy link
Member

@oliviertassinari oliviertassinari Sep 22, 2023

Choose a reason for hiding this comment

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

We miss notistack here no? https://mui.com/material-ui/react-snackbar/#notistack.

Ah, same for data grid, and all MUI X components no?

This logic is meant to help us test fixes for these components, make sure if we break something in Material UI for MUI X, we know it as soon as we work on the PR, not once Material UI is released and MUI X updates it dependencies.

Copy link
Member Author

Choose a reason for hiding this comment

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

This logic is meant to help us test fixes for these components, make sure if we break something in Material UI for MUI X, we know it as soon as we work on the PR

But we don't have any test using those components right?
If the tests pass without the alias.
I can add the alias back, but it will never be used.

loader: 'babel-loader',
options: {
// We have to apply `babel-plugin-module-resolve` to the files in `@mui/x-date-pickers`.
// Otherwise we can't import `@mui/material` from `@mui/x-date-pickers` in `yarn test:karma`.
// We have to apply `babel-plugin-module-resolve` to the files in `@mui/x-tree-view`.
// Otherwise, we can't import `@mui/material` from `@mui/x-tree-view` in `yarn test:karma`.
sourceType: 'unambiguous',
plugins: [
[
'babel-plugin-module-resolver',
{
alias: {
// all packages in this monorepo
'@mui/material': './packages/mui-material/src',
'@mui/docs': './packages/mui-docs/src',
'@mui/icons-material': './packages/mui-icons-material/lib',
'@mui/lab': './packages/mui-lab/src',
'@mui/styled-engine': './packages/mui-styled-engine/src',
'@mui/styles': './packages/mui-styles/src',
'@mui/system': './packages/mui-system/src',
'@mui/private-theming': './packages/mui-private-theming/src',
'@mui/utils': './packages/mui-utils/src',
'@mui/base': './packages/mui-base/src',
'@mui/material-next': './packages/mui-material-next/src',
'@mui/joy': './packages/mui-joy/src',
},
alias: getMuiAliases({ type: 'src', isRelative: true }),
transformFunctions: ['require'],
},
],
Expand Down
Loading