UNPKG

8.33 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[![deps][deps]][deps-url]
10[![tests][tests]][tests-url]
11[![coverage][cover]][cover-url]
12[![chat][chat]][chat-url]
13
14# webpack-serve
15
16A lean, modern, and flexible webpack development server
17
18## Browser Support
19
20Because this module leverages _native_ `WebSockets` via `webpack-hot-client`,
21the browser support for this module is limited to only those browsers which
22support native `WebSocket`. That typically means the last two major versions
23of a particular browser. You may view a table of
24[compatible browsers here](https://caniuse.com/#feat=websockets).
25
26_Note: We won't be accepting requests for changes to this facet of the module._
27
28## Getting Started
29
30To begin, you'll need to install `webpack-serve`:
31
32```console
33$ npm install webpack-serve@next --save-dev
34```
35
36_Note the `@next` as this module is still in beta._
37
38## CLI
39
40```console
41$ webpack-serve --help
42
43 Options
44 --config The webpack config to serve. Alias for <config>.
45 --content The path from which content will be served
46 --dev An object containing options for webpack-dev-middleware
47 --host The host the app should bind to
48 --http2 Instruct the server to use HTTP2
49 --https-cert Specify a cert to enable https. Must be paired with a key
50 --https-key Specify a key to enable https. Must be paired with a cert
51 --https-pass Specify a passphrase to enable https. Must be paired with a pfx file
52 --https-pfx Specify a pfx file to enable https. Must be paired with a passphrase
53 --log-level Limit all process console messages to a specific level and above
54 {dim Levels: trace, debug, info, warn, error, silent}
55 --log-time Instruct the logger for webpack-serve and dependencies to display a timestamp
56 --no-hot Instruct the client not to apply Hot Module Replacement patches
57 --no-reload Instruct middleware {italic not} to reload the page for build errors
58 --open Instruct the app to open in the default browser
59 --open-app The name of the app to open the app within
60 --open-path The path with the app a browser should open to
61 --port The port the app should listen on
62 --version Display the webpack-serve version
63
64 Examples
65 $ webpack-serve ./webpack.config.js --no-reload
66```
67
68_Note: The CLI will use your local install of webpack-serve when available,
69even when run globally._
70
71## Webpack Config `serve` Property
72
73`webpack-serve` supports the `serve` property in your webpack config file, which
74may contain any of the supported [options](#Options).
75
76## API
77
78When using the API directly, the main entry point is the `serve` function, which
79is the default export of the module.
80
81```js
82const serve = require('webpack-serve');
83const config = require('./webpack.config.js');
84
85serve({ config });
86```
87
88### serve([options])
89
90Returns an `Object` containing:
91
92- `close()` *(Function)* - Closes the server and its dependencies.
93- `on(eventName, fn)` *(Function)* - Binds a serve event to a function. See
94[Events](#events).
95
96#### options
97
98Type: `Object`
99
100Options for initializing and controlling the server provided.
101
102##### compiler
103
104Type: `webpack`
105Default: `null`
106
107An instance of a `webpack` compiler. A passed compiler's config will take
108precedence over `config` passed in options.
109
110_Note: Any `serve` configuration must be removed from the webpack config used
111to create the compiler instance, before you attempt to create it, as it's not
112a valid webpack config property._
113
114##### config
115
116Type: `Object`
117Default: `{}`
118
119An object containing the configuration for creating a new `webpack` compiler
120instance.
121
122##### content
123
124Type: `String|[String]`
125Default: `[]`
126
127The path, or array of paths, from which content will be served.
128
129##### dev
130
131Type: `Object`
132Default: `{ publicPath: '/' }`
133
134An object containing options for [webpack-dev-middleware][dev-ware].
135
136##### host
137
138Type: `Object`
139Default: `'localhost'`
140
141Sets the host that the `WebSocket` server will listen on. If this doesn't match
142the host of the server the module is used with, the module will not function
143properly.
144
145##### hot
146
147Type: `Object`
148Default: `{}`
149
150An object containing options for [webpack-hot-client][hot-client].
151
152##### http2
153
154Type: `Boolean`
155Default: `false`
156
157If using Node v9 or greater, setting this option to `true` will enable HTTP2
158support.
159
160##### https
161
162Type: `Object`
163Default: `null`
164
165Passing this option will instruct `webpack-serve` to create and serve the webpack
166bundle and accompanying content through a secure server. The object should
167contain properties matching:
168
169```js
170{
171 key: fs.readFileSync('...key'), // Private keys in PEM format.
172 cert: fs.readFileSync('...cert'), // Cert chains in PEM format.
173 pfx: <String>, // PFX or PKCS12 encoded private key and certificate chain.
174 passphrase: <String> // A shared passphrase used for a single private key and/or a PFX.
175}
176```
177
178See the [Node documentation][https-opts] for more information.
179
180##### logLevel
181
182Type: `String`
183Default: `info`
184
185Instructs `webpack-serve` to output information to the console/terminal at levels
186higher than the specified level. Valid levels:
187
188```js
189[
190 'trace',
191 'debug',
192 'info',
193 'warn',
194 'error'
195]
196```
197
198##### logTime
199
200Type: `Boolean`
201Default: `false`
202
203Instruct `webpack-serve` to prepend each line of log output with a `[HH:mm:ss]`
204timestamp.
205
206##### open
207
208Type: `Boolean|Object`
209Default: `false`
210
211Instruct the module to open the served bundle in a browser. Accepts an `Object`
212that matches:
213
214```js
215{
216 app: <String>, // The proper name of the browser app to open.
217 path: <String> // The url path on the server to open.
218}
219```
220
221##### port
222
223Type: `Number`
224Default: `8080`
225
226The port the server should listen on.
227
228## Events
229
230`webpack-serve` emits select events which can be subscribed to. For example;
231
232```js
233const serve = require('webpack-serve');
234const config = require('./webpack.config.js');
235
236serve({ config });
237
238serve.on('listening', () => {
239 console.log('happy fun time');
240});
241```
242
243#### listening
244
245Arguments: _None_
246
247## Add-on Features
248
249A core tenant of `webpack-serve` is to stay lean in terms of feature set, and to
250empower users with familiar and easily portable patterns to implement the same
251features that those familiar with `webpack-dev-server` have come to rely on. This
252makes the module far easier to maintain, which ultimately benefits the user.
253
254Luckily, flexibility baked into `webpack-serve` makes it a snap to add-on features.
255Listed below are some of the add-on patterns that can be found in
256[docs/addons](docs/addons):
257
258- [bonjour](docs/addons/bonjour.config.js)
259- [compress](docs/addons/compress)
260- [historyApiFallback](docs/addons/history-fallback.config.js)
261- [proxy](docs/addons/proxy.config.js)
262- [staticOptions](docs/addons/static-content-options.config.js)
263- [useLocalIp](docs/addons/local-ip.config.js)
264
265## Contributing
266
267We welcome your contributions! Please have a read of
268[CONTRIBUTING.md](CONTRIBUTING.md) for more information on how to get involved.
269
270## License
271
272#### [MIT](./LICENSE)
273
274[npm]: https://img.shields.io/npm/v/webpack-serve.svg
275[npm-url]: https://npmjs.com/package/webpack-serve
276
277[node]: https://img.shields.io/node/v/webpack-serve.svg
278[node-url]: https://nodejs.org
279
280[deps]: https://david-dm.org/webpack-contrib/webpack-serve.svg
281[deps-url]: https://david-dm.org/webpack-contrib/webpack-serve
282
283[tests]: http://img.shields.io/travis/webpack-contrib/webpack-serve.svg
284[tests-url]: https://travis-ci.org/webpack-contrib/webpack-serve
285
286[cover]: https://codecov.io/gh/webpack-contrib/webpack-serve/branch/master/graph/badge.svg
287[cover-url]: https://codecov.io/gh/webpack-contrib/webpack-serve
288
289[chat]: https://badges.gitter.im/webpack/webpack.svg
290[chat-url]: https://gitter.im/webpack/webpack
291
292[dev-ware]: https://github.com/webpack/webpack-dev-middleware#options
293[hot-client]: https://github.com/webpack-contrib/webpack-hot-client#options
294[https-opts]: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options