Skip to content

Commit 22fa60b

Browse files
authored
docs: add startCallback example (#3864)
1 parent 383b508 commit 22fa60b

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

examples/api/start-callback/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# API: startCallback(callback)
2+
3+
While it's recommended to run `webpack-dev-server` via the CLI, you may also choose to start a server via the API.
4+
5+
This example demonstrates using `startCallback(callback)` method. It instructs `webpack-dev-server` instance to start the server and then run the callback function.
6+
7+
```js
8+
const Webpack = require("webpack");
9+
const WebpackDevServer = require("webpack-dev-server");
10+
const webpackConfig = require("./webpack.config");
11+
12+
const compiler = Webpack(webpackConfig);
13+
const devServerOptions = { ...webpackConfig.devServer };
14+
const server = new WebpackDevServer(devServerOptions, compiler);
15+
16+
server.startCallback(() => {
17+
console.log("Successfully started server on http://localhost:8080");
18+
});
19+
```
20+
21+
Use the following command to run this example:
22+
23+
```console
24+
node server.js
25+
```
26+
27+
## What Should Happen
28+
29+
1. Open `http://localhost:8080/` in your preferred browser.
30+
2. You should see the text on the page itself change to read `Success!`.
31+
3. You should see `Successfully started server on http://localhost:8080` in the terminal output.

examples/api/start-callback/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!";

examples/api/start-callback/server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
const Webpack = require("webpack");
4+
const WebpackDevServer = require("../../../lib/Server");
5+
const webpackConfig = require("./webpack.config");
6+
7+
const compiler = Webpack(webpackConfig);
8+
const devServerOptions = { ...webpackConfig.devServer, open: true };
9+
const server = new WebpackDevServer(devServerOptions, compiler);
10+
11+
server.startCallback(() => {
12+
console.log("Successfully started server on http://localhost:8080");
13+
});
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+
output: {
11+
filename: "bundle.js",
12+
},
13+
stats: {
14+
colors: true,
15+
},
16+
});

0 commit comments

Comments
 (0)