webpack-dev-server 4.7.3 → 4.8.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/socket.js CHANGED
@@ -9,8 +9,11 @@ typeof __webpack_dev_server_client__ !== "undefined" ? typeof __webpack_dev_serv
9
9
  /* eslint-enable camelcase */
10
10
 
11
11
  var retries = 0;
12
- var maxRetries = 10;
13
- var client = null;
12
+ var maxRetries = 10; // Initialized client is exported so external consumers can utilize the same instance
13
+ // It is mutable to enforce singleton
14
+ // eslint-disable-next-line import/no-mutable-exports
15
+
16
+ export var client = null;
14
17
  /**
15
18
  * @param {string} url
16
19
  * @param {{ [handler: string]: (data?: any, params?: any) => any }} handlers
@@ -5,7 +5,7 @@
5
5
  function format(objURL) {
6
6
  var protocol = objURL.protocol || "";
7
7
 
8
- if (protocol && protocol.substr(-1) !== ":") {
8
+ if (protocol && !protocol.endsWith(":")) {
9
9
  protocol += ":";
10
10
  }
11
11
 
@@ -9,7 +9,7 @@ function parseURL(resourceQuery) {
9
9
  var options = {};
10
10
 
11
11
  if (typeof resourceQuery === "string" && resourceQuery !== "") {
12
- var searchParams = resourceQuery.substr(1).split("&");
12
+ var searchParams = resourceQuery.slice(1).split("&");
13
13
 
14
14
  for (var i = 0; i < searchParams.length; i++) {
15
15
  var pair = searchParams[i].split("=");
@@ -0,0 +1,20 @@
1
+ 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");
2
+ /**
3
+ *
4
+ * Strip [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) from a string.
5
+ * Adapted from code originally released by Sindre Sorhus
6
+ * Licensed the MIT License
7
+ *
8
+ * @param {string} string
9
+ * @return {string}
10
+ */
11
+
12
+ function stripAnsi(string) {
13
+ if (typeof string !== "string") {
14
+ throw new TypeError("Expected a `string`, got `".concat(typeof string, "`"));
15
+ }
16
+
17
+ return string.replace(ansiRegex, "");
18
+ }
19
+
20
+ export default stripAnsi;
package/lib/Server.js CHANGED
@@ -28,8 +28,8 @@ const schema = require("./options.json");
28
28
  /** @typedef {import("chokidar").WatchOptions} WatchOptions */
29
29
  /** @typedef {import("chokidar").FSWatcher} FSWatcher */
30
30
  /** @typedef {import("connect-history-api-fallback").Options} ConnectHistoryApiFallbackOptions */
31
- /** @typedef {import("bonjour").Bonjour} Bonjour */
32
- /** @typedef {import("bonjour").BonjourOptions} BonjourOptions */
31
+ /** @typedef {import("bonjour-service").Bonjour} Bonjour */
32
+ /** @typedef {import("bonjour-service").Service} BonjourOptions */
33
33
  /** @typedef {import("http-proxy-middleware").RequestHandler} RequestHandler */
34
34
  /** @typedef {import("http-proxy-middleware").Options} HttpProxyMiddlewareOptions */
35
35
  /** @typedef {import("http-proxy-middleware").Filter} HttpProxyMiddlewareOptionsFilter */
@@ -64,7 +64,7 @@ const schema = require("./options.json");
64
64
  /**
65
65
  * @typedef {Object} WatchFiles
66
66
  * @property {string | string[]} paths
67
- * @property {WatchOptions & { aggregateTimeout?: number, ignored?: string | RegExp | string[], poll?: number | boolean }} [options]
67
+ * @property {WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} [options]
68
68
  */
69
69
 
