@@ -2,14 +2,30 @@ var url = require('url');
22var SockJS = require ( "sockjs-client" ) ;
33var stripAnsi = require ( 'strip-ansi' ) ;
44
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+
519// If this bundle is inlined, use the resource query to get the correct url.
620// Else, get the url from the <script> this file was called with.
721var urlParts ;
822if ( typeof __resourceQuery === "string" && __resourceQuery ) {
923 urlParts = url . parse ( __resourceQuery . substr ( 1 ) ) ;
1024} 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 ) ;
1329}
1430
1531var sock = null ;
@@ -57,14 +73,29 @@ var onSocketMsg = {
5773 }
5874} ;
5975
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 ( {
6290 protocol : ( window . location . protocol === "https:" || urlParts . hostname === '0.0.0.0' ) ? window . location . protocol : urlParts . protocol ,
6391 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 ,
6694 pathname : urlParts . path == null || urlParts . path === '/' ? "/sockjs-node" : urlParts . path
67- } ) ) ;
95+ } ) ;
96+
97+ var newConnection = function ( ) {
98+ sock = new SockJS ( formattedUrl ) ;
6899
69100 sock . onclose = function ( ) {
70101 console . error ( "[WDS] Disconnected!" ) ;
0 commit comments