@@ -445,51 +445,124 @@ class Server {
445
445
const compilerOptions = this . getCompilerOptions ( ) ;
446
446
// TODO remove `{}` after drop webpack v4 support
447
447
const compilerWatchOptions = compilerOptions . watchOptions || { } ;
448
+ const getWatchOptions = ( watchOptions = { } ) => {
449
+ const getPolling = ( ) => {
450
+ if ( typeof watchOptions . usePolling !== "undefined" ) {
451
+ return watchOptions . usePolling ;
452
+ }
448
453
449
- const getDefaultWatchOptions = ( watchOptions = { } ) => {
450
- // duplicate the same massaging of options that watchpack performs
451
- // https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
452
- // this isn't an elegant solution, but we'll improve it in the future
453
- const usePolling =
454
- typeof watchOptions . usePolling !== "undefined"
455
- ? watchOptions . usePolling
456
- : Boolean ( watchOptions . poll ) ;
454
+ if ( typeof watchOptions . poll !== "undefined" ) {
455
+ return Boolean ( watchOptions . poll ) ;
456
+ }
457
457
458
- const interval =
459
- // eslint-disable-next-line no-nested-ternary
460
- typeof watchOptions . interval !== "undefined"
461
- ? watchOptions . interval
462
- : typeof watchOptions . poll === "number"
463
- ? watchOptions . poll
464
- : // eslint-disable-next-line no-undefined
465
- undefined ;
458
+ if ( typeof compilerWatchOptions . poll !== "undefined" ) {
459
+ return Boolean ( compilerWatchOptions . poll ) ;
460
+ }
461
+
462
+ return false ;
463
+ } ;
464
+ const getInterval = ( ) => {
465
+ if ( typeof watchOptions . interval !== "undefined" ) {
466
+ return watchOptions . interval ;
467
+ }
468
+
469
+ if ( typeof watchOptions . poll === "number" ) {
470
+ return watchOptions . poll ;
471
+ }
466
472
467
- const { poll, ...finalWatchOptions } = {
473
+ if ( typeof compilerWatchOptions . poll === "number" ) {
474
+ return compilerWatchOptions . poll ;
475
+ }
476
+ } ;
477
+
478
+ const usePolling = getPolling ( ) ;
479
+ const interval = getInterval ( ) ;
480
+ const { poll, ...rest } = watchOptions ;
481
+
482
+ return {
468
483
ignoreInitial : true ,
469
484
persistent : true ,
470
485
followSymlinks : false ,
471
486
atomic : false ,
472
487
alwaysStat : true ,
473
488
ignorePermissionErrors : true ,
474
- ...compilerWatchOptions ,
475
- ...watchOptions ,
476
- ignored : watchOptions . ignored ,
489
+ // Respect options from compiler watchOptions
477
490
usePolling,
478
491
interval,
492
+ ignored : watchOptions . ignored ,
493
+ // TODO: we respect these options for all watch options and allow developers to pass them to chokidar, but chokidar doesn't have these options maybe we need revisit that in future
494
+ ...rest ,
479
495
} ;
480
-
481
- return finalWatchOptions ;
482
496
} ;
497
+ const getStaticItem = ( optionsForStatic ) => {
498
+ const getDefaultStaticOptions = ( ) => {
499
+ return {
500
+ directory : path . join ( process . cwd ( ) , "public" ) ,
501
+ staticOptions : { } ,
502
+ publicPath : [ "/" ] ,
503
+ serveIndex : { icons : true } ,
504
+ watch : getWatchOptions ( ) ,
505
+ } ;
506
+ } ;
483
507
484
- const defaultWatchOptions = getDefaultWatchOptions ( ) ;
508
+ let item ;
485
509
486
- const defaultOptionsForStatic = {
487
- directory : path . join ( process . cwd ( ) , "public" ) ,
488
- staticOptions : { } ,
489
- publicPath : [ "/" ] ,
490
- serveIndex : { icons : true } ,
491
- // Respect options from compiler watchOptions
492
- watch : defaultWatchOptions ,
510
+ if ( typeof optionsForStatic === "undefined" ) {
511
+ item = getDefaultStaticOptions ( ) ;
512
+ } else if ( typeof optionsForStatic === "string" ) {
513
+ item = {
514
+ ...getDefaultStaticOptions ( ) ,
515
+ directory : optionsForStatic ,
516
+ } ;
517
+ } else {
518
+ const def = getDefaultStaticOptions ( ) ;
519
+
520
+ item = {
521
+ directory :
522
+ typeof optionsForStatic . directory !== "undefined"
523
+ ? optionsForStatic . directory
524
+ : def . directory ,
525
+ // TODO: do merge in the next major release
526
+ staticOptions :
527
+ typeof optionsForStatic . staticOptions !== "undefined"
528
+ ? optionsForStatic . staticOptions
529
+ : def . staticOptions ,
530
+ publicPath :
531
+ typeof optionsForStatic . publicPath !== "undefined"
532
+ ? optionsForStatic . publicPath
533
+ : def . publicPath ,
534
+ // TODO: do merge in the next major release
535
+ serveIndex :
536
+ // eslint-disable-next-line no-nested-ternary
537
+ typeof optionsForStatic . serveIndex !== "undefined"
538
+ ? typeof optionsForStatic . serveIndex === "boolean" &&
539
+ optionsForStatic . serveIndex
540
+ ? def . serveIndex
541
+ : optionsForStatic . serveIndex
542
+ : def . serveIndex ,
543
+ watch :
544
+ // eslint-disable-next-line no-nested-ternary
545
+ typeof optionsForStatic . watch !== "undefined"
546
+ ? // eslint-disable-next-line no-nested-ternary
547
+ typeof optionsForStatic . watch === "boolean"
548
+ ? optionsForStatic . watch
549
+ ? def . watch
550
+ : false
551
+ : getWatchOptions ( optionsForStatic . watch )
552
+ : def . watch ,
553
+ } ;
554
+ }
555
+
556
+ if ( Server . isAbsoluteURL ( item . directory ) ) {
557
+ throw new Error ( "Using a URL as static.directory is not supported" ) ;
558
+ }
559
+
560
+ // ensure that publicPath is an array
561
+ if ( typeof item . publicPath === "string" ) {
562
+ item . publicPath = [ item . publicPath ] ;
563
+ }
564
+
565
+ return item ;
493
566
} ;
494
567
495
568
if ( typeof options . allowedHosts === "undefined" ) {
@@ -959,55 +1032,26 @@ class Server {
959
1032
}
960
1033
961
1034
if ( typeof options . static === "undefined" ) {
962
- options . static = [ defaultOptionsForStatic ] ;
1035
+ options . static = [ getStaticItem ( ) ] ;
963
1036
} else if ( typeof options . static === "boolean" ) {
964
- options . static = options . static ? [ defaultOptionsForStatic ] : false ;
1037
+ options . static = options . static ? [ getStaticItem ( ) ] : false ;
965
1038
} else if ( typeof options . static === "string" ) {
966
- options . static = [
967
- { ...defaultOptionsForStatic , directory : options . static } ,
968
- ] ;
1039
+ options . static = [ getStaticItem ( options . static ) ] ;
969
1040
} else if ( Array . isArray ( options . static ) ) {
970
1041
options . static = options . static . map ( ( item ) => {
971
1042
if ( typeof item === "string" ) {
972
- return { ... defaultOptionsForStatic , directory : item } ;
1043
+ return getStaticItem ( item ) ;
973
1044
}
974
1045
975
- return { ... defaultOptionsForStatic , ... item } ;
1046
+ return getStaticItem ( item ) ;
976
1047
} ) ;
977
1048
} else {
978
- options . static = [ { ...defaultOptionsForStatic , ...options . static } ] ;
979
- }
980
-
981
- if ( options . static ) {
982
- options . static . forEach ( ( staticOption ) => {
983
- if ( Server . isAbsoluteURL ( staticOption . directory ) ) {
984
- throw new Error ( "Using a URL as static.directory is not supported" ) ;
985
- }
986
-
987
- // ensure that publicPath is an array
988
- if ( typeof staticOption . publicPath === "string" ) {
989
- staticOption . publicPath = [ staticOption . publicPath ] ;
990
- }
991
-
992
- // ensure that watch is an object if true
993
- if ( staticOption . watch === true ) {
994
- staticOption . watch = defaultOptionsForStatic . watch ;
995
- } else if ( typeof staticOption . watch === "object" ) {
996
- staticOption . watch = {
997
- ...getDefaultWatchOptions ( staticOption . watch ) ,
998
- } ;
999
- }
1000
-
1001
- // ensure that serveIndex is an object if true
1002
- if ( staticOption . serveIndex === true ) {
1003
- staticOption . serveIndex = defaultOptionsForStatic . serveIndex ;
1004
- }
1005
- } ) ;
1049
+ options . static = [ getStaticItem ( options . static ) ] ;
1006
1050
}
1007
1051
1008
1052
if ( typeof options . watchFiles === "string" ) {
1009
1053
options . watchFiles = [
1010
- { paths : options . watchFiles , options : defaultWatchOptions } ,
1054
+ { paths : options . watchFiles , options : getWatchOptions ( ) } ,
1011
1055
] ;
1012
1056
} else if (
1013
1057
typeof options . watchFiles === "object" &&
@@ -1017,22 +1061,18 @@ class Server {
1017
1061
options . watchFiles = [
1018
1062
{
1019
1063
paths : options . watchFiles . paths ,
1020
- options : {
1021
- ...getDefaultWatchOptions ( options . watchFiles . options || { } ) ,
1022
- } ,
1064
+ options : getWatchOptions ( options . watchFiles . options || { } ) ,
1023
1065
} ,
1024
1066
] ;
1025
1067
} else if ( Array . isArray ( options . watchFiles ) ) {
1026
1068
options . watchFiles = options . watchFiles . map ( ( item ) => {
1027
1069
if ( typeof item === "string" ) {
1028
- return { paths : item , options : defaultWatchOptions } ;
1070
+ return { paths : item , options : getWatchOptions ( ) } ;
1029
1071
}
1030
1072
1031
1073
return {
1032
1074
paths : item . paths ,
1033
- options : {
1034
- ...getDefaultWatchOptions ( item . options || { } ) ,
1035
- } ,
1075
+ options : getWatchOptions ( item . options || { } ) ,
1036
1076
} ;
1037
1077
} ) ;
1038
1078
} else {
0 commit comments