70
70
  /**
@@ -73,7 +73,7 @@ const schema = require("./options.json");
73
73
  * @property {string | string[]} [publicPath]
74
74
  * @property {boolean | ServeIndexOptions} [serveIndex]
75
75
  * @property {ServeStaticOptions} [staticOptions]
76
- * @property {boolean | WatchOptions & { aggregateTimeout?: number, ignored?: string | RegExp | string[], poll?: number | boolean }} [watch]
76
+ * @property {boolean | WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} [watch]
77
77
  */
78
78
 
79
79
  /**
@@ -110,22 +110,22 @@ const schema = require("./options.json");
110
110
  */
111
111
 
112
112
  /**
113
- * @typedef {{ [url: string]: string | HttpProxyMiddlewareOptions }} ProxyConfigMap
113
+ * @callback ByPass
114
+ * @param {Request} req
115
+ * @param {Response} res
116
+ * @param {ProxyConfigArrayItem} proxyConfig
114
117
  */
115
118
 
116
119
  /**
117
- * @typedef {HttpProxyMiddlewareOptions[]} ProxyArray
120
+ * @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
118
121
  */
119
122
 
120
123
  /**
121
- * @callback ByPass
122
- * @param {Request} req
123
- * @param {Response} res
124
- * @param {ProxyConfigArray} proxyConfig
124
+ * @typedef {(ProxyConfigArrayItem | ((req?: Request | undefined, res?: Response | undefined, next?: NextFunction | undefined) => ProxyConfigArrayItem))[]} ProxyConfigArray
125
125
  */
126
126
 
127
127
  /**
128
- * @typedef {{ path?: string | string[] | undefined, context?: string | string[] | HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions & ByPass} ProxyConfigArray
128
+ * @typedef {{ [url: string]: string | ProxyConfigArrayItem }} ProxyConfigMap
129
129
  */
130
130
 
131
131
  /**
@@ -187,14 +187,14 @@ const schema = require("./options.json");
187
187
  * @property {"auto" | "all" | string | string[]} [allowedHosts]
188
188
  * @property {boolean | ConnectHistoryApiFallbackOptions} [historyApiFallback]
189
189
  * @property {boolean} [setupExitSignals]
190
- * @property {boolean | BonjourOptions} [bonjour]
190
+ * @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
191
191
  * @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
192
192
  * @property {boolean | string | Static | Array<string | Static>} [static]
193
193
  * @property {boolean | ServerOptions} [https]
194
194
  * @property {boolean} [http2]
195
195
  * @property {"http" | "https" | "spdy" | string | ServerConfiguration} [server]
196
196
  * @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
197
- * @property {ProxyConfigMap | ProxyConfigArray | ProxyArray} [proxy]
197
+ * @property {ProxyConfigMap | ProxyConfigArrayItem | ProxyConfigArray} [proxy]
198
198
  * @property {boolean | string | Open | Array<string | Open>} [open]
199
199
  * @property {boolean} [setupExitSignals]
200
200
  * @property {boolean | ClientConfiguration} [client]
@@ -754,7 +754,7 @@ class Server {
754
754
  // TODO remove `{}` after drop webpack v4 support
755
755
  const compilerWatchOptions = compilerOptions.watchOptions || {};
756
756
  /**
757
- * @param {WatchOptions & WebpackConfiguration["watchOptions"]} watchOptions
757
+ * @param {WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} watchOptions
758
758
  * @returns {WatchOptions}
759
759
  */
