Skip to content

Commit 8c8a463

Browse files
committed
chore(serve): add handling for multiple devServer configs
1 parent 41d0b3d commit 8c8a463

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

packages/serve/src/createConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ export default function createConfig(args): any {
2323
delete options.hotOnly;
2424
}
2525

26+
// name is a valid CLI flag, but not a devServer option
27+
if (options.name) {
28+
delete options.name;
29+
}
30+
2631
return options;
2732
}
Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
1+
import logger from 'webpack-cli/lib/utils/logger';
2+
13
/**
24
*
35
* Get the devServer option from the user's compiler options
46
*
57
* @param {Object} compiler - webpack compiler
8+
* @param {Object} args - devServer args
69
*
710
* @returns {Object}
811
*/
9-
export default function getDevServerOptions(compiler): any {
10-
let devServerOptions;
11-
if (compiler.compilers) {
12-
// devServer options could be found in any of the compilers,
13-
// so simply find the first instance and use it, if there is one
14-
const comp = compiler.compilers.find((comp) => comp.options.devServer);
15-
if (comp) {
16-
devServerOptions = comp.options.devServer;
12+
export default function getDevServerOptions(compiler, args): any {
13+
const defaultOpts = {};
14+
const devServerOptions = [];
15+
const compilers = compiler.compilers || [compiler];
16+
if (args.name) {
17+
let comp = compilers.find((comp) => comp.name === args.name);
18+
// name could be an index to a compiler
19+
if (!comp && /^[0-9]$/.test(args.name)) {
20+
const index = +args.name;
21+
comp = compilers[index];
22+
}
23+
24+
if (comp && comp.options.devServer) {
25+
devServerOptions.push(comp.options.devServer);
26+
} else if (!comp) {
27+
// no compiler found
28+
logger.warn(`webpack config not found with name: ${comp.name}. Using default devServer config`);
29+
} else {
30+
// no devServer config found for compiler
31+
logger.warn('devServer config not found in specified webpack config. Using default devServer config');
1732
}
1833
} else {
19-
devServerOptions = compiler.options.devServer;
34+
compilers.forEach((comp) => {
35+
if (comp.options.devServer) {
36+
devServerOptions.push(comp.options.devServer);
37+
}
38+
});
39+
}
40+
41+
if (devServerOptions.length === 0) {
42+
devServerOptions.push(defaultOpts);
2043
}
21-
devServerOptions = devServerOptions || {};
2244

2345
return devServerOptions;
2446
}

packages/serve/src/startDevServer.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,27 @@ import mergeOptions from './mergeOptions';
1414
*/
1515
export default function startDevServer(compiler, args): void {
1616
const cliOptions = createConfig(args);
17-
const devServerOptions = getDevServerOptions(compiler);
18-
const options = mergeOptions(cliOptions, devServerOptions);
17+
const devServerOptions = getDevServerOptions(compiler, args);
1918

20-
options.host = options.host || 'localhost';
21-
options.port = options.port || 8080;
19+
const usedPorts = [];
20+
devServerOptions.forEach((devServerOpts): void => {
21+
const options = mergeOptions(cliOptions, devServerOpts);
2222

23-
const server = new Server(compiler, options);
24-
server.listen(options.port, options.host, (err): void => {
25-
if (err) {
26-
throw err;
23+
options.host = options.host || 'localhost';
24+
options.port = options.port || 8080;
25+
26+
if (usedPorts.find(options.port)) {
27+
throw new Error(
28+
'Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --name flag to specify your desired config.',
29+
);
2730
}
31+
usedPorts.push(options.port);
32+
33+
const server = new Server(compiler, options);
34+
server.listen(options.port, options.host, (err): void => {
35+
if (err) {
36+
throw err;
37+
}
38+
});
2839
});
2940
}

0 commit comments

Comments
 (0)