Skip to content

Commit fc4d97f

Browse files
committed
Merge branch 'next' of github.com:webpack/webpack-dev-server into remove-cli-flags
2 parents 230e15a + a4012b6 commit fc4d97f

8 files changed

Lines changed: 184 additions & 243 deletions

File tree

examples/general/proxy-advanced/webpack.config.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@ module.exports = setup({
88
context: __dirname,
99
entry: "./app.js",
1010
devServer: {
11-
proxy: {
12-
"/api": {
11+
port: 8080,
12+
static: {
13+
directory: ".",
14+
},
15+
proxy: [
16+
{
17+
context: "/api/nope",
18+
router: () => "http://localhost:8080",
19+
pathRewrite: () => "/bypass.html",
20+
},
21+
{
22+
context: "/api",
1323
target: "http://jsonplaceholder.typicode.com/",
1424
changeOrigin: true,
1525
pathRewrite: {
1626
"^/api": "",
1727
},
18-
bypass(req) {
19-
if (req.url === "/api/nope") {
20-
return "/bypass.html";
21-
}
22-
},
2328
},
24-
},
29+
],
2530
},
2631
});

examples/proxy/webpack.config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ module.exports = setup({
2929
onBeforeSetupMiddleware: async () => {
3030
await listenProxyServer();
3131
},
32-
proxy: {
33-
"/proxy": {
32+
proxy: [
33+
{
34+
context: "/proxy",
3435
target: "http://localhost:5000",
3536
},
36-
},
37+
],
3738
},
3839
});

lib/Server.js

Lines changed: 30 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const os = require("node:os");
44
const path = require("node:path");
55
const url = require("node:url");
6-
const util = require("node:util");
76
const fs = require("graceful-fs");
87
const ipaddr = require("ipaddr.js");
98
const { validate } = require("schema-utils");
@@ -138,14 +137,7 @@ const schema = require("./options.json");
138137
*/
139138

140139
/**
141-
* @callback ByPass
142-
* @param {Request} req
143-
* @param {Response} res
144-
* @param {ProxyConfigArrayItem} proxyConfig
145-
*/
146-
147-
/**
148-
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & { bypass?: ByPass } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
140+
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions } ProxyConfigArrayItem
149141
*/
150142

151143
/**
@@ -1477,6 +1469,23 @@ class Server {
14771469
}
14781470
}
14791471

1472+
/**
1473+
* @returns { { isColorSupported: () => boolean, colors: import("webpack").Colors } } colors support
1474+
*/
1475+
#getColors() {
1476+
const compilerOptions =
1477+
/** @type {MultiCompiler} */
1478+
(this.compiler).compilers
1479+
? /** @type {MultiCompiler} */ (this.compiler).compilers[0].webpack
1480+
: /** @type {Compiler} */ (this.compiler).webpack;
1481+
1482+
const colors = compilerOptions.cli.createColors({
1483+
useColor: compilerOptions.cli.isColorSupported(),
1484+
});
1485+
1486+
return { isColorSupported: compilerOptions.cli.isColorSupported, colors };
1487+
}
1488+
14801489
/**
14811490
* @private
14821491
* @returns {string} client transport
@@ -2131,29 +2140,13 @@ class Server {
21312140
* @returns {RequestHandler | undefined} request handler
21322141
*/
21332142
const getProxyMiddleware = (proxyConfig) => {
2134-
// It is possible to use the `bypass` method without a `target` or `router`.
2135-
// However, the proxy middleware has no use in this case, and will fail to instantiate.
2136-
if (proxyConfig.target) {
2137-
const context = proxyConfig.context || proxyConfig.path;
2138-
2139-
return createProxyMiddleware({
2140-
...proxyConfig,
2141-
pathFilter: /** @type {string} */ (context),
2142-
});
2143-
}
2144-
2145-
if (proxyConfig.router) {
2146-
return createProxyMiddleware(proxyConfig);
2147-
}
2143+
const context =
2144+
proxyConfig.context || proxyConfig.path || proxyConfig.pathFilter;
21482145

2149-
// TODO improve me after drop `bypass` to always generate error when configuration is bad
2150-
if (!proxyConfig.bypass) {
2151-
util.deprecate(
2152-
() => {},
2153-
`Invalid proxy configuration:\n\n${JSON.stringify(proxyConfig, null, 2)}\n\nThe use of proxy object notation as proxy routes has been removed.\nPlease use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options`,
2154-
"DEP_WEBPACK_DEV_SERVER_PROXY_ROUTES_ARGUMENT",
2155-
)();
2156-
}
2146+
return createProxyMiddleware({
2147+
...proxyConfig,
2148+
pathFilter: /** @type {string} */ (context),
2149+
});
21572150
};
21582151

