webpack-dev-server 3.1.12 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client/index.js CHANGED
@@ -1,12 +1,18 @@
1
1
  'use strict';
2
-
3
2
  /* global __resourceQuery WorkerGlobalScope self */
3
+
4
4
  /* eslint prefer-destructuring: off */
5
5
 
6
+ var querystring = require('querystring');
7
+
6
8
  var url = require('url');
9
+
7
10
  var stripAnsi = require('strip-ansi');
11
+
8
12
  var log = require('loglevel').getLogger('webpack-dev-server');
13
+
9
14
  var socket = require('./socket');
15
+
10
16
  var overlay = require('./overlay');
11
17
 
12
18
  function getCurrentScriptSource() {
@@ -14,30 +20,35 @@ function getCurrentScriptSource() {
14
20
  // but is not supported in all browsers.
15
21
  if (document.currentScript) {
16
22
  return document.currentScript.getAttribute('src');
17
- }
18
- // Fall back to getting all scripts in the document.
23
+ } // Fall back to getting all scripts in the document.
24
+
25
+
19
26
  var scriptElements = document.scripts || [];
20
27
  var currentScript = scriptElements[scriptElements.length - 1];
28
+
21
29
  if (currentScript) {
22
30
  return currentScript.getAttribute('src');
23
- }
24
- // Fail as there was no script to use.
31
+ } // Fail as there was no script to use.
32
+
33
+
25
34
  throw new Error('[WDS] Failed to get current script source.');
26
35
  }
27
36
 
28
- var urlParts = void 0;
37
+ var urlParts;
29
38
  var hotReload = true;
39
+
30
40
  if (typeof window !== 'undefined') {
31
41
  var qs = window.location.search.toLowerCase();
32
42
  hotReload = qs.indexOf('hotreload=false') === -1;
33
43
  }
44
+
34
45
  if (typeof __resourceQuery === 'string' && __resourceQuery) {
35
46
  // If this bundle is inlined, use the resource query to get the correct url.
36
47
  urlParts = url.parse(__resourceQuery.substr(1));
37
48
  } else {
38
49
  // Else, get the url from the <script> this file was called with.
39
- var scriptHost = getCurrentScriptSource();
40
- // eslint-disable-next-line no-useless-escape
50
+ var scriptHost = getCurrentScriptSource(); // eslint-disable-next-line no-useless-escape
51
+
41
52
  scriptHost = scriptHost.replace(/\/[^\/]+$/, '');
42
53
  urlParts = url.parse(scriptHost || '/', false, true);
43
54
  }
@@ -52,20 +63,17 @@ var currentHash = '';
52
63
  var useWarningOverlay = false;
53
64
  var useErrorOverlay = false;
54
65
  var useProgress = false;
55
-
56
66
  var INFO = 'info';
57
67
  var WARNING = 'warning';
58
68
  var ERROR = 'error';
59
- var NONE = 'none';
69
+ var NONE = 'none'; // Set the default log level
60
70
 
61
- // Set the default log level
62
- log.setDefaultLevel(INFO);
71
+ log.setDefaultLevel(INFO); // Send messages to the outside, so plugins can consume it.
63
72
 
