source: frontend/node_modules/@pmmmwh/react-refresh-webpack-plugin/client/utils/formatWebpackErrors.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/**
2 * @typedef {Object} WebpackErrorObj
3 * @property {string} moduleIdentifier
4 * @property {string} moduleName
5 * @property {string} message
6 */
7
8const friendlySyntaxErrorLabel = 'Syntax error:';
9
10/**
11 * Checks if the error message is for a syntax error.
12 * @param {string} message The raw Webpack error message.
13 * @returns {boolean} Whether the error message is for a syntax error.
14 */
15function isLikelyASyntaxError(message) {
16 return message.indexOf(friendlySyntaxErrorLabel) !== -1;
17}
18
19/**
20 * Cleans up Webpack error messages.
21 *
22 * This implementation is based on the one from [create-react-app](https://github.com/facebook/create-react-app/blob/edc671eeea6b7d26ac3f1eb2050e50f75cf9ad5d/packages/react-dev-utils/formatWebpackMessages.js).
23 * @param {string} message The raw Webpack error message.
24 * @returns {string} The formatted Webpack error message.
25 */
26function formatMessage(message) {
27 let lines = message.split('\n');
28
29 // Strip Webpack-added headers off errors/warnings
30 // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
31 lines = lines.filter(function (line) {
32 return !/Module [A-z ]+\(from/.test(line);
33 });
34
35 // Remove leading newline
36 if (lines.length > 2 && lines[1].trim() === '') {
37 lines.splice(1, 1);
38 }
39
40 // Remove duplicated newlines
41 lines = lines.filter(function (line, index, arr) {
42 return index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim();
43 });
44
45 // Clean up the file name
46 lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1');
47
48 // Cleans up verbose "module not found" messages for files and packages.
49 if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
50 lines = [
51 lines[0],
52 lines[1]
53 .replace('Error: ', '')
54 .replace('Module not found: Cannot find file:', 'Cannot find file:'),
55 ];
56 }
57
58 message = lines.join('\n');
59
60 // Clean up syntax errors
61 message = message.replace('SyntaxError:', friendlySyntaxErrorLabel);
62
63 // Internal stacks are generally useless, so we strip them -
64 // except the stacks containing `webpack:`,
65 // because they're normally from user code generated by webpack.
66 message = message.replace(/^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm, ''); // at ... ...:x:y
67 message = message.replace(/^\s*at\s((?!webpack:).)*<anonymous>[\s)]*(\n|$)/gm, ''); // at ... <anonymous>
68 message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous>
69
70 return message.trim();
71}
72
73/**
74 * Formats Webpack error messages into a more readable format.
75 * @param {Array<string | WebpackErrorObj>} errors An array of Webpack error messages.
76 * @returns {string[]} The formatted Webpack error messages.
77 */
78function formatWebpackErrors(errors) {
79 let formattedErrors = errors.map(function (errorObjOrMessage) {
80 // Webpack 5 compilation errors are in the form of descriptor objects,
81 // so we have to join pieces to get the format we want.
82 if (typeof errorObjOrMessage === 'object') {
83 return formatMessage([errorObjOrMessage.moduleName, errorObjOrMessage.message].join('\n'));
84 }
85 // Webpack 4 compilation errors are strings
86 return formatMessage(errorObjOrMessage);
87 });
88
89 if (formattedErrors.some(isLikelyASyntaxError)) {
90 // If there are any syntax errors, show just them.
91 formattedErrors = formattedErrors.filter(isLikelyASyntaxError);
92 }
93 return formattedErrors;
94}
95
96module.exports = formatWebpackErrors;
Note: See TracBrowser for help on using the repository browser.