Skip to content

Commit 31fe07e

Browse files
committed
make path human readable and fallback to show error if regex not matched
1 parent b53f903 commit 31fe07e

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

packages/react-dev-utils/formatBuildError.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,32 @@ const chalk = require('chalk');
1414

1515
module.exports = function formatBuildError(err) {
1616
const message = get(err, 'message');
17+
const stack = get(err, 'stack');
1718

1819
// Add more helpful message for UglifyJs error
19-
if (typeof message === 'string' && message.indexOf('from UglifyJs') !== -1) {
20+
if (
21+
stack &&
22+
typeof message === 'string' &&
23+
message.indexOf('from UglifyJs') !== -1
24+
) {
2025
try {
26+
const matched = /Unexpected token:(.+)\[(.+)\:(.+)\,(.+)\]\[.+\]/.exec(
27+
stack
28+
);
29+
if (!matched) {
30+
throw new Error(
31+
"The regex pattern is not matched. Maybe UglifyJs changed it's message?"
32+
);
33+
}
34+
const problemPath = matched[2];
35+
const line = matched[3];
36+
const column = matched[4];
2137
console.log(
22-
'Failed to minify the code from \n\n',
23-
chalk.yellow(
24-
/Unexpected token:(.+)\[(.+)\]\[(.+)\]/.exec(err.stack)[2]
25-
),
38+
'Failed to minify the code from this file: \n\n',
39+
chalk.yellow(`${problemPath} line ${line}:${column}`),
2640
'\n'
2741
);
28-
} catch (e) {
42+
} catch (ignored) {
2943
console.log('Failed to minify the code.', err);
3044
}
3145
console.log(

0 commit comments

Comments
 (0)