@@ -31,7 +31,7 @@ class WebpackCLI {
31
31
32
32
async makeCommand ( commandOptions , options , action ) {
33
33
const alreadyLoaded = this . program . commands . find (
34
- ( command ) => command . name ( ) === commandOptions . name || command . alias ( ) === commandOptions . alias ,
34
+ ( command ) => command . name ( ) === commandOptions . name || command . aliases ( ) . includes ( commandOptions . alias ) ,
35
35
) ;
36
36
37
37
if ( alreadyLoaded ) {
@@ -229,9 +229,9 @@ class WebpackCLI {
229
229
230
230
async run ( args , parseOptions ) {
231
231
// Built-in internal commands
232
- const bundleCommandOptions = {
233
- name : 'bundle ' ,
234
- alias : 'b' ,
232
+ const buildCommandOptions = {
233
+ name : 'build ' ,
234
+ alias : [ 'bundle' , 'b' ] ,
235
235
description : 'Run webpack (default command, can be omitted).' ,
236
236
usage : '[options]' ,
237
237
} ;
@@ -286,8 +286,25 @@ class WebpackCLI {
286
286
} ,
287
287
] ;
288
288
289
- const knownCommands = [ bundleCommandOptions , versionCommandOptions , helpCommandOptions , ...externalBuiltInCommandsInfo ] ;
290
- const isKnownCommand = ( name ) => knownCommands . find ( ( command ) => command . name === name || command . alias === name ) ;
289
+ const knownCommands = [ buildCommandOptions , versionCommandOptions , helpCommandOptions , ...externalBuiltInCommandsInfo ] ;
290
+ const isKnownCommand = ( name ) =>
291
+ knownCommands . find (
292
+ ( command ) =>
293
+ command . name === name || ( Array . isArray ( command . alias ) ? command . alias . includes ( name ) : command . alias === name ) ,
294
+ ) ;
295
+ const isBuildCommand = ( name ) =>
296
+ buildCommandOptions . name === name ||
297
+ ( Array . isArray ( buildCommandOptions . alias ) ? buildCommandOptions . alias . includes ( name ) : buildCommandOptions . alias === name ) ;
298
+ const isHelpCommand = ( name ) =>
299
+ helpCommandOptions . name === name ||
300
+ ( Array . isArray ( helpCommandOptions . alias ) ? helpCommandOptions . alias . includes ( name ) : helpCommandOptions . alias === name ) ;
301
+ const isVersionCommand = ( name ) =>
302
+ versionCommandOptions . name === name ||
303
+ ( Array . isArray ( versionCommandOptions . alias )
304
+ ? versionCommandOptions . alias . includes ( name )
305
+ : versionCommandOptions . alias === name ) ;
306
+ const findCommandByName = ( name ) =>
307
+ this . program . commands . find ( ( command ) => name === command . name ( ) || command . alias ( ) . includes ( name ) ) ;
291
308
292
309
const getCommandNameAndOptions = ( args ) => {
293
310
let commandName ;
@@ -311,16 +328,15 @@ class WebpackCLI {
311
328
312
329
const isDefault = typeof commandName === 'undefined' ;
313
330
314
- return { commandName : isDefault ? bundleCommandOptions . name : commandName , options, isDefault } ;
331
+ return { commandName : isDefault ? buildCommandOptions . name : commandName , options, isDefault } ;
315
332
} ;
316
333
const loadCommandByName = async ( commandName , allowToInstall = false ) => {
317
- if ( commandName === bundleCommandOptions . name || commandName === bundleCommandOptions . alias ) {
318
- // Make `bundle|b [options]` command
319
- await this . makeCommand ( bundleCommandOptions , this . getBuiltInOptions ( ) , async ( program ) => {
334
+ if ( isBuildCommand ( commandName ) ) {
335
+ await this . makeCommand ( buildCommandOptions , this . getBuiltInOptions ( ) , async ( program ) => {
320
336
const options = program . opts ( ) ;
321
337
322
338
if ( program . args . length > 0 ) {
323
- const possibleCommands = [ ] . concat ( [ bundleCommandOptions . name ] ) . concat ( program . args ) ;
339
+ const possibleCommands = [ ] . concat ( [ buildCommandOptions . name ] ) . concat ( program . args ) ;
324
340
325
341
logger . error ( 'Running multiple commands at the same time is not possible' ) ;
326
342
logger . error ( `Found commands: ${ possibleCommands . map ( ( item ) => `'${ item } '` ) . join ( ', ' ) } ` ) ;
@@ -330,16 +346,19 @@ class WebpackCLI {
330
346
331
347
await this . bundleCommand ( options ) ;
332
348
} ) ;
333
- } else if ( commandName === helpCommandOptions . name || commandName === helpCommandOptions . alias ) {
349
+ } else if ( isHelpCommand ( commandName ) ) {
334
350
// Stub for the `help` command
335
351
this . makeCommand ( helpCommandOptions , [ ] , ( ) => { } ) ;
336
- } else if ( commandName === versionCommandOptions . name || commandName === helpCommandOptions . alias ) {
352
+ } else if ( isVersionCommand ( commandName ) ) {
337
353
// Stub for the `help` command
338
354
this . makeCommand ( versionCommandOptions , [ ] , ( ) => { } ) ;
339
355
} else {
340
356
const builtInExternalCommandInfo = externalBuiltInCommandsInfo . find (
341
357
( externalBuiltInCommandInfo ) =>
342
- externalBuiltInCommandInfo . name === commandName || externalBuiltInCommandInfo . alias === commandName ,
358
+ externalBuiltInCommandInfo . name === commandName ||
359
+ ( typeof Array . isArray ( externalBuiltInCommandInfo . alias )
360
+ ? externalBuiltInCommandInfo . alias . includes ( commandName )
361
+ : externalBuiltInCommandInfo . alias === commandName ) ,
343
362
) ;
344
363
345
364
let pkg ;
@@ -420,9 +439,7 @@ class WebpackCLI {
420
439
const { commandName } = getCommandNameAndOptions ( this . program . args ) ;
421
440
422
441
if ( commandName ) {
423
- const command = this . program . commands . find (
424
- ( command ) => command . name ( ) === commandName || command . alias ( ) === commandName ,
425
- ) ;
442
+ const command = findCommandByName ( commandName ) ;
426
443
427
444
if ( ! command ) {
428
445
logger . error ( `Can't find and load command '${ commandName } '` ) ;
@@ -470,13 +487,7 @@ class WebpackCLI {
470
487
const outputVersion = async ( options ) => {
471
488
// Filter `bundle`, `version` and `help` commands
472
489
const possibleCommandNames = options . filter (
473
- ( options ) =>
474
- options !== bundleCommandOptions . name &&
475
- options !== bundleCommandOptions . alias &&
476
- options !== versionCommandOptions . name &&
477
- options !== versionCommandOptions . alias &&
478
- options !== helpCommandOptions . name &&
479
- options !== helpCommandOptions . alias ,
490
+ ( option ) => ! isBuildCommand ( option ) && ! isVersionCommand ( option ) && ! isHelpCommand ( option ) ,
480
491
) ;
481
492
482
493
possibleCommandNames . forEach ( ( possibleCommandName ) => {
@@ -495,9 +506,7 @@ class WebpackCLI {
495
506
await Promise . all ( possibleCommandNames . map ( ( possibleCommand ) => loadCommandByName ( possibleCommand ) ) ) ;
496
507
497
508
for ( const possibleCommandName of possibleCommandNames ) {
498
- const foundCommand = this . program . commands . find (
499
- ( command ) => command . name ( ) === possibleCommandName || command . alias ( ) === possibleCommandName ,
500
- ) ;
509
+ const foundCommand = findCommandByName ( possibleCommandName ) ;
501
510
502
511
if ( ! foundCommand ) {
503
512
logger . error ( `Unknown command '${ possibleCommandName } '` ) ;
@@ -563,9 +572,7 @@ class WebpackCLI {
563
572
} ) ,
564
573
) ;
565
574
566
- const bundleCommand = this . program . commands . find (
567
- ( command ) => command . name ( ) === bundleCommandOptions . name || command . alias ( ) === bundleCommandOptions . alias ,
568
- ) ;
575
+ const bundleCommand = findCommandByName ( buildCommandOptions . name ) ;
569
576
570
577
if ( ! isVerbose ) {
571
578
hideVerboseOptions ( bundleCommand ) ;
@@ -574,10 +581,10 @@ class WebpackCLI {
574
581
let helpInformation = bundleCommand
575
582
. helpInformation ( )
576
583
. trimRight ( )
577
- . replace ( bundleCommandOptions . description , 'The build tool for modern web applications.' )
584
+ . replace ( buildCommandOptions . description , 'The build tool for modern web applications.' )
578
585
. replace (
579
586
/ U s a g e : .+ / ,
580
- 'Usage: webpack [options]\nAlternative usage: webpack bundle [options]\nAlternative usage: webpack --config <config> [options]\nAlternative usage: webpack bundle --config <config> [options]' ,
587
+ 'Usage: webpack [options]\nAlternative usage: webpack --config <config> [options]\nAlternative usage: webpack build [options]\nAlternative usage: webpack bundle [options]\nAlternative usage: webpack b [options]\nAlternative usage: webpack build --config <config> [options]' ,
581
588
) ;
582
589
583
590
logger . raw ( helpInformation ) ;
@@ -598,7 +605,7 @@ class WebpackCLI {
598
605
599
606
await loadCommandByName ( name ) ;
600
607
601
- const command = this . program . commands . find ( ( command ) => command . name ( ) === name || command . alias ( ) === name ) ;
608
+ const command = findCommandByName ( name ) ;
602
609
603
610
if ( ! command ) {
604
611
logger . error ( `Can't find and load command '${ name } '` ) ;
@@ -612,30 +619,36 @@ class WebpackCLI {
612
619
613
620
let helpInformation = command . helpInformation ( ) . trimRight ( ) ;
614
621
615
- if ( name === bundleCommandOptions . name || name === bundleCommandOptions . alias ) {
622
+ if ( isBuildCommand ( name ) ) {
616
623
helpInformation = helpInformation
617
- . replace ( bundleCommandOptions . description , 'The build tool for modern web applications.' )
624
+ . replace ( buildCommandOptions . description , 'The build tool for modern web applications.' )
618
625
. replace (
619
626
/ U s a g e : .+ / ,
620
- 'Usage: webpack [options]\nAlternative usage: webpack bundle [options]\nAlternative usage: webpack --config <config> [options]\nAlternative usage: webpack bundle --config <config> [options]' ,
627
+ 'Usage: webpack [options]\nAlternative usage: webpack --config <config> [options]\nAlternative usage: webpack build [options]\nAlternative usage: webpack bundle [options]\nAlternative usage: webpack b [options]\nAlternative usage: webpack build --config <config> [options]' ,
621
628
) ;
622
629
}
623
630
624
631
logger . raw ( helpInformation ) ;
625
632
}
626
633
627
- const globalOptions = program . helpInformation ( ) . match ( / O p t i o n s : \n (?< globalOptions > .+ ) \n C o m m a n d s : \n / s) ;
634
+ const programHelpInformation = program . helpInformation ( ) ;
635
+ const globalOptions = programHelpInformation . match ( / O p t i o n s : \n (?< globalOptions > .+ ) \n C o m m a n d s : \n / s) ;
628
636
629
637
if ( globalOptions && globalOptions . groups . globalOptions ) {
630
638
logger . raw ( '\nGlobal options:' ) ;
631
639
logger . raw ( globalOptions . groups . globalOptions . trimRight ( ) ) ;
632
640
}
633
641
634
- if ( isGlobal ) {
635
- const globalCommands = program . helpInformation ( ) . match ( / C o m m a n d s : \n (?< globalCommands > .+ ) / s) ;
642
+ const globalCommands = programHelpInformation . match ( / C o m m a n d s : \n (?< globalCommands > .+ ) / s) ;
636
643
644
+ if ( isGlobal && globalCommands . groups . globalCommands ) {
637
645
logger . raw ( '\nCommands:' ) ;
638
- logger . raw ( globalCommands . groups . globalCommands . trimRight ( ) ) ;
646
+ logger . raw (
647
+ globalCommands . groups . globalCommands
648
+ . trimRight ( )
649
+ // `commander` doesn't support multiple alias in help
650
+ . replace ( 'build|bundle [options] ' , 'build|bundle|b [options]' ) ,
651
+ ) ;
639
652
}
640
653
641
654
logger . raw ( "\nTo see list of all supported commands and options run 'webpack --help=verbose'.\n" ) ;
@@ -665,7 +678,7 @@ class WebpackCLI {
665
678
666
679
const opts = program . opts ( ) ;
667
680
668
- if ( opts . help || commandName === helpCommandOptions . name || commandName === helpCommandOptions . alias ) {
681
+ if ( opts . help || isHelpCommand ( commandName ) ) {
669
682
let isVerbose = false ;
670
683
671
684
if ( opts . help ) {
@@ -686,7 +699,7 @@ class WebpackCLI {
686
699
await outputHelp ( optionsForHelp , isVerbose , program ) ;
687
700
}
688
701
689
- if ( opts . version || commandName === versionCommandOptions . name || commandName === versionCommandOptions . alias ) {
702
+ if ( opts . version || isVersionCommand ( commandName ) ) {
690
703
const optionsForVersion = [ ] . concat ( opts . version ? [ commandName ] : [ ] ) . concat ( options ) ;
691
704
692
705
await outputVersion ( optionsForVersion , program ) ;
@@ -700,7 +713,9 @@ class WebpackCLI {
700
713
const found = knownCommands . find ( ( commandOptions ) => distance ( commandName , commandOptions . name ) < 3 ) ;
701
714
702
715
if ( found ) {
703
- logger . error ( `Did you mean '${ found . name } ' (alias '${ found . alias } ')?` ) ;
716
+ logger . error (
717
+ `Did you mean '${ found . name } ' (alias '${ Array . isArray ( found . alias ) ? found . alias . join ( ', ' ) : found . alias } ')?` ,
718
+ ) ;
704
719
}
705
720
706
721
logger . error ( "Run 'webpack --help' to see available commands and options" ) ;
@@ -1313,7 +1328,7 @@ class WebpackCLI {
1313
1328
}
1314
1329
} ;
1315
1330
1316
- options . argv = { ...options , env : { WEBPACK_BUNDLE : true , ...options . env } } ;
1331
+ options . argv = { ...options , env : { WEBPACK_BUNDLE : true , WEBPACK_BUILD : true , ...options . env } } ;
1317
1332
1318
1333
compiler = await this . createCompiler ( options , callback ) ;
1319
1334
0 commit comments