Skip to content

Commit 922ee1a

Browse files
committed
Moved HMR logging to a module, added possibility to set log level.
1 parent 60824f1 commit 922ee1a

6 files changed

Lines changed: 60 additions & 41 deletions

File tree

‎hot/dev-server.js‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ if(module.hot) {
88
var upToDate = function upToDate() {
99
return lastHash.indexOf(__webpack_hash__) >= 0;
1010
};
11+
var log = require("./log");
1112
var check = function check() {
1213
module.hot.check(true).then(function(updatedModules) {
1314
if(!updatedModules) {
14-
console.warn("[HMR] Cannot find update. Need to do a full reload!");
15-
console.warn("[HMR] (Probably because of restarting the webpack-dev-server)");
15+
log("warning", "[HMR] Cannot find update. Need to do a full reload!");
16+
log("warning", "[HMR] (Probably because of restarting the webpack-dev-server)");
1617
window.location.reload();
1718
return;
1819
}
@@ -24,29 +25,29 @@ if(module.hot) {
2425
require("./log-apply-result")(updatedModules, updatedModules);
2526

2627
if(upToDate()) {
27-
console.log("[HMR] App is up to date.");
28+
log("info", "[HMR] App is up to date.");
2829
}
2930

3031
}).catch(function(err) {
3132
var status = module.hot.status();
3233
if(["abort", "fail"].indexOf(status) >= 0) {
33-
console.warn("[HMR] Cannot apply update. Need to do a full reload!");
34-
console.warn("[HMR] " + err.stack || err.message);
34+
log("warning", "[HMR] Cannot apply update. Need to do a full reload!");
35+
log("warning", "[HMR] " + err.stack || err.message);
3536
window.location.reload();
3637
} else {
37-
console.warn("[HMR] Update failed: " + err.stack || err.message);
38+
log("warning", "[HMR] Update failed: " + err.stack || err.message);
3839
}
3940
});
4041
};
4142
var hotEmitter = require("./emitter");
4243
hotEmitter.on("webpackHotUpdate", function(currentHash) {
4344
lastHash = currentHash;
4445
if(!upToDate() && module.hot.status() === "idle") {
45-
console.log("[HMR] Checking for updates on the server...");
46+
log("info", "[HMR] Checking for updates on the server...");
4647
check();
4748
}
4849
});
49-
console.log("[HMR] Waiting for update signal from WDS...");
50+
log("info", "[HMR] Waiting for update signal from WDS...");
5051
} else {
5152
throw new Error("[HMR] Hot Module Replacement is disabled.");
5253
}

‎hot/log-apply-result.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@ module.exports = function(updatedModules, renewedModules) {
66
var unacceptedModules = updatedModules.filter(function(moduleId) {
77
return renewedModules && renewedModules.indexOf(moduleId) < 0;
88
});
9+
var log = require("./log");
910

1011
if(unacceptedModules.length > 0) {
11-
console.warn("[HMR] The following modules couldn't be hot updated: (They would need a full reload!)");
12+
log("warning", "[HMR] The following modules couldn't be hot updated: (They would need a full reload!)");
1213
unacceptedModules.forEach(function(moduleId) {
13-
console.warn("[HMR] - " + moduleId);
14+
log("warning", "[HMR] - " + moduleId);
1415
});
1516
}
1617

1718
if(!renewedModules || renewedModules.length === 0) {
18-
console.log("[HMR] Nothing hot updated.");
19+
log("info", "[HMR] Nothing hot updated.");
1920
} else {
20-
console.log("[HMR] Updated modules:");
21+
log("info", "[HMR] Updated modules:");
2122
renewedModules.forEach(function(moduleId) {
22-
console.log("[HMR] - " + moduleId);
23+
log("info", "[HMR] - " + moduleId);
2324
});
2425
var numberIds = renewedModules.every(function(moduleId) {
2526
return typeof moduleId === "number";
2627
});
2728
if(numberIds)
28-
console.log("[HMR] Consider using the NamedModulesPlugin for module names.");
29+
log("info", "[HMR] Consider using the NamedModulesPlugin for module names.");
2930
}
3031
};

‎hot/log.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var logLevel = "info";
2+
3+
module.exports = function(level, msg) {
4+
if(logLevel === "info" && level === "info")
5+
return console.log(msg);
6+
if(["info", "warning"].indexOf(logLevel) >= 0 && level === "warning")
7+
return console.warn(msg);
8+
if(["info", "warning", "error"].indexOf(logLevel) >= 0 && level === "error")
9+
return console.error(msg);
10+
};
11+
12+
module.exports.setLogLevel = function(level) {
13+
logLevel = level;
14+
};

‎hot/only-dev-server.js‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ if(module.hot) {
88
var upToDate = function upToDate() {
99
return lastHash.indexOf(__webpack_hash__) >= 0;
1010
};
11+
var log = require("./log");
1112
var check = function check() {
1213
module.hot.check().then(function(updatedModules) {
1314
if(!updatedModules) {
14-
console.warn("[HMR] Cannot find update. Need to do a full reload!");
15-
console.warn("[HMR] (Probably because of restarting the webpack-dev-server)");
15+
log("warning", "[HMR] Cannot find update. Need to do a full reload!");
16+
log("warning", "[HMR] (Probably because of restarting the webpack-dev-server)");
1617
return;
1718
}
1819

@@ -21,14 +22,14 @@ if(module.hot) {
2122
ignoreDeclined: true,
2223
ignoreErrored: true,
2324
onUnaccepted: function(data) {
24-
console.warn("Ignored an update to unaccepted module " + data.chain.join(" -> "));
25+
log("warning", "Ignored an update to unaccepted module " + data.chain.join(" -> "));
2526
},
2627
onDeclined: function(data) {
27-
console.warn("Ignored an update to declined module " + data.chain.join(" -> "));
28+
log("warning", "Ignored an update to declined module " + data.chain.join(" -> "));
2829
},
2930
onErrored: function(data) {
30-
console.error(data.error);
31-
console.warn("Ignored an error while updating module " + data.moduleId + " (" + data.type + ")");
31+
log("error", data.error);
32+
log("warning", "Ignored an error while updating module " + data.moduleId + " (" + data.type + ")");
3233
}
3334
}).then(function(renewedModules) {
3435
if(!upToDate()) {
@@ -38,16 +39,16 @@ if(module.hot) {
3839
require("./log-apply-result")(updatedModules, renewedModules);
3940

4041
if(upToDate()) {
41-
console.log("[HMR] App is up to date.");
42+
log("info", "[HMR] App is up to date.");
4243
}
4344
});
4445
}).catch(function(err) {
4546
var status = module.hot.status();
4647
if(["abort", "fail"].indexOf(status) >= 0) {
47-
console.warn("[HMR] Cannot check for update. Need to do a full reload!");
48-
console.warn("[HMR] " + err.stack || err.message);
48+
log("warning", "[HMR] Cannot check for update. Need to do a full reload!");
49+
log("warning", "[HMR] " + err.stack || err.message);
4950
} else {
50-
console.warn("[HMR] Update check failed: " + err.stack || err.message);
51+
log("warning", "[HMR] Update check failed: " + err.stack || err.message);
5152
}
5253
});
5354
};
@@ -57,14 +58,14 @@ if(module.hot) {
5758
if(!upToDate()) {
5859
var status = module.hot.status();
5960
if(status === "idle") {
60-
console.log("[HMR] Checking for updates on the server...");
61+
log("info", "[HMR] Checking for updates on the server...");
6162
check();
6263
} else if(["abort", "fail"].indexOf(status) >= 0) {
63-
console.warn("[HMR] Cannot apply update as a previous update " + status + "ed. Need to do a full reload!");
64+
log("warning", "[HMR] Cannot apply update as a previous update " + status + "ed. Need to do a full reload!");
6465
}
6566
}
6667
});
67-
console.log("[HMR] Waiting for update signal from WDS...");
68+
log("info", "[HMR] Waiting for update signal from WDS...");
6869
} else {
6970
throw new Error("[HMR] Hot Module Replacement is disabled.");
7071
}

‎hot/poll.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
/*globals __resourceQuery */
66
if(module.hot) {
77
var hotPollInterval = +(__resourceQuery.substr(1)) || (10 * 60 * 1000);
8+
var log = require("./log");
89

910
var checkForUpdate = function checkForUpdate(fromUpdate) {
1011
if(module.hot.status() === "idle") {
1112
module.hot.check(true).then(function(updatedModules) {
1213
if(!updatedModules) {
13-
if(fromUpdate) console.log("[HMR] Update applied.");
14+
if(fromUpdate) log("info", "[HMR] Update applied.");
1415
return;
1516
}
1617
require("./log-apply-result")(updatedModules, updatedModules);
1718
checkForUpdate(true);
1819
}).catch(function(err) {
1920
var status = module.hot.status();
2021
if(["abort", "fail"].indexOf(status) >= 0) {
21-
console.warn("[HMR] Cannot apply update.");
22-
console.warn("[HMR] " + err.stack || err.message);
23-
console.warn("[HMR] You need to restart the application!");
22+
log("warning", "[HMR] Cannot apply update.");
23+
log("warning", "[HMR] " + err.stack || err.message);
24+
log("warning", "[HMR] You need to restart the application!");
2425
} else {
25-
console.warn("[HMR] Update failed: " + err.stack || err.message);
26+
log("warning", "[HMR] Update failed: " + err.stack || err.message);
2627
}
2728
});
2829
}

‎hot/signal.js‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
*/
55
/*globals __resourceQuery */
66
if(module.hot) {
7+
var log = require("./log");
78
var checkForUpdate = function checkForUpdate(fromUpdate) {
89
module.hot.check().then(function(updatedModules) {
910
if(!updatedModules) {
1011
if(fromUpdate)
11-
console.log("[HMR] Update applied.");
12+
log("info", "[HMR] Update applied.");
1213
else
13-
console.warn("[HMR] Cannot find update.");
14+
log("warning", "[HMR] Cannot find update.");
1415
return;
1516
}
1617

1718
return module.hot.apply({
1819
ignoreUnaccepted: true,
1920
onUnaccepted: function(data) {
20-
console.warn("Ignored an update to unaccepted module " + data.chain.join(" -> "));
21+
log("warning", "Ignored an update to unaccepted module " + data.chain.join(" -> "));
2122
},
2223
}).then(function(renewedModules) {
2324
require("./log-apply-result")(updatedModules, renewedModules);
@@ -27,19 +28,19 @@ if(module.hot) {
2728
}).catch(function(err) {
2829
var status = module.hot.status();
2930
if(["abort", "fail"].indexOf(status) >= 0) {
30-
console.warn("[HMR] Cannot apply update.");
31-
console.warn("[HMR] " + err.stack || err.message);
32-
console.warn("[HMR] You need to restart the application!");
31+
log("warning", "[HMR] Cannot apply update.");
32+
log("warning", "[HMR] " + err.stack || err.message);
33+
log("warning", "[HMR] You need to restart the application!");
3334
} else {
34-
console.warn("[HMR] Update failed: " + err.stack || err.message);
35+
log("warning", "[HMR] Update failed: " + err.stack || err.message);
3536
}
3637
});
3738
};
3839

3940
process.on(__resourceQuery.substr(1) || "SIGUSR2", function() {
4041
if(module.hot.status() !== "idle") {
41-
console.warn("[HMR] Got signal but currently in " + module.hot.status() + " state.");
42-
console.warn("[HMR] Need to be in idle state to start hot update.");
42+
log("warning", "[HMR] Got signal but currently in " + module.hot.status() + " state.");
43+
log("warning", "[HMR] Need to be in idle state to start hot update.");
4344
return;
4445
}
4546

0 commit comments

Comments
 (0)