forked from webpack-contrib/webpack-hot-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
166 lines (148 loc) · 4.39 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*eslint-env browser*/
/*global __resourceQuery __webpack_public_path__*/
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 (overrides.dynamicPublicPath) {
options.path = __webpack_public_path__ + options.path;
}
}
if (typeof window === 'undefined') {
// do nothing
} else 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(window.EventSource);
}
function connect(EventSource) {
var source = new 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(function() { connect(EventSource); }, options.timeout);
}
}
var reporter;
// the reporter needs to be a singleton on the page
// in case the client is being used by mutliple bundles
// we only want to report once.
// all the errors will go to all clients
var singletonKey = '__webpack_hot_middleware_reporter__';
if (typeof window !== 'undefined' && !window[singletonKey]) {
reporter = window[singletonKey] = createReporter();
}
function createReporter() {
var strip = require('strip-ansi');
var overlay;
if (typeof document !== 'undefined' && options.overlay) {
overlay = require('./client-overlay');
}
return {
problems: function(type, obj) {
if (options.warn) {
console.warn("[HMR] bundle has " + type + ":");
obj[type].forEach(function(msg) {
console.warn("[HMR] " + strip(msg));
});
}
if (overlay && type !== 'warnings') overlay.showProblems(type, obj[type]);
},
success: function() {
if (overlay) overlay.clear();
},
useCustomOverlay: function(customOverlay) {
overlay = customOverlay;
}
};
}
var processUpdate = require('./process-update');
var customHandler;
var subscribeAllHandler;
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 " + (obj.name ? obj.name + " " : "") +
"rebuilt in " + obj.time + "ms"
);
}
if (obj.errors.length > 0) {
if (reporter) reporter.problems('errors', obj);
} else {
if (reporter) {
if (obj.warnings.length > 0) reporter.problems('warnings', obj);
reporter.success();
}
processUpdate(obj.hash, obj.modules, options);
}
} else if (customHandler) {
customHandler(obj);
}
if (subscribeAllHandler) {
subscribeAllHandler(obj);
}
}
if (module) {
module.exports = {
subscribeAll: function subscribeAll(handler) {
subscribeAllHandler = handler;
},
subscribe: function subscribe(handler) {
customHandler = handler;
},
useCustomOverlay: function useCustomOverlay(customOverlay) {
if (reporter) reporter.useCustomOverlay(customOverlay);
}
};
}