Skip to content

Commit 422e374

Browse files
refactor: code
1 parent 80e6e55 commit 422e374

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

packages/serve/src/index.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,31 @@ class ServeCommand {
66
const { logger, webpack } = cli;
77

88
const loadDevServerOptions = () => {
9+
// TODO simplify this after drop webpack v4 and webpack-dev-server v3
910
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
1011
const devServer = require("webpack-dev-server");
12+
const isNewDevServerCLIAPI = typeof devServer.schema !== "undefined";
1113

12-
// TODO only keep `getArguments` after drop webpack v4
14+
let options = {};
1315

14-
const options =
15-
typeof devServer.getArguments === "function"
16-
? devServer.getArguments(webpack)
17-
: // eslint-disable-next-line node/no-extraneous-require
18-
require("webpack-dev-server/bin/cli-flags");
16+
if (isNewDevServerCLIAPI) {
17+
if (typeof webpack.cli.getArguments === "function") {
18+
options = webpack.cli.getArguments(devServer.schema);
19+
} else {
20+
options = devServer.cli.getArguments();
21+
}
22+
} else {
23+
// eslint-disable-next-line node/no-extraneous-require
24+
options = require("webpack-dev-server/bin/cli-flags");
25+
}
1926

2027
// Old options format
2128
// { devServer: [{...}, {}...] }
29+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
30+
// @ts-ignore
2231
if (options.devServer) {
32+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
33+
// @ts-ignore
2334
return options.devServer;
2435
}
2536

@@ -149,25 +160,26 @@ class ServeCommand {
149160

150161
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
151162
const devServer = require("webpack-dev-server");
163+
const isNewDevServerCLIAPI = typeof devServer.schema !== "undefined";
152164

153-
if (typeof devServer.processArguments === "function") {
165+
if (isNewDevServerCLIAPI) {
154166
const args = devServerFlags.reduce((accumulator, flag) => {
155167
accumulator[flag.name] = flag;
156-
157168
return accumulator;
158169
}, {});
159170
const values = Object.keys(devServerOptions).reduce((accumulator, name) => {
160171
const kebabName = cli.utils.toKebabCase(name);
161-
162172
if (args[kebabName]) {
163173
accumulator[kebabName] = options[name];
164174
}
165-
166175
return accumulator;
167176
}, {});
168-
const result = { ...compiler.options.devServer };
169-
170-
const problems = devServer.processArguments(cli.webpack, args, result, values);
177+
const result = Object.assign({}, compiler.options.devServer);
178+
const problems = (
179+
typeof webpack.cli.processArguments === "function"
180+
? webpack.cli
181+
: devServer.cli
182+
).processArguments(args, result, values);
171183

172184
if (problems) {
173185
const groupBy = (xs, key) => {
@@ -177,11 +189,11 @@ class ServeCommand {
177189
return rv;
178190
}, {});
179191
};
192+
180193
const problemsByPath = groupBy(problems, "path");
181194

182195
for (const path in problemsByPath) {
183196
const problems = problemsByPath[path];
184-
185197
problems.forEach((problem) => {
186198
cli.logger.error(
187199
`${cli.utils.capitalizeFirstLetter(

0 commit comments

Comments
 (0)