webpack-dev-server 5.0.4 → 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/README.md CHANGED
@@ -190,7 +190,7 @@ CLI documentation: https://webpack.js.org/api/cli/.
190
190
  Made with ♥ by the webpack team.
191
191
  ```
192
192
 
193
- > **Note**
193
+ > [!NOTE]
194
194
  >
195
195
  > _Detailed documentation for above options is available on this [link](https://webpack.js.org/configuration/dev-server/)._
196
196
 
@@ -1,6 +1,6 @@
1
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
3
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
1
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
2
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
3
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
4
4
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5
5
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
6
6
  import SockJS from "../modules/sockjs-client/index.js";
@@ -1,6 +1,6 @@
1
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
3
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
1
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
2
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
3
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
4
4
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5
5
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
6
6
  import { log } from "../utils/log.js";
package/client/index.js CHANGED
@@ -1,19 +1,17 @@
1
1
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
2
2
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
3
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
3
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
4
4
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
5
5
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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";
14
+ import { isProgressSupported, defineProgressElement } from "./progress.js";
17
15
 
18
16
  /**
19
17
  * @typedef {Object} OverlayOptions
@@ -50,8 +48,7 @@ var decodeOverlayOptions = function decodeOverlayOptions(overlayOptions) {
50
48
  var overlayFilterFunctionString = decodeURIComponent(overlayOptions[property]);
51
49
 
52
50
  // eslint-disable-next-line no-new-func
53
- var overlayFilterFunction = new Function("message", "var callback = ".concat(overlayFilterFunctionString, "\n return callback(message)"));
54
- overlayOptions[property] = overlayFilterFunction;
51
+ overlayOptions[property] = new Function("message", "var callback = ".concat(overlayFilterFunctionString, "\n return callback(message)"));
55
52
  }
56
53
  });
57
54
  }
@@ -66,12 +63,62 @@ var status = {
66
63
  currentHash: __webpack_hash__
67
64
  };
68
65
 
69
- /** @type {Options} */
70
- var options = {
71
- hot: false,
72
- liveReload: false,
73
- progress: false,
74
- 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;
75
122
  };
76
123
  var parsedResourceQuery = parseURL(__resourceQuery);
77
124
  var enabledFeatures = {
@@ -80,6 +127,14 @@ var enabledFeatures = {
80
127
  Progress: false,
81
128
  Overlay: false
82
129
  };
130
+
131
+ /** @type {Options} */
132
+ var options = {
133
+ hot: false,
134
+ liveReload: false,
135
+ progress: false,
136
+ overlay: false
137
+ };
83
138
  if (parsedResourceQuery.hot === "true") {
84
139
  options.hot = true;
85
140
  enabledFeatures["Hot Module Replacement"] = true;
@@ -120,14 +175,30 @@ if (typeof parsedResourceQuery.reconnect !== "undefined") {
120
175
  /**
121
176
  * @param {string} level
122
177
  */
123
- function setAllLogLevel(level) {
178
+ var setAllLogLevel = function setAllLogLevel(level) {
124
179
  // This is needed because the HMR logger operate separately from dev server logger
125
180
  webpackHotLog.setLogLevel(level === "verbose" || level === "log" ? "info" : level);
126
181
  setLogLevel(level);
127
- }
182
+ };
128
183
  if (options.logging) {
129
184
  setAllLogLevel(options.logging);
130
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
+ };
131
202
  logEnabledFeatures(enabledFeatures);
132
203
  self.addEventListener("beforeunload", function () {
133
204
  status.isUnloading = true;
@@ -141,6 +212,80 @@ var overlay = typeof window !== "undefined" ? createOverlay(typeof options.overl
141
212
  }) : {
142
213
  send: function send() {}
143
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
+ };
144
289
  var onSocketMessage = {
145
290
  hot: function hot() {
146
291
  if (parsedResourceQuery.hot === "false") {
@@ -205,6 +350,18 @@ var onSocketMessage = {
205
350
  if (options.progress) {
206
351
  log.info("".concat(data.pluginName ? "[".concat(data.pluginName, "] ") : "").concat(data.percent, "% - ").concat(data.msg, "."));
207
352
  }
353
+ if (isProgressSupported()) {
354
+ if (typeof options.progress === "string") {
355
+ var progress = document.querySelector("wds-progress");
356
+ if (!progress) {
357
+ defineProgressElement();
358
+ progress = document.createElement("wds-progress");
359
+ document.body.appendChild(progress);
360
+ }
361
+ progress.setAttribute("progress", data.percent);
362
+ progress.setAttribute("type", options.progress);
363
+ }
364
+ }
208
365
  sendMessage("Progress", data);
209
366
  },
210
367
  "still-ok": function stillOk() {
@@ -307,5 +464,127 @@ var onSocketMessage = {
307
464
  sendMessage("Close");
308
465
  }
309
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
+ };
310
588
  var socketURL = createSocketURL(parsedResourceQuery);
311
- socket(socketURL, onSocketMessage, options.reconnect);
589
+ socket(socketURL, onSocketMessage, options.reconnect);
590
+ export { getCurrentScriptSource, parseURL, createSocketURL };