Skip to content

docs: add stopCallback method example #3876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/api/stop-callback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# API: stopCallback(callback)

While it's recommended to run `webpack-dev-server` via the CLI, you may also choose to start a server via the API.

This example demonstrates using `stopCallback(callback)` method. It instructs `webpack-dev-server` instance to stop the server and then run the callback function.

```js
const Webpack = require("webpack");
const WebpackDevServer = require("webpack-dev-server");
const webpackConfig = require("./webpack.config");

const compiler = Webpack(webpackConfig);
const devServerOptions = { ...webpackConfig.devServer };
const server = new WebpackDevServer(devServerOptions, compiler);

server.startCallback(() => {
console.log("Successfully started server on http://localhost:8080");
});

const stopServer = () =>
server.stopCallback(() => {
console.log("Server stopped.");
});

setTimeout(stopServer, 5000);
```

Use the following command to run this example:

```console
node server.js
```

## What Should Happen

1. The script should start the server and open `http://localhost:8080/` in your default browser.
2. You should see the text on the page itself change to read `Success! Reload the page after 5 seconds.`.
3. After 5 seconds, the script will stop the server. Confirm by reloading the browser page after 5 seconds.
4. You should see `Server stopped.` in your terminal output.
6 changes: 6 additions & 0 deletions examples/api/stop-callback/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

const target = document.querySelector("#target");

target.classList.add("pass");
target.innerHTML = "Success! Reload the page after 5 seconds.";
20 changes: 20 additions & 0 deletions examples/api/stop-callback/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

const Webpack = require("webpack");
const WebpackDevServer = require("../../../lib/Server");
const webpackConfig = require("./webpack.config");

const compiler = Webpack(webpackConfig);
const devServerOptions = { ...webpackConfig.devServer, open: true };
const server = new WebpackDevServer(devServerOptions, compiler);

server.startCallback(() => {
console.log("Successfully started server on http://localhost:8080");
});

const stopServer = () =>
server.stopCallback(() => {
console.log("Server stopped.");
});

setTimeout(stopServer, 5000);
16 changes: 16 additions & 0 deletions examples/api/stop-callback/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

// our setup function adds behind-the-scenes bits to the config that all of our
// examples need
const { setup } = require("../../util");

module.exports = setup({
context: __dirname,
entry: "./app.js",
output: {
filename: "bundle.js",
},
stats: {
colors: true,
},
});