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