|
| 1 | +# Migration guide |
| 2 | + |
| 3 | +This document serves as a migration guide for `webpack-dev-server@5.0.0`. |
| 4 | + |
| 5 | +## ⚠ BREAKING CHANGES |
| 6 | + |
| 7 | +- Minimum supported `Node.js` version is `16.10.0`. |
| 8 | +- Minimum supported `webpack` version is `5.0.0`. |
| 9 | +- Minimum compatible `webpack-cli` version is `4.7.0` but we recommend using the latest version. |
| 10 | +- The `http2` and `https` options were removed in favor of [the `server` option](https://webpack.js.org/configuration/dev-server/#devserverserver). |
| 11 | + |
| 12 | + v4: |
| 13 | + |
| 14 | + ```js |
| 15 | + module.exports = { |
| 16 | + devServer: { |
| 17 | + http2: true, |
| 18 | + https: { |
| 19 | + ca: "./path/to/server.pem", |
| 20 | + pfx: "./path/to/server.pfx", |
| 21 | + key: "./path/to/server.key", |
| 22 | + cert: "./path/to/server.crt", |
| 23 | + passphrase: "webpack-dev-server", |
| 24 | + requestCert: true, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }; |
| 28 | + ``` |
| 29 | + |
| 30 | + v5: |
| 31 | + |
| 32 | + ```js |
| 33 | + module.exports = { |
| 34 | + //... |
| 35 | + devServer: { |
| 36 | + server: { |
| 37 | + type: "spdy", // or use 'https' |
| 38 | + options: { |
| 39 | + ca: "./path/to/server.pem", |
| 40 | + pfx: "./path/to/server.pfx", |
| 41 | + key: "./path/to/server.key", |
| 42 | + cert: "./path/to/server.crt", |
| 43 | + passphrase: "webpack-dev-server", |
| 44 | + requestCert: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | + ``` |
| 50 | + |
| 51 | +- The `server.options.cacert` option was removed in favor of the `server.options.ca` option. |
| 52 | + |
| 53 | + v4: |
| 54 | + |
| 55 | + ```js |
| 56 | + module.exports = { |
| 57 | + //... |
| 58 | + devServer: { |
| 59 | + server: { |
| 60 | + type: "https", // or use 'https' |
| 61 | + options: { |
| 62 | + cacert: "./path/to/server.pem", |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }; |
| 67 | + ``` |
| 68 | + |
| 69 | + v5: |
| 70 | + |
| 71 | + ```js |
| 72 | + module.exports = { |
| 73 | + //... |
| 74 | + devServer: { |
| 75 | + server: { |
| 76 | + type: "https", |
| 77 | + options: { |
| 78 | + ca: "./path/to/server.pem", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }; |
| 83 | + ``` |
| 84 | + |
| 85 | +- The `onAfterSetupMiddleware` and `onBeforeSetupMiddleware` options were removed in favor of [the `setupMiddlewares` option](https://webpack.js.org/configuration/dev-server/#devserversetupmiddlewares). |
| 86 | + |
| 87 | + v4: |
| 88 | + |
| 89 | + ```js |
| 90 | + module.exports = { |
| 91 | + //... |
| 92 | + devServer: { |
| 93 | + onAfterSetupMiddleware: function (devServer) { |
| 94 | + if (!devServer) { |
| 95 | + throw new Error("webpack-dev-server is not defined"); |
| 96 | + } |
| 97 | + |
| 98 | + devServer.app.get("/some/after-path", function (req, res) { |
| 99 | + res.json({ custom: "response" }); |
| 100 | + }); |
| 101 | + }, |
| 102 | + onBeforeSetupMiddleware: function (devServer) { |
| 103 | + if (!devServer) { |
| 104 | + throw new Error("webpack-dev-server is not defined"); |
| 105 | + } |
| 106 | + |
| 107 | + devServer.app.get("/some/before-path", function (req, res) { |
| 108 | + res.json({ custom: "response" }); |
| 109 | + }); |
| 110 | + }, |
| 111 | + }, |
| 112 | + }; |
| 113 | + ``` |
| 114 | + |
| 115 | + v5: |
| 116 | + |
| 117 | + ```js |
| 118 | + module.exports = { |
| 119 | + // ... |
| 120 | + devServer: { |
| 121 | + setupMiddlewares: (middlewares, devServer) => { |
| 122 | + if (!devServer) { |
| 123 | + throw new Error("webpack-dev-server is not defined"); |
| 124 | + } |
| 125 | + |
| 126 | + devServer.app.get("/setup-middleware/some/path", (_, response) => { |
| 127 | + response.send("setup-middlewares option GET"); |
| 128 | + }); |
| 129 | + |
| 130 | + // Use the `unshift` method if you want to run a middleware before all other middlewares |
| 131 | + // or when you are migrating from the `onBeforeSetupMiddleware` option |
| 132 | + middlewares.unshift({ |
| 133 | + name: "first-in-array", |
| 134 | + // `path` is optional |
| 135 | + path: "/some/before-path", |
| 136 | + middleware: (req, res) => { |
| 137 | + res.send("Foo!"); |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + // Use the `push` method if you want to run a middleware after all other middlewares |
| 142 | + // or when you are migrating from the `onAfterSetupMiddleware` option |
| 143 | + middlewares.push({ |
| 144 | + name: "hello-world-test-one", |
| 145 | + // `path` is optional |
| 146 | + path: "/some/after-bar", |
| 147 | + middleware: (req, res) => { |
| 148 | + res.send("Foo Bar!"); |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + middlewares.push((req, res) => { |
| 153 | + res.send("Hello World!"); |
| 154 | + }); |
| 155 | + |
| 156 | + return middlewares; |
| 157 | + }, |
| 158 | + }, |
| 159 | + }; |
| 160 | + ``` |
| 161 | + |
| 162 | +- The `proxy` option's schema was updated to accept only an array. |
| 163 | + |
| 164 | +v4: |
| 165 | + |
| 166 | +```js |
| 167 | +module.exports = { |
| 168 | + //... |
| 169 | + devServer: { |
| 170 | + proxy: { |
| 171 | + "/api": { |
| 172 | + target: "http://localhost:3000", |
| 173 | + changeOrigin: true, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | +}; |
| 178 | +``` |
| 179 | + |
| 180 | +v5: |
| 181 | + |
| 182 | +```js |
| 183 | +module.exports = { |
| 184 | + //... |
| 185 | + devServer: { |
| 186 | + proxy: [ |
| 187 | + { |
| 188 | + context: ["/api"], |
| 189 | + target: "http://localhost:3000", |
| 190 | + changeOrigin: true, |
| 191 | + }, |
| 192 | + ], |
| 193 | + }, |
| 194 | +}; |
| 195 | +``` |
| 196 | + |
| 197 | +- The `--open-app` cli option was removed in favor of the `--open-app-name` option. |
| 198 | +- The `--web-socket-server` cli option was removed in favor of the `--web-socket-server-type` option. |
| 199 | +- The `magicHtml` option was removed without replacement. |
| 200 | +- The value of the `WEBPACK_SERVE` environment variable was changed from `true`(boolean) to `'true'` (string). |
| 201 | +- [`webpack-dev-middleware`](https://github.com/webpack/webpack-dev-middleware) was update to v6. |
| 202 | +- The `constructor` arguments were changed, now the first argument is dev server options, the second is compiler. |
| 203 | + |
| 204 | + v4: |
| 205 | + |
| 206 | + ```js |
| 207 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 208 | + const devServer = new Server(compiler, devServerOptions); |
| 209 | + ``` |
| 210 | + |
| 211 | + v5: |
| 212 | + |
| 213 | + ```js |
| 214 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 215 | + const devServer = new Server(devServerOptions, compiler); |
| 216 | + ``` |
| 217 | + |
| 218 | +- The `listen` method is deprecated in favor the [async `start`](https://webpack.js.org/api/webpack-dev-server/#start) or the [`startCallback`](https://webpack.js.org/api/webpack-dev-server/#startcallbackcallback) methods |
| 219 | + |
| 220 | + v4: |
| 221 | + |
| 222 | + ```js |
| 223 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 224 | + const devServer = new Server(compiler, devServerOptions); |
| 225 | + |
| 226 | + devServer.listen(devServerOptions.host, devServerOptions.port, () => { |
| 227 | + console.log("Running"); |
| 228 | + }); |
| 229 | + ``` |
| 230 | + |
| 231 | + v5: |
| 232 | + |
| 233 | + ```js |
| 234 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 235 | + const devServer = new Server(devServerOptions, compiler); |
| 236 | + |
| 237 | + (async () => { |
| 238 | + await devServer.start(); |
| 239 | + |
| 240 | + console.log("Running"); |
| 241 | + })(); |
| 242 | + ``` |
| 243 | + |
| 244 | + ```js |
| 245 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 246 | + const devServer = new Server(devServerOptions, compiler); |
| 247 | + |
| 248 | + devServer.startCallback(() => { |
| 249 | + console.log("Running"); |
| 250 | + }); |
| 251 | + ``` |
| 252 | + |
| 253 | +- The `close` method was removed in favor the [async `stop`](https://webpack.js.org/api/webpack-dev-server/#stop) or the [`stopCallback`](https://webpack.js.org/api/webpack-dev-server/#stopcallbackcallback) methods. |
| 254 | + |
| 255 | + v4: |
| 256 | + |
| 257 | + ```js |
| 258 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 259 | + const devServer = new Server(compiler, devServerOptions); |
| 260 | + |
| 261 | + devServer.listen(devServerOptions.host, devServerOptions.port, () => { |
| 262 | + console.log("Running"); |
| 263 | + |
| 264 | + devServer.close(() => { |
| 265 | + console.log("Closed"); |
| 266 | + }); |
| 267 | + }); |
| 268 | + ``` |
| 269 | + |
| 270 | + v5: |
| 271 | + |
| 272 | + ```js |
| 273 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 274 | + const devServer = new Server(devServerOptions, compiler); |
| 275 | + |
| 276 | + (async () => { |
| 277 | + await devServer.start(); |
| 278 | + |
| 279 | + console.log("Running"); |
| 280 | + |
| 281 | + await devServer.stop(); |
| 282 | + |
| 283 | + console.log("Closed"); |
| 284 | + })(); |
| 285 | + ``` |
| 286 | + |
| 287 | + ```js |
| 288 | + const devServerOptions = { host: "127.0.0.1", port: 8080 }; |
| 289 | + const devServer = new Server(devServerOptions, compiler); |
| 290 | + |
| 291 | + devServer.startCallback(() => { |
| 292 | + console.log("Running"); |
| 293 | + |
| 294 | + devServer.stopCallback(() => { |
| 295 | + console.log("Closed"); |
| 296 | + }); |
| 297 | + }); |
| 298 | + ``` |
| 299 | + |
| 300 | +- The `content-changed` method was removed in favor of the `static-changed` method from `onSocketMessage`. |
| 301 | + |
| 302 | + v4: |
| 303 | + |
| 304 | + ```js |
| 305 | + onSocketMessage["content-changed"](); |
| 306 | + ``` |
| 307 | + |
| 308 | + v5: |
| 309 | + |
| 310 | + ```js |
| 311 | + onSocketMessage["static-changed"](); |
| 312 | + ``` |
| 313 | + |
| 314 | +## Deprecations |
| 315 | + |
| 316 | +- The `bypass` option is deprecated for proxy in favor of the `router` and the `context` options. [Read more here](https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#http-proxy-middleware-options). |
0 commit comments