Skip to content

Commit

Permalink
docs: add startCallback example (#3864)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Sep 22, 2021
1 parent 383b508 commit 22fa60b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/api/start-callback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# API: startCallback(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 `startCallback(callback)` method. It instructs `webpack-dev-server` instance to start 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");
});
```

Use the following command to run this example:

```console
node server.js
```

## What Should Happen

1. Open `http://localhost:8080/` in your preferred browser.
2. You should see the text on the page itself change to read `Success!`.
3. You should see `Successfully started server on http://localhost:8080` in the terminal output.
6 changes: 6 additions & 0 deletions examples/api/start-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!";
13 changes: 13 additions & 0 deletions examples/api/start-callback/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"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");
});
16 changes: 16 additions & 0 deletions examples/api/start-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,
},
});

0 comments on commit 22fa60b

Please sign in to comment.