760
760
  const getWatchOptions = (watchOptions = {}) => {
@@ -1132,13 +1132,15 @@ class Server {
1132
1132
 
1133
1133
  // cert is more than 30 days old, kill it with fire
1134
1134
  if ((now - Number(certificateStat.ctime)) / certificateTtl > 30) {
1135
- const del = require("del");
1135
+ const { promisify } = require("util");
1136
+ const rimraf = require("rimraf");
1137
+ const del = promisify(rimraf);
1136
1138
 
1137
1139
  this.logger.info(
1138
1140
  "SSL certificate is more than 30 days old. Removing..."
1139
1141
  );
1140
1142
 
1141
- await del([certificatePath], { force: true });
1143
+ await del(certificatePath);
1142
1144
 
1143
1145
  certificateExists = false;
1144
1146
  }
@@ -1380,10 +1382,10 @@ class Server {
1380
1382
  Object.prototype.hasOwnProperty.call(options.proxy, "target") ||
1381
1383
  Object.prototype.hasOwnProperty.call(options.proxy, "router")
1382
1384
  ) {
1383
- /** @type {ProxyArray} */
1385
+ /** @type {ProxyConfigArray} */
1384
1386
  (options.proxy) = [/** @type {ProxyConfigMap} */ (options.proxy)];
1385
1387
  } else {
1386
- /** @type {ProxyArray} */
1388
+ /** @type {ProxyConfigArray} */
1387
1389
  (options.proxy) = Object.keys(options.proxy).map(
1388
1390
  /**
1389
1391
  * @param {string} context
@@ -1421,50 +1423,48 @@ class Server {
1421
1423
  }
1422
1424
  }
1423
1425
 
1424
- /** @type {ProxyArray} */
1426
+ /** @type {ProxyConfigArray} */
1425
1427
  (options.proxy) =
1426
- /** @type {ProxyArray} */
1427
- (options.proxy).map(
1428
+ /** @type {ProxyConfigArray} */
1429
+ (options.proxy).map((item) => {
1430
+ if (typeof item === "function") {
1431
+ return item;
1432
+ }
1433
+
1428
1434
  /**
1429
- * @param {HttpProxyMiddlewareOptions} item
1430
- * @returns {HttpProxyMiddlewareOptions}
1435
+ * @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
1436
+ * @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
1431
1437
  */
1432
- (item) => {
1433
- /**
1434
- * @param {"info" | "warn" | "error" | "debug" | "silent" | undefined | "none" | "log" | "verbose"} level
1435
- * @returns {"info" | "warn" | "error" | "debug" | "silent" | undefined}
1436
- */
1437
- const getLogLevelForProxy = (level) => {
1438
- if (level === "none") {
1439
- return "silent";
1440
- }
1441
-
1442
- if (level === "log") {
1443
- return "info";
1444
- }
1445
-
1446
- if (level === "verbose") {
1447
- return "debug";
1448
- }
1449
-
1450
- return level;
1451
- };
1438
+ const getLogLevelForProxy = (level) => {
1439
+ if (level === "none") {
1440
+ return "silent";
1441
+ }
1452
1442
 
1453
- if (typeof item.logLevel === "undefined") {
1454
- item.logLevel = getLogLevelForProxy(
1455
- compilerOptions.infrastructureLogging
1456
- ? compilerOptions.infrastructureLogging.level
1457
- : "info"
1458
- );
1443
+ if (level === "log") {
1444
+ return "info";
1459
1445
  }
1460
1446
 
1461
- if (typeof item.logProvider === "undefined") {
1462
- item.logProvider = () => this.logger;
1447
+ if (level === "verbose") {
1448
+ return "debug";
1463
1449
  }
1464
1450
 
1465
- return item;
1451
+ return level;
1452
+ };
1453
+
1454
+ if (typeof item.logLevel === "undefined") {
1455
+ item.logLevel = getLogLevelForProxy(
1456
+ compilerOptions.infrastructureLogging
1457
+ ? compilerOptions.infrastructureLogging.level
1458
+ : "info"
1459
+ );
1466
1460
  }
1467
- );
1461
+
1462
+ if (typeof item.logProvider === "undefined") {
1463
+ item.logProvider = () => this.logger;
1464
+ }
1465
+
1466
+ return item;
1467
+ });
1468
1468
  }
1469
1469
 
1470
1470
  if (typeof options.setupExitSignals === "undefined") {
@@ -2124,7 +2124,7 @@ class Server {
2124
2124
  const { createProxyMiddleware } = require("http-proxy-middleware");
2125
2125
 
2126
2126
  /**
2127
- * @param {ProxyConfigArray} proxyConfig
2127
+ * @param {ProxyConfigArrayItem} proxyConfig
2128
2128
  * @returns {RequestHandler | undefined}
2129
2129
  */
2130
2130
  const getProxyMiddleware = (proxyConfig) => {
@@ -2160,93 +2160,91 @@ class Server {
2160
2160
  * }
2161
2161
  * ]
2162
2162
  */
2163
- /** @type {ProxyArray} */
2164
- (this.options.proxy).forEach(
2163
+ /** @type {ProxyConfigArray} */
2164
+ (this.options.proxy).forEach((proxyConfigOrCallback) => {
2165
2165
  /**
2166
- * @param {any} proxyConfigOrCallback
2166
+ * @type {RequestHandler}
2167
2167
  */
2168
- (proxyConfigOrCallback) => {
2169
- /**
2170
- * @type {RequestHandler}
2171
- */
2172
- let proxyMiddleware;
2168
+ let proxyMiddleware;
2173
2169
 
2174
- let proxyConfig =
2175
- typeof proxyConfigOrCallback === "function"
2176
- ? proxyConfigOrCallback()
2177
- : proxyConfigOrCallback;
2170
+ let proxyConfig =
2171
+ typeof proxyConfigOrCallback === "function"
2172
+ ? proxyConfigOrCallback()
2173
+ : proxyConfigOrCallback;
2178
2174
 
2179
- proxyMiddleware =
2180
- /** @type {RequestHandler} */
2181
- (getProxyMiddleware(proxyConfig));
2175
+ proxyMiddleware =
2176
+ /** @type {RequestHandler} */
2177
+ (getProxyMiddleware(proxyConfig));
2182
2178
 
2183
- if (proxyConfig.ws) {
2184
- this.webSocketProxies.push(proxyMiddleware);
2185
- }
2179
+ if (proxyConfig.ws) {
2180
+ this.webSocketProxies.push(proxyMiddleware);
2181
+ }
2186
2182
 
2187
- /**
2188
- * @param {Request} req
2189
- * @param {Response} res
2190
- * @param {NextFunction} next
2191
- * @returns {Promise<void>}
2192
- */
2193
- const handler = async (req, res, next) => {
2194
- if (typeof proxyConfigOrCallback === "function") {
2195
- const newProxyConfig = proxyConfigOrCallback(req, res, next);
2196
-
2197
- if (newProxyConfig !== proxyConfig) {
2198
- proxyConfig = newProxyConfig;
2199
- proxyMiddleware =
2200
- /** @type {RequestHandler} */
2201
- (getProxyMiddleware(proxyConfig));
2202
- }
2183
+ /**
2184
+ * @param {Request} req
2185
+ * @param {Response} res
2186
+ * @param {NextFunction} next
2187
+ * @returns {Promise<void>}
2188
+ */
2189
+ const handler = async (req, res, next) => {
2190
+ if (typeof proxyConfigOrCallback === "function") {
2191
+ const newProxyConfig = proxyConfigOrCallback(req, res, next);
2192
+
2193
+ if (newProxyConfig !== proxyConfig) {
2194
+ proxyConfig = newProxyConfig;
2195
+ proxyMiddleware =
2196
+ /** @type {RequestHandler} */
2197
+ (getProxyMiddleware(proxyConfig));
2203
2198
  }
2199
+ }
2204
2200
 
2205
- // - Check if we have a bypass function defined
2206
- // - In case the bypass function is defined we'll retrieve the
2207
- // bypassUrl from it otherwise bypassUrl would be null
2208
- // TODO remove in the next major in favor `context` and `router` options
2209
- const isByPassFuncDefined =
2210
- typeof proxyConfig.bypass === "function";
2211
- const bypassUrl = isByPassFuncDefined
2212
- ? await proxyConfig.bypass(req, res, proxyConfig)
2213
- : null;
2214
-
2215
- if (typeof bypassUrl === "boolean") {
2216
- // skip the proxy
2217
- // @ts-ignore
2218
- req.url = null;
2219
- next();
2220
- } else if (typeof bypassUrl === "string") {
2221
- // byPass to that url
2222
- req.url = bypassUrl;
2223
- next();
2224
- } else if (proxyMiddleware) {
2225
- return proxyMiddleware(req, res, next);
2226
- } else {
2227
- next();
2228
- }
2229
- };
2201
+ // - Check if we have a bypass function defined
2202
+ // - In case the bypass function is defined we'll retrieve the
2203
+ // bypassUrl from it otherwise bypassUrl would be null
2204
+ // TODO remove in the next major in favor `context` and `router` options
2205
+ const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
2206
+ const bypassUrl = isByPassFuncDefined
2207
+ ? await /** @type {ByPass} */ (proxyConfig.bypass)(
2208
+ req,
2209
+ res,
2210
+ proxyConfig
2211
+ )
2212
+ : null;
2213
+
2214
+ if (typeof bypassUrl === "boolean") {
2215
+ // skip the proxy
2216
+ // @ts-ignore
2217
+ req.url = null;
2218
+ next();
2219
+ } else if (typeof bypassUrl === "string") {
2220
+ // byPass to that url
2221
+ req.url = bypassUrl;
2222
+ next();
2223
+ } else if (proxyMiddleware) {
2224
+ return proxyMiddleware(req, res, next);
2225
+ } else {
2226
+ next();
2227
+ }
2228
+ };
2230
2229
 
2231
- middlewares.push({
2232
- name: "http-proxy-middleware",
2233
- middleware: handler,
2234
- });
2235
- // Also forward error requests to the proxy so it can handle them.
2236
- middlewares.push({
2237
- name: "http-proxy-middleware-error-handler",
2238
- middleware:
2239
- /**
2240
- * @param {Error} error
2241
- * @param {Request} req
2242
- * @param {Response} res
2243
- * @param {NextFunction} next
2244
- * @returns {any}
2245
- */
2246
- (error, req, res, next) => handler(req, res, next),
2247
- });
2248
- }
2249
- );
2230
+ middlewares.push({
2231
+ name: "http-proxy-middleware",
2232
+ middleware: handler,
2233
+ });
2234
+ // Also forward error requests to the proxy so it can handle them.
2235
+ middlewares.push({
2236
+ name: "http-proxy-middleware-error-handler",
2237
+ middleware:
2238
+ /**
2239
+ * @param {Error} error
2240
+ * @param {Request} req
2241
+ * @param {Response} res
2242
+ * @param {NextFunction} next
2243
+ * @returns {any}
2244
+ */
2245
+ (error, req, res, next) => handler(req, res, next),
2246
+ });
2247
+ });
2250
2248
 
2251
2249
  middlewares.push({
2252
2250
  name: "webpack-dev-middleware",
@@ -2595,14 +2593,18 @@ class Server {
2595
2593
  * @returns {void}
2596
2594
  */
2597
2595
  runBonjour() {
2596
+ const { Bonjour } = require("bonjour-service");
2598
2597
  /**
2599
2598
  * @private
2600
- * @type {import("bonjour").Bonjour | undefined}
2599
+ * @type {Bonjour | undefined}
2601
2600
  */
2602
- this.bonjour = require("bonjour")();
2601
+ this.bonjour = new Bonjour();
2603
2602
  this.bonjour.publish({
2603
+ // @ts-expect-error
2604
2604
  name: `Webpack Dev Server ${os.hostname()}:${this.options.port}`,
2605
+ // @ts-expect-error
2605
2606
  port: /** @type {number} */ (this.options.port),
2607
+ // @ts-expect-error
2606
2608
  type:
2607
2609
  /** @type {ServerConfiguration} */
2608
2610
  (this.options.server).type === "http" ? "http" : "https",