webpack-dev-server 3.1.14 → 3.3.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,39 +85,50 @@ 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.
82
- if (useWarningOverlay || useErrorOverlay) overlay.clear();
88
+ log.info('[WDS] App updated. Recompiling...'); // fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
89
+
90
+ if (useWarningOverlay || useErrorOverlay) {
91
+ overlay.clear();
92
+ }
93
+
83
94
  sendMsg('Invalid');
84
95
  },
85
96
  hash: function hash(_hash) {
86
97
  currentHash = _hash;
87
98
  },
88
-
89
99
  'still-ok': function stillOk() {
90
100
  log.info('[WDS] Nothing changed.');
91
- if (useWarningOverlay || useErrorOverlay) overlay.clear();
101
+
102
+ if (useWarningOverlay || useErrorOverlay) {
103
+ overlay.clear();
104
+ }
105
+
92
106
  sendMsg('StillOk');
93
107
  },
94
108
  'log-level': function logLevel(level) {
95
109
  var hotCtx = require.context('webpack/hot', false, /^\.\/log$/);
110
+
96
111
  if (hotCtx.keys().indexOf('./log') !== -1) {
97
112
  hotCtx('./log').setLogLevel(level);
98
113
  }
114
+
99
115
  switch (level) {
100
116
  case INFO:
101
117
  case ERROR:
102
118
  log.setLevel(level);
103
119
  break;
120
+
104
121
  case WARNING:
105
122
  // loglevel's warning name is different from webpack's
106
123
  log.setLevel('warn');
107
124
  break;
125
+
108
126
  case NONE:
109
127
  log.disableAll();
110
128
  break;
129
+
111
130
  default:
112
- log.error('[WDS] Unknown clientLogLevel \'' + level + '\'');
131
+ log.error("[WDS] Unknown clientLogLevel '".concat(level, "'"));
113
132
  }
114
133
  },
115
134
  overlay: function overlay(value) {
@@ -128,46 +147,72 @@ var onSocketMsg = {
128
147
  useProgress = _progress;
129
148
  }
130
149
  },
131
-
132
150
  'progress-update': function progressUpdate(data) {
133
- if (useProgress) log.info('[WDS] ' + data.percent + '% - ' + data.msg + '.');
151
+ if (useProgress) {
152
+ log.info("[WDS] ".concat(data.percent, "% - ").concat(data.msg, "."));
153
+ }
154
+
134
155
  sendMsg('Progress', data);
135
156
  },
136
157
  ok: function ok() {
137
158
  sendMsg('Ok');
138
- if (useWarningOverlay || useErrorOverlay) overlay.clear();
139
- if (initial) return initial = false; // eslint-disable-line no-return-assign
159
+
160
+ if (useWarningOverlay || useErrorOverlay) {
161
+ overlay.clear();
162
+ }
163
+
164
+ if (initial) {
165
+ return initial = false;
166
+ } // eslint-disable-line no-return-assign
167
+
168
+
140
169
  reloadApp();
141
170
  },
142
-
143
171
  'content-changed': function contentChanged() {
144
172
  log.info('[WDS] Content base changed. Reloading...');
145
173
  self.location.reload();
146
174
  },
147
175
  warnings: function warnings(_warnings) {
148
176
  log.warn('[WDS] Warnings while compiling.');
177
+
149
178
  var strippedWarnings = _warnings.map(function (warning) {
150
179
  return stripAnsi(warning);
151
180
  });
181
+
152
182
  sendMsg('Warnings', strippedWarnings);
183
+
153
184
  for (var i = 0; i < strippedWarnings.length; i++) {
154
185
  log.warn(strippedWarnings[i]);
155
186
  }
156
- if (useWarningOverlay) overlay.showMessage(_warnings);
157
187
 
158
- if (initial) return initial = false; // eslint-disable-line no-return-assign
188
+ if (useWarningOverlay) {
189
+ overlay.showMessage(_warnings);
190
+ }
191
+
192
+ if (initial) {
193
+ return initial = false;
194
+ } // eslint-disable-line no-return-assign
195
+
196
+
159
197
  reloadApp();
160
198
  },
