@@ -30,44 +30,58 @@ if(typeof __resourceQuery === "string" && __resourceQuery) {
30
30
var sock = null ;
31
31
var hot = false ;
32
32
var initial = true ;
33
+ var connected = false ;
33
34
var 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
+ }
34
45
35
46
var onSocketMsg = {
36
47
hot : function ( ) {
37
48
hot = true ;
38
- console . log ( "[WDS] Hot Module Replacement enabled." ) ;
49
+ log ( "info" , "[WDS] Hot Module Replacement enabled." ) ;
39
50
} ,
40
51
invalid : function ( ) {
41
- console . log ( "[WDS] App updated. Recompiling..." ) ;
52
+ log ( "info" , "[WDS] App updated. Recompiling..." ) ;
42
53
} ,
43
54
hash : function ( hash ) {
44
55
currentHash = hash ;
45
56
} ,
46
57
"still-ok" : function ( ) {
47
- console . log ( "[WDS] Nothing changed." )
58
+ log ( "info" , "[WDS] Nothing changed." )
59
+ } ,
60
+ "log-level" : function ( level ) {
61
+ logLevel = level ;
48
62
} ,
49
63
ok : function ( ) {
50
64
if ( initial ) return initial = false ;
51
65
reloadApp ( ) ;
52
66
} ,
53
67
warnings : function ( warnings ) {
54
- console . log ( "[WDS] Warnings while compiling." ) ;
68
+ log ( "info" , "[WDS] Warnings while compiling." ) ;
55
69
for ( var i = 0 ; i < warnings . length ; i ++ )
56
70
console . warn ( stripAnsi ( warnings [ i ] ) ) ;
57
71
if ( initial ) return initial = false ;
58
72
reloadApp ( ) ;
59
73
} ,
60
74
errors : function ( errors ) {
61
- console . log ( "[WDS] Errors while compiling." ) ;
75
+ log ( "info" , "[WDS] Errors while compiling." ) ;
62
76
for ( var i = 0 ; i < errors . length ; i ++ )
63
77
console . error ( stripAnsi ( errors [ i ] ) ) ;
64
78
if ( initial ) return initial = false ;
65
79
reloadApp ( ) ;
66
80
} ,
67
81
"proxy-error" : function ( errors ) {
68
- console . log ( "[WDS] Proxy error." ) ;
82
+ log ( "info" , "[WDS] Proxy error." ) ;
69
83
for ( var i = 0 ; i < errors . length ; i ++ )
70
- console . error ( stripAnsi ( errors [ i ] ) ) ;
84
+ log ( "error" , stripAnsi ( errors [ i ] ) ) ;
71
85
if ( initial ) return initial = false ;
72
86
}
73
87
} ;
@@ -82,7 +96,10 @@ var newConnection = function() {
82
96
} ) ) ;
83
97
84
98
sock . onclose = function ( ) {
85
- console . error ( "[WDS] Disconnected!" ) ;
99
+ if ( connected )
100
+ log ( "error" , "[WDS] Disconnected!" ) ;
101
+
102
+ connected = false ;
86
103
87
104
// Try to reconnect.
88
105
sock = null ;
@@ -92,6 +109,7 @@ var newConnection = function() {
92
109
} ;
93
110
94
111
sock . onmessage = function ( e ) {
112
+ connected = true ;
95
113
// This assumes that all data sent via the websocket is JSON.
96
114
var msg = JSON . parse ( e . data ) ;
97
115
onSocketMsg [ msg . type ] ( msg . data ) ;
@@ -102,15 +120,15 @@ newConnection();
102
120
103
121
function reloadApp ( ) {
104
122
if ( hot ) {
105
- console . log ( "[WDS] App hot update..." ) ;
123
+ log ( "info" , "[WDS] App hot update..." ) ;
106
124
var hotEmitter = require ( "webpack/hot/emitter" ) ;
107
125
hotEmitter . emit ( "webpackHotUpdate" , currentHash ) ;
108
126
if ( typeof window !== "undefined" ) {
109
127
// broadcast update to window
110
128
window . postMessage ( "webpackHotUpdate" + currentHash , "*" ) ;
111
129
}
112
130
} else {
113
- console . log ( "[WDS] App updated. Reloading..." ) ;
131
+ log ( "info" , "[WDS] App updated. Reloading..." ) ;
114
132
window . location . reload ( ) ;
115
133
}
116
134
}
0 commit comments