21592152
/**
@@ -2219,40 +2212,7 @@ class Server {
22192212
}
22202213
}
22212214

2222-
// - Check if we have a bypass function defined
2223-
// - In case the bypass function is defined we'll retrieve the
2224-
// bypassUrl from it otherwise bypassUrl would be null
2225-
// TODO remove in the next major in favor `context` and `router` options
2226-
const isByPassFuncDefined = typeof proxyConfig.bypass === "function";
2227-
if (isByPassFuncDefined) {
2228-
util.deprecate(
2229-
() => {},
2230-
"Using the 'bypass' option is deprecated. Please use the 'router' or 'context' options. Read more at https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options",
2231-
"DEP_WEBPACK_DEV_SERVER_PROXY_BYPASS_ARGUMENT",
2232-
)();
2233-
}
2234-
const bypassUrl = isByPassFuncDefined
2235-
? await /** @type {ByPass} */ (proxyConfig.bypass)(
2236-
req,
2237-
res,
2238-
proxyConfig,
2239-
)
2240-
: null;
2241-
2242-
if (typeof bypassUrl === "boolean") {
2243-
// skip the proxy
2244-
res.statusCode = 404;
2245-
req.url = "";
2246-
next();
2247-
} else if (typeof bypassUrl === "string") {
2248-
// byPass to that url
2249-
req.url = bypassUrl;
2250-
next();
2251-
} else if (proxyMiddleware) {
2252-
return proxyMiddleware(req, res, next);
2253-
} else {
2254-
next();
2255-
}
2215+
return proxyMiddleware(req, res, next);
22562216
};
22572217

22582218
middlewares.push({
@@ -2735,7 +2695,10 @@ class Server {
27352695
* @returns {Promise<void>}
27362696
*/
27372697
async logStatus() {
2738-
const { cyan, isColorSupported, red } = require("colorette");
2698+
const {
2699+
isColorSupported,
2700+
colors: { cyan, red },
2701+
} = this.#getColors();
27392702

27402703
/**
27412704
* @param {Compiler["options"]} compilerOptions compiler options
@@ -2756,7 +2719,7 @@ class Server {
27562719
/** @type {boolean} */
27572720
(/** @type {StatsOptions} */ (compilerOptions.stats).colors);
27582721
} else {
2759-
colorsEnabled = isColorSupported;
2722+
colorsEnabled = isColorSupported();
27602723
}
27612724

27622725
return colorsEnabled;

migration-v6.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This document serves as a migration guide for `webpack-dev-server@6.0.0`.
55
## ⚠ Breaking Changes
66

77
- Minimum supported `Node.js` version is `20.9.0`.
8+
- Minimum supported `webpack` version is `5.101.0`.
89
- Support for **SockJS** in the WebSocket transport has been removed. Now, only **native WebSocket** is supported, or **custom** client and server implementations can be used.
910
- The options for passing to the `proxy` have changed. Please refer to the [http-proxy-middleware migration guide](https://github.com/chimurai/http-proxy-middleware/blob/master/MIGRATION.md) for details.
1011
- Remove support for the spdy server type. Use the http2 server type instead; however, since Express does not work correctly with it, a custom server (e.g., Connect or Hono) should be used.
@@ -72,3 +73,46 @@ This document serves as a migration guide for `webpack-dev-server@6.0.0`.
7273
- server-options-cacert -> server.options.ca
7374
- server-options-cacert-reset -> server.options.ca
7475
- web-socket-server -> web-socket-server-type
76+
77+
- The bypass function in the proxy configuration was removed. Use the `pathFilter` and `router` for similar functionality. See the example below.
78+
79+
v4:
80+
81+
```js
82+
module.exports = {
83+
// ...
84+
devServer: {
85+
proxy: [
86+
{
87+
context: "/api",
88+
bypass(req, res, proxyOptions) {
89+
if (req.url.startsWith("/api/special")) {
90+
return "/special.html";
91+
}
92+
},
93+
},
94+
],
95+
},
96+
};
97+
```
98+
99+
v5:
100+
101+
```js
102+
module.exports = {
103+
// ...
104+
devServer: {
105+
proxy: [
106+
{
107+
pathFilter: "/api/special",
108+
router: () => "http://localhost:3000", // Original Server
109+
pathRewrite: () => "/special.html",
110+
},
111+
],
112+
},
113+
};
114+
```
115+
116+
When `bypass` was used and that function returned a boolean, it would automatically result in a `404` request. This can’t be achieved in a similar way now, or, if it returned a string, you can do what was done in the example above.
117+
118+
`bypass` also allowed sending data; this can no longer be done. If you really need to do it, you’d have to create a new route in the proxy that sends the same data, or alternatively create a new route on the main server and, following the example above, send the data you wanted.

0 commit comments

Comments
 (0)