You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[webpack-dev-server](https://github.com/webpack/webpack-dev-server) can be used to quickly develop an application. See the [development guide](/guides/development/) to get started.
29
29
30
-
This page describes the options that affect the behavior of webpack-dev-server (short: dev-server) <Badgetext="version >= 4.0.0" />. Migration guide from `v3` to `v4` can be found [here](https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md).
30
+
This page describes the options that affect the behavior of webpack-dev-server (short: dev-server) <Badgetext="version >= 5.0.0" />. Migration guide from `v4` to `v5` can be found [here](https://github.com/webpack/webpack-dev-server/blob/master/migration-v5.md).
31
31
32
-
W> `webpack-dev-server v4.0.0+` requires `node >= v12.13.0`, `webpack >= v4.37.0` (but we recommend using `webpack >= v5.0.0`), and `webpack-cli >= v4.7.0`.
32
+
W> `webpack-dev-server v5.0.0+` requires `node >= v18.12.0`, `webpack >= v5.0.0` and `webpack-cli >= v4.7.0`, we recommend using the latest version.
T> When providing a custom client and server implementation make sure that they are compatible with one another to communicate successfully.
@@ -607,148 +607,6 @@ module.exports = {
607
607
};
608
608
```
609
609
610
-
## devServer.http2
611
-
612
-
`boolean`
613
-
614
-
Serve over HTTP/2 using [spdy](https://www.npmjs.com/package/spdy). This option is ignored for Node 15.0.0 and above, as spdy is broken for those versions. The dev server will migrate over to Node's built-in HTTP/2 once [Express](https://expressjs.com/) supports it.
615
-
616
-
HTTP/2 with a self-signed certificate:
617
-
618
-
**webpack.config.js**
619
-
620
-
```javascript
621
-
module.exports= {
622
-
//...
623
-
devServer: {
624
-
http2:true,
625
-
},
626
-
};
627
-
```
628
-
629
-
Usage via CLI
630
-
631
-
```bash
632
-
npx webpack serve --http2
633
-
```
634
-
635
-
To disable:
636
-
637
-
```bash
638
-
npx webpack serve --no-http2
639
-
```
640
-
641
-
Provide your own certificate using the [https](#devserverhttps) option:
642
-
643
-
**webpack.config.js**
644
-
645
-
```javascript
646
-
constfs=require('fs');
647
-
648
-
module.exports= {
649
-
//...
650
-
devServer: {
651
-
http2:true,
652
-
https: {
653
-
key:fs.readFileSync('/path/to/server.key'),
654
-
cert:fs.readFileSync('/path/to/server.crt'),
655
-
ca:fs.readFileSync('/path/to/ca.pem'),
656
-
},
657
-
},
658
-
};
659
-
```
660
-
661
-
To pass your certificate via CLI, use the following options:
`webpack-dev-server >= v4.2.0` allows you to set additional [TLS options](https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options) like `minVersion`. Also, you can directly pass the contents of respective files:
W> Don't specify `https.ca` and `https.cacert` options together, if specified `https.ca` will be used. `https.cacert` is deprecated and will be removed in the next major release.
749
-
750
-
W> This option is deprecated in favor of the [devServer.server](#devserverserver) option.
W> Live reloading works only with web related [targets](/configuration/target/#string) like `web`, `webworker`, `electron-renderer` and `node-webkit`.
1044
902
1045
-
## devServer.magicHtml
1046
-
1047
-
`boolean = true`
1048
-
1049
-
<Badgetext="v4.1.0+" />
1050
-
1051
-
Tell dev-server to enable/disable magic HTML routes (routes corresponding to your webpack output, for example `/main` for `main.js`).
1052
-
1053
-
**webpack.config.js**
1054
-
1055
-
```javascript
1056
-
module.exports= {
1057
-
//...
1058
-
devServer: {
1059
-
magicHtml:true,
1060
-
},
1061
-
};
1062
-
```
1063
-
1064
-
Usage via the CLI:
1065
-
1066
-
```bash
1067
-
npx webpack serve --magic-html
1068
-
```
1069
-
1070
-
To disable:
1071
-
1072
-
```bash
1073
-
npx webpack serve --no-magic-html
1074
-
```
1075
-
1076
-
## devServer.onAfterSetupMiddleware
1077
-
1078
-
`function (devServer)`
1079
-
1080
-
Provides the ability to execute custom middleware after all other middleware
1081
-
internally within the server.
1082
-
1083
-
**webpack.config.js**
1084
-
1085
-
```javascript
1086
-
module.exports= {
1087
-
//...
1088
-
devServer: {
1089
-
onAfterSetupMiddleware:function (devServer) {
1090
-
if (!devServer) {
1091
-
thrownewError('webpack-dev-server is not defined');
1092
-
}
1093
-
1094
-
devServer.app.get('/some/path', function (req, res) {
1095
-
res.json({ custom:'response' });
1096
-
});
1097
-
},
1098
-
},
1099
-
};
1100
-
```
1101
-
1102
-
W> This option is deprecated in favor of the [devServer.setupMiddlewares](#devserversetupmiddlewares) option.
1103
-
1104
-
## devServer.onBeforeSetupMiddleware
1105
-
1106
-
`function (devServer)`
1107
-
1108
-
Provides the ability to execute custom middleware prior to all other middleware
1109
-
internally within the server. This could be used to define custom handlers, for
1110
-
example:
1111
-
1112
-
**webpack.config.js**
1113
-
1114
-
```javascript
1115
-
module.exports= {
1116
-
//...
1117
-
devServer: {
1118
-
onBeforeSetupMiddleware:function (devServer) {
1119
-
if (!devServer) {
1120
-
thrownewError('webpack-dev-server is not defined');
1121
-
}
1122
-
1123
-
devServer.app.get('/some/path', function (req, res) {
1124
-
res.json({ custom:'response' });
1125
-
});
1126
-
},
1127
-
},
1128
-
};
1129
-
```
1130
-
1131
-
W> This option is deprecated in favor of the [devServer.setupMiddlewares](#devserversetupmiddlewares) option.
1132
-
1133
903
## devserver.onListening
1134
904
1135
905
`function (devServer)`
@@ -1309,7 +1079,7 @@ npx webpack serve --port auto
1309
1079
1310
1080
## devServer.proxy
1311
1081
1312
-
`object``[object, function]`
1082
+
`[object, function]`
1313
1083
1314
1084
Proxying some URLs can be useful when you have a separate API backend development server and you want to send API requests on the same domain.
1315
1085
@@ -1323,9 +1093,12 @@ With a backend on `localhost:3000`, you can use this to enable proxying:
1323
1093
module.exports= {
1324
1094
//...
1325
1095
devServer: {
1326
-
proxy: {
1327
-
'/api':'http://localhost:3000',
1328
-
},
1096
+
proxy: [
1097
+
{
1098
+
context: ['/api'],
1099
+
target:'http://localhost:3000',
1100
+
},
1101
+
],
1329
1102
},
1330
1103
};
1331
1104
```
@@ -1340,12 +1113,13 @@ If you don't want `/api` to be passed along, we need to rewrite the path:
1340
1113
module.exports= {
1341
1114
//...
1342
1115
devServer: {
1343
-
proxy: {
1344
-
'/api': {
1116
+
proxy: [
1117
+
{
1118
+
context: ['/api'],
1345
1119
target:'http://localhost:3000',
1346
1120
pathRewrite: { '^/api':'' },
1347
1121
},
1348
-
},
1122
+
],
1349
1123
},
1350
1124
};
1351
1125
```
@@ -1358,12 +1132,13 @@ A backend server running on HTTPS with an invalid certificate will not be accept
1358
1132
module.exports= {
1359
1133
//...
1360
1134
devServer: {
1361
-
proxy: {
1362
-
'/api': {
1363
-
target:'https://other-server.example.com',
1135
+
proxy: [
1136
+
{
1137
+
context: ['/api'],
1138
+
target:'http://localhost:3000',
1364
1139
secure:false,
1365
1140
},
1366
-
},
1141
+
],
1367
1142
},
1368
1143
};
1369
1144
```
@@ -1384,8 +1159,9 @@ E.g. for a browser request, you want to serve an HTML page, but for an API reque
1384
1159
module.exports= {
1385
1160
//...
1386
1161
devServer: {
1387
-
proxy: {
1388
-
'/api': {
1162
+
proxy: [
1163
+
{
1164
+
context: ['/api'],
1389
1165
target:'http://localhost:3000',
1390
1166
bypass:function (req, res, proxyOptions) {
1391
1167
if (req.headers.accept.indexOf('html') !==-1) {
@@ -1394,11 +1170,13 @@ module.exports = {
1394
1170
}
1395
1171
},
1396
1172
},
1397
-
},
1173
+
],
1398
1174
},
1399
1175
};
1400
1176
```
1401
1177
1178
+
W> 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).
1179
+
1402
1180
If you want to proxy multiple, specific paths to the same target, you can use an array of one or more objects with a `context` property:
1403
1181
1404
1182
**webpack.config.js**
@@ -1428,10 +1206,12 @@ module.exports = {
1428
1206
devMiddleware: {
1429
1207
index:false, // specify to enable root proxying
1430
1208
},
1431
-
proxy: {
1432
-
context: () =>true,
1433
-
target:'http://localhost:1234',
1434
-
},
1209
+
proxy: [
1210
+
{
1211
+
context: () =>true,
1212
+
target:'http://localhost:1234',
1213
+
},
1214
+
],
1435
1215
},
1436
1216
};
1437
1217
```
@@ -1444,12 +1224,13 @@ The origin of the host header is kept when proxying by default, you can set `cha
1444
1224
module.exports= {
1445
1225
//...
1446
1226
devServer: {
1447
-
proxy: {
1448
-
'/api': {
1227
+
proxy: [
1228
+
{
1229
+
context: ['/api'],
1449
1230
target:'http://localhost:3000',
1450
1231
changeOrigin:true,
1451
1232
},
1452
-
},
1233
+
],
1453
1234
},
1454
1235
};
1455
1236
```
@@ -1575,8 +1356,6 @@ module.exports = {
1575
1356
};
1576
1357
```
1577
1358
1578
-
W> Don't specify `server.options.ca` and `server.options.cacert` options together, if specified `server.options.ca` will be used. `server.options.cacert` is deprecated and will be removed in the next major release.
0 commit comments