1
1
var url = require ( 'url' ) ;
2
- var SockJS = require ( "sockjs-client" ) ;
3
2
var stripAnsi = require ( 'strip-ansi' ) ;
3
+ var socket = require ( './socket' ) ;
4
4
5
5
function getCurrentScriptSource ( ) {
6
6
// `document.currentScript` is the most accurate way to find the current script,
@@ -16,10 +16,9 @@ function getCurrentScriptSource() {
16
16
throw new Error ( "[WDS] Failed to get current script source" ) ;
17
17
}
18
18
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.
21
19
var 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.
23
22
urlParts = url . parse ( __resourceQuery . substr ( 1 ) ) ;
24
23
} else {
25
24
// Else, get the url from the <script> this file was called with.
@@ -28,54 +27,70 @@ if (typeof __resourceQuery === "string" && __resourceQuery) {
28
27
urlParts = url . parse ( ( scriptHost ? scriptHost : "/" ) , false , true ) ;
29
28
}
30
29
31
- var sock = null ;
32
30
var hot = false ;
33
31
var initial = true ;
34
32
var 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
+ }
35
43
36
44
var onSocketMsg = {
37
45
hot : function ( ) {
38
46
hot = true ;
39
- console . log ( "[WDS] Hot Module Replacement enabled." ) ;
47
+ log ( "info" , "[WDS] Hot Module Replacement enabled." ) ;
40
48
} ,
41
49
invalid : function ( ) {
42
- console . log ( "[WDS] App updated. Recompiling..." ) ;
50
+ log ( "info" , "[WDS] App updated. Recompiling..." ) ;
43
51
} ,
44
52
hash : function ( hash ) {
45
53
currentHash = hash ;
46
54
} ,
47
55
"still-ok" : function ( ) {
48
- console . log ( "[WDS] Nothing changed." )
56
+ log ( "info" , "[WDS] Nothing changed." )
57
+ } ,
58
+ "log-level" : function ( level ) {
59
+ logLevel = level ;
49
60
} ,
50
61
ok : function ( ) {
51
62
if ( initial ) return initial = false ;
52
63
reloadApp ( ) ;
53
64
} ,
54
65
warnings : function ( warnings ) {
55
- console . log ( "[WDS] Warnings while compiling." ) ;
66
+ log ( "info" , "[WDS] Warnings while compiling." ) ;
56
67
for ( var i = 0 ; i < warnings . length ; i ++ )
57
68
console . warn ( stripAnsi ( warnings [ i ] ) ) ;
58
69
if ( initial ) return initial = false ;
59
70
reloadApp ( ) ;
60
71
} ,
61
72
errors : function ( errors ) {
62
- console . log ( "[WDS] Errors while compiling." ) ;
73
+ log ( "info" , "[WDS] Errors while compiling." ) ;
63
74
for ( var i = 0 ; i < errors . length ; i ++ )
64
75
console . error ( stripAnsi ( errors [ i ] ) ) ;
65
76
if ( initial ) return initial = false ;
66
77
reloadApp ( ) ;
67
78
} ,
68
79
"proxy-error" : function ( errors ) {
69
- console . log ( "[WDS] Proxy error." ) ;
80
+ log ( "info" , "[WDS] Proxy error." ) ;
70
81
for ( var i = 0 ; i < errors . length ; i ++ )
71
- console . error ( stripAnsi ( errors [ i ] ) ) ;
82
+ log ( "error" , stripAnsi ( errors [ i ] ) ) ;
72
83
if ( initial ) return initial = false ;
84
+ } ,
85
+ close : function ( ) {
86
+ log ( "error" , "[WDS] Disconnected!" ) ;
73
87
}
74
88
} ;
75
89
76
-
77
90
var 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' ) {
79
94
// why do we need this check?
80
95
// hostname n/a for file protocol (example, when using electron, ionic)
81
96
// see: https://github.com/webpack/webpack-dev-server/pull/384
@@ -84,44 +99,30 @@ if(!urlParts.hostname || urlParts.hostname === '0.0.0.0') {
84
99
}
85
100
}
86
101
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
+ }
109
109
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
+ } ) ;
116
117
117
- newConnection ( ) ;
118
+ socket ( socketUrl , onSocketMsg ) ;
118
119
119
120
function reloadApp ( ) {
120
121
if ( hot ) {
121
- console . log ( "[WDS] App hot update..." ) ;
122
+ log ( "info" , "[WDS] App hot update..." ) ;
122
123
window . postMessage ( "webpackHotUpdate" + currentHash , "*" ) ;
123
124
} else {
124
- console . log ( "[WDS] App updated. Reloading..." ) ;
125
+ log ( "info" , "[WDS] App updated. Reloading..." ) ;
125
126
window . location . reload ( ) ;
126
127
}
127
128
}
0 commit comments