@@ -43,7 +43,10 @@ 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
51
49
52
if ( this . options . transportMode !== undefined ) {
@@ -53,75 +56,34 @@ class Server {
53
56
}
54
57
55
58
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
59
updateCompiler ( this . compiler , this . options ) ;
64
60
65
- this . heartbeatInterval = 30000 ;
66
- // this.SocketServerImplementation is a class, so it must be instantiated before use
67
- this . socketServerImplementation = getSocketServerImplementation (
61
+ // TODO: need to investigate
62
+ // if this value is deleted, all transportMode will be broken
63
+ // this.sockPath = this.options.sockPath;
64
+ // this.sockPath = this.options.sockPath;
65
+
66
+ this . SocketServerImplementation = getSocketServerImplementation (
68
67
this . options
69
68
) ;
70
69
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
- // Replace leading and trailing slashes to normalize path
95
- this . sockPath = `/${
96
- this . options . sockPath
97
- ? this . options . sockPath . replace ( / ^ \/ | \/ $ / g, '' )
98
- : 'sockjs-node'
99
- } `;
100
-
101
- if ( this . progress ) {
70
+ if ( this . options . progress ) {
102
71
this . setupProgressPlugin ( ) ;
103
72
}
104
-
105
73
this . setupHooks ( ) ;
106
74
this . setupApp ( ) ;
107
75
this . setupCheckHostRoute ( ) ;
108
76
this . setupDevMiddleware ( ) ;
109
-
110
- // set express routes
111
- routes ( this ) ;
112
-
113
- // Keep track of websocket proxies for external websocket upgrade.
114
- this . websocketProxies = [ ] ;
115
-
116
77
this . setupFeatures ( ) ;
117
78
this . setupHttps ( ) ;
118
79
this . createServer ( ) ;
119
80
81
+ routes ( this ) ;
120
82
killable ( this . listeningApp ) ;
121
83
122
- // Proxy websockets without the initial http request
84
+ // Proxy WebSockets without the initial http request
123
85
// https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
124
- this . websocketProxies . forEach ( function ( wsProxy ) {
86
+ this . websocketProxies . forEach ( ( wsProxy ) => {
125
87
this . listeningApp . on ( 'upgrade' , wsProxy . upgrade ) ;
126
88
} , this ) ;
127
89
}
@@ -170,8 +132,8 @@ class Server {
170
132
compile . tap ( 'webpack-dev-server' , invalidPlugin ) ;
171
133
invalid . tap ( 'webpack-dev-server' , invalidPlugin ) ;
172
134
done . tap ( 'webpack-dev-server' , ( stats ) => {
173
- this . _sendStats ( this . sockets , this . getStats ( stats ) ) ;
174
- this . _stats = stats ;
135
+ this . sendStats ( this . sockets , this . getStats ( stats ) ) ;
136
+ this . stats = stats ;
175
137
} ) ;
176
138
} ;
177
139
@@ -554,10 +516,7 @@ class Server {
554
516
}
555
517
}
556
518
557
- // checking if it's set to true or not set (Default : undefined => true)
558
- this . serveIndex = this . serveIndex || this . serveIndex === undefined ;
559
-
560
- if ( this . options . contentBase && this . serveIndex ) {
519
+ if ( this . options . contentBase && this . options . serveIndex ) {
561
520
runnableFeatures . push ( 'contentBaseIndex' ) ;
562
521
}
563
522
@@ -664,8 +623,7 @@ class Server {
664
623
}
665
624
666
625
createSocketServer ( ) {
667
- const SocketServerImplementation = this . socketServerImplementation ;
668
- this . socketServer = new SocketServerImplementation ( this ) ;
626
+ this . socketServer = new this . SocketServerImplementation ( this ) ;
669
627
670
628
this . socketServer . onConnection ( ( connection , headers ) => {
671
629
if ( ! connection ) {
@@ -697,8 +655,8 @@ class Server {
697
655
}
698
656
} ) ;
699
657
700
- if ( this . clientLogLevel ) {
701
- this . sockWrite ( [ connection ] , 'log-level' , this . clientLogLevel ) ;
658
+ if ( this . options . clientLogLevel ) {
659
+ this . sockWrite ( [ connection ] , 'log-level' , this . options . clientLogLevel ) ;
702
660
}
703
661
704
662
if ( this . options . hot === true || this . options . hot === 'only' ) {
@@ -710,19 +668,19 @@ class Server {
710
668
this . sockWrite ( [ connection ] , 'liveReload' , this . options . liveReload ) ;
711
669
}
712
670
713
- if ( this . progress ) {
714
- this . sockWrite ( [ connection ] , 'progress' , this . progress ) ;
671
+ if ( this . options . progress ) {
672
+ this . sockWrite ( [ connection ] , 'progress' , this . options . progress ) ;
715
673
}
716
674
717
- if ( this . clientOverlay ) {
718
- this . sockWrite ( [ connection ] , 'overlay' , this . clientOverlay ) ;
675
+ if ( this . options . clientOverlay ) {
676
+ this . sockWrite ( [ connection ] , 'overlay' , this . options . clientOverlay ) ;
719
677
}
720
678
721
- if ( ! this . _stats ) {
679
+ if ( ! this . stats ) {
722
680
return ;
723
681
}
724
682
725
- this . _sendStats ( [ connection ] , this . getStats ( this . _stats ) , true ) ;
683
+ this . sendStats ( [ connection ] , this . getStats ( this . stats ) , true ) ;
726
684
} ) ;
727
685
}
728
686
@@ -794,8 +752,8 @@ class Server {
794
752
getStats ( statsObj ) {
795
753
const stats = Server . DEFAULT_STATS ;
796
754
797
- if ( this . originalStats . warningsFilter ) {
798
- stats . warningsFilter = this . originalStats . warningsFilter ;
755
+ if ( this . options . stats . warningsFilter ) {
756
+ stats . warningsFilter = this . options . stats . warningsFilter ;
799
757
}
800
758
801
759
return statsObj . toJson ( stats ) ;
@@ -807,10 +765,10 @@ class Server {
807
765
}
808
766
809
767
setContentHeaders ( req , res , next ) {
810
- if ( this . headers ) {
768
+ if ( this . options . headers ) {
811
769
// eslint-disable-next-line
812
- for ( const name in this . headers ) {
813
- res . setHeader ( name , this . headers [ name ] ) ;
770
+ for ( const name in this . options . headers ) {
771
+ res . setHeader ( name , this . options . headers [ name ] ) ;
814
772
}
815
773
}
816
774
@@ -827,7 +785,7 @@ class Server {
827
785
828
786
checkHeaders ( headers , headerToCheck ) {
829
787
// allow user to opt-out this security check, at own risk
830
- if ( this . disableHostCheck ) {
788
+ if ( this . options . disableHostCheck ) {
831
789
return true ;
832
790
}
833
791
@@ -867,11 +825,14 @@ class Server {
867
825
if ( isValidHostname ) {
868
826
return true ;
869
827
}
828
+
829
+ const allowedHosts = this . options . allowedHosts ;
830
+
870
831
// always allow localhost host, for convenience
871
832
// allow if hostname is in allowedHosts
872
- if ( this . allowedHosts && this . allowedHosts . length ) {
873
- for ( let hostIdx = 0 ; hostIdx < this . allowedHosts . length ; hostIdx ++ ) {
874
- const allowedHost = this . allowedHosts [ hostIdx ] ;
833
+ if ( allowedHosts && allowedHosts . length ) {
834
+ for ( let hostIdx = 0 ; hostIdx < allowedHosts . length ; hostIdx ++ ) {
835
+ const allowedHost = allowedHosts [ hostIdx ] ;
875
836
876
837
if ( allowedHost === hostname ) {
877
838
return true ;
@@ -893,10 +854,12 @@ class Server {
893
854
}
894
855
895
856
// also allow public hostname if provided
896
- if ( typeof this . publicHost === 'string' ) {
897
- const idxPublic = this . publicHost . indexOf ( ':' ) ;
857
+ if ( typeof this . options . public === 'string' ) {
858
+ const idxPublic = this . options . public . indexOf ( ':' ) ;
898
859
const publicHostname =
899
- idxPublic >= 0 ? this . publicHost . substr ( 0 , idxPublic ) : this . publicHost ;
860
+ idxPublic >= 0
861
+ ? this . options . public . substr ( 0 , idxPublic )
862
+ : this . options . public ;
900
863
901
864
if ( hostname === publicHostname ) {
902
865
return true ;
@@ -935,7 +898,7 @@ class Server {
935
898
}
936
899
937
900
// send stats to a socket or multiple sockets
938
- _sendStats ( sockets , stats , force ) {
901
+ sendStats ( sockets , stats , force ) {
939
902
const shouldEmit =
940
903
! force &&
941
904
stats &&
@@ -962,10 +925,10 @@ class Server {
962
925
// duplicate the same massaging of options that watchpack performs
963
926
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
964
927
// this isn't an elegant solution, but we'll improve it in the future
965
- const usePolling = this . watchOptions . poll ? true : undefined ;
928
+ const usePolling = this . options . watchOptions . poll ? true : undefined ;
966
929
const interval =
967
- typeof this . watchOptions . poll === 'number'
968
- ? this . watchOptions . poll
930
+ typeof this . options . watchOptions . poll === 'number'
931
+ ? this . options . watchOptions . poll
969
932
: undefined ;
970
933
971
934
const watchOptions = {
@@ -975,7 +938,7 @@ class Server {
975
938
atomic : false ,
976
939
alwaysStat : true ,
977
940
ignorePermissionErrors : true ,
978
- ignored : this . watchOptions . ignored ,
941
+ ignored : this . options . watchOptions . ignored ,
979
942
usePolling,
980
943
interval,
981
944
} ;
0 commit comments