webpack-dev-server 3.4.1 → 3.7.0

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
@@ -3,119 +3,74 @@
3
3
 
4
4
  /* eslint prefer-destructuring: off */
5
5
 
6
- var querystring = require('querystring');
7
-
8
- var url = require('url');
9
-
10
6
  var stripAnsi = require('strip-ansi');
11
7
 
12
- var log = require('loglevel').getLogger('webpack-dev-server');
13
-
14
8
  var socket = require('./socket');
15
9
 
16
10
  var overlay = require('./overlay');
17
11
 
18
- function getCurrentScriptSource() {
19
- // `document.currentScript` is the most accurate way to find the current script,
20
- // but is not supported in all browsers.
21
- if (document.currentScript) {
22
- return document.currentScript.getAttribute('src');
23
- } // Fall back to getting all scripts in the document.
12
+ var _require = require('./utils/log'),
13
+ log = _require.log,
14
+ setLogLevel = _require.setLogLevel;
24
15
 
16
+ var sendMessage = require('./utils/sendMessage');
25
17
 
26
- var scriptElements = document.scripts || [];
27
- var currentScript = scriptElements[scriptElements.length - 1];
18
+ var reloadApp = require('./utils/reloadApp');
28
19
 
29
- if (currentScript) {
30
- return currentScript.getAttribute('src');
31
- } // Fail as there was no script to use.
32
-
33
-
34
- throw new Error('[WDS] Failed to get current script source.');
35
- }
20
+ var createSocketUrl = require('./utils/createSocketUrl');
36
21
 
37
- var urlParts;
38
- var hotReload = true;
22
+ var status = {
23
+ isUnloading: false,
24
+ currentHash: ''
25
+ };
26
+ var options = {
27
+ hot: false,
28
+ hotReload: true,
29
+ liveReload: false,
30
+ initial: true,
31
+ useWarningOverlay: false,
32
+ useErrorOverlay: false,
33
+ useProgress: false
34
+ };
35
+ var socketUrl = createSocketUrl(__resourceQuery);
36
+ self.addEventListener('beforeunload', function () {
37
+ status.isUnloading = true;
38
+ });
39
39
 
40
40
  if (typeof window !== 'undefined') {
41
41
  var qs = window.location.search.toLowerCase();
42
- hotReload = qs.indexOf('hotreload=false') === -1;
42
+ options.hotReload = qs.indexOf('hotreload=false') === -1;
43
43
  }
44
44
 
