Skip to content

Commit a9a0510

Browse files
Allow specifying EventStream
1 parent 1a58659 commit a9a0510

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ var subscribeAllHandler;
241241
function processMessage(obj) {
242242
switch (obj.action) {
243243
case 'building':
244+
if (obj.name && options.name && obj.name !== options.name) {
245+
return;
246+
}
244247
if (options.log) {
245248
console.log(
246249
'[HMR] bundle ' +
@@ -250,6 +253,9 @@ function processMessage(obj) {
250253
}
251254
break;
252255
case 'built':
256+
if (obj.name && options.name && obj.name !== options.name) {
257+
return;
258+
}
253259
if (options.log) {
254260
console.log(
255261
'[HMR] bundle ' +

middleware.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = webpackHotMiddleware;
2+
module.exports.createEventStream = createEventStream;
23

34
var helpers = require('./helpers');
45
var pathMatch = helpers.pathMatch;
@@ -12,7 +13,7 @@ function webpackHotMiddleware(compiler, opts) {
1213
opts.statsOptions =
1314
typeof opts.statsOptions == 'undefined' ? {} : opts.statsOptions;
1415

15-
var eventStream = createEventStream(opts.heartbeat);
16+
var eventStream = opts.eventStream || createEventStream(opts.heartbeat);
1617
var latestStats = null;
1718
var closed = false;
1819

@@ -27,7 +28,7 @@ function webpackHotMiddleware(compiler, opts) {
2728
if (closed) return;
2829
latestStats = null;
2930
if (opts.log) opts.log('webpack building...');
30-
eventStream.publish({ action: 'building' });
31+
eventStream.publish({ action: 'building', name: compiler.name });
3132
}
3233
function onDone(statsResult) {
3334
if (closed) return;

test/client-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ describe('client', function () {
377377
close: sinon.spy(),
378378
}),
379379
};
380+
s.stub(console, 'log');
380381
});
381382
beforeEach(loadClient);
382383
it('should not trigger webpack if event obj name is different', function () {
@@ -409,6 +410,26 @@ describe('client', function () {
409410
);
410411
sinon.assert.notCalled(processUpdate);
411412
});
413+
it('should not log building if obj name is different', function () {
414+
var eventSource = window.EventSource.lastCall.returnValue;
415+
eventSource.onmessage(
416+
makeMessage({
417+
name: 'bar',
418+
action: 'building',
419+
})
420+
);
421+
sinon.assert.notCalled(console.log);
422+
});
423+
it('should not log built if obj name is different', function () {
424+
var eventSource = window.EventSource.lastCall.returnValue;
425+
eventSource.onmessage(
426+
makeMessage({
427+
name: 'bar',
428+
action: 'built',
429+
})
430+
);
431+
sinon.assert.notCalled(console.log);
432+
});
412433
});
413434

414435
context('with no browser environment', function () {

test/middleware-test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ describe('middleware', function () {
1919
.expect('Content-Type', /^text\/event-stream\b/)
2020
.end(done);
2121
});
22+
it('uses provided eventStream', function (done) {
23+
setupServer({
24+
eventStream: {
25+
handler: function (req, res) {
26+
res.writeHead(201, { 'Content-Type': 'fake content type' });
27+
res.write('\n');
28+
},
29+
},
30+
})();
31+
request('/__webpack_hmr')
32+
.expect('Content-Type', 'fake content type')
33+
.end(done);
34+
});
2235
it('should heartbeat every 10 seconds', function (done) {
2336
request('/__webpack_hmr').end(function (err, res) {
2437
if (err) return done(err);
@@ -48,13 +61,14 @@ describe('middleware', function () {
4861
if (err) return done(err);
4962

5063
res.on('data', verify);
51-
64+
compiler.name = 'test name';
5265
compiler.emit('invalid');
5366

5467
function verify() {
5568
assert.equal(res.events.length, 1);
5669
var event = JSON.parse(res.events[0].substring(5));
5770
assert.equal(event.action, 'building');
71+
assert.equal(event.name, compiler.name);
5872
done();
5973
}
6074
});

0 commit comments

Comments
 (0)