Skip to content

Commit ee3b320

Browse files
authored
docs: add onListening example (#3702)
1 parent 8837b2c commit ee3b320

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

examples/on-listening/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# onListening
2+
3+
Provides the ability to execute a custom function when webpack-dev-server starts listening for connections on a port.
4+
5+
**webpack.config.js**
6+
7+
```js
8+
module.exports = {
9+
// ...
10+
devServer: {
11+
onListening: (devServer) => {
12+
const port = devServer.server.address().port;
13+
console.log("Listening on port:", port);
14+
},
15+
},
16+
};
17+
```
18+
19+
To run this example use the following command:
20+
21+
```console
22+
npx webpack serve --open
23+
```
24+
25+
## What Should Happen
26+
27+
1. The script should open `https://localhost:8080/` in your default browser.
28+
2. You should see the text on the page itself change to read `Success!`.
29+
3. Check the terminal output, you should see `Listening on port: 8080` in the output.

examples/on-listening/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
const target = document.querySelector("#target");
4+
5+
target.classList.add("pass");
6+
target.innerHTML = "Success!";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
// our setup function adds behind-the-scenes bits to the config that all of our
4+
// examples need
5+
const { setup } = require("../util");
6+
7+
module.exports = setup({
8+
context: __dirname,
9+
entry: "./app.js",
10+
devServer: {
11+
onListening: (devServer) => {
12+
const port = devServer.server.address().port;
13+
console.log("Listening on port:", port);
14+
},
15+
},
16+
});

0 commit comments

Comments
 (0)