forked from webpack-contrib/webpack-hot-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
131 lines (114 loc) · 3.2 KB
/
client.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*eslint-env browser*/
/*global __resourceQuery*/
var options = {
path: "/__webpack_hmr",
timeout: 20 * 1000,
overlay: true,
reload: false,
log: true,
warn: true
};
if (__resourceQuery) {
var querystring = require('querystring');
var overrides = querystring.parse(__resourceQuery.slice(1));
if (overrides.path) options.path = overrides.path;
if (overrides.timeout) options.timeout = overrides.timeout;
if (overrides.overlay) options.overlay = overrides.overlay !== 'false';
if (overrides.reload) options.reload = overrides.reload !== 'false';
if (overrides.noInfo && overrides.noInfo !== 'false') {
options.log = false;
}
if (overrides.quiet && overrides.quiet !== 'false') {
options.log = false;
options.warn = false;
}
}
if (typeof window.EventSource === 'undefined') {
console.warn(
"webpack-hot-middleware's client requires EventSource to work. " +
"You should include a polyfill if you want to support this browser: " +
"https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events#Tools"
);
} else {
connect();
}
function connect() {
var source = new window.EventSource(options.path);
var lastActivity = new Date();
source.onopen = handleOnline;
source.onmessage = handleMessage;
source.onerror = handleDisconnect;
var timer = setInterval(function() {
if ((new Date() - lastActivity) > options.timeout) {
handleDisconnect();
}
}, options.timeout / 2);
function handleOnline() {
if (options.log) console.log("[HMR] connected");
lastActivity = new Date();
}
function handleMessage(event) {
lastActivity = new Date();
if (event.data == "\uD83D\uDC93") {
return;
}
try {
processMessage(JSON.parse(event.data));
} catch (ex) {
if (options.warn) {
console.warn("Invalid HMR message: " + event.data + "\n" + ex);
}
}
}
function handleDisconnect() {
clearInterval(timer);
source.close();
setTimeout(connect, options.timeout);
}
}
var strip = require('strip-ansi');
var overlay;
if (options.overlay) {
overlay = require('./client-overlay');
}
function problems(type, obj) {
if (options.warn) console.warn("[HMR] bundle has " + type + ":");
var list = [];
obj[type].forEach(function(msg) {
var clean = strip(msg);
if (options.warn) console.warn("[HMR] " + clean);
list.push(clean);
});
if (overlay && type !== 'warnings') overlay.showProblems(list);
}
function success() {
if (overlay) overlay.clear();
}
var processUpdate = require('./process-update');
var customHandler;
function processMessage(obj) {
if (obj.action == "building") {
if (options.log) console.log("[HMR] bundle rebuilding");
} else if (obj.action == "built") {
if (options.log) console.log("[HMR] bundle rebuilt in " + obj.time + "ms");
if (obj.errors.length > 0) {
problems('errors', obj);
} else {
if (obj.warnings.length > 0) {
problems('warnings', obj);
} else {
success();
}
processUpdate(obj.hash, obj.modules, options);
}
} else if (customHandler) {
customHandler(obj);
}
}
if (module) {
module.exports = {
subscribe: function subscribe(handler) {
customHandler = handler;
}
};
}