Skip to content

Commit 3c23d52

Browse files
committed
Improve CLI output for errors.
1 parent 5ab6e17 commit 3c23d52

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

bin/webpack-dev-server.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ function versionInfo() {
2222
"webpack " + require("webpack/package.json").version;
2323
}
2424

25-
function colorize(useColor, msg) {
25+
function colorInfo(useColor, msg) {
2626
if(useColor)
2727
// Make text blue and bold, so it *pops*
2828
return "\u001b[1m\u001b[34m" + msg + "\u001b[39m\u001b[22m";
2929
return msg;
3030
}
3131

32+
function colorError(useColor, msg) {
33+
if(useColor)
34+
// Make text red and bold, so it *pops*
35+
return "\u001b[1m\u001b[31m" + msg + "\u001b[39m\u001b[22m";
36+
return msg;
37+
}
38+
3239
var yargs = require("yargs")
3340
.usage(versionInfo() +
3441
"\nUsage: http://webpack.github.io/docs/webpack-dev-server.html");
@@ -320,15 +327,13 @@ function processOptions(wpOpt) {
320327
}
321328

322329
var compiler;
330+
// TODO: This is only temporarily, we'll probably make our own ValidationError for WDS errors.
331+
var WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
323332
try {
324333
compiler = webpack(wpOpt);
325334
} catch(e) {
326-
var WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
327335
if(e instanceof WebpackOptionsValidationError) {
328-
if(options.stats.colors)
329-
console.error("\u001b[1m\u001b[31m" + e.message + "\u001b[39m\u001b[22m");
330-
else
331-
console.error(e.message);
336+
console.error(colorError(options.stats.colors, e.message));
332337
process.exit(1); // eslint-disable-line
333338
}
334339
throw e;
@@ -342,7 +347,16 @@ function processOptions(wpOpt) {
342347

343348
var uri = domain + (options.inline !== false ? "/" : "/webpack-dev-server/");
344349

345-
var server = new Server(compiler, options);
350+
var server;
351+
try {
352+
server = new Server(compiler, options);
353+
} catch(e) {
354+
if(e instanceof WebpackOptionsValidationError) {
355+
console.error(colorError(options.stats.colors, e.message));
356+
process.exit(1); // eslint-disable-line
357+
}
358+
throw e;
359+
}
346360

347361
if(options.socket) {
348362
server.listeningApp.on("error", function(e) {
@@ -380,18 +394,18 @@ function processOptions(wpOpt) {
380394

381395
function reportReadiness(uri, options) {
382396
var useColor = options.stats.colors;
383-
var startSentence = "Project is running at " + colorize(useColor, uri)
397+
var startSentence = "Project is running at " + colorInfo(useColor, uri)
384398
if(options.socket) {
385-
startSentence = "Listening to socket at " + colorize(useColor, options.socket);
399+
startSentence = "Listening to socket at " + colorInfo(useColor, options.socket);
386400
}
387401
console.log((argv["progress"] ? "\n" : "") + startSentence);
388402

389-
console.log("webpack output is served from " + colorize(useColor, options.publicPath));
403+
console.log("webpack output is served from " + colorInfo(useColor, options.publicPath));
390404
var contentBase = Array.isArray(options.contentBase) ? options.contentBase.join(", ") : options.contentBase;
391405
if(contentBase)
392-
console.log("Content not from webpack is served from " + colorize(useColor, contentBase));
406+
console.log("Content not from webpack is served from " + colorInfo(useColor, contentBase));
393407
if(options.historyApiFallback)
394-
console.log("404s will fallback to " + colorize(useColor, options.historyApiFallback.index || "/index.html"));
408+
console.log("404s will fallback to " + colorInfo(useColor, options.historyApiFallback.index || "/index.html"));
395409
if(options.open)
396410
open(uri);
397411
}

0 commit comments

Comments
 (0)