161
199
  errors: function errors(_errors) {
162
200
  log.error('[WDS] Errors while compiling. Reload prevented.');
201
+
163
202
  var strippedErrors = _errors.map(function (error) {
164
203
  return stripAnsi(error);
165
204
  });
205
+
166
206
  sendMsg('Errors', strippedErrors);
207
+
167
208
  for (var i = 0; i < strippedErrors.length; i++) {
168
209
  log.error(strippedErrors[i]);
169
210
  }
170
- if (useErrorOverlay) overlay.showMessage(_errors);
211
+
212
+ if (useErrorOverlay) {
213
+ overlay.showMessage(_errors);
214
+ }
215
+
171
216
  initial = false;
172
217
  },
173
218
  error: function error(_error) {
@@ -178,11 +223,10 @@ var onSocketMsg = {
178
223
  sendMsg('Close');
179
224
  }
180
225
  };
181
-
182
226
  var hostname = urlParts.hostname;
183
227
  var protocol = urlParts.protocol;
228
+ var port = urlParts.port; // check ipv4 and ipv6 `all hostname`
184
229
 
185
- // check ipv4 and ipv6 `all hostname`
186
230
  if (hostname === '0.0.0.0' || hostname === '::') {
187
231
  // why do we need this check?
188
232
  // hostname n/a for file protocol (example, when using electron, ionic)
@@ -190,13 +234,14 @@ if (hostname === '0.0.0.0' || hostname === '::') {
190
234
  // eslint-disable-next-line no-bitwise
191
235
  if (self.location.hostname && !!~self.location.protocol.indexOf('http')) {
192
236
  hostname = self.location.hostname;
237
+ port = self.location.port;
193
238
  }
194
- }
195
-
196
- // `hostname` can be empty when the script path is relative. In that case, specifying
239
+ } // `hostname` can be empty when the script path is relative. In that case, specifying
197
240
  // a protocol would result in an invalid URL.
198
241
  // When https is used in the app, secure websockets are always necessary
199
242
  // because the browser doesn't accept non-secure websockets.
243
+
244
+
200
245
  if (hostname && (self.location.protocol === 'https:' || urlParts.hostname === '0.0.0.0')) {
201
246
  protocol = self.location.protocol;
202
247
  }
@@ -205,12 +250,13 @@ var socketUrl = url.format({
205
250
  protocol: protocol,
206
251
  auth: urlParts.auth,
207
252
  hostname: hostname,
208
- port: urlParts.port,
209
- pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : urlParts.path
253
+ port: port,
254
+ // If sockPath is provided it'll be passed in via the __resourceQuery as a
255
+ // query param so it has to be parsed out of the querystring in order for the
256
+ // client to open the socket to the correct location.
257
+ pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : querystring.parse(urlParts.path).sockPath || urlParts.path
210
258
  });
211
-
212
259
  socket(socketUrl, onSocketMsg);
213
-
214
260
  var isUnloading = false;
215
261
  self.addEventListener('beforeunload', function () {
216
262
  isUnloading = true;
@@ -220,24 +266,28 @@ function reloadApp() {
220
266
  if (isUnloading || !hotReload) {
221
267
  return;
222
268
  }
269
+
223
270
  if (_hot) {
224
- log.info('[WDS] App hot update...');
225
- // eslint-disable-next-line global-require
271
+ log.info('[WDS] App hot update...'); // eslint-disable-next-line global-require
272
+
226
273
  var hotEmitter = require('webpack/hot/emitter');
274
+
227
275
  hotEmitter.emit('webpackHotUpdate', currentHash);
276
+
228
277
  if (typeof self !== 'undefined' && self.window) {
229
278
  // broadcast update to window
230
- self.postMessage('webpackHotUpdate' + currentHash, '*');
279
+ self.postMessage("webpackHotUpdate".concat(currentHash), '*');
231
280
  }
232
281
  } else {
233
- var rootWindow = self;
234
- // use parent window for reload (in case we're in an iframe with no valid src)
282
+ var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
283
+
235
284
  var intervalId = self.setInterval(function () {
236
285
  if (rootWindow.location.protocol !== 'about:') {
237
286
  // reload immediately if protocol is valid
238
287
  applyReload(rootWindow, intervalId);
239
288
  } else {
240
289
  rootWindow = rootWindow.parent;
290
+
241
291
  if (rootWindow.parent === rootWindow) {
242
292
  // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
243
293
  applyReload(rootWindow, intervalId);