@@ -43,76 +43,36 @@ class Server {
43
43
44
44
this . compiler = compiler ;
45
45
this . options = options ;
46
-
46
+ this . sockets = [ ] ;
47
+ this . contentBaseWatchers = [ ] ;
48
+ // Keep track of websocket proxies for external websocket upgrade.
49
+ this . websocketProxies = [ ] ;
47
50
this . log = _log || createLogger ( options ) ;
48
-
49
- if ( this . options . transportMode !== undefined ) {
50
- this . log . warn (
51
- 'transportMode is an experimental option, meaning its usage could potentially change without warning'
52
- ) ;
53
- }
51
+ // this value of ws can be overwritten for tests
52
+ this . wsHeartbeatInterval = 30000 ;
54
53
55
54
normalizeOptions ( this . compiler , this . options ) ;
56
-
57
- // don't move this position because addEntries called by updateCompiler checks this.options.hot|hotOnly
58
- this . options . hot =
59
- typeof this . options . hot === 'boolean' || this . options . hot === 'only'
60
- ? this . options . hot
61
- : true ;
62
-
63
55
updateCompiler ( this . compiler , this . options ) ;
64
56
65
- this . heartbeatInterval = 30000 ;
66
- // this.SocketServerImplementation is a class, so it must be instantiated before use
67
- this . socketServerImplementation = getSocketServerImplementation (
57
+ this . SocketServerImplementation = getSocketServerImplementation (
68
58
this . options
69
59
) ;
70
60
71
- this . originalStats =
72
- this . options . stats && Object . keys ( this . options . stats ) . length
73
- ? this . options . stats
74
- : { } ;
75
-
76
- this . sockets = [ ] ;
77
- this . contentBaseWatchers = [ ] ;
78
-
79
- // TODO this.<property> is deprecated (remove them in next major release.) in favor this.options.<property>
80
- this . headers = this . options . headers ;
81
- this . progress = this . options . progress ;
82
-
83
- this . serveIndex = this . options . serveIndex ;
84
-
85
- this . clientOverlay = this . options . overlay ;
86
- this . clientLogLevel = this . options . clientLogLevel ;
87
-
88
- this . publicHost = this . options . public ;
89
- this . allowedHosts = this . options . allowedHosts ;
90
- this . disableHostCheck = ! ! this . options . disableHostCheck ;
91
-
92
- this . watchOptions = options . watchOptions || { } ;
93
-
94
- if ( this . progress ) {
61
+ if ( this . options . progress ) {
95
62
this . setupProgressPlugin ( ) ;
96
63
}
97
-
98
64
this . setupHooks ( ) ;
99
65
this . setupApp ( ) ;
100
66
this . setupCheckHostRoute ( ) ;
101
67
this . setupDevMiddleware ( ) ;
102
-
103
- // set express routes
104
- routes ( this ) ;
105
-
106
- // Keep track of websocket proxies for external websocket upgrade.
107
- this . websocketProxies = [ ] ;
108
-
109
68
this . setupFeatures ( ) ;
110
69
this . setupHttps ( ) ;
111
70
this . createServer ( ) ;
112
71
72
+ routes ( this ) ;
113
73
killable ( this . listeningApp ) ;
114
74
115
- // Proxy websockets without the initial http request
75
+ // Proxy WebSocket without the initial http request
116
76
// https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
117
77
this . websocketProxies . forEach ( function ( wsProxy ) {
118
78
this . listeningApp . on ( 'upgrade' , wsProxy . upgrade ) ;
@@ -163,8 +123,8 @@ class Server {
163
123
compile . tap ( 'webpack-dev-server' , invalidPlugin ) ;
164
124
invalid . tap ( 'webpack-dev-server' , invalidPlugin ) ;
165
125
done . tap ( 'webpack-dev-server' , ( stats ) => {
166
- this . _sendStats ( this . sockets , this . getStats ( stats ) ) ;
167
- this . _stats = stats ;
126
+ this . sendStats ( this . sockets , this . getStats ( stats ) ) ;
127
+ this . stats = stats ;
168
128
} ) ;
169
129
} ;
170
130
@@ -534,10 +494,7 @@ class Server {
534
494
}
535
495
}
536
496
537
- // checking if it's set to true or not set (Default : undefined => true)
538
- this . serveIndex = this . serveIndex || this . serveIndex === undefined ;
539
-
540
- if ( this . options . contentBase && this . serveIndex ) {
497
+ if ( this . options . contentBase && this . options . serveIndex ) {
541
498
runnableFeatures . push ( 'contentBaseIndex' ) ;
542
499
}
543
500
@@ -625,8 +582,7 @@ class Server {
625
582
}
626
583
627
584
createSocketServer ( ) {
628
- const SocketServerImplementation = this . socketServerImplementation ;
629
- this . socketServer = new SocketServerImplementation ( this ) ;
585
+ this . socketServer = new this . SocketServerImplementation ( this ) ;
630
586
631
587
this . socketServer . onConnection ( ( connection , headers ) => {
632
588
if ( ! connection ) {
@@ -658,8 +614,8 @@ class Server {
658
614
}
659
615
} ) ;
660
616
661
- if ( this . clientLogLevel ) {
662
- this . sockWrite ( [ connection ] , 'log-level' , this . clientLogLevel ) ;
617
+ if ( this . options . clientLogLevel ) {
618
+ this . sockWrite ( [ connection ] , 'log-level' , this . options . clientLogLevel ) ;
663
619
}
664
620
665
621
if ( this . options . hot === true || this . options . hot === 'only' ) {
@@ -671,19 +627,19 @@ class Server {
671
627
this . sockWrite ( [ connection ] , 'liveReload' , this . options . liveReload ) ;
672
628
}
673
629
674
- if ( this . progress ) {
675
- this . sockWrite ( [ connection ] , 'progress' , this . progress ) ;
630
+ if ( this . options . progress ) {
631
+ this . sockWrite ( [ connection ] , 'progress' , this . options . progress ) ;
676
632
}
677
633
678
- if ( this . clientOverlay ) {
679
- this . sockWrite ( [ connection ] , 'overlay' , this . clientOverlay ) ;
634
+ if ( this . options . clientOverlay ) {
635
+ this . sockWrite ( [ connection ] , 'overlay' , this . options . clientOverlay ) ;
680
636
}
681
637
682
- if ( ! this . _stats ) {
638
+ if ( ! this . stats ) {
683
639
return ;
684
640
}
685
641
686
- this . _sendStats ( [ connection ] , this . getStats ( this . _stats ) , true ) ;
642
+ this . sendStats ( [ connection ] , this . getStats ( this . stats ) , true ) ;
687
643
} ) ;
688
644
}
689
645
@@ -755,8 +711,8 @@ class Server {
755
711
getStats ( statsObj ) {
756
712
const stats = Server . DEFAULT_STATS ;
757
713
758
- if ( this . originalStats . warningsFilter ) {
759
- stats . warningsFilter = this . originalStats . warningsFilter ;
714
+ if ( this . options . stats . warningsFilter ) {
715
+ stats . warningsFilter = this . options . stats . warningsFilter ;
760
716
}
761
717
762
718
return statsObj . toJson ( stats ) ;
@@ -768,10 +724,10 @@ class Server {
768
724
}
769
725
770
726
setContentHeaders ( req , res , next ) {
771
- if ( this . headers ) {
727
+ if ( this . options . headers ) {
772
728
// eslint-disable-next-line
773
- for ( const name in this . headers ) {
774
- res . setHeader ( name , this . headers [ name ] ) ;
729
+ for ( const name in this . options . headers ) {
730
+ res . setHeader ( name , this . options . headers [ name ] ) ;
775
731
}
776
732
}
777
733
@@ -788,7 +744,7 @@ class Server {
788
744
789
745
checkHeaders ( headers , headerToCheck ) {
790
746
// allow user to opt-out this security check, at own risk
791
- if ( this . disableHostCheck ) {
747
+ if ( this . options . disableHostCheck ) {
792
748
return true ;
793
749
}
794
750
@@ -828,11 +784,14 @@ class Server {
828
784
if ( isValidHostname ) {
829
785
return true ;
830
786
}
787
+
788
+ const allowedHosts = this . options . allowedHosts ;
789
+
831
790
// always allow localhost host, for convenience
832
791
// allow if hostname is in allowedHosts
833
- if ( this . allowedHosts && this . allowedHosts . length ) {
834
- for ( let hostIdx = 0 ; hostIdx < this . allowedHosts . length ; hostIdx ++ ) {
835
- const allowedHost = this . allowedHosts [ hostIdx ] ;
792
+ if ( allowedHosts && allowedHosts . length ) {
793
+ for ( let hostIdx = 0 ; hostIdx < allowedHosts . length ; hostIdx ++ ) {
794
+ const allowedHost = allowedHosts [ hostIdx ] ;
836
795
837
796
if ( allowedHost === hostname ) {
838
797
return true ;
@@ -854,10 +813,12 @@ class Server {
854
813
}
855
814
856
815
// also allow public hostname if provided
857
- if ( typeof this . publicHost === 'string' ) {
858
- const idxPublic = this . publicHost . indexOf ( ':' ) ;
816
+ if ( typeof this . options . public === 'string' ) {
817
+ const idxPublic = this . options . public . indexOf ( ':' ) ;
859
818
const publicHostname =
860
- idxPublic >= 0 ? this . publicHost . substr ( 0 , idxPublic ) : this . publicHost ;
819
+ idxPublic >= 0
820
+ ? this . options . public . substr ( 0 , idxPublic )
821
+ : this . options . public ;
861
822
862
823
if ( hostname === publicHostname ) {
863
824
return true ;
@@ -896,7 +857,7 @@ class Server {
896
857
}
897
858
898
859
// send stats to a socket or multiple sockets
899
- _sendStats ( sockets , stats , force ) {
860
+ sendStats ( sockets , stats , force ) {
900
861
const shouldEmit =
901
862
! force &&
902
863
stats &&
@@ -923,10 +884,10 @@ class Server {
923
884
// duplicate the same massaging of options that watchpack performs
924
885
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
925
886
// this isn't an elegant solution, but we'll improve it in the future
926
- const usePolling = this . watchOptions . poll ? true : undefined ;
887
+ const usePolling = this . options . watchOptions . poll ? true : undefined ;
927
888
const interval =
928
- typeof this . watchOptions . poll === 'number'
929
- ? this . watchOptions . poll
889
+ typeof this . options . watchOptions . poll === 'number'
890
+ ? this . options . watchOptions . poll
930
891
: undefined ;
931
892
932
893
const watchOptions = {
@@ -936,7 +897,7 @@ class Server {
936
897
atomic : false ,
937
898
alwaysStat : true ,
938
899
ignorePermissionErrors : true ,
939
- ignored : this . watchOptions . ignored ,
900
+ ignored : this . options . watchOptions . ignored ,
940
901
usePolling,
941
902
interval,
942
903
} ;
0 commit comments