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
Copy file name to clipboardExpand all lines: src/content/configuration/dev-server.md
+78-67Lines changed: 78 additions & 67 deletions
Original file line number
Diff line number
Diff line change
@@ -26,13 +26,11 @@ contributors:
26
26
27
27
This page describes the options that affect the behavior of webpack-dev-server (short: dev-server).
28
28
29
-
T> Options that are compatible with [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) have 🔑 next to them.
30
-
31
29
## `devServer`
32
30
33
31
`object`
34
32
35
-
This set of options is picked up by [webpack-dev-server](https://github.com/webpack/webpack-dev-server) and can be used to change its behavior in various ways. Here's a rudimentary example that gzips and serves everything from our `dist/` directory in the project root:
33
+
This set of options is picked up by [webpack-dev-server](https://github.com/webpack/webpack-dev-server) and can be used to change its behavior in various ways. Here's a rudimentary example that gzips and serves everything from our `public/` directory in the project root:
36
34
37
35
**webpack.config.js**
38
36
@@ -43,7 +41,7 @@ module.exports = {
43
41
//...
44
42
devServer: {
45
43
static: {
46
-
directory:path.join(__dirname, 'dist'),
44
+
directory:path.join(__dirname, 'public'),
47
45
},
48
46
compress:true,
49
47
port:9000,
@@ -54,14 +52,16 @@ module.exports = {
54
52
When the server is started, there will be a message prior to the list of resolved modules:
55
53
56
54
```bash
57
-
http://localhost:9000/
58
-
webpack output is served from /build/
59
-
Content not from webpack is served from /path/to/dist/
<i> [webpack-dev-server] On Your Network (IPv4): http://197.158.164.104:9000/
58
+
<i> [webpack-dev-server] On Your Network (IPv6): http://[fe80::1]:9000/
59
+
<i> [webpack-dev-server] Content not from webpack is served from '/path/to/public' directory
60
60
```
61
61
62
62
that will give some background on where the server is located and what it's serving.
63
63
64
-
If you're using dev-server through the Node.js API, the options in `devServer` will be ignored. Pass the options as a second parameter instead: `new WebpackDevServer(compiler, {...})`. [See here](https://github.com/webpack/webpack-dev-server/tree/master/examples/api/simple) for an example of how to use webpack-dev-server through the Node.js API.
64
+
If you're using dev-server through the Node.js API, the options in `devServer` will be ignored. Pass the options as the first parameter instead: `new WebpackDevServer({...}, compiler)`. [See here](https://github.com/webpack/webpack-dev-server/tree/master/examples/api/simple) for an example of how to use webpack-dev-server through the Node.js API.
65
65
66
66
W> You cannot use the second `compiler` argument (a callback) when using `WebpackDevServer`.
67
67
@@ -83,28 +83,6 @@ npx webpack serve
83
83
84
84
A list of CLI options for `serve` is available [here](https://github.com/webpack/webpack-cli/blob/master/SERVE-OPTIONS.md)
85
85
86
-
## `devServer.onAfterSetupMiddleware`
87
-
88
-
`function (app, server, compiler)`
89
-
90
-
Provides the ability to execute custom middleware after all other middleware
91
-
internally within the server.
92
-
93
-
**webpack.config.js**
94
-
95
-
```javascript
96
-
module.exports= {
97
-
//...
98
-
devServer: {
99
-
onAfterSetupMiddleware:function (devServer) {
100
-
devServer.app.get('/some/path', function (req, res) {
101
-
res.json({ custom:'response' });
102
-
});
103
-
},
104
-
},
105
-
};
106
-
```
107
-
108
86
## `devServer.allowedHosts`
109
87
110
88
`'auto' | 'all'``[string]`
@@ -162,29 +140,6 @@ To use this option with the CLI pass the `--allowed-hosts` as following:
It is possible to configure advanced options for watching static files from [`static.directory`](#directory). See the [`chokidar`](https://github.com/paulmillr/chokidar) documentation for the possible options.
1308
1308
1309
+
**webpack.config.js**
1310
+
1309
1311
```javascript
1310
1312
constpath=require('path');
1311
1313
@@ -1329,6 +1331,8 @@ module.exports = {
1329
1331
1330
1332
This option allows you to configure list of globs/directories/files to watch for file changes. For example:
1331
1333
1334
+
**webpack.config.js**
1335
+
1332
1336
```javascript
1333
1337
module.exports= {
1334
1338
//...
@@ -1340,6 +1344,8 @@ module.exports = {
1340
1344
1341
1345
It is possible to configure advanced options for watching files. See the [`chokidar`](https://github.com/paulmillr/chokidar) documentation for the possible options.
To create a custom server implementation, create a class that extends [`BaseServer`](https://github.com/webpack/webpack-dev-server/blob/master/lib/servers/BaseServer.js).
1367
+
This option allows us either to choose the current web-socket server or to provide custom web-socket server implementation.
1362
1368
1363
-
Using path to `CustomServer.js`, a custom WebSocket server implementation, along with the compatible `'ws'` client:
1369
+
The current default mode is `'ws'`. This mode uses [`ws`](https://www.npmjs.com/package/ws) as a server, and native WebSockets on the client.
1370
+
1371
+
**webpack.config.js**
1364
1372
1365
1373
```javascript
1366
1374
module.exports= {
1367
1375
//...
1368
1376
devServer: {
1369
-
transportMode: {
1370
-
client:'ws',
1371
-
server:require.resolve('./CustomServer'),
1372
-
},
1377
+
webSocketServer:'ws',
1373
1378
},
1374
1379
};
1375
1380
```
1376
1381
1377
-
Using class exported by `CustomServer.js`, a custom WebSocket server implementation, along with the compatible `'ws'` client:
1382
+
To create a custom server implementation, create a class that extends [`BaseServer`](https://github.com/webpack/webpack-dev-server/blob/master/lib/servers/BaseServer.js).
1383
+
1384
+
Using path to `CustomServer.js`, a custom WebSocket server implementation, along with the compatible `'ws'` client:
0 commit comments