forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for errors and warnings in build script as well (facebook#440)
- Loading branch information
Showing
3 changed files
with
87 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
var chalk = require('chalk'); | ||
|
||
// Some custom utilities to prettify Webpack output. | ||
// This is a little hacky. | ||
// It would be easier if webpack provided a rich error object. | ||
var friendlySyntaxErrorLabel = 'Syntax error:'; | ||
function isLikelyASyntaxError(message) { | ||
return message.indexOf(friendlySyntaxErrorLabel) !== -1; | ||
} | ||
function formatMessage(message) { | ||
return message | ||
// Make some common errors shorter: | ||
.replace( | ||
// Babel syntax error | ||
'Module build failed: SyntaxError:', | ||
friendlySyntaxErrorLabel | ||
) | ||
.replace( | ||
// Webpack file not found error | ||
/Module not found: Error: Cannot resolve 'file' or 'directory'/, | ||
'Module not found:' | ||
) | ||
// Internal stacks are generally useless so we strip them | ||
.replace(/^\s*at\s.*:\d+:\d+[\s\)]*\n/gm, '') // at ... ...:x:y | ||
// Webpack loader names obscure CSS filenames | ||
.replace('./~/css-loader!./~/postcss-loader!', ''); | ||
} | ||
|
||
module.exports = function (stats) { | ||
// We have switched off the default Webpack output in WebpackDevServer | ||
// options so we are going to "massage" the warnings and errors and present | ||
// them in a readable focused way. | ||
// We use stats.toJson({}, true) to make output more compact and readable: | ||
// https://github.com/facebookincubator/create-react-app/issues/401#issuecomment-238291901 | ||
var json = stats.toJson({}, true); | ||
var formattedErrors = json.errors.map(message => | ||
'Error in ' + formatMessage(message) | ||
); | ||
var formattedWarnings = json.warnings.map(message => | ||
'Warning in ' + formatMessage(message) | ||
); | ||
if (stats.hasErrors()) { | ||
console.log(chalk.red('Failed to compile.')); | ||
console.log(); | ||
if (formattedErrors.some(isLikelyASyntaxError)) { | ||
// If there are any syntax errors, show just them. | ||
// This prevents a confusing ESLint parsing error | ||
// preceding a much more useful Babel syntax error. | ||
formattedErrors = formattedErrors.filter(isLikelyASyntaxError); | ||
} | ||
formattedErrors.forEach(message => { | ||
console.log(message); | ||
console.log(); | ||
}); | ||
// If errors exist, ignore warnings. | ||
return; | ||
} | ||
if (stats.hasWarnings()) { | ||
console.log(chalk.yellow('Compiled with warnings.')); | ||
console.log(); | ||
formattedWarnings.forEach(message => { | ||
console.log(message); | ||
console.log(); | ||
}); | ||
// Teach some ESLint tricks. | ||
console.log('You may use special comments to disable some warnings.'); | ||
console.log('Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.'); | ||
console.log('Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'); | ||
} | ||
}; |