/
startDevServer.ts
34 lines (31 loc) · 1.07 KB
/
startDevServer.ts
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
/**
*
* Starts the devServer
*
* @param {Object} compiler - a webpack compiler
* @param {Object} options - devServer options
*
* @returns {Void}
*/
export default function startDevServer(compiler, options): void {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Server = require('webpack-dev-server/lib/Server');
const firstWpOpt = compiler.compilers ? compiler.compilers[0].options : compiler.options;
const devServerOptions = firstWpOpt.devServer || {};
const host = options.host || devServerOptions.host || 'localhost';
const port = options.port || devServerOptions.port || 8080;
// socket should not have a default value, because it should only be used if the
// user explicitly provides it
const socket = options.socket || devServerOptions.socket;
options.host = host;
options.port = port;
if (socket) {
options.socket = socket;
}
const server = new Server(compiler, options);
server.listen(socket || port, host, (err): void => {
if (err) {
throw err;
}
});
}