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
36 changes: 7 additions & 29 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
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 defaultAlias = {
'@mui/material': resolveAliasPath('./packages/mui-material/src'),
'@mui/docs': resolveAliasPath('./packages/mui-docs/src'),
'@mui/icons-material': resolveAliasPath('./packages/mui-icons-material/lib'),
'@mui/lab': resolveAliasPath('./packages/mui-lab/src'),
'@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('src', true);

const productionPlugins = [
['babel-plugin-react-remove-properties', { properties: ['data-mui-test'] }],
Expand Down Expand Up @@ -91,8 +73,8 @@ module.exports = function getBabelConfig(api) {
plugins.push([
'babel-plugin-module-resolver',
{
alias: defaultAlias,
root: ['./'],
alias: muiAliases,
},
]);
}
Expand All @@ -118,7 +100,7 @@ module.exports = function getBabelConfig(api) {
'babel-plugin-module-resolver',
{
root: ['./'],
alias: defaultAlias,
alias: muiAliases,
},
],
],
Expand All @@ -128,12 +110,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 @@ -151,7 +129,7 @@ module.exports = function getBabelConfig(api) {
'babel-plugin-module-resolver',
{
root: ['./'],
alias: defaultAlias,
alias: muiAliases,
},
],
],
Expand All @@ -162,7 +140,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 path = require('path');

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 @@ -4,6 +4,7 @@ const pkg = require('../package.json');
const withDocsInfra = require('./nextConfigDocsInfra');
const { findPages } = require('./src/modules/utils/find');
const { LANGUAGES, LANGUAGES_SSR, LANGUAGES_IGNORE_PAGES } = require('./src/modules/constants');
const getMuiAliases = require('../scripts/muiAliases');

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

Expand Down Expand Up @@ -95,7 +96,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 @@ -111,22 +112,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('src'),
Copy link
Member

@Janpot Janpot Nov 22, 2022

Choose a reason for hiding this comment

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

Now that aliases moved to webpack (through typescript paths), is this aliasing still necessary?

Copy link
Member

@Janpot Janpot Nov 22, 2022

Choose a reason for hiding this comment

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

Yes it seems. In Toolpad we don't use typescript paths, but do aliasing directly in webpack. I believe then it gets picked up under node_modules as well and this babel plugin should become unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

For the core these aliases are needed to make sure that when we use X components on the marketing werbsite, the @mui/material (& co) imports from @mui/x-date-pickers / @mui/x-data-grid are using the local one and not the one from their node modules (which causes issues).

But if we have a way to say to Next to also resolve some node modules with the TS aliases, that would be great

Copy link
Member

Choose a reason for hiding this comment

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

But if we have a way to say to Next to also resolve some node modules with the TS aliases, that would be great

Not with TS aliases I believe. But with webpack aliases it seems to work that way

Copy link
Member Author

Choose a reason for hiding this comment

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

But we don't have any webpack alias defined no ?

Copy link
Member

@Janpot Janpot Nov 22, 2022

Choose a reason for hiding this comment

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

We probably won't be able to get rid of the typescript paths soon as it will be necessary for vscode. But I'd already consider it a win if we can remove them from babel everywhere. Even if that means moving it to webpack, at least it would be where it makes sense.

The real solution is to update our build process in a way that we can rely on yarn workspace linking to resolve local packages. That way we can get rid of all aliasing. There's other obstacles though that currently block us from going there (mainly the build folders that contain the published packages with their subpath exports).

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 struggle to apply the webpack aliases on the node_modules, if you have an idea on how to do it (to be able to drop babel which is necessary for notistack and the X deps)

Copy link
Member

Choose a reason for hiding this comment

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

I updated #33199 and it still seems to work. Check out the changes to next.config.js without whitespace: https://github.com/mui/material-ui/pull/33199/files?diff=split&w=1#diff-97081d4c5e67d7fb92c87085affbac02322e2b4c53c01c5e65f500fc3b67a402

and babel.config.js: https://github.com/mui/material-ui/pull/33199/files?diff=split&w=1#diff-2ed4f5b03d34a87ef641e9e36af4a98a1c0ddaf74d07ce93665957be69b7b09a

Will give a quick try if I can replace next-transpile-modules with the upcoming built-in transpilePackages

Copy link
Member Author

Choose a reason for hiding this comment

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

OK but I guess it will only works with the doc, we will still have to keep babel for the testing.
That's still a win for sure.

Copy link
Member

@Janpot Janpot Nov 23, 2022

Choose a reason for hiding this comment

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

Yes, sorry for the confusion, I'm only talking about the docs here. It's the only thing we have to deal with from Toolpad's perspective

},
],
],
Expand Down
1 change: 0 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"ast-types": "^0.14.2",
"autoprefixer": "^10.4.13",
"autosuggest-highlight": "^3.3.4",
"babel-plugin-module-resolver": "^4.1.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
3 changes: 2 additions & 1 deletion packages/mui-base/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"rootDir": "./src"
},
"include": ["src/**/*.ts*"],
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"]
"exclude": ["src/**/*.spec.ts*", "src/**/*.test.ts*"],
"references": [{ "path": "../mui-utils/tsconfig.build.json" }]
}
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.*", "**/test-utils.tsx"],
"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" }
]
}
3 changes: 2 additions & 1 deletion packages/mui-system/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"rootDir": "./src"
},
"include": ["src/**/*.ts*"],
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"]
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"],
"references": [{ "path": "../mui-utils/tsconfig.build.json" }]
}
4 changes: 4 additions & 0 deletions packages/mui-utils/src/integerPropType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Requireable } from 'prop-types';

declare const integerPropType: Requireable<number>;
Copy link
Member Author

Choose a reason for hiding this comment

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

TS was complaining that integerPropType had no typing file when I moved to composite build

export default integerPropType;
1 change: 1 addition & 0 deletions packages/mui-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Actual .ts source files are transpiled via babel
"extends": "./tsconfig",
"compilerOptions": {
"composite": true,
"declaration": true,
"noEmit": false,
"emitDeclarationOnly": true,
Expand Down
43 changes: 43 additions & 0 deletions scripts/muiAliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const path = require('path');

/**
* @param {'build' | 'src'} type Define if the target should be the built version or the source.
* @param {boolean} isRelative If `true` the path will be relative to the repository root.
*/
function getMuiAliases(type, isRelative = false) {
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
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/${type === 'src' ? 'lib' : 'build'}`,
),
'@mui/joy': resolveAliasPath(`packages/mui-joy/${type}`),
'@mui/lab': resolveAliasPath(`packages/mui-lab/${type}`),
'@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;
15 changes: 2 additions & 13 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 @@ -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('build'),
},
entry: { [entry.id]: path.join(workspaceRoot, entry.path) },
// TODO: 'browserslist:modern'
Expand Down
37 changes: 0 additions & 37 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,43 +133,6 @@ 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 😆

{
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)/,
use: {
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`.
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',
},
transformFunctions: ['require'],
},
],
],
},
},
},
{
test: /\.(js|mjs|ts|tsx)$/,
use: {
Expand Down
Loading