File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ - Use ` webpack --watch ` , not ` webpack serve ` . ` webpack serve ` creates its own
42+ standalone dev server; if you also have a plugin instance in ` plugins[] ` ,
43+ the plugin detects the standalone server and stays passive to avoid binding
44+ the same port twice.
45+ - A plain ` webpack ` build (no watch) will not start the server — the plugin
46+ detects build mode and stays passive so the build can finish and the process
47+ can exit normally.
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+
3+ const target = document . querySelector ( "#target" ) ;
4+
5+ target . classList . add ( "pass" ) ;
6+ target . innerHTML = "Success!" ;
Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments