@@ -10,6 +10,7 @@ const { toKebabCase } = require('./utils/helpers');
10
10
const assignFlagDefaults = require ( './utils/flag-defaults' ) ;
11
11
const { writeFileSync } = require ( 'fs' ) ;
12
12
const { options : coloretteOptions } = require ( 'colorette' ) ;
13
+ const WebpackCLIPlugin = require ( './plugins/WebpackCLIPlugin' ) ;
13
14
14
15
// CLI arg resolvers
15
16
const handleConfigResolution = require ( './groups/ConfigGroup' ) ;
@@ -212,24 +213,27 @@ class WebpackCLI extends GroupHelper {
212
213
return this . runOptionGroups ( args ) ;
213
214
}
214
215
215
- createCompiler ( options ) {
216
+ handleError ( error ) {
217
+ // https://github.com/webpack/webpack/blob/master/lib/index.js#L267
218
+ // https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
219
+ const ValidationError = webpack . ValidationError || webpack . WebpackOptionsValidationError ;
220
+
221
+ // In case of schema errors print and exit process
222
+ // For webpack@4 and webpack@5
223
+ if ( error instanceof ValidationError ) {
224
+ logger . error ( error . message ) ;
225
+ } else {
226
+ logger . error ( error ) ;
227
+ }
228
+ }
229
+
230
+ createCompiler ( options , callback ) {
216
231
let compiler ;
217
232
218
233
try {
219
- compiler = webpack ( options ) ;
234
+ compiler = webpack ( options , callback ) ;
220
235
} catch ( error ) {
221
- // https://github.com/webpack/webpack/blob/master/lib/index.js#L267
222
- // https://github.com/webpack/webpack/blob/v4.44.2/lib/webpack.js#L90
223
- const ValidationError = webpack . ValidationError ? webpack . ValidationError : webpack . WebpackOptionsValidationError ;
224
-
225
- // In case of schema errors print and exit process
226
- // For webpack@4 and webpack@5
227
- if ( error instanceof ValidationError ) {
228
- logger . error ( error . message ) ;
229
- } else {
230
- logger . error ( error ) ;
231
- }
232
-
236
+ this . handleError ( error ) ;
233
237
process . exit ( 2 ) ;
234
238
}
235
239
@@ -245,54 +249,35 @@ class WebpackCLI extends GroupHelper {
245
249
async run ( args , cliOptions ) {
246
250
await this . processArgs ( args , cliOptions ) ;
247
251
248
- const compiler = this . createCompiler ( this . compilerConfiguration ) ;
249
-
250
- const options = this . compilerConfiguration ;
251
- const outputOptions = this . outputConfiguration ;
252
-
253
- if ( outputOptions . interactive ) {
254
- const interactive = require ( './utils/interactive' ) ;
252
+ let compiler ;
255
253
256
- return interactive ( compiler , options , outputOptions ) ;
257
- }
254
+ let options = this . compilerConfiguration ;
255
+ let outputOptions = this . outputConfiguration ;
258
256
259
- const compilers = compiler . compilers ? compiler . compilers : [ compiler ] ;
260
- const isWatchMode = Boolean ( compilers . find ( ( compiler ) => compiler . options . watch ) ) ;
261
257
const isRawOutput = typeof outputOptions . json === 'undefined' ;
262
258
263
259
if ( isRawOutput ) {
264
- for ( const compiler of compilers ) {
265
- if ( outputOptions . progress ) {
266
- const { ProgressPlugin } = webpack ;
267
-
268
- let progressPluginExists ;
269
-
270
- if ( compiler . options . plugins ) {
271
- progressPluginExists = Boolean ( compiler . options . plugins . find ( ( e ) => e instanceof ProgressPlugin ) ) ;
272
- }
260
+ const webpackCLIPlugin = new WebpackCLIPlugin ( {
261
+ progress : outputOptions . progress ,
262
+ } ) ;
273
263
274
- if ( ! progressPluginExists ) {
275
- new ProgressPlugin ( ) . apply ( compiler ) ;
276
- }
264
+ const addPlugin = ( options ) => {
265
+ if ( ! options . plugins ) {
266
+ options . plugins = [ ] ;
277
267
}
268
+ options . plugins . unshift ( webpackCLIPlugin ) ;
269
+ } ;
270
+ if ( Array . isArray ( options ) ) {
271
+ options . forEach ( addPlugin ) ;
272
+ } else {
273
+ addPlugin ( options ) ;
278
274
}
279
-
280
- compiler . hooks . watchRun . tap ( 'watchInfo' , ( compilation ) => {
281
- if ( compilation . options . bail && isWatchMode ) {
282
- logger . warn ( 'You are using "bail" with "watch". "bail" will still exit webpack when the first error is found.' ) ;
283
- }
284
-
285
- logger . success ( `Compilation${ compilation . name ? `${ compilation . name } ` : '' } starting...` ) ;
286
- } ) ;
287
- compiler . hooks . done . tap ( 'watchInfo' , ( compilation ) => {
288
- logger . success ( `Compilation${ compilation . name ? `${ compilation . name } ` : '' } finished` ) ;
289
- } ) ;
290
275
}
291
276
292
277
const callback = ( error , stats ) => {
293
278
if ( error ) {
294
- logger . error ( error ) ;
295
- process . exit ( 1 ) ;
279
+ this . handleError ( error ) ;
280
+ process . exit ( 2 ) ;
296
281
}
297
282
298
283
if ( stats . hasErrors ( ) ) {
@@ -314,9 +299,11 @@ class WebpackCLI extends GroupHelper {
314
299
return stats ;
315
300
} ;
316
301
302
+ const getStatsOptionsFromCompiler = ( compiler ) => getStatsOptions ( compiler . options ? compiler . options . stats : undefined ) ;
303
+
317
304
const foundStats = compiler . compilers
318
- ? { children : compiler . compilers . map ( ( compiler ) => getStatsOptions ( compiler . options . stats ) ) }
319
- : getStatsOptions ( compiler . options . stats ) ;
305
+ ? { children : compiler . compilers . map ( getStatsOptionsFromCompiler ) }
306
+ : getStatsOptionsFromCompiler ( compiler ) ;
320
307
321
308
if ( outputOptions . json === true ) {
322
309
process . stdout . write ( JSON . stringify ( stats . toJson ( foundStats ) , null , 2 ) + '\n' ) ;
@@ -335,46 +322,17 @@ class WebpackCLI extends GroupHelper {
335
322
} else {
336
323
logger . raw ( `${ stats . toString ( foundStats ) } ` ) ;
337
324
}
338
-
339
- if ( isWatchMode ) {
340
- logger . success ( 'watching files for updates...' ) ;
341
- }
342
325
} ;
343
326
344
- if ( isWatchMode ) {
345
- const watchOptions = ( compiler . options && compiler . options . watchOptions ) || { } ;
327
+ compiler = this . createCompiler ( options , callback ) ;
346
328
347
- if ( watchOptions . stdin ) {
348
- process . stdin . on ( 'end' , function ( ) {
349
- process . exit ( ) ;
350
- } ) ;
351
- process . stdin . resume ( ) ;
352
- }
353
-
354
- return new Promise ( ( resolve ) => {
355
- compiler . watch ( watchOptions , ( error , stats ) => {
356
- callback ( error , stats ) ;
329
+ if ( compiler && outputOptions . interactive ) {
330
+ const interactive = require ( './utils/interactive' ) ;
357
331
358
- resolve ( ) ;
359
- } ) ;
360
- } ) ;
361
- } else {
362
- return new Promise ( ( resolve ) => {
363
- compiler . run ( ( error , stats ) => {
364
- if ( compiler . close ) {
365
- compiler . close ( ( ) => {
366
- callback ( error , stats ) ;
367
-
368
- resolve ( ) ;
369
- } ) ;
370
- } else {
371
- callback ( error , stats ) ;
372
-
373
- resolve ( ) ;
374
- }
375
- } ) ;
376
- } ) ;
332
+ interactive ( compiler , options , outputOptions ) ;
377
333
}
334
+
335
+ return Promise . resolve ( ) ;
378
336
}
379
337
}
380
338
0 commit comments