@@ -30,44 +30,58 @@ if(typeof __resourceQuery === "string" && __resourceQuery) {
3030var sock = null ;
3131var hot = false ;
3232var initial = true ;
33+ var connected = false ;
3334var currentHash = "" ;
35+ var logLevel = "info" ;
36+
37+ function log ( level , msg ) {
38+ if ( logLevel === "info" && level === "info" )
39+ return console . log ( msg ) ;
40+ if ( [ "info" , "warning" ] . indexOf ( logLevel ) >= 0 && level === "warning" )
41+ return console . warn ( msg ) ;
42+ if ( [ "info" , "warning" , "error" ] . indexOf ( logLevel ) >= 0 && level === "error" )
43+ return console . error ( msg ) ;
44+ }
3445
3546var onSocketMsg = {
3647 hot : function ( ) {
3748 hot = true ;
38- console . log ( "[WDS] Hot Module Replacement enabled." ) ;
49+ log ( "info" , "[WDS] Hot Module Replacement enabled." ) ;
3950 } ,
4051 invalid : function ( ) {
41- console . log ( "[WDS] App updated. Recompiling..." ) ;
52+ log ( "info" , "[WDS] App updated. Recompiling..." ) ;
4253 } ,
4354 hash : function ( hash ) {
4455 currentHash = hash ;
4556 } ,
4657 "still-ok" : function ( ) {
47- console . log ( "[WDS] Nothing changed." )
58+ log ( "info" , "[WDS] Nothing changed." )
59+ } ,
60+ "log-level" : function ( level ) {
61+ logLevel = level ;
4862 } ,
4963 ok : function ( ) {
5064 if ( initial ) return initial = false ;
5165 reloadApp ( ) ;
5266 } ,
5367 warnings : function ( warnings ) {
54- console . log ( "[WDS] Warnings while compiling." ) ;
68+ log ( "info" , "[WDS] Warnings while compiling." ) ;
5569 for ( var i = 0 ; i < warnings . length ; i ++ )
5670 console . warn ( stripAnsi ( warnings [ i ] ) ) ;
5771 if ( initial ) return initial = false ;
5872 reloadApp ( ) ;
5973 } ,
6074 errors : function ( errors ) {
61- console . log ( "[WDS] Errors while compiling." ) ;
75+ log ( "info" , "[WDS] Errors while compiling." ) ;
6276 for ( var i = 0 ; i < errors . length ; i ++ )
6377 console . error ( stripAnsi ( errors [ i ] ) ) ;
6478 if ( initial ) return initial = false ;
6579 reloadApp ( ) ;
6680 } ,
6781 "proxy-error" : function ( errors ) {
68- console . log ( "[WDS] Proxy error." ) ;
82+ log ( "info" , "[WDS] Proxy error." ) ;
6983 for ( var i = 0 ; i < errors . length ; i ++ )
70- console . error ( stripAnsi ( errors [ i ] ) ) ;
84+ log ( "error" , stripAnsi ( errors [ i ] ) ) ;
7185 if ( initial ) return initial = false ;
7286 }
7387} ;
@@ -82,7 +96,10 @@ var newConnection = function() {
8296 } ) ) ;
8397
8498 sock . onclose = function ( ) {
85- console . error ( "[WDS] Disconnected!" ) ;
99+ if ( connected )
100+ log ( "error" , "[WDS] Disconnected!" ) ;
101+
102+ connected = false ;
86103
87104 // Try to reconnect.
88105 sock = null ;
@@ -92,6 +109,7 @@ var newConnection = function() {
92109 } ;
93110
94111 sock . onmessage = function ( e ) {
112+ connected = true ;
95113 // This assumes that all data sent via the websocket is JSON.
96114 var msg = JSON . parse ( e . data ) ;
97115 onSocketMsg [ msg . type ] ( msg . data ) ;
@@ -102,15 +120,15 @@ newConnection();
102120
103121function reloadApp ( ) {
104122 if ( hot ) {
105- console . log ( "[WDS] App hot update..." ) ;
123+ log ( "info" , "[WDS] App hot update..." ) ;
106124 var hotEmitter = require ( "webpack/hot/emitter" ) ;
107125 hotEmitter . emit ( "webpackHotUpdate" , currentHash ) ;
108126 if ( typeof window !== "undefined" ) {
109127 // broadcast update to window
110128 window . postMessage ( "webpackHotUpdate" + currentHash , "*" ) ;
111129 }
112130 } else {
113- console . log ( "[WDS] App updated. Reloading..." ) ;
131+ log ( "info" , "[WDS] App updated. Reloading..." ) ;
114132 window . location . reload ( ) ;
115133 }
116134}
0 commit comments