64
- // Send messages to the outside, so plugins can consume it.
65
73
  function sendMsg(type, data) {
66
74
  if (typeof self !== 'undefined' && (typeof WorkerGlobalScope === 'undefined' || !(self instanceof WorkerGlobalScope))) {
67
75
  self.postMessage({
68
- type: 'webpack' + type,
76
+ type: "webpack".concat(type),
69
77
  data: data
70
78
  }, '*');
71
79
  }
@@ -77,15 +85,14 @@ var onSocketMsg = {
77
85
  log.info('[WDS] Hot Module Replacement enabled.');
78
86
  },
79
87
  invalid: function invalid() {
80
- log.info('[WDS] App updated. Recompiling...');
81
- // fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
88
+ log.info('[WDS] App updated. Recompiling...'); // fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
89
+
82
90
  if (useWarningOverlay || useErrorOverlay) overlay.clear();
83
91
  sendMsg('Invalid');
84
92
  },
85
93
  hash: function hash(_hash) {
86
94
  currentHash = _hash;
87
95
  },
88
-
89
96
  'still-ok': function stillOk() {
90
97
  log.info('[WDS] Nothing changed.');
91
98
  if (useWarningOverlay || useErrorOverlay) overlay.clear();
@@ -93,23 +100,28 @@ var onSocketMsg = {
93
100
  },
94
101
  'log-level': function logLevel(level) {
95
102
  var hotCtx = require.context('webpack/hot', false, /^\.\/log$/);
103
+
96
104
  if (hotCtx.keys().indexOf('./log') !== -1) {
97
105
  hotCtx('./log').setLogLevel(level);
98
106
  }
107
+
99
108
  switch (level) {
100
109
  case INFO:
101
110
  case ERROR:
102
111
  log.setLevel(level);
103
112
  break;
113
+
104
114
  case WARNING:
105
115
  // loglevel's warning name is different from webpack's
106
116
  log.setLevel('warn');
107
117
  break;
118
+
108
119
  case NONE:
109
120
  log.disableAll();
110
121
  break;
122
+
111
123
  default:
112
- log.error('[WDS] Unknown clientLogLevel \'' + level + '\'');
124
+ log.error("[WDS] Unknown clientLogLevel '".concat(level, "'"));
113
125
  }
114
126
  },
115
127
  overlay: function overlay(value) {
@@ -128,45 +140,52 @@ var onSocketMsg = {
128
140
  useProgress = _progress;
129
141
  }
130
142
  },
131
-
132
143
  'progress-update': function progressUpdate(data) {
133
- if (useProgress) log.info('[WDS] ' + data.percent + '% - ' + data.msg + '.');
144
+ if (useProgress) log.info("[WDS] ".concat(data.percent, "% - ").concat(data.msg, "."));
134
145
  sendMsg('Progress', data);
135
146
  },
136
147
  ok: function ok() {
137
148
  sendMsg('Ok');
138
149
  if (useWarningOverlay || useErrorOverlay) overlay.clear();
139
150
  if (initial) return initial = false; // eslint-disable-line no-return-assign
151
+
140
152
  reloadApp();
141
153
  },
142
-
143
154
  'content-changed': function contentChanged() {
144
155
  log.info('[WDS] Content base changed. Reloading...');
145
156
  self.location.reload();
146
157
  },
147
158
  warnings: function warnings(_warnings) {
148
159
  log.warn('[WDS] Warnings while compiling.');
160
+
149
161
  var strippedWarnings = _warnings.map(function (warning) {
150
162
  return stripAnsi(warning);
151
163
  });
164
+
152
165
  sendMsg('Warnings', strippedWarnings);
166
+
153
167
  for (var i = 0; i < strippedWarnings.length; i++) {
154
168
  log.warn(strippedWarnings[i]);
155
169
  }
156
- if (useWarningOverlay) overlay.showMessage(_warnings);
157
170
 
171
+ if (useWarningOverlay) overlay.showMessage(_warnings);
158
172
  if (initial) return initial = false; // eslint-disable-line no-return-assign
173
+
159
174
  reloadApp();
160
175
  },
161
176
  errors: function errors(_errors) {
162
177
  log.error('[WDS] Errors while compiling. Reload prevented.');
178
+
163
179
  var strippedErrors = _errors.map(function (error) {
164
180
  return stripAnsi(error);
165
181
  });
182
+
166
183
  sendMsg('Errors', strippedErrors);
184
+
167
185
  for (var i = 0; i < strippedErrors.length; i++) {
168
186
  log.error(strippedErrors[i]);
169
187
  }
188
+
170
189
  if (useErrorOverlay) overlay.showMessage(_errors);
171
190
  initial = false;
172
191
  },
@@ -178,11 +197,9 @@ var onSocketMsg = {
178
197
  sendMsg('Close');
179
198
  }
180
199
  };
181
-
182
200
  var hostname = urlParts.hostname;
183
- var protocol = urlParts.protocol;
201
+ var protocol = urlParts.protocol; // check ipv4 and ipv6 `all hostname`
184
202
 
185
- // check ipv4 and ipv6 `all hostname`
186
203
  if (hostname === '0.0.0.0' || hostname === '::') {
187
204
  // why do we need this check?
188
205
  // hostname n/a for file protocol (example, when using electron, ionic)
@@ -191,12 +208,12 @@ if (hostname === '0.0.0.0' || hostname === '::') {
191
208
  if (self.location.hostname && !!~self.location.protocol.indexOf('http')) {
192
209
  hostname = self.location.hostname;
193
210
  }
194
- }
195
-
196
- // `hostname` can be empty when the script path is relative. In that case, specifying
211
+ } // `hostname` can be empty when the script path is relative. In that case, specifying
197
212
  // a protocol would result in an invalid URL.
198
213
  // When https is used in the app, secure websockets are always necessary
199
214
  // because the browser doesn't accept non-secure websockets.
215
+
216
+
200
217
  if (hostname && (self.location.protocol === 'https:' || urlParts.hostname === '0.0.0.0')) {
201
218
  protocol = self.location.protocol;
202
219
  }
@@ -206,11 +223,12 @@ var socketUrl = url.format({
206
223
  auth: urlParts.auth,
207
224
  hostname: hostname,
208
225
  port: urlParts.port,
209
- pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : urlParts.path
226
+ // If sockPath is provided it'll be passed in via the __resourceQuery as a
227
+ // query param so it has to be parsed out of the querystring in order for the
228
+ // client to open the socket to the correct location.
229
+ pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : querystring.parse(urlParts.path).sockPath || urlParts.path
210
230
  });
211
-
212
231
  socket(socketUrl, onSocketMsg);
213
-
214
232
  var isUnloading = false;
215
233
  self.addEventListener('beforeunload', function () {
216
234
  isUnloading = true;
@@ -220,24 +238,28 @@ function reloadApp() {
220
238
  if (isUnloading || !hotReload) {
221
239
  return;
222
240
  }
241
+
223
242
  if (_hot) {
224
- log.info('[WDS] App hot update...');
225
- // eslint-disable-next-line global-require
243
+ log.info('[WDS] App hot update...'); // eslint-disable-next-line global-require
244
+
226
245
  var hotEmitter = require('webpack/hot/emitter');
246
+
227
247
  hotEmitter.emit('webpackHotUpdate', currentHash);
248
+
228
249
  if (typeof self !== 'undefined' && self.window) {
229
250
  // broadcast update to window
230
- self.postMessage('webpackHotUpdate' + currentHash, '*');
251
+ self.postMessage("webpackHotUpdate".concat(currentHash), '*');
231
252
  }
232
253
  } else {
233
- var rootWindow = self;
234
- // use parent window for reload (in case we're in an iframe with no valid src)
254
+ var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
255
+
235
256
  var intervalId = self.setInterval(function () {
236
257
  if (rootWindow.location.protocol !== 'about:') {
237
258
  // reload immediately if protocol is valid
238
259
  applyReload(rootWindow, intervalId);
239
260
  } else {
240
261
  rootWindow = rootWindow.parent;
262
+
241
263
  if (rootWindow.parent === rootWindow) {
242
264
  // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
243
265
  applyReload(rootWindow, intervalId);