Skip to content

Allow specifying EventStream #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ var subscribeAllHandler;
function processMessage(obj) {
switch (obj.action) {
case 'building':
if (obj.name && options.name && obj.name !== options.name) {
return;
}
if (options.log) {
console.log(
'[HMR] bundle ' +
Expand All @@ -250,6 +253,9 @@ function processMessage(obj) {
}
break;
case 'built':
if (obj.name && options.name && obj.name !== options.name) {
return;
}
if (options.log) {
console.log(
'[HMR] bundle ' +
Expand Down
5 changes: 3 additions & 2 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = webpackHotMiddleware;
module.exports.createEventStream = createEventStream;

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

var eventStream = createEventStream(opts.heartbeat);
var eventStream = opts.eventStream || createEventStream(opts.heartbeat);
var latestStats = null;
var closed = false;

Expand All @@ -27,7 +28,7 @@ function webpackHotMiddleware(compiler, opts) {
if (closed) return;
latestStats = null;
if (opts.log) opts.log('webpack building...');
eventStream.publish({ action: 'building' });
eventStream.publish({ action: 'building', name: compiler.name });
}
function onDone(statsResult) {
if (closed) return;
Expand Down
21 changes: 21 additions & 0 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ describe('client', function () {
close: sinon.spy(),
}),
};
s.stub(console, 'log');
});
beforeEach(loadClient);
it('should not trigger webpack if event obj name is different', function () {
Expand Down Expand Up @@ -409,6 +410,26 @@ describe('client', function () {
);
sinon.assert.notCalled(processUpdate);
});
it('should not log building if obj name is different', function () {
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(
makeMessage({
name: 'bar',
action: 'building',
})
);
sinon.assert.notCalled(console.log);
});
it('should not log built if obj name is different', function () {
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(
makeMessage({
name: 'bar',
action: 'built',
})
);
sinon.assert.notCalled(console.log);
});
});

context('with no browser environment', function () {
Expand Down
16 changes: 15 additions & 1 deletion test/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ describe('middleware', function () {
.expect('Content-Type', /^text\/event-stream\b/)
.end(done);
});
it('uses provided eventStream', function (done) {
setupServer({
eventStream: {
handler: function (req, res) {
res.writeHead(201, { 'Content-Type': 'fake content type' });
res.write('\n');
},
},
})();
request('/__webpack_hmr')
.expect('Content-Type', 'fake content type')
.end(done);
});
it('should heartbeat every 10 seconds', function (done) {
request('/__webpack_hmr').end(function (err, res) {
if (err) return done(err);
Expand Down Expand Up @@ -48,13 +61,14 @@ describe('middleware', function () {
if (err) return done(err);

res.on('data', verify);

compiler.name = 'test name';
compiler.emit('invalid');

function verify() {
assert.equal(res.events.length, 1);
var event = JSON.parse(res.events[0].substring(5));
assert.equal(event.action, 'building');
assert.equal(event.name, compiler.name);
done();
}
});
Expand Down