Skip to content

Commit

Permalink
feat: onListening option
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 29, 2019
1 parent 87cfaa7 commit f831e3f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ class Server {
}

setupBeforeFeature() {
// Todo rename onBeforeSetupMiddleware in next major release
// Todo pass only `this` argument
this.options.before(this.app, this, this.compiler);
}

Expand All @@ -442,6 +444,8 @@ class Server {
}

setupAfterFeature() {
// Todo rename onAfterSetupMiddleware in next major release
// Todo pass only `this` argument
this.options.after(this.app, this, this.compiler);
}

Expand Down Expand Up @@ -757,6 +761,10 @@ class Server {
if (fn) {
fn.call(this.listeningApp, err);
}

if (typeof this.options.onListening === 'function') {
this.options.onListening(this);
}
});
}

Expand Down
4 changes: 4 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
"noInfo": {
"type": "boolean"
},
"onListening": {
"instanceof": "Function"
},
"open": {
"anyOf": [
{
Expand Down Expand Up @@ -402,6 +405,7 @@
"logTime": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#logtime)",
"mimeTypes": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devservermimetypes-)",
"noInfo": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservernoinfo-)",
"onListening": "should be {Function} (https://webpack.js.org/configuration/dev-server/#onlistening)",
"open": "should be {String|Boolean} (https://webpack.js.org/configuration/dev-server/#devserveropen)",
"openPage": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserveropenpage)",
"overlay": "should be {Boolean|Object} (https://webpack.js.org/configuration/dev-server/#devserveroverlay)",
Expand Down
12 changes: 12 additions & 0 deletions test/CreateConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,4 +967,16 @@ describe('createConfig', () => {
).toMatchSnapshot();
expect(webpackConfigNoStats).toMatchSnapshot();
});

it('onListening option', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { onListening: () => {} },
}),
argv,
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});
});
15 changes: 15 additions & 0 deletions test/__snapshots__/CreateConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,21 @@ Object {
}
`;

exports[`createConfig onListening option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"noInfo": true,
"onListening": [Function],
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig open option (boolean) (in devServer config) 1`] = `
Object {
"hot": true,
Expand Down
30 changes: 30 additions & 0 deletions test/onListening.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const testServer = require('./helpers/test-server');
const config = require('./fixtures/simple-config/webpack.config');

describe('Before And After options', () => {
let onListeningIsRunning = false;

beforeAll((done) => {
testServer.start(
config,
{
onListening: (devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}

onListeningIsRunning = true;
},
},
done
);
});

afterAll(testServer.close);

it('should handle before route', () => {
expect(onListeningIsRunning).toBe(true);
});
});
4 changes: 4 additions & 0 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ describe('options', () => {
success: [true],
failure: [''],
},
onListening: {
success: [() => {}],
failure: [''],
},
open: {
success: [true, ''],
failure: [{}],
Expand Down

0 comments on commit f831e3f

Please sign in to comment.