Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser console messages should respect clientLogLevel #921

Merged
merged 3 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 34 additions & 22 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global __resourceQuery WorkerGlobalScope */
var url = require("url");
var stripAnsi = require("strip-ansi");
var log = require("loglevel")
var socket = require("./socket");
var overlay = require("./overlay");

Expand Down Expand Up @@ -32,18 +33,16 @@ if(typeof __resourceQuery === "string" && __resourceQuery) {
var hot = false;
var initial = true;
var currentHash = "";
var logLevel = "info";
var useWarningOverlay = false;
var useErrorOverlay = false;

function log(level, msg) {
if(logLevel === "info" && level === "info")
return console.log(msg);
if(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning")
return console.warn(msg);
if(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error")
return console.error(msg);
}
var INFO = "info";
var WARNING = "warning";
var ERROR = "error";
var NONE = "none";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is duplicating the same functionality as the quiet option though, isn't it? https://webpack.js.org/configuration/dev-server/#devserver-quiet-

Copy link
Contributor Author

@jameslai jameslai Jun 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specific lines you highlighted are not duplicating the quiet functionality, but absolutely our consumption of a log level of none (see https://webpack.js.org/configuration/dev-server/#devserver-clientloglevel for the documentation specifying the use of none) most certainly seems like duplicate functionality. I think this is a good question for the Webpack team, being that we technically have two options which will do roughly the same thing - should one of these options be deprecated?

A proposed solution is to deprecate the use of none as an option for clientLogLevel since that is technically not a log level (which the name clientLogLevel suggests), and leaving quiet flagged as true for completely silencing output.

The other option is to deprecate the use of quiet entirely and keep all logging options within the single configuration value of clientLogLevel.


// Set the default log level
log.setDefaultLevel(INFO);

// Send messages to the outside, so plugins can consume it.
function sendMsg(type, data) {
Expand All @@ -62,22 +61,35 @@ function sendMsg(type, data) {
var onSocketMsg = {
hot: function() {
hot = true;
log("info", "[WDS] Hot Module Replacement enabled.");
log.info("[WDS] Hot Module Replacement enabled.");
},
invalid: function() {
log("info", "[WDS] App updated. Recompiling...");
log.info("[WDS] App updated. Recompiling...");
sendMsg("Invalid");
},
hash: function(hash) {
currentHash = hash;
},
"still-ok": function() {
log("info", "[WDS] Nothing changed.")
log.info("[WDS] Nothing changed.")
if(useWarningOverlay || useErrorOverlay) overlay.clear();
sendMsg("StillOk");
},
"log-level": function(level) {
logLevel = level;
switch(level) {
case INFO:
case ERROR:
log.setLevel(level);
break;
case WARNING:
log.setLevel("warn"); // loglevel's warning name is different from webpack's
break;
case NONE:
log.disableAll();
break;
default:
log.error("[WDS] Unknown clientLogLevel '" + level + "'");
}
},
"overlay": function(overlay) {
if(typeof document !== "undefined") {
Expand All @@ -97,37 +109,37 @@ var onSocketMsg = {
reloadApp();
},
"content-changed": function() {
log("info", "[WDS] Content base changed. Reloading...")
log.info("[WDS] Content base changed. Reloading...")
self.location.reload();
},
warnings: function(warnings) {
log("info", "[WDS] Warnings while compiling.");
log.warn("[WDS] Warnings while compiling.");
var strippedWarnings = warnings.map(function(warning) {
return stripAnsi(warning);
});
sendMsg("Warnings", strippedWarnings);
for(var i = 0; i < strippedWarnings.length; i++)
log("warning", strippedWarnings[i]);
log.warn(strippedWarnings[i]);
if(useWarningOverlay) overlay.showMessage(warnings);

if(initial) return initial = false;
reloadApp();
},
errors: function(errors) {
log("info", "[WDS] Errors while compiling. Reload prevented.");
log.error("[WDS] Errors while compiling. Reload prevented.");
var strippedErrors = errors.map(function(error) {
return stripAnsi(error);
});
sendMsg("Errors", strippedErrors);
for(var i = 0; i < strippedErrors.length; i++)
log("error", strippedErrors[i]);
log.error(strippedErrors[i]);
if(useErrorOverlay) overlay.showMessage(errors);
},
error: function(error) {
console.error(error);
log.error(error);
},
close: function() {
log("error", "[WDS] Disconnected!");
log.error("[WDS] Disconnected!");
sendMsg("Close");
}
};
Expand Down Expand Up @@ -174,15 +186,15 @@ function reloadApp() {
return;
}
if(hot) {
log("info", "[WDS] App hot update...");
log.info("[WDS] App hot update...");
var hotEmitter = require("webpack/hot/emitter");
hotEmitter.emit("webpackHotUpdate", currentHash);
if(typeof self !== "undefined" && self.window) {
// broadcast update to window
self.postMessage("webpackHotUpdate" + currentHash, "*");
}
} else {
log("info", "[WDS] App updated. Reloading...");
log.info("[WDS] App updated. Reloading...");
self.location.reload();
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"jquery": "^2.2.0",
"less": "^2.5.1",
"less-loader": "~2.2.0",
"loglevel": "^1.4.1",
"mocha": "^3.0.2",
"mocha-sinon": "^1.1.6",
"pug": "^2.0.0-beta5",
Expand Down