@@ -14,6 +14,8 @@ const routes = require('./utils/routes');
14
14
const getSocketServerImplementation = require ( './utils/getSocketServerImplementation' ) ;
15
15
const getCompilerConfigArray = require ( './utils/getCompilerConfigArray' ) ;
16
16
const setupExitSignals = require ( './utils/setupExitSignals' ) ;
17
+ const getStatsOption = require ( './utils/getStatsOption' ) ;
18
+ const getColorsOption = require ( './utils/getColorsOption' ) ;
17
19
const schema = require ( './options.json' ) ;
18
20
19
21
if ( ! process . env . WEBPACK_SERVE ) {
@@ -40,6 +42,7 @@ class Server {
40
42
this . wsHeartbeatInterval = 30000 ;
41
43
42
44
normalizeOptions ( this . compiler , this . options ) ;
45
+
43
46
this . applyDevServerPlugin ( ) ;
44
47
45
48
this . SocketServerImplementation = getSocketServerImplementation (
@@ -629,8 +632,6 @@ class Server {
629
632
}
630
633
631
634
showStatus ( ) {
632
- const getColorsOption = require ( './utils/getColorsOption' ) ;
633
-
634
635
const useColor = getColorsOption ( getCompilerConfigArray ( this . compiler ) ) ;
635
636
const protocol = this . options . https ? 'https' : 'http' ;
636
637
const { address, port } = this . server . address ( ) ;
@@ -644,20 +645,20 @@ class Server {
644
645
let networkUrlIPv4 ;
645
646
let networkUrlIPv6 ;
646
647
647
- if ( this . hostname ) {
648
- if ( this . hostname === 'localhost' ) {
648
+ if ( this . options . host ) {
649
+ if ( this . options . host === 'localhost' ) {
649
650
localhost = prettyPrintUrl ( 'localhost' ) ;
650
651
} else {
651
652
let isIP ;
652
653
653
654
try {
654
- isIP = ipaddr . parse ( this . hostname ) ;
655
+ isIP = ipaddr . parse ( this . options . host ) ;
655
656
} catch ( error ) {
656
657
// Ignore
657
658
}
658
659
659
660
if ( ! isIP ) {
660
- server = prettyPrintUrl ( this . hostname ) ;
661
+ server = prettyPrintUrl ( this . options . host ) ;
661
662
}
662
663
}
663
664
}
@@ -754,36 +755,62 @@ class Server {
754
755
if ( this . options . open ) {
755
756
const runOpen = require ( './utils/runOpen' ) ;
756
757
757
- const openTarget = prettyPrintUrl ( this . hostname || 'localhost' ) ;
758
+ const openTarget = prettyPrintUrl ( this . options . host || 'localhost' ) ;
758
759
759
760
runOpen ( openTarget , this . options . open , this . logger ) ;
760
761
}
761
762
}
762
763
763
764
listen ( port , hostname , fn ) {
764
- if ( hostname === 'local-ip' ) {
765
- this . hostname = internalIp . v4 . sync ( ) || internalIp . v6 . sync ( ) || '0.0.0.0' ;
766
- } else if ( hostname === 'local-ipv4' ) {
767
- this . hostname = internalIp . v4 . sync ( ) || '0.0.0.0' ;
768
- } else if ( hostname === 'local-ipv6' ) {
769
- this . hostname = internalIp . v6 . sync ( ) || '::' ;
770
- } else {
771
- this . hostname = hostname ;
765
+ if (
766
+ typeof port !== 'undefined' &&
767
+ typeof this . options . port !== 'undefined' &&
768
+ port !== this . options . port
769
+ ) {
770
+ this . options . port = port ;
771
+
772
+ this . logger . warn (
773
+ 'The "port" specified in options is different from the port passed as an argument. Will be used from arguments.'
774
+ ) ;
775
+ }
776
+
777
+ if ( ! this . options . port ) {
778
+ this . options . port = port ;
772
779
}
773
780
774
- if ( typeof port !== 'undefined' && port !== this . options . port ) {
781
+ if (
782
+ typeof hostname !== 'undefined' &&
783
+ typeof this . options . host !== 'undefined' &&
784
+ hostname !== this . options . host
785
+ ) {
786
+ this . options . host = hostname ;
787
+
775
788
this . logger . warn (
776
- 'The port specified in options and the port passed as an argument is different .'
789
+ 'The "host" specified in options is different from the host passed as an argument. Will be used from arguments .'
777
790
) ;
778
791
}
779
792
780
- return (
781
- Server . getFreePort ( port || this . options . port )
782
- // eslint-disable-next-line no-shadow
783
- . then ( ( port ) => {
784
- this . options . port = port ;
793
+ if ( ! this . options . host ) {
794
+ this . options . host = hostname ;
795
+ }
796
+
797
+ if ( this . options . host === 'local-ip' ) {
798
+ this . options . host =
799
+ internalIp . v4 . sync ( ) || internalIp . v6 . sync ( ) || '0.0.0.0' ;
800
+ } else if ( this . options . host === 'local-ipv4' ) {
801
+ this . options . host = internalIp . v4 . sync ( ) || '0.0.0.0' ;
802
+ } else if ( this . options . host === 'local-ipv6' ) {
803
+ this . options . host = internalIp . v6 . sync ( ) || '::' ;
804
+ }
805
+
806
+ return Server . getFreePort ( this . options . port )
807
+ . then ( ( foundPort ) => {
808
+ this . options . port = foundPort ;
785
809
786
- return this . server . listen ( port , this . hostname , ( error ) => {
810
+ return this . server . listen (
811
+ this . options . port ,
812
+ this . options . host ,
813
+ ( error ) => {
787
814
if ( this . options . hot || this . options . liveReload ) {
788
815
this . createSocketServer ( ) ;
789
816
}
@@ -803,14 +830,14 @@ class Server {
803
830
if ( typeof this . options . onListening === 'function' ) {
804
831
this . options . onListening ( this ) ;
805
832
}
806
- } ) ;
807
- } )
808
- . catch ( ( error ) => {
809
- if ( fn ) {
810
- fn . call ( this . server , error ) ;
811
833
}
812
- } )
813
- ) ;
834
+ ) ;
835
+ } )
836
+ . catch ( ( error ) => {
837
+ if ( fn ) {
838
+ fn . call ( this . server , error ) ;
839
+ }
840
+ } ) ;
814
841
}
815
842
816
843
close ( cb ) {
@@ -874,8 +901,6 @@ class Server {
874
901
}
875
902
876
903
getStats ( statsObj ) {
877
- const getStatsOption = require ( './utils/getStatsOption' ) ;
878
-
879
904
const stats = Server . DEFAULT_STATS ;
880
905
881
906
const configArr = getCompilerConfigArray ( this . compiler ) ;
@@ -950,12 +975,12 @@ class Server {
950
975
// these are removed from the hostname in url.parse(),
951
976
// so we have the pure IPv6-address in hostname.
952
977
// always allow localhost host, for convenience (hostname === 'localhost')
953
- // allow hostname of listening address (hostname === this.hostname )
978
+ // allow hostname of listening address (hostname === this.options.host )
954
979
const isValidHostname =
955
980
ipaddr . IPv4 . isValid ( hostname ) ||
956
981
ipaddr . IPv6 . isValid ( hostname ) ||
957
982
hostname === 'localhost' ||
958
- hostname === this . hostname ;
983
+ hostname === this . options . host ;
959
984
960
985
if ( isValidHostname ) {
961
986
return true ;
0 commit comments