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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
'use strict';
const stringify = require('json-stringify-safe');
const weblog = require('webpack-log');
const WebSocket = require('ws');
const HotClientError = require('./lib/HotClientError');
const { modifyCompiler, payload, sendData, validateCompiler } = require('./lib/util');
const defaults = {
autoConfigure: true,
host: 'localhost',
hot: true,
https: false,
logLevel: 'info',
logTime: false,
port: 8081,
reload: true,
send: {
errors: true,
warnings: true
},
server: null,
stats: {
context: process.cwd()
},
test: false
};
const timefix = 11000;
module.exports = (compiler, opts) => {
const options = Object.assign({}, defaults, opts);
const log = weblog({
name: 'hot',
id: 'webpack-hot-client',
level: options.logLevel,
timestamp: options.logTime
});
const envTarget = process.env.WHC_TARGET;
// issue #36. some alternative compiler targets still run in a server, but we
// don't want to be in the business of supporting them all.
if (!envTarget) {
process.env.WHC_TARGET = 'web';
} else if (envTarget !== 'web') {
log.warn(`WHC_TARGET changed to '${envTarget}'. Changing this value is allowed but is unsupported.`);
}
if (typeof options.host === 'string') {
options.host = {
server: options.host,
client: options.host
};
} else if (!options.host.server) {
throw new HotClientError('`options.server` must be defined when setting host to an Object');
} else if (!options.host.client) {
throw new HotClientError('`options.client` must be defined when setting host to an Object');
}
/* istanbul ignore if */
if (options.host.client !== options.host.server) {
log.warn('`options.host.client` does not match `options.host.server`. This can cause unpredictable behavior in the browser.');
}
if (options.autoConfigure) {
validateCompiler(compiler);
}
const { host, port, server } = options;
const wssOptions = options.server ? { server } : { host: host.server, port };
const wss = new WebSocket.Server(wssOptions);
let stats;
options.log = log;
if (options.server) {
const addr = options.server.address();
log.info(`WebSocket Server Attached to ${addr.address}:${addr.port}`);
}
function broadcast(data) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
}
wss.broadcast = broadcast;
if (options.server) {
options.webSocket = {
host: wss._server.address().address, // eslint-disable-line no-underscore-dangle
port: wss._server.address().port // eslint-disable-line no-underscore-dangle
};
} else {
options.webSocket = { host: host.client, port };
}
if (options.autoConfigure) {
modifyCompiler(compiler, options);
}
const compile = (comp) => {
const compilerName = comp.name || '<unnamed compiler>';
stats = null;
log.info('webpack: Compiling...');
broadcast(payload('compile', { compilerName }));
};
const done = (result) => {
log.info('webpack: Compiling Done');
// apply a fix for compiler.watch as outline here: ff0000-ad-tech/wp-plugin-watch-offset
result.startTime -= timefix; // eslint-disable-line no-param-reassign
stats = result;
const jsonStats = stats.toJson(options.stats);
/* istanbul ignore if */
if (!jsonStats) {
options.log.error('compiler done: `stats` is undefined');
}
sendData(broadcast, jsonStats, options);
};
const invalid = (filePath, comp) => {
const context = comp.context || comp.options.context || process.cwd();
const fileName = (filePath || '<unknown>')
.replace(context, '')
.substring(1);
log.info('webpack: Bundle Invalidated');
broadcast(payload('invalid', { fileName }));
};
// as of webpack@4 MultiCompiler no longer exports the compile hook
const compilers = compiler.compilers || [compiler];
for (const comp of compilers) {
comp.hooks.compile.tap('WebpackHotClient', () => {
compile(comp);
});
// we need the compiler object reference here, otherwise we'd let the
// MultiHook do it's thing in a MultiCompiler situation.
comp.hooks.invalid.tap('WebpackHotClient', (filePath) => {
invalid(filePath, comp);
});
}
compiler.hooks.done.tap('WebpackHotClient', done);
wss.on('error', (err) => {
/* istanbul ignore next */
log.error('WebSocket Server Error', err);
});
wss.on('listening', () => {
// eslint-disable-next-line no-shadow
const { host, port } = options;
log.info(`WebSocket Server Listening at ${host.server}:${port}`);
});
wss.on('connection', (socket) => {
log.info('WebSocket Client Connected');
socket.on('error', (err) => {
/* istanbul ignore if */
if (err.errno !== 'ECONNRESET') {
log.warn('client socket error', JSON.stringify(err));
}
});
socket.on('message', (data) => {
const message = JSON.parse(data);
if (message.type === 'broadcast') {
for (const client of wss.clients) {
if (client.readyState === WebSocket.OPEN) {
client.send(stringify(message.data));
}
}
}
});
// only send stats to newly connected clients if no previous clients have
// connected
if (stats && !wss.clients.length) {
const jsonStats = stats.toJson(options.stats);
/* istanbul ignore if */
if (!jsonStats) {
options.log.error('Client Connection: `stats` is undefined');
}
sendData(broadcast, jsonStats, options);
}
});
return {
close(callback) {
try {
wss.close(callback);
} catch (err) {
/* istanbul ignore next */
log.error(err);
}
},
options: Object.freeze(Object.assign({}, options)),
wss
};
};
|