Skip to content

Commit 635b907

Browse files
refactor: dev server logic (#4666)
1 parent 129bb68 commit 635b907

1 file changed

Lines changed: 65 additions & 83 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 65 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,11 +1234,10 @@ class WebpackCLI implements IWebpackCLI {
12341234
const loadDevServerOptions = () => {
12351235
const devServer = require(WEBPACK_DEV_SERVER_PACKAGE);
12361236

1237-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1238-
const options: Record<string, any> = this.webpack.cli.getArguments(devServer.schema);
1237+
const options: Record<string, WebpackCLIBuiltInOption> = this.webpack.cli.getArguments(
1238+
devServer.schema,
1239+
) as unknown as Record<string, WebpackCLIBuiltInOption>;
12391240

1240-
// New options format
1241-
// { flag1: {}, flag2: {} }
12421241
return Object.keys(options).map((key) => {
12431242
options[key].name = key;
12441243

@@ -1249,40 +1248,35 @@ class WebpackCLI implements IWebpackCLI {
12491248
await this.makeCommand(
12501249
WebpackCLI.#commands.serve,
12511250
async () => {
1252-
let devServerFlags = [];
1253-
12541251
this.webpack = await this.loadWebpack();
12551252

1253+
let devServerOptions = [];
1254+
12561255
try {
1257-
devServerFlags = loadDevServerOptions();
1256+
devServerOptions = loadDevServerOptions();
12581257
} catch (error) {
12591258
this.logger.error(
12601259
`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`,
12611260
);
12621261
process.exit(2);
12631262
}
12641263

1265-
const builtInOptions = this.getBuiltInOptions();
1264+
const webpackOptions = this.getBuiltInOptions();
12661265

1267-
return [...builtInOptions, ...devServerFlags];
1266+
return [...webpackOptions, ...devServerOptions];
12681267
},
12691268
async (entries: string[], options) => {
12701269
const builtInOptions = this.getBuiltInOptions();
1271-
let devServerFlags = [];
1270+
let devServerFlags: WebpackCLIBuiltInOption[] = [];
12721271

12731272
try {
12741273
devServerFlags = loadDevServerOptions();
12751274
} catch {
12761275
// Nothing, to prevent future updates
12771276
}
12781277

1279-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1280-
const webpackCLIOptions: Record<string, any> = {};
1281-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1282-
const devServerCLIOptions: Record<string, any> = {};
1283-
1284-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1285-
const processors: ((opts: Record<string, any>) => void)[] = [];
1278+
const webpackCLIOptions: Partial<WebpackDevServerOptions> = {};
1279+
const devServerCLIOptions: Record<string, WebpackCLIBuiltInOption> = {};
12861280

12871281
for (const optionName in options) {
12881282
const kebabedOption = this.toKebabCase(optionName);
@@ -1293,25 +1287,16 @@ class WebpackCLI implements IWebpackCLI {
12931287
if (isBuiltInOption) {
12941288
webpackCLIOptions[optionName] = options[optionName];
12951289
} else {
1296-
const needToProcess = devServerFlags.find(
1297-
(devServerOption) =>
1298-
devServerOption.name === kebabedOption && devServerOption.processor,
1299-
);
1300-
1301-
if (needToProcess) {
1302-
processors.push(needToProcess.processor);
1303-
}
1304-
13051290
devServerCLIOptions[optionName] = options[optionName];
13061291
}
13071292
}
13081293

1309-
for (const processor of processors) {
1310-
processor(devServerCLIOptions);
1311-
}
1312-
13131294
if (entries.length > 0) {
1314-
webpackCLIOptions.entry = [...entries, ...(webpackCLIOptions.entry || [])];
1295+
// @ts-expect-error Need investigate
1296+
webpackCLIOptions.entry = [
1297+
...(entries as string[]),
1298+
...((webpackCLIOptions.entry || []) as string[]),
1299+
];
13151300
}
13161301

13171302
webpackCLIOptions.argv = {
@@ -1361,68 +1346,65 @@ class WebpackCLI implements IWebpackCLI {
13611346
continue;
13621347
}
13631348

1364-
const args = devServerFlags.reduce(
1365-
(accumulator, flag) => {
1366-
accumulator[flag.name] = flag;
1349+
const result = { ...compilerForDevServer.options.devServer };
13671350

1368-
return accumulator;
1369-
},
1370-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1371-
{} as Record<string, any>,
1372-
);
1373-
const values = Object.keys(devServerCLIOptions).reduce(
1374-
(accumulator, name) => {
1375-
const kebabName = this.toKebabCase(name);
1351+
const args: Record<string, Argument> = {};
13761352

1377-
if (args[kebabName]) {
1378-
accumulator[kebabName] = options[name];
1379-
}
1353+
for (const flag of devServerFlags) {
1354+
args[flag.name] = flag as unknown as Argument;
1355+
}
13801356

1381-
return accumulator;
1382-
},
1383-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1384-
{} as Record<string, any>,
1385-
);
1386-
const result = { ...compilerForDevServer.options.devServer };
1387-
const problems = this.webpack.cli.processArguments(args, result, values);
1388-
1389-
if (problems) {
1390-
const groupBy = <K extends keyof Problem & StringsKeys<Problem>>(
1391-
xs: Problem[],
1392-
key: K,
1393-
) =>
1394-
xs.reduce(
1395-
(rv: Record<string, Problem[]>, problem: Problem) => {
1396-
const path = problem[key];
1397-
1398-
(rv[path] ||= []).push(problem);
1399-
1400-
return rv;
1401-
},
1402-
{} as Record<string, Problem[]>,
1403-
);
1357+
const values: ProcessedArguments = {};
1358+
1359+
for (const name of Object.keys(options)) {
1360+
const kebabName = this.toKebabCase(name);
1361+
1362+
if (args[kebabName] !== undefined) {
1363+
values[kebabName] = options[name];
1364+
}
1365+
}
14041366

1405-
const problemsByPath = groupBy<"path">(problems, "path");
1367+
if (Object.keys(values).length > 0) {
1368+
const problems = this.webpack.cli.processArguments(args, result, values);
14061369

1407-
for (const path in problemsByPath) {
1408-
const problems = problemsByPath[path];
1370+
if (problems) {
1371+
const groupBy = <K extends keyof Problem & StringsKeys<Problem>>(
1372+
xs: Problem[],
1373+
key: K,
1374+
) =>
1375+
xs.reduce(
1376+
(rv: Record<string, Problem[]>, problem: Problem) => {
1377+
const path = problem[key];
14091378

1410-
for (const problem of problems) {
1411-
this.logger.error(
1412-
`${this.capitalizeFirstLetter(problem.type.replace("-", " "))}${
1413-
problem.value ? ` '${problem.value}'` : ""
1414-
} for the '--${problem.argument}' option${
1415-
problem.index ? ` by index '${problem.index}'` : ""
1416-
}`,
1379+
(rv[path] ||= []).push(problem);
1380+
1381+
return rv;
1382+
},
1383+
{} as Record<string, Problem[]>,
14171384
);
14181385

1419-
if (problem.expected) {
1420-
this.logger.error(`Expected: '${problem.expected}'`);
1386+
const problemsByPath = groupBy<"path">(problems, "path");
1387+
1388+
for (const path in problemsByPath) {
1389+
const problems = problemsByPath[path];
1390+
1391+
for (const problem of problems) {
1392+
this.logger.error(
1393+
`${this.capitalizeFirstLetter(problem.type.replace("-", " "))}${
1394+
problem.value ? ` '${problem.value}'` : ""
1395+
} for the '--${problem.argument}' option${
1396+
problem.index ? ` by index '${problem.index}'` : ""
1397+
}`,
1398+
);
1399+
1400+
if (problem.expected) {
1401+
this.logger.error(`Expected: '${problem.expected}'`);
1402+
}
14211403
}
14221404
}
1423-
}
14241405

1425-
process.exit(2);
1406+
process.exit(2);
1407+
}
14261408
}
14271409

14281410
const devServerOptions: WebpackDevServerOptions = result as WebpackDevServerOptions;
@@ -2529,7 +2511,7 @@ class WebpackCLI implements IWebpackCLI {
25292511
}
25302512

25312513
if (Object.keys(values).length > 0) {
2532-
const problems: Problem[] | null = this.webpack.cli.processArguments(args, item, values);
2514+
const problems = this.webpack.cli.processArguments(args, item, values);
25332515

25342516
if (problems) {
25352517
const groupBy = <K extends keyof Problem & StringsKeys<Problem>>(xs: Problem[], key: K) =>

0 commit comments

Comments
 (0)