UNPKG

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