|
1 | 1 | import { type Compiler, type cli } from "webpack"; |
2 | | -import { type IWebpackCLI, type StringsKeys, type WebpackDevServerOptions } from "webpack-cli"; |
| 2 | +import { |
| 3 | + type IWebpackCLI, |
| 4 | + type StringsKeys, |
| 5 | + WebpackCLICommandOption, |
| 6 | + type WebpackDevServerOptions, |
| 7 | +} from "webpack-cli"; |
3 | 8 |
|
4 | 9 | const WEBPACK_PACKAGE = process.env.WEBPACK_PACKAGE || "webpack"; |
5 | 10 | const WEBPACK_DEV_SERVER_PACKAGE = process.env.WEBPACK_DEV_SERVER_PACKAGE || "webpack-dev-server"; |
6 | 11 |
|
7 | 12 | type Problem = NonNullable<ReturnType<(typeof cli)["processArguments"]>>[0]; |
8 | 13 |
|
9 | 14 | class ServeCommand { |
10 | | - async apply(cli: IWebpackCLI): Promise<void> { |
11 | | - const loadDevServerOptions = () => { |
12 | | - const devServer = require(WEBPACK_DEV_SERVER_PACKAGE); |
| 15 | + async apply(cli: IWebpackCLI, options: WebpackCLICommandOption[]): Promise<void> { |
| 16 | + const devServer = require(WEBPACK_DEV_SERVER_PACKAGE); |
13 | 17 |
|
| 18 | + const devServerFlags = Object.entries( |
14 | 19 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
15 | | - const options: Record<string, any> = cli.webpack.cli.getArguments(devServer.schema); |
16 | | - // New options format |
17 | | - // { flag1: {}, flag2: {} } |
18 | | - return Object.keys(options).map((key) => { |
19 | | - options[key].name = key; |
20 | | - |
21 | | - return options[key]; |
22 | | - }); |
23 | | - }; |
24 | | - |
25 | | - await cli.makeCommand( |
26 | | - { |
27 | | - name: "serve [entries...]", |
28 | | - alias: ["server", "s"], |
29 | | - description: "Run the webpack dev server and watch for source file changes while serving.", |
30 | | - usage: "[entries...] [options]", |
31 | | - pkg: "@webpack-cli/serve", |
32 | | - dependencies: [WEBPACK_PACKAGE, WEBPACK_DEV_SERVER_PACKAGE], |
33 | | - }, |
34 | | - async () => { |
35 | | - let devServerFlags = []; |
36 | | - |
37 | | - cli.webpack = await cli.loadWebpack(); |
38 | | - |
39 | | - try { |
40 | | - devServerFlags = loadDevServerOptions(); |
41 | | - } catch (error) { |
42 | | - cli.logger.error( |
43 | | - `You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`, |
44 | | - ); |
45 | | - process.exit(2); |
46 | | - } |
47 | | - |
48 | | - const builtInOptions = cli.getBuiltInOptions(); |
49 | | - |
50 | | - return [...builtInOptions, ...devServerFlags]; |
51 | | - }, |
| 20 | + cli.webpack.cli.getArguments(devServer.schema) as Record<string, any>, |
| 21 | + ).map(([name, option]) => ({ name, ...option, group: "core", hidden: true })); |
| 22 | + |
| 23 | + await cli.makeCommand({ |
| 24 | + name: "serve [entries...]", |
| 25 | + alias: ["server", "s"], |
| 26 | + description: "Run the webpack dev server and watch for source file changes while serving.", |
| 27 | + options: [...options, ...devServerFlags.flatMap(cli.makeOption.bind(cli))], |
| 28 | + dependencies: [WEBPACK_PACKAGE, WEBPACK_DEV_SERVER_PACKAGE], |
52 | 29 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
53 | | - async (entries: string[], options: any) => { |
| 30 | + async action(entries: string[], options: any) { |
54 | 31 | const builtInOptions = cli.getBuiltInOptions(); |
55 | | - let devServerFlags = []; |
56 | | - |
57 | | - try { |
58 | | - devServerFlags = loadDevServerOptions(); |
59 | | - } catch { |
60 | | - // Nothing, to prevent future updates |
61 | | - } |
62 | 32 |
|
63 | 33 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
64 | 34 | const webpackCLIOptions: Record<string, any> = {}; |
@@ -243,7 +213,7 @@ class ServeCommand { |
243 | 213 | process.exit(2); |
244 | 214 | } |
245 | 215 | }, |
246 | | - ); |
| 216 | + }); |
247 | 217 | } |
248 | 218 | } |
249 | 219 |
|
|
0 commit comments