Skip to content

Commit 61d0cdf

Browse files
evilebottnawihiroppy
authored andcommittedMay 29, 2019
feat: onListening option (#1930)
1 parent 9297988 commit 61d0cdf

6 files changed

+73
-0
lines changed
 

‎lib/Server.js

+8
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ class Server {
414414
}
415415

416416
setupBeforeFeature() {
417+
// Todo rename onBeforeSetupMiddleware in next major release
418+
// Todo pass only `this` argument
417419
this.options.before(this.app, this, this.compiler);
418420
}
419421

@@ -422,6 +424,8 @@ class Server {
422424
}
423425

424426
setupAfterFeature() {
427+
// Todo rename onAfterSetupMiddleware in next major release
428+
// Todo pass only `this` argument
425429
this.options.after(this.app, this, this.compiler);
426430
}
427431

@@ -722,6 +726,10 @@ class Server {
722726
if (fn) {
723727
fn.call(this.listeningApp, err);
724728
}
729+
730+
if (typeof this.options.onListening === 'function') {
731+
this.options.onListening(this);
732+
}
725733
});
726734
}
727735

‎lib/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
"noInfo": {
196196
"type": "boolean"
197197
},
198+
"onListening": {
199+
"instanceof": "Function"
200+
},
198201
"open": {
199202
"anyOf": [
200203
{
@@ -402,6 +405,7 @@
402405
"logTime": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#logtime)",
403406
"mimeTypes": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devservermimetypes-)",
404407
"noInfo": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservernoinfo-)",
408+
"onListening": "should be {Function} (https://webpack.js.org/configuration/dev-server/#onlistening)",
405409
"open": "should be {String|Boolean} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
406410
"openPage": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserveropenpage)",
407411
"overlay": "should be {Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserveroverlay)",

‎test/CreateConfig.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -967,4 +967,16 @@ describe('createConfig', () => {
967967
).toMatchSnapshot();
968968
expect(webpackConfigNoStats).toMatchSnapshot();
969969
});
970+
971+
it('onListening option', () => {
972+
const config = createConfig(
973+
Object.assign({}, webpackConfig, {
974+
devServer: { onListening: () => {} },
975+
}),
976+
argv,
977+
{ port: 8080 }
978+
);
979+
980+
expect(config).toMatchSnapshot();
981+
});
970982
});

‎test/__snapshots__/CreateConfig.test.js.snap

+15
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,21 @@ Object {
740740
}
741741
`;
742742

743+
exports[`createConfig onListening option 1`] = `
744+
Object {
745+
"hot": true,
746+
"hotOnly": false,
747+
"noInfo": true,
748+
"onListening": [Function],
749+
"port": 8080,
750+
"publicPath": "/",
751+
"stats": Object {
752+
"cached": false,
753+
"cachedAssets": false,
754+
},
755+
}
756+
`;
757+
743758
exports[`createConfig open option (boolean) (in devServer config) 1`] = `
744759
Object {
745760
"hot": true,

‎test/onListening.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const testServer = require('./helpers/test-server');
4+
const config = require('./fixtures/simple-config/webpack.config');
5+
6+
describe('Before And After options', () => {
7+
let onListeningIsRunning = false;
8+
9+
beforeAll((done) => {
10+
testServer.start(
11+
config,
12+
{
13+
onListening: (devServer) => {
14+
if (!devServer) {
15+
throw new Error('webpack-dev-server is not defined');
16+
}
17+
18+
onListeningIsRunning = true;
19+
},
20+
},
21+
done
22+
);
23+
});
24+
25+
afterAll(testServer.close);
26+
27+
it('should handle before route', () => {
28+
expect(onListeningIsRunning).toBe(true);
29+
});
30+
});

‎test/options.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ describe('options', () => {
244244
success: [true],
245245
failure: [''],
246246
},
247+
onListening: {
248+
success: [() => {}],
249+
failure: [''],
250+
},
247251
open: {
248252
success: [true, ''],
249253
failure: [{}],

0 commit comments

Comments
 (0)
Please sign in to comment.