UNPKG

22.3 kBMarkdownView Raw
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
4 </a>
5</div>
6
7[![npm][npm]][npm-url]
8[![node][node]][node-url]
9[![tests][tests]][tests-url]
10[![coverage][cover]][cover-url]
11[![chat][chat]][chat-url]
12[![downloads][downloads]][npm-url]
13[![contributors][contributors]][contributors-url]
14
15# webpack-dev-server
16
17Use [webpack](https://webpack.js.org) with a development server that provides
18live reloading. This should be used for **development only**.
19
20It uses [webpack-dev-middleware][middleware-url] under the hood, which provides
21fast in-memory access to the webpack assets.
22
23## Table of Contents
24
25- [Getting Started](#getting-started)
26- [Usage](#usage)
27 - [With the CLI](#with-the-cli)
28 - [With NPM Scripts](#with-npm-scripts)
29 - [With the API](#with-the-api)
30 - [The Result](#the-result)
31- [Browser Support](#browser-support)
32- [Support](#support)
33- [Contributing](#contributing)
34- [Attribution](#attribution)
35- [License](#license)
36
37## Getting Started
38
39First things first, install the module:
40
41```console
42npm install webpack-dev-server --save-dev
43```
44
45_Note: While you can install and run webpack-dev-server globally, we recommend
46installing it locally. webpack-dev-server will always use a local installation
47over a global one._
48
49## Usage
50
51There are two main, recommended methods of using the module:
52
53### With the CLI
54
55The easiest way to use it is with the [webpack CLI](https://webpack.js.org/api/cli/). In the directory where your
56`webpack.config.js` is, run:
57
58```console
59npx webpack serve
60```
61
62Following options are available with `webpack serve`:
63
64```
65Usage: webpack serve|server|s [entries...] [options]
66
67Run the webpack dev server.
68
69Options:
70 -c, --config <value...> Provide path to a webpack configuration file e.g. ./webpack.config.js.
71 --config-name <value...> Name of the configuration to use.
72 -m, --merge Merge two or more configurations using 'webpack-merge'.
73 --env <value...> Environment passed to the configuration when it is a function.
74 --node-env <value> Sets process.env.NODE_ENV to the specified value.
75 --progress [value] Print compilation progress during build.
76 -j, --json [value] Prints result as JSON or store it in a file.
77 --entry <value...> The entry point(s) of your application e.g. ./src/main.js.
78 -o, --output-path <value> Output location of the file generated by webpack e.g. ./dist/.
79 -t, --target <value> Sets the build target e.g. node.
80 -d, --devtool <value> Determine source maps to use.
81 --no-devtool Do not generate source maps.
82 --mode <value> Defines the mode to pass to webpack.
83 --name <value> Name of the configuration. Used when loading multiple configurations.
84 --stats [value] It instructs webpack on how to treat the stats e.g. verbose.
85 --no-stats Disable stats output.
86 --watch-options-stdin Stop watching when stdin stream has ended.
87 --no-watch-options-stdin Do not stop watching when stdin stream has ended.
88 --allowed-hosts <value...> Allows to enumerate the hosts from which access to the dev server are allowed (useful when you are proxying dev server, by default is 'auto').
89 --allowed-hosts-reset Clear all items provided in 'allowedHosts' configuration. Allows to enumerate the hosts from which access to the dev server are allowed (useful when you are proxying dev server, by default is 'auto').
90 --bonjour Allows to broadcasts dev server via ZeroConf networking on start.
91 --no-bonjour Disallows to broadcasts dev server via ZeroConf networking on start.
92 --client-web-socket-transport <value> Allows to set custom web socket transport to communicate with dev server.
93 --no-client Negative 'client' option.
94 --client-logging <value> Allows to specify options for client script in the browser or disable client script.
95 --client-progress Prints compilation progress in percentage in the browser.
96 --no-client-progress Does not print compilation progress in percentage in the browser.
97 --client-reconnect [value] Tells dev-server the number of times it should try to reconnect the client.
98 --no-client-reconnect Tells dev-server to not to try to connect the client.
99 --client-overlay Enables a full-screen overlay in the browser when there are compiler errors or warnings.
100 --no-client-overlay Disables a full-screen overlay in the browser when there are compiler errors or warnings.
101 --client-overlay-errors Enables a full-screen overlay in the browser when there are compiler errors.
102 --no-client-overlay-errors Negative 'client-overlay-errors' option.
103 --client-overlay-warnings Enables a full-screen overlay in the browser when there are compiler warnings.
104 --no-client-overlay-warnings Negative 'client-overlay-warnings' option.
105 --client-web-socket-url <value> Allows to specify URL to web socket server (useful when you're proxying dev server and client script does not always know where to connect to).
106 --client-web-socket-url-hostname <value> Tells clients connected to devServer to use the provided hostname.
107 --client-web-socket-url-port <value> Tells clients connected to devServer to use the provided port.
108 --client-web-socket-url-pathname <value> Tells clients connected to devServer to use the provided path to connect.
109 --client-web-socket-url-protocol <value> Tells clients connected to devServer to use the provided protocol.
110 --client-web-socket-url-username <value> Tells clients connected to devServer to use the provided username to authenticate.
111 --client-web-socket-url-password <value> Tells clients connected to devServer to use the provided password to authenticate.
112 --web-socket-server <value> Deprecated: please use 'webSocketServer.type'/'--web-socket-server-type' option. Allows to set web socket server and options (by default 'ws').
113 --no-web-socket-server Negative 'web-socket-server' option.
114 --web-socket-server-type <value> Allows to set web socket server and options (by default 'ws').
115 --compress Enables gzip compression for everything served.
116 --no-compress Disables gzip compression for everything served.
117 --history-api-fallback Allows to proxy requests through a specified index page (by default 'index.html'), useful for Single Page Applications that utilise the HTML5 History API.
118 --no-history-api-fallback Negative 'history-api-fallback' option.
119 --host <value> Allows to specify a hostname to use.
120 --hot [value] Enables Hot Module Replacement.
121 --no-hot Disables Hot Module Replacement.
122 --http2 Allows to serve over HTTP/2 using SPDY. Deprecated, it will be removed in favor of the `server` option.
123 --no-http2 Does not serve over HTTP/2 using SPDY.
124--https Allows to configure the server's listening socket for TLS (by default, dev server will be served over HTTP). Deprecated, it will be removed in favor
125 of the `server` option.
126 --no-https Disallows to configure the server's listening socket for TLS (by default, dev server will be served over HTTP).
127 --https-passphrase <value> Passphrase for a pfx file. Deprecated, it will be removed in favor of the `server.options.passphrase` option.
128 --https-request-cert Request for an SSL certificate. Deprecated, it will be removed in favor of the `server.options.requestCert`
129 option.
130 --no-https-request-cert Negative 'https-request-cert' option.
131 --https-ca <value...> Path to an SSL CA certificate or content of an SSL CA certificate. Deprecated, it will be removed in favor of the
132 `server.options.ca` option.
133 --https-ca-reset Clear all items provided in 'https.ca' configuration. Path to an SSL CA certificate or content of an SSL CA
134 certificate. Deprecated, it will be removed in favor of the `server.options.ca` option.
135 --https-cacert <value...> Path to an SSL CA certificate or content of an SSL CA certificate. Deprecated, it will be removed in favor of the
136 `server.options.cacert` option.
137 --https-cacert-reset Clear all items provided in 'https.cacert' configuration. Path to an SSL CA certificate or content of an SSL CA
138 certificate. Deprecated, it will be removed in favor of the `server.options.cacert` option.
139 --https-cert <value...> Path to an SSL certificate or content of an SSL certificate. Deprecated, it will be removed in favor of the
140 `server.options.cert` option.
141 --https-cert-reset Clear all items provided in 'https.cert' configuration. Path to an SSL certificate or content of an SSL
142 certificate. Deprecated, it will be removed in favor of the `server.options.cert` option.
143 --https-crl <value...> Path to PEM formatted CRLs (Certificate Revocation Lists) or content of PEM formatted CRLs (Certificate
144 Revocation Lists). Deprecated, it will be removed in favor of the `server.options.crl` option.
145 --https-crl-reset Clear all items provided in 'https.crl' configuration. Path to PEM formatted CRLs (Certificate Revocation Lists)
146 or content of PEM formatted CRLs (Certificate Revocation Lists). Deprecated, it will be removed in favor of the
147 `server.options.crl` option.
148 --https-key <value...> Path to an SSL key or content of an SSL key. Deprecated, it will be removed in favor of the `server.options.key`
149 option.
150 --https-key-reset Clear all items provided in 'https.key' configuration. Path to an SSL key or content of an SSL key. Deprecated,
151 it will be removed in favor of the `server.options.key` option.
152 --https-pfx <value...> Path to an SSL pfx file or content of an SSL pfx file. Deprecated, it will be removed in favor of the
153 `server.options.pfx` option.
154 --https-pfx-reset Clear all items provided in 'https.pfx' configuration. Path to an SSL pfx file or content of an SSL pfx file.
155 Deprecated, it will be removed in favor of the `server.options.pfx` option.
156 --ipc [value] Listen to a unix socket.
157 --live-reload Enables reload/refresh the page(s) when file changes are detected (enabled by default).
158 --no-live-reload Disables reload/refresh the page(s) when file changes are detected (enabled by default)
159 --magic-html Tells dev-server whether to enable magic HTML routes (routes corresponding to your webpack output, for example '/main' for 'main.js').
160 --no-magic-html Disables magic HTML routes (routes corresponding to your webpack output, for example '/main' for 'main.js').
161 --open [value...] Allows to configure dev server to open the browser(s) and page(s) after server had been started (set it to true to open your default browser).
162 --no-open Does not open the default browser.
163 --open-target <value...> Opens specified page in browser.
164 --open-app-name <value...> Open specified browser.
165 --open-app <value...> Open specified browser.
166 --open-reset Clear all items provided in 'open' configuration. Allows to configure dev server to open the browser(s) and page(s) after server had been started (set it to true to open your default browser).
167 --open-target-reset Clear all items provided in 'open.target' configuration. Opens specified page in browser.
168 --open-app-name-reset Clear all items provided in 'open.app.name' configuration. Open specified browser.
169 --port <value> Allows to specify a port to use.
170 --server-type <value> Allows to set server and options (by default 'http').
171 --server-options-passphrase <value> Passphrase for a pfx file.
172 --server-options-request-cert Request for an SSL certificate.
173 --no-server-options-request-cert Negative 'server-options-request-cert' option.
174 --server-options-ca <value...> Path to an SSL CA certificate or content of an SSL CA certificate.
175 --server-options-ca-reset Clear all items provided in 'server.options.ca' configuration. Path to an SSL CA certificate or content
176 of an SSL CA certificate.
177 --server-options-cacert <value...> Path to an SSL CA certificate or content of an SSL CA certificate.
178 --server-options-cacert-reset Clear all items provided in 'server.options.cacert' configuration. Path to an SSL CA certificate or
179 content of an SSL CA certificate.
180 --server-options-cert <value...> Path to an SSL certificate or content of an SSL certificate.
181 --server-options-cert-reset Clear all items provided in 'server.options.cert' configuration. Path to an SSL certificate or content
182 of an SSL certificate.
183 --server-options-crl <value...> Path to PEM formatted CRLs (Certificate Revocation Lists) or content of PEM formatted CRLs (Certificate
184 Revocation Lists).
185 --server-options-crl-reset Clear all items provided in 'server.options.crl' configuration. Path to PEM formatted CRLs (Certificate
186 Revocation Lists) or content of PEM formatted CRLs (Certificate Revocation Lists).
187 --server-options-key <value...> Path to an SSL key or content of an SSL key.
188 --server-options-key-reset Clear all items provided in 'server.options.key' configuration. Path to an SSL key or content of an SSL
189 key.
190 --server-options-pfx <value...> Path to an SSL pfx file or content of an SSL pfx file.
191 --server-options-pfx-reset Clear all items provided in 'server.options.pfx' configuration. Path to an SSL pfx file or content of
192 an SSL pfx file.
193 --static [value...] Allows to configure options for serving static files from directory (by default 'public' directory).
194 --no-static Negative 'static' option.
195 --static-directory <value...> Directory for static contents.
196 --static-public-path <value...> The static files will be available in the browser under this public path.
197 --static-serve-index Tells dev server to use serveIndex middleware when enabled.
198 --no-static-serve-index Does not tell dev server to use serveIndex middleware.
199 --static-watch Watches for files in static content directory.
200 --no-static-watch Does not watch for files in static content directory.
201 --static-reset Clear all items provided in 'static' configuration. Allows to configure options for serving static files from directory (by default 'public' directory).
202 --static-public-path-reset Clear all items provided in 'static.publicPath' configuration. The static files will be available in the browser under this public path.
203 --watch-files <value...> Allows to configure list of globs/directories/files to watch for file changes.
204 --watch-files-reset Clear all items provided in 'watchFiles' configuration. Allows to configure list of globs/directories/files to watch for file changes.
205
206Global options:
207 --color Enable colors on console.
208 --no-color Disable colors on console.
209 -v, --version Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands.
210 -h, --help [verbose] Display help for commands and options.
211
212To see list of all supported commands and options run 'webpack --help=verbose'.
213
214Webpack documentation: https://webpack.js.org/.
215CLI documentation: https://webpack.js.org/api/cli/.
216Made with ♥ by the webpack team.
217```
218
219_**Note**: For more information on above options explore this [link](https://webpack.js.org/configuration/dev-server/)._
220
221### With NPM Scripts
222
223NPM package.json scripts are a convenient and useful means to run locally installed
224binaries without having to be concerned about their full paths. Simply define a
225script as such:
226
227```json
228{
229 "scripts": {
230 "serve": "webpack serve"
231 }
232}
233```
234
235And run the following in your terminal/console:
236
237```console
238npm run serve
239```
240
241NPM will automagically reference the binary in `node_modules` for you, and
242execute the file or command.
243
244### With the API
245
246While it's recommended to run webpack-dev-server via the CLI, you may also choose to start a server via the API.
247
248See the related [API documentation for `webpack-dev-server`](https://webpack.js.org/api/webpack-dev-server/).
249
250### The Result
251
252Either method will start a server instance and begin listening for connections
253from `localhost` on port `8080`.
254
255webpack-dev-server is configured by default to support live-reload of files as
256you edit your assets while the server is running.
257
258See [**the documentation**][docs-url] for more use cases and options.
259
260## Browser Support
261
262While `webpack-dev-server` transpiles the client (browser) scripts to an ES5
263state, the project only officially supports the _last two versions of major
264browsers_. We simply don't have the resources to support every whacky
265browser out there.
266
267If you find a bug with an obscure / old browser, we would actively welcome a
268Pull Request to resolve the bug.
269
270## Support
271
272We do our best to keep Issues in the repository focused on bugs, features, and
273needed modifications to the code for the module. Because of that, we ask users
274with general support, "how-to", or "why isn't this working" questions to try one
275of the other support channels that are available.
276
277Your first-stop-shop for support for webpack-dev-server should be the excellent
278[documentation][docs-url] for the module. If you see an opportunity for improvement
279of those docs, please head over to the [webpack.js.org repo][wjo-url] and open a
280pull request.
281
282From there, we encourage users to visit the [webpack Gitter chat][chat-url] and
283talk to the fine folks there. If your quest for answers comes up dry in chat,
284head over to [StackOverflow][stack-url] and do a quick search or open a new
285question. Remember; It's always much easier to answer questions that include your
286`webpack.config.js` and relevant files!
287
288If you're twitter-savvy you can tweet [#webpack][hash-url] with your question
289and someone should be able to reach out and lend a hand.
290
291If you have discovered a :bug:, have a feature suggestion, or would like to see
292a modification, please feel free to create an issue on Github. _Note: The issue
293template isn't optional, so please be sure not to remove it, and please fill it
294out completely._
295
296## Contributing
297
298We welcome your contributions! Please have a read of [CONTRIBUTING.md](CONTRIBUTING.md) for more information on how to get involved.
299
300## Attribution
301
302This project is heavily inspired by [peerigon/nof5](https://github.com/peerigon/nof5).
303
304## License
305
306#### [MIT](./LICENSE)
307
308[npm]: https://img.shields.io/npm/v/webpack-dev-server.svg
309[npm-url]: https://npmjs.com/package/webpack-dev-server
310[node]: https://img.shields.io/node/v/webpack-dev-server.svg
311[node-url]: https://nodejs.org
312[tests]: https://github.com/webpack/webpack-dev-server/workflows/webpack-dev-server/badge.svg
313[tests-url]: https://github.com/webpack/webpack-dev-server/actions?query=workflow%3Awebpack-dev-server
314[cover]: https://codecov.io/gh/webpack/webpack-dev-server/branch/master/graph/badge.svg
315[cover-url]: https://codecov.io/gh/webpack/webpack-dev-server
316[chat]: https://badges.gitter.im/webpack/webpack.svg
317[chat-url]: https://gitter.im/webpack/webpack
318[docs-url]: https://webpack.js.org/configuration/dev-server/#devserver
319[hash-url]: https://twitter.com/search?q=webpack
320[middleware-url]: https://github.com/webpack/webpack-dev-middleware
321[stack-url]: https://stackoverflow.com/questions/tagged/webpack-dev-server
322[uglify-url]: https://github.com/webpack-contrib/uglifyjs-webpack-plugin
323[wjo-url]: https://github.com/webpack/webpack.js.org
324[downloads]: https://img.shields.io/npm/dm/webpack-dev-server.svg
325[contributors-url]: https://github.com/webpack/webpack-dev-server/graphs/contributors
326[contributors]: https://img.shields.io/github/contributors/webpack/webpack-dev-server.svg