Skip to content

Commit

Permalink
feat($scripts): Clean-up errors from Elm Compiler before showing them
Browse files Browse the repository at this point in the history
Added a function for stripping redundant text from error messages
  • Loading branch information
halfzebra committed Sep 23, 2017
1 parent 20b25f8 commit 1250f60
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
3 changes: 2 additions & 1 deletion config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ module.exports = {
// the line below with these two lines if you prefer the stock client:
// require.resolve('webpack-dev-server/client') + '?/',
// require.resolve('webpack/hot/dev-server'),
require.resolve('react-dev-utils/webpackHotDevClient'),
// require.resolve('react-dev-utils/webpackHotDevClient'),
require.resolve('../scripts/utils/webpackHotDevClient'),

// Errors should be considered fatal in development
require.resolve('react-error-overlay'),
Expand Down
4 changes: 2 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const paths = require('../config/paths');
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
const highlightElmCompilerErrors = require('./utils/highlightElmCompilerErrors');
const formatElmCompilerErrors = require('./utils/formatElmCompilerErrors');

const measureFileSizesBeforeBuild =
FileSizeReporter.measureFileSizesBeforeBuild;
Expand Down Expand Up @@ -90,7 +90,7 @@ function build(previousFileSizes) {
if (err) {
return reject(err);
}
const messages = highlightElmCompilerErrors(
const messages = formatElmCompilerErrors(
formatWebpackMessages(stats.toJson({}, true))
);
if (messages.errors.length) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function performEject(pkg) {
fs.copySync(path.resolve(__dirname, 'build.js'), './scripts/build.js');
fs.copySync(path.resolve(__dirname, 'start.js'), './scripts/start.js');
fs.copySync(
path.resolve(__dirname, './utils/highlightElmCompilerErrors.js'),
'./scripts/utils/highlightElmCompilerErrors.js'
path.resolve(__dirname, './utils/formatElmCompilerErrors.js'),
'./scripts/utils/formatElmCompilerErrors.js'
);
fs.copySync(path.resolve(__dirname, '../config'), './config');
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
const clearConsole = require('react-dev-utils/clearConsole');
const openBrowser = require('react-dev-utils/openBrowser');
const createDevServerConfig = require('../config/webpackDevServer.config');
const highlightElmCompilerErrors = require('./utils/highlightElmCompilerErrors');
const formatElmCompilerErrors = require('./utils/formatElmCompilerErrors');
const paths = require('../config/paths');

if (fs.existsSync('elm-package.json') === false) {
Expand Down Expand Up @@ -100,7 +100,7 @@ function createCompiler(webpack, config, appName, urls) {
// 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.
const messages = highlightElmCompilerErrors(
const messages = formatElmCompilerErrors(
formatWebpackMessages(stats.toJson({}, true))
);

Expand Down
27 changes: 27 additions & 0 deletions scripts/utils/formatElmCompilerErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var chalk = require('chalk');

function stripRedundantInfo(error) {
console.log(error);
return error.replace(
/(Module build failed: Error: Compiler process exited with error Compilation failed)\n--/g,
'\n--'
);
}

module.exports = function formatElmCompilerErrors(messages) {
var errors = messages.errors;
var warnings = messages.warnings;
return errors.length > 0
? {
errors: errors.map(x =>
x
.replace(/(--\s[A-Z\s]+-+\s.*\.elm\n)/g, chalk.cyan('$1'))
.replace(/(\n\s*)(\^+)/g, '$1' + chalk.red('$2'))
.replace(/(\d+\|)(>)/g, '$1' + chalk.bold(chalk.red('$2')))
).map(stripRedundantInfo),
warnings: warnings
}
: messages;
};
19 changes: 0 additions & 19 deletions scripts/utils/highlightElmCompilerErrors.js

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var url = require('url');
var launchEditorEndpoint = require('react-dev-utils/launchEditorEndpoint');
var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
var ErrorOverlay = require('react-error-overlay');
var highlightElmCompilerErrors = require('./highlightElmCompilerErrors');
var formatElmCompilerErrors = require('./formatElmCompilerErrors');

ErrorOverlay.startReportingRuntimeErrors({
launchEditorEndpoint: launchEditorEndpoint,
Expand Down Expand Up @@ -142,7 +142,7 @@ function handleErrors(errors) {
hasCompileErrors = true;

// "Massage" webpack messages.
var formatted = highlightElmCompilerErrors(
var formatted = formatElmCompilerErrors(
formatWebpackMessages({
errors: errors,
warnings: []
Expand Down

0 comments on commit 1250f60

Please sign in to comment.