forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbabel.config.mjs
More file actions
127 lines (118 loc) · 3.57 KB
/
Copy pathbabel.config.mjs
File metadata and controls
127 lines (118 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// @ts-check
import { fileURLToPath } from 'node:url';
import * as path from 'node:path';
// @ts-ignore
import getBaseConfig from '@mui/internal-code-infra/babel-config';
/**
* @typedef {import('@babel/core')} babel
*/
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const errorCodesPath = path.resolve(dirname, './docs/public/static/error-codes.json');
/**
* @param {string} relativeToBabelConf
* @returns {string}
*/
function resolveAliasPath(relativeToBabelConf) {
const resolvedPath = path.relative(process.cwd(), path.resolve(dirname, relativeToBabelConf));
return `./${resolvedPath.replace('\\', '/')}`;
}
/** @type {babel.ConfigFunction} */
export default function getBabelConfig(api) {
const baseConfig = getBaseConfig(api);
const useESModules = api.env(['regressions', 'stable']);
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/internal-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/system': resolveAliasPath('./packages/mui-system/src'),
'@mui/private-theming': resolveAliasPath('./packages/mui-private-theming/src'),
'@mui/utils': resolveAliasPath('./packages/mui-utils/src'),
'@mui/joy': resolveAliasPath('./packages/mui-joy/src'),
'@mui/internal-docs-utils': resolveAliasPath('./packages-internal/docs-utils/src'),
'@mui/internal-test-utils': resolveAliasPath('./packages-internal/test-utils/src'),
docs: resolveAliasPath('./docs'),
test: resolveAliasPath('./test'),
};
/** @type {babel.PluginItem[]} */
const plugins = [
[
'@mui/internal-babel-plugin-minify-errors',
{
missingError: 'annotate',
errorCodesPath,
runtimeModule: '@mui/utils/formatMuiErrorMessage',
},
],
];
if (process.env.NODE_ENV === 'test') {
plugins.push([
'babel-plugin-module-resolver',
{
alias: defaultAlias,
root: ['./'],
},
]);
}
const basePlugins = (baseConfig.plugins || []).filter(
(/** @type {[unknown, unknown, string]} */ [, , pluginName]) =>
pluginName !== '@mui/internal-babel-plugin-display-name',
);
basePlugins.push(...plugins);
return {
...baseConfig,
plugins: basePlugins,
overrides: [
{
exclude: /\.test\.(m?js|ts|tsx)$/,
plugins: ['@babel/plugin-transform-react-constant-elements'],
},
],
env: {
coverage: {
plugins: [
'babel-plugin-istanbul',
[
'babel-plugin-module-resolver',
{
root: ['./'],
alias: defaultAlias,
},
],
],
},
development: {
plugins: [
[
'babel-plugin-module-resolver',
{
alias: {
...defaultAlias,
modules: './modules',
},
root: ['./'],
},
],
],
},
test: {
sourceMaps: 'both',
plugins: [
[
'babel-plugin-module-resolver',
{
root: ['./'],
alias: defaultAlias,
},
],
],
},
},
};
}