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 |
|
16 | A lean, modern, and flexible webpack development server
|
17 |
|
18 | ## Browser Support
|
19 |
|
20 | Because this module leverages _native_ `WebSockets` via `webpack-hot-client`,
|
21 | the browser support for this module is limited to only those browsers which
|
22 | support native `WebSocket`. That typically means the last two major versions
|
23 | of 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 |
|
30 | To 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,
|
69 | even when run globally._
|
70 |
|
71 | ## Webpack Config `serve` Property
|
72 |
|
73 | `webpack-serve` supports the `serve` property in your webpack config file, which
|
74 | may contain any of the supported [options](#Options).
|
75 |
|
76 | ## API
|
77 |
|
78 | When using the API directly, the main entry point is the `serve` function, which
|
79 | is the default export of the module.
|
80 |
|
81 | ```js
|
82 | const serve = require('webpack-serve');
|
83 | const config = require('./webpack.config.js');
|
84 |
|
85 | serve({ config });
|
86 | ```
|
87 |
|
88 | ### serve([options])
|
89 |
|
90 | Returns 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 |
|
98 | Type: `Object`
|
99 |
|
100 | Options for initializing and controlling the server provided.
|
101 |
|
102 | ##### compiler
|
103 |
|
104 | Type: `webpack`
|
105 | Default: `null`
|
106 |
|
107 | An instance of a `webpack` compiler. A passed compiler's config will take
|
108 | precedence over `config` passed in options.
|
109 |
|
110 | _Note: Any `serve` configuration must be removed from the webpack config used
|
111 | to create the compiler instance, before you attempt to create it, as it's not
|
112 | a valid webpack config property._
|
113 |
|
114 | ##### config
|
115 |
|
116 | Type: `Object`
|
117 | Default: `{}`
|
118 |
|
119 | An object containing the configuration for creating a new `webpack` compiler
|
120 | instance.
|
121 |
|
122 | ##### content
|
123 |
|
124 | Type: `String|[String]`
|
125 | Default: `[]`
|
126 |
|
127 | The path, or array of paths, from which content will be served.
|
128 |
|
129 | ##### dev
|
130 |
|
131 | Type: `Object`
|
132 | Default: `{ publicPath: '/' }`
|
133 |
|
134 | An object containing options for [webpack-dev-middleware][dev-ware].
|
135 |
|
136 | ##### host
|
137 |
|
138 | Type: `Object`
|
139 | Default: `'localhost'`
|
140 |
|
141 | Sets the host that the `WebSocket` server will listen on. If this doesn't match
|
142 | the host of the server the module is used with, the module will not function
|
143 | properly.
|
144 |
|
145 | ##### hot
|
146 |
|
147 | Type: `Object`
|
148 | Default: `{}`
|
149 |
|
150 | An object containing options for [webpack-hot-client][hot-client].
|
151 |
|
152 | ##### http2
|
153 |
|
154 | Type: `Boolean`
|
155 | Default: `false`
|
156 |
|
157 | If using Node v9 or greater, setting this option to `true` will enable HTTP2
|
158 | support.
|
159 |
|
160 | ##### https
|
161 |
|
162 | Type: `Object`
|
163 | Default: `null`
|
164 |
|
165 | Passing this option will instruct `webpack-serve` to create and serve the webpack
|
166 | bundle and accompanying content through a secure server. The object should
|
167 | contain 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 |
|
178 | See the [Node documentation][https-opts] for more information.
|
179 |
|
180 | ##### logLevel
|
181 |
|
182 | Type: `String`
|
183 | Default: `info`
|
184 |
|
185 | Instructs `webpack-serve` to output information to the console/terminal at levels
|
186 | higher 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 |
|
200 | Type: `Boolean`
|
201 | Default: `false`
|
202 |
|
203 | Instruct `webpack-serve` to prepend each line of log output with a `[HH:mm:ss]`
|
204 | timestamp.
|
205 |
|
206 | ##### open
|
207 |
|
208 | Type: `Boolean|Object`
|
209 | Default: `false`
|
210 |
|
211 | Instruct the module to open the served bundle in a browser. Accepts an `Object`
|
212 | that 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 |
|
223 | Type: `Number`
|
224 | Default: `8080`
|
225 |
|
226 | The 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
|
233 | const serve = require('webpack-serve');
|
234 | const config = require('./webpack.config.js');
|
235 |
|
236 | serve({ config });
|
237 |
|
238 | serve.on('listening', () => {
|
239 | console.log('happy fun time');
|
240 | });
|
241 | ```
|
242 |
|
243 | #### listening
|
244 |
|
245 | Arguments: _None_
|
246 |
|
247 | ## Add-on Features
|
248 |
|
249 | A core tenant of `webpack-serve` is to stay lean in terms of feature set, and to
|
250 | empower users with familiar and easily portable patterns to implement the same
|
251 | features that those familiar with `webpack-dev-server` have come to rely on. This
|
252 | makes the module far easier to maintain, which ultimately benefits the user.
|
253 |
|
254 | Luckily, flexibility baked into `webpack-serve` makes it a snap to add-on features.
|
255 | Listed 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 |
|
267 | We 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
|