11var url = require ( 'url' ) ;
2- var SockJS = require ( "sockjs-client" ) ;
32var stripAnsi = require ( 'strip-ansi' ) ;
3+ var socket = require ( './socket' ) ;
44
55function getCurrentScriptSource ( ) {
66 // `document.currentScript` is the most accurate way to find the current script,
@@ -16,10 +16,9 @@ function getCurrentScriptSource() {
1616 throw new Error ( "[WDS] Failed to get current script source" ) ;
1717}
1818
19- // If this bundle is inlined, use the resource query to get the correct url.
20- // Else, get the url from the <script> this file was called with.
2119var urlParts ;
22- if ( typeof __resourceQuery === "string" && __resourceQuery ) {
20+ if ( typeof __resourceQuery === "string" && __resourceQuery ) {
21+ // If this bundle is inlined, use the resource query to get the correct url.
2322 urlParts = url . parse ( __resourceQuery . substr ( 1 ) ) ;
2423} else {
2524 // Else, get the url from the <script> this file was called with.
@@ -28,54 +27,70 @@ if (typeof __resourceQuery === "string" && __resourceQuery) {
2827 urlParts = url . parse ( ( scriptHost ? scriptHost : "/" ) , false , true ) ;
2928}
3029
31- var sock = null ;
3230var hot = false ;
3331var initial = true ;
3432var currentHash = "" ;
33+ var logLevel = "info" ;
34+
35+ function log ( level , msg ) {
36+ if ( logLevel === "info" && level === "info" )
37+ return console . log ( msg ) ;
38+ if ( [ "info" , "warning" ] . indexOf ( logLevel ) >= 0 && level === "warning" )
39+ return console . warn ( msg ) ;
40+ if ( [ "info" , "warning" , "error" ] . indexOf ( logLevel ) >= 0 && level === "error" )
41+ return console . error ( msg ) ;
42+ }
3543
3644var onSocketMsg = {
3745 hot : function ( ) {
3846 hot = true ;
39- console . log ( "[WDS] Hot Module Replacement enabled." ) ;
47+ log ( "info" , "[WDS] Hot Module Replacement enabled." ) ;
4048 } ,
4149 invalid : function ( ) {
42- console . log ( "[WDS] App updated. Recompiling..." ) ;
50+ log ( "info" , "[WDS] App updated. Recompiling..." ) ;
4351 } ,
4452 hash : function ( hash ) {
4553 currentHash = hash ;
4654 } ,
4755 "still-ok" : function ( ) {
48- console . log ( "[WDS] Nothing changed." )
56+ log ( "info" , "[WDS] Nothing changed." )
57+ } ,
58+ "log-level" : function ( level ) {
59+ logLevel = level ;
4960 } ,
5061 ok : function ( ) {
5162 if ( initial ) return initial = false ;
5263 reloadApp ( ) ;
5364 } ,
5465 warnings : function ( warnings ) {
55- console . log ( "[WDS] Warnings while compiling." ) ;
66+ log ( "info" , "[WDS] Warnings while compiling." ) ;
5667 for ( var i = 0 ; i < warnings . length ; i ++ )
5768 console . warn ( stripAnsi ( warnings [ i ] ) ) ;
5869 if ( initial ) return initial = false ;
5970 reloadApp ( ) ;
6071 } ,
6172 errors : function ( errors ) {
62- console . log ( "[WDS] Errors while compiling." ) ;
73+ log ( "info" , "[WDS] Errors while compiling." ) ;
6374 for ( var i = 0 ; i < errors . length ; i ++ )
6475 console . error ( stripAnsi ( errors [ i ] ) ) ;
6576 if ( initial ) return initial = false ;
6677 reloadApp ( ) ;
6778 } ,
6879 "proxy-error" : function ( errors ) {
69- console . log ( "[WDS] Proxy error." ) ;
80+ log ( "info" , "[WDS] Proxy error." ) ;
7081 for ( var i = 0 ; i < errors . length ; i ++ )
71- console . error ( stripAnsi ( errors [ i ] ) ) ;
82+ log ( "error" , stripAnsi ( errors [ i ] ) ) ;
7283 if ( initial ) return initial = false ;
84+ } ,
85+ close : function ( ) {
86+ log ( "error" , "[WDS] Disconnected!" ) ;
7387 }
7488} ;
7589
76-
7790var hostname = urlParts . hostname ;
78- if ( ! urlParts . hostname || urlParts . hostname === '0.0.0.0' ) {
91+ var protocol = urlParts . protocol ;
92+
93+ if ( urlParts . hostname === '0.0.0.0' ) {
7994 // why do we need this check?
8095 // hostname n/a for file protocol (example, when using electron, ionic)
8196 // see: https://github.com/webpack/webpack-dev-server/pull/384
@@ -84,44 +99,30 @@ if(!urlParts.hostname || urlParts.hostname === '0.0.0.0') {
8499 }
85100}
86101
87- var port = ( ! urlParts . port || urlParts . port === '0' ) ? window . location . port : urlParts . port ;
88-
89- var formattedUrl = url . format ( {
90- protocol : ( window . location . protocol === "https:" || urlParts . hostname === '0.0.0.0' ) ? window . location . protocol : urlParts . protocol ,
91- auth : urlParts . auth ,
92- hostname : hostname ,
93- port : port ,
94- pathname : urlParts . path == null || urlParts . path === '/' ? "/sockjs-node" : urlParts . path
95- } ) ;
96-
97- var newConnection = function ( ) {
98- sock = new SockJS ( formattedUrl ) ;
99-
100- sock . onclose = function ( ) {
101- console . error ( "[WDS] Disconnected!" ) ;
102-
103- // Try to reconnect.
104- sock = null ;
105- setTimeout ( function ( ) {
106- newConnection ( ) ;
107- } , 2000 ) ;
108- } ;
102+ // `hostname` can be empty when the script path is relative. In that case, specifying
103+ // a protocol would result in an invalid URL.
104+ // When https is used in the app, secure websockets are always necessary
105+ // because the browser doesn't accept non-secure websockets.
106+ if ( hostname && ( window . location . protocol === "https:" || urlParts . hostname === '0.0.0.0' ) ) {
107+ protocol = window . location . protocol ;
108+ }
109109
110- sock . onmessage = function ( e ) {
111- // This assumes that all data sent via the websocket is JSON.
112- var msg = JSON . parse ( e . data ) ;
113- onSocketMsg [ msg . type ] ( msg . data ) ;
114- } ;
115- } ;
110+ var socketUrl = url . format ( {
111+ protocol : protocol ,
112+ auth : urlParts . auth ,
113+ hostname : hostname ,
114+ port : ( urlParts . port === '0' ) ? window . location . port : urlParts . port ,
115+ pathname : urlParts . path == null || urlParts . path === '/' ? "/sockjs-node" : urlParts . path
116+ } ) ;
116117
117- newConnection ( ) ;
118+ socket ( socketUrl , onSocketMsg ) ;
118119
119120function reloadApp ( ) {
120121 if ( hot ) {
121- console . log ( "[WDS] App hot update..." ) ;
122+ log ( "info" , "[WDS] App hot update..." ) ;
122123 window . postMessage ( "webpackHotUpdate" + currentHash , "*" ) ;
123124 } else {
124- console . log ( "[WDS] App updated. Reloading..." ) ;
125+ log ( "info" , "[WDS] App updated. Reloading..." ) ;
125126 window . location . reload ( ) ;
126127 }
127128}
0 commit comments