@@ -3,15 +3,34 @@ import startDevServer from "./startDevServer";
3
3
class ServeCommand {
4
4
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
5
5
async apply ( cli : any ) : Promise < void > {
6
- const { logger } = cli ;
6
+ const { logger, webpack } = cli ;
7
7
8
8
const loadDevServerOptions = ( ) => {
9
+ // TODO simplify this after drop webpack v4 and webpack-dev-server v3
9
10
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
10
- const options = require ( "webpack-dev-server/bin/cli-flags" ) ;
11
+ const devServer = require ( "webpack-dev-server" ) ;
12
+ const isNewDevServerCLIAPI = typeof devServer . schema !== "undefined" ;
13
+
14
+ let options = { } ;
15
+
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
+ }
11
26
12
27
// Old options format
13
28
// { devServer: [{...}, {}...] }
29
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
30
+ // @ts -ignore
14
31
if ( options . devServer ) {
32
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
+ // @ts -ignore
15
34
return options . devServer ;
16
35
}
17
36
@@ -64,7 +83,7 @@ class ServeCommand {
64
83
// eslint-disable-next-line @typescript-eslint/no-explicit-any
65
84
const webpackOptions : Record < string , any > = { } ;
66
85
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67
- const devServerOptions : Record < string , any > = { } ;
86
+ let devServerOptions : Record < string , any > = { } ;
68
87
69
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70
89
const processors : Array < ( opts : Record < string , any > ) => void > = [ ] ;
@@ -139,6 +158,65 @@ class ServeCommand {
139
158
process . stdin . resume ( ) ;
140
159
}
141
160
161
+ // eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-extraneous-require
162
+ const devServer = require ( "webpack-dev-server" ) ;
163
+ const isNewDevServerCLIAPI = typeof devServer . schema !== "undefined" ;
164
+
165
+ if ( isNewDevServerCLIAPI ) {
166
+ const args = devServerFlags . reduce ( ( accumulator , flag ) => {
167
+ accumulator [ flag . name ] = flag ;
168
+ return accumulator ;
169
+ } , { } ) ;
170
+ const values = Object . keys ( devServerOptions ) . reduce ( ( accumulator , name ) => {
171
+ const kebabName = cli . utils . toKebabCase ( name ) ;
172
+ if ( args [ kebabName ] ) {
173
+ accumulator [ kebabName ] = options [ name ] ;
174
+ }
175
+ return accumulator ;
176
+ } , { } ) ;
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 ) ;
183
+
184
+ if ( problems ) {
185
+ const groupBy = ( xs , key ) => {
186
+ return xs . reduce ( ( rv , x ) => {
187
+ ( rv [ x [ key ] ] = rv [ x [ key ] ] || [ ] ) . push ( x ) ;
188
+
189
+ return rv ;
190
+ } , { } ) ;
191
+ } ;
192
+
193
+ const problemsByPath = groupBy ( problems , "path" ) ;
194
+
195
+ for ( const path in problemsByPath ) {
196
+ const problems = problemsByPath [ path ] ;
197
+ problems . forEach ( ( problem ) => {
198
+ cli . logger . error (
199
+ `${ cli . utils . capitalizeFirstLetter (
200
+ problem . type . replace ( / - / g, " " ) ,
201
+ ) } ${ problem . value ? ` '${ problem . value } '` : "" } for the '--${
202
+ problem . argument
203
+ } ' option${
204
+ problem . index ? ` by index '${ problem . index } '` : ""
205
+ } `,
206
+ ) ;
207
+
208
+ if ( problem . expected ) {
209
+ cli . logger . error ( `Expected: '${ problem . expected } '` ) ;
210
+ }
211
+ } ) ;
212
+ }
213
+
214
+ process . exit ( 2 ) ;
215
+ }
216
+
217
+ devServerOptions = result ;
218
+ }
219
+
142
220
try {
143
221
servers = await startDevServer ( compiler , devServerOptions , options , logger ) ;
144
222
} catch ( error ) {
0 commit comments