Skip to content

Commit 73d3fec

Browse files
fix: respect --stats, --color and --no-color option for serve c… (#2312)
1 parent 4e21053 commit 73d3fec

19 files changed

+234
-414
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"strip-ansi": "^6.0.0",
9191
"ts-jest": "^26.4.3",
9292
"typescript": "^3.9.7",
93-
"webpack": "^5.11.0",
93+
"webpack": "^5.11.1",
9494
"webpack-bundle-analyzer": "^4.3.0",
9595
"webpack-dev-server": "^3.11.1",
9696
"yeoman-test": "^2.7.0"

packages/serve/__tests__/__snapshots__/startDevServer.test.ts.snap

-89
This file was deleted.

packages/serve/__tests__/startDevServer.test.ts

-173
This file was deleted.

packages/serve/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class ServeCommand {
108108
}
109109

110110
try {
111-
servers = await startDevServer(compiler, devServerOptions, logger);
111+
servers = await startDevServer(compiler, devServerOptions, options, logger);
112112
} catch (error) {
113113
if (error.name === 'ValidationError') {
114114
logger.error(error.message);

packages/serve/src/startDevServer.ts

+40-14
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { devServerOptionsType } from './types';
55
* Starts the devServer
66
*
77
* @param {Object} compiler - a webpack compiler
8-
* @param {Object} cliOptions - devServer args
8+
* @param {Object} devServerCliOptions - dev server CLI options
9+
* @param {Object} cliOptions - CLI options
910
* @param {Object} logger - logger
1011
*
1112
* @returns {Object[]} array of resulting servers
1213
*/
13-
export default async function startDevServer(compiler, cliOptions, logger): Promise<object[]> {
14+
export default async function startDevServer(compiler, devServerCliOptions, cliOptions, logger): Promise<object[]> {
1415
let devServerVersion, Server, findPort;
1516

1617
try {
@@ -25,15 +26,15 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
2526
process.exit(2);
2627
}
2728

28-
const mergeOptions = (cliOptions: devServerOptionsType, devServerOptions: devServerOptionsType): devServerOptionsType => {
29+
const mergeOptions = (devServerOptions: devServerOptionsType, devServerCliOptions: devServerOptionsType): devServerOptionsType => {
2930
// CLI options should take precedence over devServer options,
3031
// and CLI options should have no default values included
31-
const options = { ...devServerOptions, ...cliOptions };
32+
const options = { ...devServerOptions, ...devServerCliOptions };
3233

33-
if (devServerOptions.client && cliOptions.client) {
34+
if (devServerOptions.client && devServerCliOptions.client) {
3435
// the user could set some client options in their devServer config,
3536
// then also specify client options on the CLI
36-
options.client = { ...devServerOptions.client, ...cliOptions.client };
37+
options.client = { ...devServerOptions.client, ...devServerCliOptions.client };
3738
}
3839

3940
return options;
@@ -59,23 +60,48 @@ export default async function startDevServer(compiler, cliOptions, logger): Prom
5960
const devServersOptions = [];
6061

6162
for (const compilerWithDevServerOption of compilersWithDevServerOption) {
62-
const options = mergeOptions(cliOptions, compilerWithDevServerOption.options.devServer || {});
63+
const options = mergeOptions(compilerWithDevServerOption.options.devServer || {}, devServerCliOptions);
6364

6465
if (isDevServer4) {
6566
options.port = await findPort(options.port);
6667
options.client = options.client || {};
6768
options.client.port = options.client.port || options.port;
6869
} else {
69-
if (!options.publicPath) {
70-
options.publicPath =
71-
typeof compilerWithDevServerOption.options.output.publicPath === 'undefined' ||
72-
compilerWithDevServerOption.options.output.publicPath === 'auto'
73-
? '/'
74-
: compilerWithDevServerOption.options.output.publicPath;
75-
}
70+
const getPublicPathOption = () => {
71+
const normalizePublicPath = (publicPath) => (typeof publicPath === 'undefined' || publicPath === 'auto' ? '/' : publicPath);
72+
73+
if (cliOptions.outputPublicPath) {
74+
return normalizePublicPath(compilerWithDevServerOption.options.output.publicPath);
75+
}
76+
77+
// webpack-dev-server@3
78+
if (options.publicPath) {
79+
return normalizePublicPath(options.publicPath);
80+
}
81+
82+
// webpack-dev-server@4
83+
if (options.dev && options.dev.publicPath) {
84+
return normalizePublicPath(options.dev.publicPath);
85+
}
86+
87+
return normalizePublicPath(compilerWithDevServerOption.options.output.publicPath);
88+
};
89+
const getStatsOption = () => {
90+
if (cliOptions.stats) {
91+
return compilerWithDevServerOption.options.stats;
92+
}
93+
94+
if (options.stats) {
95+
return options.stats;
96+
}
97+
98+
return compilerWithDevServerOption.options.stats;
99+
};
76100

77101
options.host = options.host || 'localhost';
78102
options.port = options.port || 8080;
103+
options.stats = getStatsOption();
104+
options.publicPath = getPublicPathOption();
79105
}
80106

81107
if (options.port) {

packages/serve/src/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export type devServerOptionsType = {
22
bonjour?: boolean;
33
client?: devServerClientOptions;
44
compress?: boolean;
5-
dev?: object;
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
dev?: Record<string, any>;
67
firewall?: boolean | string[];
78
headers?: object;
89
historyApiFallback?: boolean | object;
@@ -28,6 +29,8 @@ export type devServerOptionsType = {
2829
transportMode?: object | string;
2930
useLocalIp?: boolean;
3031
publicPath?: undefined;
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
stats?: any;
3134
};
3235

3336
type devServerClientOptions = {

0 commit comments

Comments
 (0)