Skip to content

Commit 3d3676e

Browse files
committed
docs: update
1 parent d78de29 commit 3d3676e

File tree

1 file changed

+78
-67
lines changed

1 file changed

+78
-67
lines changed

src/content/configuration/dev-server.md

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ contributors:
2626

2727
This page describes the options that affect the behavior of webpack-dev-server (short: dev-server).
2828

29-
T> Options that are compatible with [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) have 🔑 next to them.
30-
3129
## `devServer`
3230

3331
`object`
3432

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:
3634

3735
**webpack.config.js**
3836

@@ -43,7 +41,7 @@ module.exports = {
4341
//...
4442
devServer: {
4543
static: {
46-
directory: path.join(__dirname, 'dist'),
44+
directory: path.join(__dirname, 'public'),
4745
},
4846
compress: true,
4947
port: 9000,
@@ -54,14 +52,16 @@ module.exports = {
5452
When the server is started, there will be a message prior to the list of resolved modules:
5553

5654
```bash
57-
http://localhost:9000/
58-
webpack output is served from /build/
59-
Content not from webpack is served from /path/to/dist/
55+
<i> [webpack-dev-server] Project is running at:
56+
<i> [webpack-dev-server] Loopback: http://localhost:9000/
57+
<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
6060
```
6161

6262
that will give some background on where the server is located and what it's serving.
6363

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.
6565

6666
W> You cannot use the second `compiler` argument (a callback) when using `WebpackDevServer`.
6767

@@ -83,28 +83,6 @@ npx webpack serve
8383

8484
A list of CLI options for `serve` is available [here](https://github.com/webpack/webpack-cli/blob/master/SERVE-OPTIONS.md)
8585

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-
10886
## `devServer.allowedHosts`
10987

11088
`'auto' | 'all'` `[string]`
@@ -162,29 +140,6 @@ To use this option with the CLI pass the `--allowed-hosts` as following:
162140
npx webpack serve --entry ./entry/file --output-path ./output/path --allowed-hosts .host.com --allowed-hosts host2.com
163141
```
164142

165-
## `devServer.onBeforeSetupMiddleware`
166-
167-
`function (app, server, compiler)`
168-
169-
Provides the ability to execute custom middleware prior to all other middleware
170-
internally within the server. This could be used to define custom handlers, for
171-
example:
172-
173-
**webpack.config.js**
174-
175-
```javascript
176-
module.exports = {
177-
//...
178-
devServer: {
179-
onBeforeSetupMiddleware: function (app, server, compiler) {
180-
app.get('/some/path', function (req, res) {
181-
res.json({ custom: 'response' });
182-
});
183-
},
184-
},
185-
};
186-
```
187-
188143
## `devServer.bonjour`
189144

190145
`boolean` `object`
@@ -829,6 +784,51 @@ npx webpack serve --no-live-reload
829784

830785
W> Live reloading works only with web related [targets](/configuration/target/#string) like `web`, `webworker`, `electron-renderer` and `node-webkit`.
831786

787+
## `devServer.onAfterSetupMiddleware`
788+
789+
`function (app, server, compiler)`
790+
791+
Provides the ability to execute custom middleware after all other middleware
792+
internally within the server.
793+
794+
**webpack.config.js**
795+
796+
```javascript
797+
module.exports = {
798+
//...
799+
devServer: {
800+
onAfterSetupMiddleware: function (devServer) {
801+
devServer.app.get('/some/path', function (req, res) {
802+
res.json({ custom: 'response' });
803+
});
804+
},
805+
},
806+
};
807+
```
808+
809+
## `devServer.onBeforeSetupMiddleware`
810+
811+
`function (app, server, compiler)`
812+
813+
Provides the ability to execute custom middleware prior to all other middleware
814+
internally within the server. This could be used to define custom handlers, for
815+
example:
816+
817+
**webpack.config.js**
818+
819+
```javascript
820+
module.exports = {
821+
//...
822+
devServer: {
823+
onBeforeSetupMiddleware: function (app, server, compiler) {
824+
app.get('/some/path', function (req, res) {
825+
res.json({ custom: 'response' });
826+
});
827+
},
828+
},
829+
};
830+
```
831+
832832
## devServer.open
833833

834834
`boolean` `string` `[string]` `object` `[object]`
@@ -1306,6 +1306,8 @@ npx webpack serve --no-static-watch
13061306

13071307
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.
13081308

1309+
**webpack.config.js**
1310+
13091311
```javascript
13101312
const path = require('path');
13111313

@@ -1329,6 +1331,8 @@ module.exports = {
13291331

13301332
This option allows you to configure list of globs/directories/files to watch for file changes. For example:
13311333

1334+
**webpack.config.js**
1335+
13321336
```javascript
13331337
module.exports = {
13341338
//...
@@ -1340,6 +1344,8 @@ module.exports = {
13401344

13411345
It is possible to configure advanced options for watching files. See the [`chokidar`](https://github.com/paulmillr/chokidar) documentation for the possible options.
13421346

1347+
**webpack.config.js**
1348+
13431349
```javascript
13441350
module.exports = {
13451351
//...
@@ -1356,48 +1362,53 @@ module.exports = {
13561362

13571363
## devServer.webSocketServer
13581364

1359-
`string` `path` `function`
1365+
`false | 'sockjs' | 'ws'` `string` `function` `object`
13601366

1361-
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.
13621368

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**
13641372

13651373
```javascript
13661374
module.exports = {
13671375
//...
13681376
devServer: {
1369-
transportMode: {
1370-
client: 'ws',
1371-
server: require.resolve('./CustomServer'),
1372-
},
1377+
webSocketServer: 'ws',
13731378
},
13741379
};
13751380
```
13761381

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:
1385+
1386+
**webpack.config.js**
13781387

13791388
```javascript
13801389
module.exports = {
13811390
//...
13821391
devServer: {
1383-
transportMode: {
1384-
client: 'ws',
1385-
server: require('./CustomServer'),
1392+
client: {
1393+
webSocketTransport: 'ws',
13861394
},
1395+
webSocketServer: require.resolve('./CustomServer'),
13871396
},
13881397
};
13891398
```
13901399

13911400
Using custom, compatible WebSocket client and server implementations:
13921401

1402+
**webpack.config.js**
1403+
13931404
```javascript
13941405
module.exports = {
13951406
//...
13961407
devServer: {
1397-
transportMode: {
1398-
client: require.resolve('./CustomClient'),
1399-
server: require.resolve('./CustomServer'),
1408+
client: {
1409+
webSocketTransport: require.resolve('./CustomClient'),
14001410
},
1411+
webSocketServer: require.resolve('./CustomServer'),
14011412
},
14021413
};
14031414
```

0 commit comments

Comments
 (0)