Skip to content

Commit 0c9efe9

Browse files
authored
docs: add start example (#3848)
1 parent d8cc133 commit 0c9efe9

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

examples/api/start/README.md

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

examples/api/start/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/server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
const runServer = async () => {
12+
console.log("Starting server...");
13+
await server.start();
14+
};
15+
16+
runServer();

examples/api/start/webpack.config.js

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)