File tree Expand file tree Collapse file tree 4 files changed +88
-0
lines changed Expand file tree Collapse file tree 4 files changed +88
-0
lines changed Original file line number Diff line number Diff line change
1
+ # API: stop
2
+
3
+ While it's recommended to run ` webpack-dev-server ` via the CLI, you may also
4
+ choose to stop a server via the API.
5
+
6
+ This example demonstrates using ` stop ` method. It instructs ` webpack-dev-server ` instance to stop the server.
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
+ const stopServer = async () => {
23
+ console .log (" Stopping server..." );
24
+ await server .stop ();
25
+ };
26
+
27
+ runServer ();
28
+
29
+ setTimeout (stopServer, 5000 );
30
+ ```
31
+
32
+ Use the following command to run this example:
33
+
34
+ ``` console
35
+ node server.js
36
+ ```
37
+
38
+ ## What Should Happen
39
+
40
+ 1 . The script should start the server and open ` http://localhost:8080/ ` in your default browser.
41
+ 2 . You should see the text on the page itself change to read ` Success! Reload the page after 5 seconds. ` .
42
+ 3 . After 5 seconds, the script will stop the server. Confirm by reloading the browser page after 5 seconds.
43
+ 4 . You should see ` Stopping server... ` in your terminal output.
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! Reload the page after 5 seconds." ;
Original file line number Diff line number Diff line change
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
+ const stopServer = async ( ) => {
17
+ console . log ( "Stopping server..." ) ;
18
+ await server . stop ( ) ;
19
+ } ;
20
+
21
+ runServer ( ) ;
22
+
23
+ setTimeout ( stopServer , 5000 ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments