Skip to content

Commit

Permalink
Respect quiet and noInfo setting in browser console
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SpaceK33z committed Aug 31, 2016
1 parent 6606717 commit 4e1a7ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
39 changes: 27 additions & 12 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,58 @@ var sock = null;
var hot = false;
var initial = true;
var currentHash = "";
var quiet = false;
var noInfo = true;
var logLevel = 3; // 3 = all, 2 = warnings and errors, 1 = only errors, 0 = quiet

function log(level, msg) {
if(logLevel >= 3 && level === "info")
return console.log(msg);
if(logLevel >= 2 && level === "warning")
return console.warn(msg);
if(logLevel >= 1 && level === "error")
return console.error(msg);
}

var onSocketMsg = {
hot: function() {
hot = true;
console.log("[WDS] Hot Module Replacement enabled.");
log("info", "[WDS] Hot Module Replacement enabled.");
},
invalid: function() {
console.log("[WDS] App updated. Recompiling...");
log("info", "[WDS] App updated. Recompiling...");
},
hash: function(hash) {
currentHash = hash;
},
"still-ok": function() {
console.log("[WDS] Nothing changed.")
log("info", "[WDS] Nothing changed.")
},
"log-level": function(level) {
logLevel = level;
},
ok: function() {
if(initial) return initial = false;
reloadApp();
},
warnings: function(warnings) {
console.log("[WDS] Warnings while compiling.");
log("info", "[WDS] Warnings while compiling.");
for(var i = 0; i < warnings.length; i++)
console.warn(stripAnsi(warnings[i]));
log("warn", stripAnsi(warnings[i]));
if(initial) return initial = false;
reloadApp();
},
errors: function(errors) {
console.log("[WDS] Errors while compiling.");
log("info", "[WDS] Errors while compiling.");
for(var i = 0; i < errors.length; i++)
console.error(stripAnsi(errors[i]));
log("error", stripAnsi(errors[i]));
if(initial) return initial = false;
reloadApp();
},
"proxy-error": function(errors) {
console.log("[WDS] Proxy error.");
log("info", "[WDS] Proxy error.");
for(var i = 0; i < errors.length; i++)
console.error(stripAnsi(errors[i]));
log("error", stripAnsi(errors[i]));
if(initial) return initial = false;
}
};
Expand All @@ -68,7 +83,7 @@ var newConnection = function() {
}));

sock.onclose = function() {
console.error("[WDS] Disconnected!");
log("error", "[WDS] Disconnected!");

// Try to reconnect.
sock = null;
Expand All @@ -88,15 +103,15 @@ newConnection();

function reloadApp() {
if(hot) {
console.log("[WDS] App hot update...");
log("info", "[WDS] App hot update...");
var hotEmitter = require("webpack/hot/emitter");
hotEmitter.emit("webpackHotUpdate", currentHash);
if(typeof window !== "undefined") {
// broadcast update to window
window.postMessage("webpackHotUpdate" + currentHash, "*");
}
} else {
console.log("[WDS] App updated. Reloading...");
log("info", "[WDS] App updated. Reloading...");
window.location.reload();
}
}
5 changes: 5 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function Server(compiler, options) {

this.hot = options.hot || options.hotOnly;
this.headers = options.headers;
this.noInfo = options.noInfo;
this.quiet = options.quiet;
this.sockets = [];

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

if(this.hot) this.sockWrite([conn], "hot");

if(this.quiet || this.noInfo)
this.sockWrite([conn], "log-level", this.quiet ? 0 : 2);
if(!this._stats) return;
this._sendStats([conn], this._stats.toJson(), true);
}.bind(this));
Expand Down

0 comments on commit 4e1a7ed

Please sign in to comment.