You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
23 KiB

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