45
- if (typeof __resourceQuery === 'string' && __resourceQuery) {
46
- // If this bundle is inlined, use the resource query to get the correct url.
47
- urlParts = url.parse(__resourceQuery.substr(1));
48
- } else {
49
- // Else, get the url from the <script> this file was called with.
50
- var scriptHost = getCurrentScriptSource(); // eslint-disable-next-line no-useless-escape
51
-
52
- scriptHost = scriptHost.replace(/\/[^\/]+$/, '');
53
- urlParts = url.parse(scriptHost || '/', false, true);
54
- }
55
-
56
- if (!urlParts.port || urlParts.port === '0') {
57
- urlParts.port = self.location.port;
58
- }
59
-
60
- var _hot = false;
61
- var _liveReload = false;
62
- var initial = true;
63
- var currentHash = '';
64
- var useWarningOverlay = false;
65
- var useErrorOverlay = false;
66
- var useProgress = false;
67
- var INFO = 'info';
68
- var WARN = 'warn';
69
- var ERROR = 'error';
70
- var DEBUG = 'debug';
71
- var TRACE = 'trace';
72
- var SILENT = 'silent'; // deprecated
73
- // TODO: remove these at major released
74
- // https://github.com/webpack/webpack-dev-server/pull/1825
75
-
76
- var WARNING = 'warning';
77
- var NONE = 'none'; // Set the default log level
78
-
79
- log.setDefaultLevel(INFO); // Send messages to the outside, so plugins can consume it.
80
-
81
- function sendMsg(type, data) {
82
- if (typeof self !== 'undefined' && (typeof WorkerGlobalScope === 'undefined' || !(self instanceof WorkerGlobalScope))) {
83
- self.postMessage({
84
- type: "webpack".concat(type),
85
- data: data
86
- }, '*');
87
- }
88
- }
89
-
90
- var onSocketMsg = {
45
+ var onSocketMessage = {
91
46
  hot: function hot() {
92
- _hot = true;
47
+ options.hot = true;
93
48
  log.info('[WDS] Hot Module Replacement enabled.');
94
49
  },
95
50
  liveReload: function liveReload() {
96
- _liveReload = true;
51
+ options.liveReload = true;
97
52
  log.info('[WDS] Live Reloading enabled.');
98
53
  },
99
54
  invalid: function invalid() {
100
55
  log.info('[WDS] App updated. Recompiling...'); // fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
101
56
 
102
- if (useWarningOverlay || useErrorOverlay) {
57
+ if (options.useWarningOverlay || options.useErrorOverlay) {
103
58
  overlay.clear();
104
59
  }
105
60
 
106
- sendMsg('Invalid');
61
+ sendMessage('Invalid');
107
62
  },
108
63
  hash: function hash(_hash) {
109
- currentHash = _hash;
64
+ status.currentHash = _hash;
110
65
  },
111
66
  'still-ok': function stillOk() {
112
67
  log.info('[WDS] Nothing changed.');
113
68
 
114
- if (useWarningOverlay || useErrorOverlay) {
69
+ if (options.useWarningOverlay || options.useErrorOverlay) {
115
70
  overlay.clear();
116
71
  }
117
72
 
118
- sendMsg('StillOk');
73
+ sendMessage('StillOk');
119
74
  },
120
75
  'log-level': function logLevel(level) {
121
76
  var hotCtx = require.context('webpack/hot', false, /^\.\/log$/);
@@ -124,67 +79,44 @@ var onSocketMsg = {
124
79
  hotCtx('./log').setLogLevel(level);
125
80
  }
126
81
 
127
- switch (level) {
128
- case INFO:
129
- case WARN:
130
- case DEBUG:
131
- case TRACE:
132
- case ERROR:
133
- log.setLevel(level);
134
- break;
135
- // deprecated
136
-
137
- case WARNING:
138
- // loglevel's warning name is different from webpack's
139
- log.setLevel('warn');
140
- break;
141
- // deprecated
142
-
143
- case NONE:
144
- case SILENT:
145
- log.disableAll();
146
- break;
147
-
148
- default:
149
- log.error("[WDS] Unknown clientLogLevel '".concat(level, "'"));
150
- }
82
+ setLogLevel(level);
151
83
  },
152
84
  overlay: function overlay(value) {
153
85
  if (typeof document !== 'undefined') {
154
86
  if (typeof value === 'boolean') {
155
- useWarningOverlay = false;
156
- useErrorOverlay = value;
87
+ options.useWarningOverlay = false;
88
+ options.useErrorOverlay = value;
157
89
  } else if (value) {
158
- useWarningOverlay = value.warnings;
159
- useErrorOverlay = value.errors;
90
+ options.useWarningOverlay = value.warnings;
91
+ options.useErrorOverlay = value.errors;
160
92
  }
161
93
  }
162
94
  },
163
95
  progress: function progress(_progress) {
164
96
  if (typeof document !== 'undefined') {
165
- useProgress = _progress;
97
+ options.useProgress = _progress;
166
98
  }
167
99
  },
168
100
  'progress-update': function progressUpdate(data) {
169
- if (useProgress) {
101
+ if (options.useProgress) {
170
102
  log.info("[WDS] ".concat(data.percent, "% - ").concat(data.msg, "."));
171
103
  }
172
104
 
173
- sendMsg('Progress', data);
105
+ sendMessage('Progress', data);
174
106
  },
175
107
  ok: function ok() {
176
- sendMsg('Ok');
108
+ sendMessage('Ok');
177
109
 
178
- if (useWarningOverlay || useErrorOverlay) {
110
+ if (options.useWarningOverlay || options.useErrorOverlay) {
179
111
  overlay.clear();
180
112
  }
181
113
 
182
- if (initial) {
183
- return initial = false;
114
+ if (options.initial) {
115
+ return options.initial = false;
184
116
  } // eslint-disable-line no-return-assign
185
117
 
186
118
 
187
- reloadApp();
119
+ reloadApp(options, status);
188
120
  },
189
121
  'content-changed': function contentChanged() {
190
122
  log.info('[WDS] Content base changed. Reloading...');
@@ -197,22 +129,22 @@ var onSocketMsg = {
197
129
  return stripAnsi(warning);
198
130
  });
199
131
 
200
- sendMsg('Warnings', strippedWarnings);
132
+ sendMessage('Warnings', strippedWarnings);
201
133
 
202
134
  for (var i = 0; i < strippedWarnings.length; i++) {
203
135
  log.warn(strippedWarnings[i]);
204
136
  }
205
137
 
206
- if (useWarningOverlay) {
138
+ if (options.useWarningOverlay) {
207
139
  overlay.showMessage(_warnings);
208
140
  }
209
141
 
210
- if (initial) {
211
- return initial = false;
142
+ if (options.initial) {
143
+ return options.initial = false;
212
144
  } // eslint-disable-line no-return-assign
213
145
 
214
146
 
215
- reloadApp();
147
+ reloadApp(options, status);
216
148
  },
217
149
  errors: function errors(_errors) {
218
150
  log.error('[WDS] Errors while compiling. Reload prevented.');
@@ -221,117 +153,24 @@ var onSocketMsg = {
221
153
  return stripAnsi(error);
222
154
  });
223
155
 
224
- sendMsg('Errors', strippedErrors);
156
+ sendMessage('Errors', strippedErrors);
225
157
 
226
158
  for (var i = 0; i < strippedErrors.length; i++) {
227
159
  log.error(strippedErrors[i]);
228
160
  }
229
161
 
230
- if (useErrorOverlay) {
162
+ if (options.useErrorOverlay) {
231
163
  overlay.showMessage(_errors);
232
164
  }
233
165
 
234
- initial = false;
166
+ options.initial = false;
235
167
  },
236
168
  error: function error(_error) {
237
169
  log.error(_error);
238
170
  },
239
171
  close: function close() {
240
172
  log.error('[WDS] Disconnected!');
241
- sendMsg('Close');
173
+ sendMessage('Close');
242
174
  }
243
175
  };
244
- var hostname = urlParts.hostname;
245
- var protocol = urlParts.protocol; // check ipv4 and ipv6 `all hostname`
246
-
247
- if (hostname === '0.0.0.0' || hostname === '::') {
248
- // why do we need this check?
249
- // hostname n/a for file protocol (example, when using electron, ionic)
250
- // see: https://github.com/webpack/webpack-dev-server/pull/384
251
- // eslint-disable-next-line no-bitwise
252
- if (self.location.hostname && !!~self.location.protocol.indexOf('http')) {
253
- hostname = self.location.hostname;
254
- }
255
- } // `hostname` can be empty when the script path is relative. In that case, specifying
256
- // a protocol would result in an invalid URL.
257
- // When https is used in the app, secure websockets are always necessary
258
- // because the browser doesn't accept non-secure websockets.
259
-
260
-
261
- if (hostname && (self.location.protocol === 'https:' || urlParts.hostname === '0.0.0.0')) {
262
- protocol = self.location.protocol;
263
- } // default values of the sock url if they are not provided
264
-
265
-
266
- var sockHost = hostname;
267
- var sockPath = '/sockjs-node';
268
- var sockPort = urlParts.port;
269
-
270
- if (urlParts.path !== null && // eslint-disable-next-line no-undefined
271
- urlParts.path !== undefined && urlParts.path !== '/') {
272
- var parsedQuery = querystring.parse(urlParts.path); // all of these sock url params are optionally passed in through
273
- // __resourceQuery, so we need to fall back to the default if
274
- // they are not provided
275
-
276
- sockHost = parsedQuery.sockHost || sockHost;
277
- sockPath = parsedQuery.sockPath || sockPath;
278
- sockPort = parsedQuery.sockPort || sockPort;
279
- }
280
-
281
- var socketUrl = url.format({
282
- protocol: protocol,
283
- auth: urlParts.auth,
284
- hostname: sockHost,
285
- port: sockPort,
286
- // If sockPath is provided it'll be passed in via the __resourceQuery as a
287
- // query param so it has to be parsed out of the querystring in order for the
288
- // client to open the socket to the correct location.
289
- pathname: sockPath
290
- });
291
- socket(socketUrl, onSocketMsg);
292
- var isUnloading = false;
293
- self.addEventListener('beforeunload', function () {
294
- isUnloading = true;
295
- });
296
-
297
- function reloadApp() {
298
- if (isUnloading || !hotReload) {
299
- return;
300
- }
301
-
302
- if (_hot) {
303
- log.info('[WDS] App hot update...'); // eslint-disable-next-line global-require
304
-
305
- var hotEmitter = require('webpack/hot/emitter');
306
-
307
- hotEmitter.emit('webpackHotUpdate', currentHash);
308
-
309
- if (typeof self !== 'undefined' && self.window) {
310
- // broadcast update to window
311
- self.postMessage("webpackHotUpdate".concat(currentHash), '*');
312
- }
313
- } // allow refreshing the page only if liveReload isn't disabled
314
- else if (_liveReload) {
315
- var rootWindow = self; // use parent window for reload (in case we're in an iframe with no valid src)
316
-
317
- var intervalId = self.setInterval(function () {
318
- if (rootWindow.location.protocol !== 'about:') {
319
- // reload immediately if protocol is valid
320
- applyReload(rootWindow, intervalId);
321
- } else {
322
- rootWindow = rootWindow.parent;
323
-
324
- if (rootWindow.parent === rootWindow) {
325
- // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
326
- applyReload(rootWindow, intervalId);
327
- }
328
- }
329
- });
330
- }
331
-
332
- function applyReload(rootWindow, intervalId) {
333
- clearInterval(intervalId);
334
- log.info('[WDS] App updated. Reloading...');
335
- rootWindow.location.reload();
336
- }
337
- }
176
+ socket(socketUrl, onSocketMessage);