source: frontend/node_modules/react-dev-utils/formatWebpackMessages.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[9af201e]1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10const friendlySyntaxErrorLabel = 'Syntax error:';
11
12function isLikelyASyntaxError(message) {
13 return message.indexOf(friendlySyntaxErrorLabel) !== -1;
14}
15
16// Cleans up webpack error messages.
17function formatMessage(message) {
18 let lines = [];
19
20 if (typeof message === 'string') {
21 lines = message.split('\n');
22 } else if ('message' in message) {
23 lines = message['message'].split('\n');
24 } else if (Array.isArray(message)) {
25 message.forEach(message => {
26 if ('message' in message) {
27 lines = message['message'].split('\n');
28 }
29 });
30 }
31
32 // Strip webpack-added headers off errors/warnings
33 // https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
34 lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
35
36 // Transform parsing error into syntax error
37 // TODO: move this to our ESLint formatter?
38 lines = lines.map(line => {
39 const parsingError = /Line (\d+):(?:(\d+):)?\s*Parsing error: (.+)$/.exec(
40 line
41 );
42 if (!parsingError) {
43 return line;
44 }
45 const [, errorLine, errorColumn, errorMessage] = parsingError;
46 return `${friendlySyntaxErrorLabel} ${errorMessage} (${errorLine}:${errorColumn})`;
47 });
48
49 message = lines.join('\n');
50 // Smoosh syntax errors (commonly found in CSS)
51 message = message.replace(
52 /SyntaxError\s+\((\d+):(\d+)\)\s*(.+?)\n/g,
53 `${friendlySyntaxErrorLabel} $3 ($1:$2)\n`
54 );
55 // Clean up export errors
56 message = message.replace(
57 /^.*export '(.+?)' was not found in '(.+?)'.*$/gm,
58 `Attempted import error: '$1' is not exported from '$2'.`
59 );
60 message = message.replace(
61 /^.*export 'default' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
62 `Attempted import error: '$2' does not contain a default export (imported as '$1').`
63 );
64 message = message.replace(
65 /^.*export '(.+?)' \(imported as '(.+?)'\) was not found in '(.+?)'.*$/gm,
66 `Attempted import error: '$1' is not exported from '$3' (imported as '$2').`
67 );
68 lines = message.split('\n');
69
70 // Remove leading newline
71 if (lines.length > 2 && lines[1].trim() === '') {
72 lines.splice(1, 1);
73 }
74 // Clean up file name
75 lines[0] = lines[0].replace(/^(.*) \d+:\d+-\d+$/, '$1');
76
77 // Cleans up verbose "module not found" messages for files and packages.
78 if (lines[1] && lines[1].indexOf('Module not found: ') === 0) {
79 lines = [
80 lines[0],
81 lines[1]
82 .replace('Error: ', '')
83 .replace('Module not found: Cannot find file:', 'Cannot find file:'),
84 ];
85 }
86
87 // Add helpful message for users trying to use Sass for the first time
88 if (lines[1] && lines[1].match(/Cannot find module.+sass/)) {
89 lines[1] = 'To import Sass files, you first need to install sass.\n';
90 lines[1] +=
91 'Run `npm install sass` or `yarn add sass` inside your workspace.';
92 }
93
94 message = lines.join('\n');
95 // Internal stacks are generally useless so we strip them... with the
96 // exception of stacks containing `webpack:` because they're normally
97 // from user code generated by webpack. For more information see
98 // https://github.com/facebook/create-react-app/pull/1050
99 message = message.replace(
100 /^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,
101 ''
102 ); // at ... ...:x:y
103 message = message.replace(/^\s*at\s<anonymous>(\n|$)/gm, ''); // at <anonymous>
104 lines = message.split('\n');
105
106 // Remove duplicated newlines
107 lines = lines.filter(
108 (line, index, arr) =>
109 index === 0 || line.trim() !== '' || line.trim() !== arr[index - 1].trim()
110 );
111
112 // Reassemble the message
113 message = lines.join('\n');
114 return message.trim();
115}
116
117function formatWebpackMessages(json) {
118 const formattedErrors = json.errors.map(formatMessage);
119 const formattedWarnings = json.warnings.map(formatMessage);
120 const result = { errors: formattedErrors, warnings: formattedWarnings };
121 if (result.errors.some(isLikelyASyntaxError)) {
122 // If there are any syntax errors, show just them.
123 result.errors = result.errors.filter(isLikelyASyntaxError);
124 }
125 return result;
126}
127
128module.exports = formatWebpackMessages;
Note: See TracBrowser for help on using the repository browser.