/
index.js
71 lines (60 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var io = require("socket.io-client");
var stripAnsi = require('strip-ansi');
var scriptElements = document.getElementsByTagName("script");
io = io.connect(typeof __resourceQuery === "string" && __resourceQuery ?
__resourceQuery.substr(1) :
scriptElements[scriptElements.length-1].getAttribute("src").replace(/\/[^\/]+$/, "")
);
var hot = false;
var initial = true;
var currentHash = "";
io.on("hot", function() {
hot = true;
console.log("[WDS] Hot Module Replacement enabled.");
});
io.on("invalid", function() {
console.log("[WDS] App updated. Recompiling...");
});
io.on("hash", function(hash) {
currentHash = hash;
});
io.on("still-ok", function() {
console.log("[WDS] Nothing changed.")
});
io.on("ok", function() {
if(initial) return initial = false;
reloadApp();
});
io.on("warnings", function(warnings) {
console.log("[WDS] Warnings while compiling.");
for(var i = 0; i < warnings.length; i++)
console.warn(stripAnsi(warnings[i]));
if(initial) return initial = false;
reloadApp();
});
io.on("errors", function(errors) {
console.log("[WDS] Errors while compiling.");
for(var i = 0; i < errors.length; i++)
console.error(stripAnsi(errors[i]));
if(initial) return initial = false;
reloadApp();
});
io.on("proxy-error", function(errors) {
console.log("[WDS] Proxy error.");
for(var i = 0; i < errors.length; i++)
console.error(stripAnsi(errors[i]));
if(initial) return initial = false;
reloadApp();
});
io.on("disconnect", function() {
console.error("[WDS] Disconnected!");
});
function reloadApp() {
if(hot) {
console.log("[WDS] App hot update...");
window.postMessage("webpackHotUpdate" + currentHash, "*");
} else {
console.log("[WDS] App updated. Reloading...");
window.location.reload();
}
}