|
| 1 | +import logger from 'webpack-cli/lib/utils/logger'; |
| 2 | + |
1 | 3 | /** |
2 | 4 | * |
3 | 5 | * Get the devServer option from the user's compiler options |
4 | 6 | * |
5 | 7 | * @param {Object} compiler - webpack compiler |
| 8 | + * @param {Object} args - devServer args |
6 | 9 | * |
7 | 10 | * @returns {Object} |
8 | 11 | */ |
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'); |
17 | 32 | } |
18 | 33 | } 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); |
20 | 43 | } |
21 | | - devServerOptions = devServerOptions || {}; |
22 | 44 |
|
23 | 45 | return devServerOptions; |
24 | 46 | } |
0 commit comments