Skip to content

Commit 07b85dc

Browse files
committed
docs: add example for using webpack-dev-server as a plugin
Add a runnable example (app, webpack config and README) showing how to configure and use webpack-dev-server as a webpack plugin.
1 parent 33fedcc commit 07b85dc

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

examples/api/plugin/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# API: Plugin
2+
3+
Use `webpack-dev-server` as a webpack plugin by adding an instance to
4+
`plugins[]`. The dev server starts when the first compilation finishes and
5+
stops when the compiler closes — no separate `server.start()` call is needed.
6+
7+
```js
8+
// webpack.config.js
9+
const WebpackDevServer = require("webpack-dev-server");
10+
11+
module.exports = {
12+
// ...
13+
plugins: [new WebpackDevServer({ port: 8080, open: true })],
14+
};
15+
```
16+
17+
If you have existing `devServer` options in your config, spread them into the
18+
plugin instance — the plugin reads its options from its constructor argument,
19+
not from `config.devServer`:
20+
21+
```js
22+
const devServerOptions = { ...config.devServer, open: true };
23+
config.plugins.push(new WebpackDevServer(devServerOptions));
24+
```
25+
26+
## Run
27+
28+
```console
29+
npx webpack --watch
30+
```
31+
32+
## What should happen
33+
34+
1. Open `http://localhost:8080/` in your preferred browser.
35+
2. You should see the text on the page itself change to read `Success!`.
36+
3. Press `Ctrl+C` in the terminal — `webpack-cli` closes the compiler, which
37+
fires the plugin's `shutdown` hook, stopping the dev server cleanly.
38+
39+
## Notes
40+
41+
- The plugin works with both `webpack --watch` and `webpack serve`. With
42+
`webpack serve`, `webpack-cli` already creates its own standalone dev server
43+
for the same compiler, so you would end up with two servers running. If
44+
that's intentional (e.g. different ports/hosts), make sure the plugin's
45+
`port` does not clash with the one `webpack-cli` resolves from
46+
`config.devServer` and CLI args. Otherwise prefer one or the other.

examples/api/plugin/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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
const WebpackDevServer = require("../../../lib/Server");
4+
// our setup function adds behind-the-scenes bits to the config that all of our
5+
// examples need
6+
const { setup } = require("../../util");
7+
8+
const config = setup({
9+
context: __dirname,
10+
entry: "./app.js",
11+
output: {
12+
filename: "bundle.js",
13+
},
14+
stats: {
15+
colors: true,
16+
},
17+
});
18+
19+
// `setup()` populates `config.devServer.setupMiddlewares` so that the example
20+
// layout assets (CSS, favicon, icons under `.assets/`) are served by the dev
21+
// server. Forward those options to the plugin instance — without them the
22+
// `<link rel="stylesheet">` from the shared layout would 404.
23+
config.plugins.push(
24+
new WebpackDevServer({ ...config.devServer, port: 8090, open: true }),
25+
);
26+
27+
module.exports = config;

0 commit comments

Comments
 (0)