Skip to content

Commit 4e1a7ed

Browse files
committed
Respect quiet and noInfo setting in browser console
Previously, when you used `quiet: true` or `noInfo: true` via the Node API or CLI, this only had effect on the output in the terminal (issue #109). With this PR, these settings will be respected in the browser console as well. Maybe it would be more flexible to add a separate option for this, but this would also mean more configuration. Looking for feedback on this.
1 parent 6606717 commit 4e1a7ed

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

client/index.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,58 @@ var sock = null;
1717
var hot = false;
1818
var initial = true;
1919
var currentHash = "";
20+
var quiet = false;
21+
var noInfo = true;
22+
var logLevel = 3; // 3 = all, 2 = warnings and errors, 1 = only errors, 0 = quiet
23+
24+
function log(level, msg) {
25+
if(logLevel >= 3 && level === "info")
26+
return console.log(msg);
27+
if(logLevel >= 2 && level === "warning")
28+
return console.warn(msg);
29+
if(logLevel >= 1 && level === "error")
30+
return console.error(msg);
31+
}
2032

2133
var onSocketMsg = {
2234
hot: function() {
2335
hot = true;
24-
console.log("[WDS] Hot Module Replacement enabled.");
36+
log("info", "[WDS] Hot Module Replacement enabled.");
2537
},
2638
invalid: function() {
27-
console.log("[WDS] App updated. Recompiling...");
39+
log("info", "[WDS] App updated. Recompiling...");
2840
},
2941
hash: function(hash) {
3042
currentHash = hash;
3143
},
3244
"still-ok": function() {
33-
console.log("[WDS] Nothing changed.")
45+
log("info", "[WDS] Nothing changed.")
46+
},
47+
"log-level": function(level) {
48+
logLevel = level;
3449
},
3550
ok: function() {
3651
if(initial) return initial = false;
3752
reloadApp();
3853
},
3954
warnings: function(warnings) {
40-
console.log("[WDS] Warnings while compiling.");
55+
log("info", "[WDS] Warnings while compiling.");
4156
for(var i = 0; i < warnings.length; i++)
42-
console.warn(stripAnsi(warnings[i]));
57+
log("warn", stripAnsi(warnings[i]));
4358
if(initial) return initial = false;
4459
reloadApp();
4560
},
4661
errors: function(errors) {
47-
console.log("[WDS] Errors while compiling.");
62+
log("info", "[WDS] Errors while compiling.");
4863
for(var i = 0; i < errors.length; i++)
49-
console.error(stripAnsi(errors[i]));
64+
log("error", stripAnsi(errors[i]));
5065
if(initial) return initial = false;
5166
reloadApp();
5267
},
5368
"proxy-error": function(errors) {
54-
console.log("[WDS] Proxy error.");
69+
log("info", "[WDS] Proxy error.");
5570
for(var i = 0; i < errors.length; i++)
56-
console.error(stripAnsi(errors[i]));
71+
log("error", stripAnsi(errors[i]));
5772
if(initial) return initial = false;
5873
}
5974
};
@@ -68,7 +83,7 @@ var newConnection = function() {
6883
}));
6984

7085
sock.onclose = function() {
71-
console.error("[WDS] Disconnected!");
86+
log("error", "[WDS] Disconnected!");
7287

7388
// Try to reconnect.
7489
sock = null;
@@ -88,15 +103,15 @@ newConnection();
88103

89104
function reloadApp() {
90105
if(hot) {
91-
console.log("[WDS] App hot update...");
106+
log("info", "[WDS] App hot update...");
92107
var hotEmitter = require("webpack/hot/emitter");
93108
hotEmitter.emit("webpackHotUpdate", currentHash);
94109
if(typeof window !== "undefined") {
95110
// broadcast update to window
96111
window.postMessage("webpackHotUpdate" + currentHash, "*");
97112
}
98113
} else {
99-
console.log("[WDS] App updated. Reloading...");
114+
log("info", "[WDS] App updated. Reloading...");
100115
window.location.reload();
101116
}
102117
}

lib/Server.js

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ function Server(compiler, options) {
2121

2222
this.hot = options.hot || options.hotOnly;
2323
this.headers = options.headers;
24+
this.noInfo = options.noInfo;
25+
this.quiet = options.quiet;
2426
this.sockets = [];
2527

2628
// Listening for events
@@ -339,6 +341,9 @@ Server.prototype.listen = function() {
339341
}.bind(this));
340342

341343
if(this.hot) this.sockWrite([conn], "hot");
344+
345+
if(this.quiet || this.noInfo)
346+
this.sockWrite([conn], "log-level", this.quiet ? 0 : 2);
342347
if(!this._stats) return;
343348
this._sendStats([conn], this._stats.toJson(), true);
344349
}.bind(this));

0 commit comments

Comments
 (0)