Skip to content

Commit ce3386d

Browse files
authored
[code-infra] Allow specifying a custom error formatter module for error minifcation (mui#45291)
1 parent 427c9bd commit ce3386d

File tree

27 files changed

+382
-137
lines changed

27 files changed

+382
-137
lines changed

.codesandbox/ci.json

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"packages/mui-system",
2121
"packages/mui-types",
2222
"packages/mui-utils",
23+
"packages-internal/babel-plugin-minify-errors",
2324
"packages-internal/babel-plugin-resolve-imports",
2425
"packages-internal/docs-utils",
2526
"packages-internal/scripts",
@@ -35,6 +36,7 @@
3536
"@mui/internal-docs-utils": "packages-internal/docs-utils",
3637
"@mui/internal-markdown": "packages/markdown",
3738
"@mui/internal-scripts": "packages-internal/scripts",
39+
"@mui/internal-babel-plugin-minify-errors": "packages-internal/babel-plugin-minify-errors",
3840
"@mui/internal-babel-plugin-resolve-imports": "packages-internal/babel-plugin-resolve-imports",
3941
"@mui/lab": "packages/mui-lab/build",
4042
"@mui/material-nextjs": "packages/mui-material-nextjs/build",

babel.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module.exports = function getBabelConfig(api) {
106106
{
107107
missingError,
108108
errorCodesPath,
109+
runtimeModule: '@mui/utils/formatMuiErrorMessage',
109110
},
110111
],
111112
...(useESModules

dangerfile.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
// eslint-disable-next-line import/no-unresolved
2+
const tsx = require('tsx/cjs/api');
3+
14
// danger uses babelify under the hood. The implementation is buggy and fails on our
25
// custom plugins in our codebase. We'll just disable it and do our own compilation.
36
// Danger must always be run with envirnonent variable `DANGER_DISABLE_TRANSPILATION="true"`.
4-
require('@babel/register')({
5-
extensions: ['.js', '.ts', '.tsx'],
6-
});
7-
require('./dangerFileContent');
7+
tsx.require('./dangerFileContent', __filename);

packages-internal/babel-plugin-minify-errors/__fixtures__/.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ module.exports = {
88
// Babel import helpers do not add one.
99
// Since this is auto generated code we don't care about code style.
1010
'import/newline-after-import': 'off',
11+
'import/no-unresolved': 'off',
12+
'@typescript-eslint/no-unused-vars': 'off',
13+
'no-undef': 'off',
1114
},
1215
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"42": "MUI: This is a test error message.\nWith a second line."
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw /* minify-error */ new Error('MUI: This is a test error message.\n' + 'With a second line.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import _formatErrorMessage from '@custom/error-formatter';
2+
throw new Error(
3+
process.env.NODE_ENV !== 'production'
4+
? 'MUI: This is a test error message.\n' + 'With a second line.'
5+
: _formatErrorMessage(42),
6+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"imports": {
3+
"#error-formatter": "#error-formatter-2",
4+
"#error-formatter-2": "#error-formatter-3",
5+
"#error-formatter-3": "@custom/error-formatter"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"42": "MUI: This is a test error message.\nWith a second line."
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function formatter() {
2+
return 'Hello world';
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw /* minify-error */ new Error('MUI: This is a test error message.\n' + 'With a second line.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import _formatErrorMessage from './error/formatter.js';
2+
throw new Error(
3+
process.env.NODE_ENV !== 'production'
4+
? 'MUI: This is a test error message.\n' + 'With a second line.'
5+
: _formatErrorMessage(42),
6+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"#error-formatter": "./error/formatter.ts"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"42": "MUI: This is a test error message.\nWith a second line."
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw /* minify-error */ new Error('MUI: This is a test error message.\n' + 'With a second line.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import _formatErrorMessage from '@custom/error-formatter';
2+
throw new Error(
3+
process.env.NODE_ENV !== 'production'
4+
? 'MUI: This is a test error message.\n' + 'With a second line.'
5+
: _formatErrorMessage(42),
6+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"#error-formatter": "@custom/error-formatter"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"42": "MUI: This is a test error message.\nWith a second line."
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw /* minify-error */ new Error('MUI: This is a test error message.\n' + 'With a second line.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import _formatErrorMessage from '@custom/error-formatter';
2+
throw new Error(
3+
process.env.NODE_ENV !== 'production'
4+
? 'MUI: This is a test error message.\n' + 'With a second line.'
5+
: _formatErrorMessage(42),
6+
);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import _formatMuiErrorMessage from '@mui/utils/formatMuiErrorMessage';
2-
throw new Error(process.env.NODE_ENV !== 'production' ? 'exists' : _formatMuiErrorMessage(1));
3-
throw new Error(
4-
process.env.NODE_ENV !== 'production' ? 'will be created' : _formatMuiErrorMessage(2),
5-
);
1+
import _formatErrorMessage from '@mui/utils/formatMuiErrorMessage';
2+
throw new Error(process.env.NODE_ENV !== 'production' ? 'exists' : _formatErrorMessage(1));
3+
throw new Error(process.env.NODE_ENV !== 'production' ? 'will be created' : _formatErrorMessage(2));
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import _formatMuiErrorMessage from '@mui/utils/formatMuiErrorMessage';
1+
import _formatErrorMessage from '@mui/utils/formatMuiErrorMessage';
22
const foo = 'foo';
33
const bar = 'bar';
44
throw new Error(
5-
process.env.NODE_ENV !== 'production'
6-
? `MUI: ${foo}, ${bar}`
7-
: _formatMuiErrorMessage(1, foo, bar),
5+
process.env.NODE_ENV !== 'production' ? `MUI: ${foo}, ${bar}` : _formatErrorMessage(1, foo, bar),
86
);
97
throw new Error(
108
process.env.NODE_ENV !== 'production'
119
? `MUI: ${foo}` + `, ${bar}`
12-
: _formatMuiErrorMessage(1, foo, bar),
10+
: _formatErrorMessage(1, foo, bar),
1311
);
1412
throw new Error(
1513
process.env.NODE_ENV !== 'production'
1614
? 'MUI: ' + `${foo}, ${bar}`
17-
: _formatMuiErrorMessage(1, foo, bar),
15+
: _formatErrorMessage(1, foo, bar),
1816
);
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import _formatMuiErrorMessage from '@mui/utils/formatMuiErrorMessage';
1+
import _formatErrorMessage from '@mui/utils/formatMuiErrorMessage';
22
throw new Error(
33
process.env.NODE_ENV !== 'production'
44
? 'MUI: Expected valid input target.\n' + 'Did you use `inputComponent`'
5-
: _formatMuiErrorMessage(1),
5+
: _formatErrorMessage(1),
66
);
77
throw new Error(
88
process.env.NODE_ENV !== 'production'
99
? `MUI: Expected valid input target.\n` + `Did you use \`inputComponent\``
10-
: _formatMuiErrorMessage(1),
10+
: _formatErrorMessage(1),
1111
);

0 commit comments

Comments
 (0)