webpack-dev-server 5.1.0 → 5.2.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
@@ -6,14 +6,11 @@ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e =
6
6
  /* global __resourceQuery, __webpack_hash__ */
7
7
  /// <reference types="webpack/module" />
8
8
  import webpackHotLog from "webpack/hot/log.js";
9
- import stripAnsi from "./utils/stripAnsi.js";
10
- import parseURL from "./utils/parseURL.js";
9
+ import hotEmitter from "webpack/hot/emitter.js";
11
10
  import socket from "./socket.js";
12
11
  import { formatProblem, createOverlay } from "./overlay.js";
13
- import { log, logEnabledFeatures, setLogLevel } from "./utils/log.js";
12
+ import { log, setLogLevel } from "./utils/log.js";
14
13
  import sendMessage from "./utils/sendMessage.js";
15
- import reloadApp from "./utils/reloadApp.js";
16
- import createSocketURL from "./utils/createSocketURL.js";
17
14
  import { isProgressSupported, defineProgressElement } from "./progress.js";
18
15
 
19
16
  /**
@@ -51,8 +48,7 @@ var decodeOverlayOptions = function decodeOverlayOptions(overlayOptions) {
51
48
  var overlayFilterFunctionString = decodeURIComponent(overlayOptions[property]);
52
49
 
53
50
  // eslint-disable-next-line no-new-func
54
- var overlayFilterFunction = new Function("message", "var callback = ".concat(overlayFilterFunctionString, "\n return callback(message)"));
55
- overlayOptions[property] = overlayFilterFunction;
51
+ overlayOptions[property] = new Function("message", "var callback = ".concat(overlayFilterFunctionString, "\n return callback(message)"));
56
52
  }
57
53
  });
58
54
  }
@@ -67,12 +63,62 @@ var status = {
67
63
  currentHash: __webpack_hash__
68
64
  };
69
65
 
70
- /** @type {Options} */
71
- var options = {
72
- hot: false,
73
- liveReload: false,
74
- progress: false,
75
- overlay: false
66
+ /**
67
+ * @returns {string}
68
+ */
69
+ var getCurrentScriptSource = function getCurrentScriptSource() {
70
+ // `document.currentScript` is the most accurate way to find the current script,
71
+ // but is not supported in all browsers.
72
+ if (document.currentScript) {
73
+ return document.currentScript.getAttribute("src");
74
+ }
75
+
76
+ // Fallback to getting all scripts running in the document.
77
+ var scriptElements = document.scripts || [];
78
+ var scriptElementsWithSrc = Array.prototype.filter.call(scriptElements, function (element) {
79
+ return element.getAttribute("src");
80
+ });
81
+ if (scriptElementsWithSrc.length > 0) {
82
+ var currentScript = scriptElementsWithSrc[scriptElementsWithSrc.length - 1];
83
+ return currentScript.getAttribute("src");
84
+ }
85
+
86
+ // Fail as there was no script to use.
87
+ throw new Error("[webpack-dev-server] Failed to get current script source.");
88
+ };
89
+
90
+ /**
91
+ * @param {string} resourceQuery
92
+ * @returns {{ [key: string]: string | boolean }}
93
+ */
94
+ var parseURL = function parseURL(resourceQuery) {
95
+ /** @type {{ [key: string]: string }} */
96
+ var result = {};
97
+ if (typeof resourceQuery === "string" && resourceQuery !== "") {
98
+ var searchParams = resourceQuery.slice(1).split("&");
99
+ for (var i = 0; i < searchParams.length; i++) {
100
+ var pair = searchParams[i].split("=");
101
+ result[pair[0]] = decodeURIComponent(pair[1]);
102
+ }
103
+ } else {
104
+ // Else, get the url from the <script> this file was called with.
105
+ var scriptSource = getCurrentScriptSource();
106
+ var scriptSourceURL;
107
+ try {
108
+ // The placeholder `baseURL` with `window.location.href`,
109
+ // is to allow parsing of path-relative or protocol-relative URLs,
110
+ // and will have no effect if `scriptSource` is a fully valid URL.
111
+ scriptSourceURL = new URL(scriptSource, self.location.href);
112
+ } catch (error) {
113
+ // URL parsing failed, do nothing.
114
+ // We will still proceed to see if we can recover using `resourceQuery`
115
+ }
116
+ if (scriptSourceURL) {
117
+ result = scriptSourceURL;
118
+ result.fromCurrentScript = true;
119
+ }
120
+ }
121
+ return result;
76
122
  };
77
123
  var parsedResourceQuery = parseURL(__resourceQuery);
78
124
  var enabledFeatures = {
@@ -81,6 +127,14 @@ var enabledFeatures = {
81
127
  Progress: false,
82
128
  Overlay: false
83
129
  };
130
+
131
+ /** @type {Options} */
132
+ var options = {
133
+ hot: false,
134
+ liveReload: false,
135
+ progress: false,
136
+ overlay: false
137
+ };
84
138
  if (parsedResourceQuery.hot === "true") {
85
139
  options.hot = true;
86
140
  enabledFeatures["Hot Module Replacement"] = true;
@@ -121,14 +175,30 @@ if (typeof parsedResourceQuery.reconnect !== "undefined") {
121
175
  /**
122
176
  * @param {string} level
123
177
  */
124
- function setAllLogLevel(level) {
178
+ var setAllLogLevel = function setAllLogLevel(level) {
125
179
  // This is needed because the HMR logger operate separately from dev server logger
126
180
  webpackHotLog.setLogLevel(level === "verbose" || level === "log" ? "info" : level);
127
181
  setLogLevel(level);
128
- }
182
+ };
129
183
  if (options.logging) {
130
184
  setAllLogLevel(options.logging);
131
185
  }
186
+ var logEnabledFeatures = function logEnabledFeatures(features) {
187
+ var listEnabledFeatures = Object.keys(features);
188
+ if (!features || listEnabledFeatures.length === 0) {
189
+ return;
190
+ }
191
+ var logString = "Server started:";
192
+
193
+ // Server started: Hot Module Replacement enabled, Live Reloading enabled, Overlay disabled.
194
+ for (var i = 0; i < listEnabledFeatures.length; i++) {
195
+ var key = listEnabledFeatures[i];
196
+ logString += " ".concat(key, " ").concat(features[key] ? "enabled" : "disabled", ",");
197
+ }
198
+ // replace last comma with a period
199
+ logString = logString.slice(0, -1).concat(".");
200
+ log.info(logString);
201
+ };
132
202
  logEnabledFeatures(enabledFeatures);
133
203
  self.addEventListener("beforeunload", function () {
134
204
  status.isUnloading = true;
@@ -142,6 +212,80 @@ var overlay = typeof window !== "undefined" ? createOverlay(typeof options.overl
142
212
  }) : {
143
213
  send: function send() {}
144
214
  };
215
+
216
+ /**
217
+ * @param {Options} options
218
+ * @param {Status} currentStatus
219
+ */
220
+ var reloadApp = function reloadApp(_ref, currentStatus) {
221
+ var hot = _ref.hot,
222
+ liveReload = _ref.liveReload;
223
+ if (currentStatus.isUnloading) {
224
+ return;
225
+ }
226
+ var currentHash = currentStatus.currentHash,
227
+ previousHash = currentStatus.previousHash;
228
+ var isInitial = currentHash.indexOf(/** @type {string} */previousHash) >= 0;
229
+ if (isInitial) {
230
+ return;
231
+ }
232
+
233
+ /**
234
+ * @param {Window} rootWindow
235
+ * @param {number} intervalId
236
+ */
237
+ function applyReload(rootWindow, intervalId) {
238
+ clearInterval(intervalId);
239
+ log.info("App updated. Reloading...");
240
+ rootWindow.location.reload();
241
+ }
242
+ var search = self.location.search.toLowerCase();
243
+ var allowToHot = search.indexOf("webpack-dev-server-hot=false") === -1;
244
+ var allowToLiveReload = search.indexOf("webpack-dev-server-live-reload=false") === -1;
245
+ if (hot && allowToHot) {
246
+ log.info("App hot update...");
247
+ hotEmitter.emit("webpackHotUpdate", currentStatus.currentHash);
248
+ if (typeof self !== "undefined" && self.window) {
249
+ // broadcast update to window
250
+ self.postMessage("webpackHotUpdate".concat(currentStatus.currentHash), "*");
251
+ }
252
+ }
253
+ // allow refreshing the page only if liveReload isn't disabled
254
+ else if (liveReload && allowToLiveReload) {
255
+ var rootWindow = self;
256
+
257
+ // use parent window for reload (in case we're in an iframe with no valid src)
258
+ var intervalId = self.setInterval(function () {
259
+ if (rootWindow.location.protocol !== "about:") {
260
+ // reload immediately if protocol is valid
261
+ applyReload(rootWindow, intervalId);
262
+ } else {
263
+ rootWindow = rootWindow.parent;
264
+ if (rootWindow.parent === rootWindow) {
265
+ // if parent equals current window we've reached the root which would continue forever, so trigger a reload anyways
266
+ applyReload(rootWindow, intervalId);
267
+ }
268
+ }
269
+ });
270
+ }
271
+ };
272
+ var ansiRegex = new RegExp(["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)", "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))"].join("|"), "g");
273
+
274
+ /**
275
+ *
276
+ * Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) from a string.
277
+ * Adapted from code originally released by Sindre Sorhus
278
+ * Licensed the MIT License
279
+ *
280
+ * @param {string} string
281
+ * @return {string}
282
+ */
283
+ var stripAnsi = function stripAnsi(string) {
284
+ if (typeof string !== "string") {
285
+ throw new TypeError("Expected a `string`, got `".concat(typeof string, "`"));
286
+ }
287
+ return string.replace(ansiRegex, "");
288
+ };
145
289
  var onSocketMessage = {
146
290
  hot: function hot() {
147
291
  if (parsedResourceQuery.hot === "false") {
@@ -320,5 +464,127 @@ var onSocketMessage = {
320
464
  sendMessage("Close");
321
465
  }
322
466
  };
467
+
468
+ /**
469
+ * @param {{ protocol?: string, auth?: string, hostname?: string, port?: string, pathname?: string, search?: string, hash?: string, slashes?: boolean }} objURL
470
+ * @returns {string}
471
+ */
472
+ var formatURL = function formatURL(objURL) {
473
+ var protocol = objURL.protocol || "";
474
+ if (protocol && protocol.substr(-1) !== ":") {
475
+ protocol += ":";
476
+ }
477
+ var auth = objURL.auth || "";
478
+ if (auth) {
479
+ auth = encodeURIComponent(auth);
480
+ auth = auth.replace(/%3A/i, ":");
481
+ auth += "@";
482
+ }
483
+ var host = "";
484
+ if (objURL.hostname) {
485
+ host = auth + (objURL.hostname.indexOf(":") === -1 ? objURL.hostname : "[".concat(objURL.hostname, "]"));
486
+ if (objURL.port) {
487
+ host += ":".concat(objURL.port);
488
+ }
489
+ }
490
+ var pathname = objURL.pathname || "";
491
+ if (objURL.slashes) {
492
+ host = "//".concat(host || "");
493
+ if (pathname && pathname.charAt(0) !== "/") {
494
+ pathname = "/".concat(pathname);
495
+ }
496
+ } else if (!host) {
497
+ host = "";
498
+ }
499
+ var search = objURL.search || "";
500
+ if (search && search.charAt(0) !== "?") {
501
+ search = "?".concat(search);
502
+ }
503
+ var hash = objURL.hash || "";
504
+ if (hash && hash.charAt(0) !== "#") {
505
+ hash = "#".concat(hash);
506
+ }
507
+ pathname = pathname.replace(/[?#]/g,
508
+ /**
509
+ * @param {string} match
510
+ * @returns {string}
511
+ */
512
+ function (match) {
513
+ return encodeURIComponent(match);
514
+ });
515
+ search = search.replace("#", "%23");
516
+ return "".concat(protocol).concat(host).concat(pathname).concat(search).concat(hash);
517
+ };
518
+
519
+ /**
520
+ * @param {URL & { fromCurrentScript?: boolean }} parsedURL
521
+ * @returns {string}
522
+ */
523
+ var createSocketURL = function createSocketURL(parsedURL) {
524
+ var hostname = parsedURL.hostname;
525
+
526
+ // Node.js module parses it as `::`
527
+ // `new URL(urlString, [baseURLString])` parses it as '[::]'
528
+ var isInAddrAny = hostname === "0.0.0.0" || hostname === "::" || hostname === "[::]";
529
+
530
+ // why do we need this check?
531
+ // hostname n/a for file protocol (example, when using electron, ionic)
532
+ // see: https://github.com/webpack/webpack-dev-server/pull/384
533
+ if (isInAddrAny && self.location.hostname && self.location.protocol.indexOf("http") === 0) {
534
+ hostname = self.location.hostname;
535
+ }
536
+ var socketURLProtocol = parsedURL.protocol || self.location.protocol;
537
+
538
+ // When https is used in the app, secure web sockets are always necessary because the browser doesn't accept non-secure web sockets.
539
+ if (socketURLProtocol === "auto:" || hostname && isInAddrAny && self.location.protocol === "https:") {
540
+ socketURLProtocol = self.location.protocol;
541
+ }
542
+ socketURLProtocol = socketURLProtocol.replace(/^(?:http|.+-extension|file)/i, "ws");
543
+ var socketURLAuth = "";
544
+
545
+ // `new URL(urlString, [baseURLstring])` doesn't have `auth` property
546
+ // Parse authentication credentials in case we need them
547
+ if (parsedURL.username) {
548
+ socketURLAuth = parsedURL.username;
549
+
550
+ // Since HTTP basic authentication does not allow empty username,
551
+ // we only include password if the username is not empty.
552
+ if (parsedURL.password) {
553
+ // Result: <username>:<password>
554
+ socketURLAuth = socketURLAuth.concat(":", parsedURL.password);
555
+ }
556
+ }
557
+
558
+ // In case the host is a raw IPv6 address, it can be enclosed in
559
+ // the brackets as the brackets are needed in the final URL string.
560
+ // Need to remove those as url.format blindly adds its own set of brackets
561
+ // if the host string contains colons. That would lead to non-working
562
+ // double brackets (e.g. [[::]]) host
563
+ //
564
+ // All of these web socket url params are optionally passed in through resourceQuery,
565
+ // so we need to fall back to the default if they are not provided
566
+ var socketURLHostname = (hostname || self.location.hostname || "localhost").replace(/^\[(.*)\]$/, "$1");
567
+ var socketURLPort = parsedURL.port;
568
+ if (!socketURLPort || socketURLPort === "0") {
569
+ socketURLPort = self.location.port;
570
+ }
571
+
572
+ // If path is provided it'll be passed in via the resourceQuery as a
573
+ // query param so it has to be parsed out of the querystring in order for the
574
+ // client to open the socket to the correct location.
575
+ var socketURLPathname = "/ws";
576
+ if (parsedURL.pathname && !parsedURL.fromCurrentScript) {
577
+ socketURLPathname = parsedURL.pathname;
578
+ }
579
+ return formatURL({
580
+ protocol: socketURLProtocol,
581
+ auth: socketURLAuth,
582
+ hostname: socketURLHostname,
583
+ port: socketURLPort,
584
+ pathname: socketURLPathname,
585
+ slashes: true
586
+ });
587
+ };
323
588
  var socketURL = createSocketURL(parsedResourceQuery);
324
- socket(socketURL, onSocketMessage, options.reconnect);
589
+ socket(socketURL, onSocketMessage, options.reconnect);
590
+ export { getCurrentScriptSource, parseURL, createSocketURL };
@@ -134,7 +134,7 @@ var TIMERS_SYMBOL = (typeof Symbol !== "undefined" ? Symbol : function (i) { ret
134
134
  var TIMERS_AGGREGATES_SYMBOL = (typeof Symbol !== "undefined" ? Symbol : function (i) { return i; })("webpack logger aggregated times");
135
135
  var WebpackLogger = /*#__PURE__*/function () {
136
136
  /**
137
- * @param {function(LogTypeEnum, any[]=): void} log log function
137
+ * @param {function(LogTypeEnum, EXPECTED_ANY[]=): void} log log function
138
138
  * @param {function(string | function(): string): WebpackLogger} getChildLogger function to create child logger
139
139
  */
140
140
  function WebpackLogger(log, getChildLogger) {
@@ -144,7 +144,7 @@ var WebpackLogger = /*#__PURE__*/function () {
144
144
  }
145
145
 
146
146
  /**
147
- * @param {...any} args args
147
+ * @param {...EXPECTED_ANY} args args
148
148
  */
149
149
  return _createClass(WebpackLogger, [{
150
150
  key: "error",
@@ -156,7 +156,7 @@ var WebpackLogger = /*#__PURE__*/function () {
156
156
  }
157
157
 
158
158
  /**
159
- * @param {...any} args args
159
+ * @param {...EXPECTED_ANY} args args
160
160
  */
161
161
  }, {
162
162
  key: "warn",
@@ -168,7 +168,7 @@ var WebpackLogger = /*#__PURE__*/function () {
168
168
  }
169
169
 
170
170
  /**
171
- * @param {...any} args args
171
+ * @param {...EXPECTED_ANY} args args
172
172
  */
173
173
  }, {
174
174
  key: "info",
@@ -180,7 +180,7 @@ var WebpackLogger = /*#__PURE__*/function () {
180
180
  }
181
181
 
182
182
  /**
183
- * @param {...any} args args
183
+ * @param {...EXPECTED_ANY} args args
184
184
  */
185
185
  }, {
186
186
  key: "log",
@@ -192,7 +192,7 @@ var WebpackLogger = /*#__PURE__*/function () {
192
192
  }
193
193
 
194
194
  /**
195
- * @param {...any} args args
195
+ * @param {...EXPECTED_ANY} args args
196
196
  */
197
197
  }, {
198
198
  key: "debug",
@@ -204,8 +204,8 @@ var WebpackLogger = /*#__PURE__*/function () {
204
204
  }
205
205
 
206
206
  /**
207
- * @param {any} assertion assertion
208
- * @param {...any} args args
207
+ * @param {EXPECTED_ANY} assertion assertion
208
+ * @param {...EXPECTED_ANY} args args
209
209
  */
210
210
  }, {
211
211
  key: "assert",
@@ -229,7 +229,7 @@ var WebpackLogger = /*#__PURE__*/function () {
229
229
  }
230
230
 
231
231
  /**
232
- * @param {...any} args args
232
+ * @param {...EXPECTED_ANY} args args
233
233
  */
234
234
  }, {
235
235
  key: "status",
@@ -241,7 +241,7 @@ var WebpackLogger = /*#__PURE__*/function () {
241
241
  }
242
242
 
243
243
  /**
244
- * @param {...any} args args
244
+ * @param {...EXPECTED_ANY} args args
245
245
  */
246
246
  }, {
247
247
  key: "group",
@@ -253,7 +253,7 @@ var WebpackLogger = /*#__PURE__*/function () {
253
253
  }
254
254
 
255
255
  /**
256
- * @param {...any} args args
256
+ * @param {...EXPECTED_ANY} args args
257
257
  */
258
258
  }, {
259
259
  key: "groupCollapsed",
@@ -455,24 +455,24 @@ var _require = __webpack_require__(/*! ./Logger */ "./node_modules/webpack/lib/l
455
455
  /** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
456
456
 
457
457
  /** @typedef {function(string): boolean} FilterFunction */
458
- /** @typedef {function(string, LogTypeEnum, any[]=): void} LoggingFunction */
458
+ /** @typedef {function(string, LogTypeEnum, EXPECTED_ANY[]=): void} LoggingFunction */
459
459
 
460
460
  /**
461
461
  * @typedef {object} LoggerConsole
462
462
  * @property {function(): void} clear
463
463
  * @property {function(): void} trace
464
- * @property {(...args: any[]) => void} info
465
- * @property {(...args: any[]) => void} log
466
- * @property {(...args: any[]) => void} warn
467
- * @property {(...args: any[]) => void} error
468
- * @property {(...args: any[]) => void=} debug
469
- * @property {(...args: any[]) => void=} group
470
- * @property {(...args: any[]) => void=} groupCollapsed
471
- * @property {(...args: any[]) => void=} groupEnd
472
- * @property {(...args: any[]) => void=} status
473
- * @property {(...args: any[]) => void=} profile
474
- * @property {(...args: any[]) => void=} profileEnd
475
- * @property {(...args: any[]) => void=} logTime
464
+ * @property {(...args: EXPECTED_ANY[]) => void} info
465
+ * @property {(...args: EXPECTED_ANY[]) => void} log
466
+ * @property {(...args: EXPECTED_ANY[]) => void} warn
467
+ * @property {(...args: EXPECTED_ANY[]) => void} error
468
+ * @property {(...args: EXPECTED_ANY[]) => void=} debug
469
+ * @property {(...args: EXPECTED_ANY[]) => void=} group
470
+ * @property {(...args: EXPECTED_ANY[]) => void=} groupCollapsed
471
+ * @property {(...args: EXPECTED_ANY[]) => void=} groupEnd
472
+ * @property {(...args: EXPECTED_ANY[]) => void=} status
473
+ * @property {(...args: EXPECTED_ANY[]) => void=} profile
474
+ * @property {(...args: EXPECTED_ANY[]) => void=} profileEnd
475
+ * @property {(...args: EXPECTED_ANY[]) => void=} logTime
476
476
  */
477
477
 
478
478
  /**
@@ -543,7 +543,7 @@ module.exports = function (_ref) {
543
543
  /**
544
544
  * @param {string} name name of the logger
545
545
  * @param {LogTypeEnum} type type of the log entry
546
- * @param {any[]=} args arguments of the log entry
546
+ * @param {EXPECTED_ANY[]=} args arguments of the log entry
547
547
  * @returns {void}
548
548
  */
549
549
  var logger = function logger(name, type, args) {
@@ -788,6 +788,8 @@ module.exports.hooks = {
788
788
  /******/
789
789
  /************************************************************************/
790
790
  var __webpack_exports__ = {};
791
+ // This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
792
+ !function() {
791
793
  /*!********************************************!*\
792
794
  !*** ./client-src/modules/logger/index.js ***!
793
795
  \********************************************/
@@ -797,8 +799,9 @@ __webpack_require__.r(__webpack_exports__);
797
799
  /* harmony export */ });
798
800
  /* harmony import */ var webpack_lib_logging_runtime_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! webpack/lib/logging/runtime.js */ "./node_modules/webpack/lib/logging/runtime.js");
799
801
 
802
+ }();
800
803
  var __webpack_export_target__ = exports;
801
- for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i];
804
+ for(var __webpack_i__ in __webpack_exports__) __webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
802
805
  if(__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, "__esModule", { value: true });
803
806
  /******/ })()
804
807
  ;
@@ -5013,7 +5013,7 @@ module.exports = Url;
5013
5013
  /******/
5014
5014
  /************************************************************************/
5015
5015
  var __webpack_exports__ = {};
5016
- // This entry need to be wrapped in an IIFE because it need to be in strict mode.
5016
+ // This entry needs to be wrapped in an IIFE because it needs to be in strict mode.
5017
5017
  !function() {
5018
5018
  "use strict";
5019
5019
  /*!***************************************************!*\