@@ -2,14 +2,30 @@ var url = require('url');
2
2
var SockJS = require ( "sockjs-client" ) ;
3
3
var stripAnsi = require ( 'strip-ansi' ) ;
4
4
5
+ function getCurrentScriptSource ( ) {
6
+ // `document.currentScript` is the most accurate way to find the current script,
7
+ // but is not supported in all browsers.
8
+ if ( document . currentScript )
9
+ return document . currentScript . getAttribute ( "src" ) ;
10
+ // Fall back to getting all scripts in the document.
11
+ var scriptElements = document . scripts || [ ] ;
12
+ var currentScript = scriptElements [ scriptElements . length - 1 ] ;
13
+ if ( currentScript )
14
+ return currentScript . getAttribute ( "src" ) ;
15
+ // Fail as there was no script to use.
16
+ throw new Error ( "[WDS] Failed to get current script source" ) ;
17
+ }
18
+
5
19
// If this bundle is inlined, use the resource query to get the correct url.
6
20
// Else, get the url from the <script> this file was called with.
7
21
var urlParts ;
8
22
if ( typeof __resourceQuery === "string" && __resourceQuery ) {
9
23
urlParts = url . parse ( __resourceQuery . substr ( 1 ) ) ;
10
24
} else {
11
- var scriptElements = document . getElementsByTagName ( "script" ) ;
12
- urlParts = url . parse ( scriptElements [ scriptElements . length - 1 ] . getAttribute ( "src" ) . replace ( / \/ [ ^ \/ ] + $ / , "" ) )
25
+ // Else, get the url from the <script> this file was called with.
26
+ var scriptHost = getCurrentScriptSource ( ) ;
27
+ scriptHost = scriptHost . replace ( / \/ [ ^ \/ ] + $ / , "" ) ;
28
+ urlParts = url . parse ( ( scriptHost ? scriptHost : "/" ) , false , true ) ;
13
29
}
14
30
15
31
var sock = null ;
@@ -57,14 +73,29 @@ var onSocketMsg = {
57
73
}
58
74
} ;
59
75
60
- var newConnection = function ( ) {
61
- sock = new SockJS ( url . format ( {
76
+
77
+ var hostname = urlParts . hostname ;
78
+ if ( ! urlParts . hostname || urlParts . hostname === '0.0.0.0' ) {
79
+ // why do we need this check?
80
+ // hostname n/a for file protocol (example, when using electron, ionic)
81
+ // see: https://github.com/webpack/webpack-dev-server/pull/384
82
+ if ( window . location . hostname && ! ! ~ window . location . protocol . indexOf ( 'http' ) ) {
83
+ hostname = window . location . hostname ;
84
+ }
85
+ }
86
+
87
+ var port = ( ! urlParts . port || urlParts . port === '0' ) ? window . location . port : urlParts . port ;
88
+
89
+ var formattedUrl = url . format ( {
62
90
protocol : ( window . location . protocol === "https:" || urlParts . hostname === '0.0.0.0' ) ? window . location . protocol : urlParts . protocol ,
63
91
auth : urlParts . auth ,
64
- hostname : ( urlParts . hostname === '0.0.0.0' ) ? window . location . hostname : urlParts . hostname ,
65
- port : ( urlParts . port === '0' ) ? window . location . port : urlParts . port ,
92
+ hostname : hostname ,
93
+ port : port ,
66
94
pathname : urlParts . path == null || urlParts . path === '/' ? "/sockjs-node" : urlParts . path
67
- } ) ) ;
95
+ } ) ;
96
+
97
+ var newConnection = function ( ) {
98
+ sock = new SockJS ( formattedUrl ) ;
68
99
69
100
sock . onclose = function ( ) {
70
101
console . error ( "[WDS] Disconnected!" ) ;
0 commit comments