UNPKG

11.6 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-dev-middleware
15
16An express-style development middleware for use with [webpack](https://webpack.js.org)
17bundles and allows for serving of the files emitted from webpack.
18This should be used for **development only**.
19
20Some of the benefits of using this middleware include:
21
22- No files are written to disk, rather it handles files in memory
23- If files changed in watch mode, the middleware delays requests until compiling
24has completed.
25- Supports hot module reload (HMR).
26
27## Getting Started
28
29First thing's first, install the module:
30
31```console
32npm install webpack-dev-middleware --save-dev
33```
34
35_Note: We do not recommend installing this module globally._
36
37## Requirements
38
39`webpack-dev-middleware` requires Node v6 or higher, and must be used with a
40server that accepts express-style middleware.
41
42## Usage
43
44```js
45const webpack = require('webpack');
46const middleware = require('webpack-dev-middleware');
47const compiler = webpack({ .. webpack options .. });
48const express = require('express');
49const app = express();
50
51app.use(middleware(compiler, {
52 // webpack-dev-middleware options
53}));
54
55app.listen(3000, () => console.log('Example app listening on port 3000!'))
56```
57
58## Options
59
60The middleware accepts an `options` Object. The following is a property reference
61for the Object.
62
63_Note: The `publicPath` property is required, whereas all other options are optional_
64
65### headers
66
67Type: `Object`
68Default: `undefined`
69
70This property allows a user to pass custom HTTP headers on each request. eg.
71`{ "X-Custom-Header": "yes" }`
72
73index
74
75Type: `String`
76Default: `undefined`
77
78"index.html",
79// The index path for web server, defaults to "index.html".
80// If falsy (but not undefined), the server will not respond to requests to the root URL.
81
82
83### lazy
84
85Type: `Boolean`
86Default: `undefined`
87
88This option instructs the module to operate in 'lazy' mode, meaning that it won't
89recompile when files change, but rather on each request.
90
91### logger
92
93Type: `Object`
94Default: [`log`](/webpack/webpack-dev-middleware/blob/master/lib/log.js)
95
96In the rare event that a user would like to provide a custom logging interface,
97this property allows the user to assign one. The module leverages
98[`loglevel`](https://github.com/pimterry/loglevel#documentation)
99for logging management by default, and any custom logger must adhere to the same
100exports for compatibility. Specifically, all custom loggers must have the
101following exported methods at a minimum:
102
103- `log.trace`
104- `log.debug`
105- `log.info`
106- `log.warn`
107- `log.error`
108
109Please see the documentation for `loglevel` for more information.
110
111### logLevel
112
113Type: `String`
114Default: `'info'`
115
116This property defines the level of messages that the module will log. Valid levels
117include:
118
119- `trace`
120- `debug`
121- `info`
122- `warn`
123- `error`
124- `silent`
125
126Setting a log level means that all other levels below it will be visible in the
127console. Setting `logLevel: 'silent'` will hide all console output. The module
128leverages [`loglevel`](https://github.com/pimterry/loglevel#documentation)
129for logging management, and more information can be found on its page.
130
131## logTime
132
133Type: `Boolean`
134Default: `false`
135
136If `true` the log output of the module will be prefixed by a timestamp in the
137`HH:mm:ss` format.
138
139### mimeTypes
140
141Type: `Object`
142Default: `null`
143
144This property allows a user to register custom mime types or extension mappings.
145eg. `{ 'text/html': [ 'phtml' ] }`. Please see the documentation for
146[`node-mime`](https://github.com/broofa/node-mime#mimedefine) for more information.
147
148### publicPath
149
150Type: `String`
151_Required_
152
153The public path that the middleware is bound to. _Best Prectice: use the same
154`publicPath` defined in your webpack config._
155
156### reporter
157
158Type: `Object`
159Default: `undefined`
160
161Allows users to provide a custom reporter to handle logging within the module.
162Please see the [default reporter](/webpack/webpack-dev-middleware/blob/master/lib/reporter.js)
163for an example.
164
165### serverSideRender
166
167Type: `Boolean`
168Default: `undefined`
169
170Instructs the module to enable or disable the server-side rendering mode. Please
171see [Server-Side Rendering](#server-side-rendering) for more information.
172
173### stats
174Type: `Object`
175Default: `{ context: process.cwd() }`
176
177Options for formatting statistics displayed during and after compile. For more
178information and property details, please see the
179[webpack documentation](https://webpack.js.org/configuration/stats/#stats).
180
181### watchOptions
182
183Type: `Object`
184Default: `{ aggregateTimeout: 200 }``
185
186The module accepts an `Object` containing options for file watching, which is
187passed directly to the compiler provided. For more information on watch options
188please see the [webpack documentation](https://webpack.js.org/configuration/watch/#watchoptions)
189
190## API
191
192`webpack-dev-middleware` also provides convenience methods that can be use to
193interact with the middleware at runtime:
194
195### `close(callback)`
196
197Instructs a webpack-dev-middleware instance to stop watching for file changes.
198
199### Parameters
200
201#### callback
202
203Type: `Function`
204
205A function executed once the middleware has stopped watching.
206
207### `invalidate()`
208
209Instructs a webpack-dev-middleware instance to recompile the bundle.
210e.g. after a change to the configuration.
211
212```js
213const webpack = require('webpack');
214const compiler = webpack({ ... });
215const middlware = require('webpack-dev-middleware');
216const instance = middleware(compiler);
217
218app.use(instance);
219
220setTimeout(() => {
221 // After a short delay the configuration is changed and a banner plugin is added
222 // to the config
223 compiler.apply(new webpack.BannerPlugin('A new banner'));
224
225 // Recompile the bundle with the banner plugin:
226 instance.invalidate();
227}, 1000);
228```
229
230### `waitUntilValid(callback)`
231
232Executes a callback function when the compiler bundle is valid, typically after
233compilation.
234
235### Parameters
236
237#### callback
238
239Type: `Function`
240
241A function executed when the bundle becomes valid. If the bundle is
242valid at the time of calling, the callback is executed immediately.
243
244```js
245const webpack = require('webpack');
246const compiler = webpack({ ... });
247const middlware = require('webpack-dev-middleware');
248const instance = middleware(compiler);
249
250app.use(instance);
251
252instance.waitUntilValid(() => {
253 console.log('Package is in a valid state');
254});
255```
256
257## Server-Side Rendering
258
259_Note: this feature is experimental and may be removed or changed completely in the future._
260
261In order to develop an app using server-side rendering, we need access to the
262[`stats`](https://github.com/webpack/docs/wiki/node.js-api#stats), which is
263generated with each build.
264
265With server-side rendering enabled, `webpack-dev-middleware` sets the `stat` to
266`res.locals.webpackStats` before invoking the next middleware, allowing a
267developer to render the page body and manage the response to clients.
268
269_Note: Requests for bundle files will still be handled by
270`webpack-dev-middleware` and all requests will be pending until the build
271process is finished with server-side rendering enabled._
272
273Example Implementation:
274
275```js
276const webpack = require('webpack');
277const compiler = webpack({ ... });
278const middlware = require('webpack-dev-middleware');
279
280// This function makes server rendering of asset references consistent with different webpack chunk/entry configurations
281function normalizeAssets(assets) {
282 return Array.isArray(assets) ? assets : [assets]
283}
284
285app.use(middleware(compiler, { serverSideRender: true })
286
287// The following middleware would not be invoked until the latest build is finished.
288app.use((req, res) => {
289 const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
290
291 // then use `assetsByChunkName` for server-sider rendering
292 // For example, if you have only one main chunk:
293 res.send(`
294<html>
295 <head>
296 <title>My App</title>
297 ${normalizeAssets(assetsByChunkName.main)
298 .filter(path => path.endsWith('.css'))
299 .map(path => `<link rel="stylesheet" href="${path}" />`)
300 .join('\n')}
301 </head>
302 <body>
303 <div id="root"></div>
304 ${normalizeAssets(assetsByChunkName.main)
305 .filter(path => path.endsWith('.js'))
306 .map(path => `<script src="${path}"></script>`)
307 .join('\n')}
308 </body>
309</html>
310 `)
311
312})
313```
314
315## Support
316
317We do our best to keep Issues in the repository focused on bugs, features, and
318needed modifications to the code for the module. Because of that, we ask users
319with general support, "how-to", or "why isn't this working" questions to try one
320of the other support channels that are available.
321
322Your first-stop-shop for support for webpack-dev-server should by the excellent
323[documentation][docs-url] for the module. If you see an opportunity for improvement
324of those docs, please head over to the [webpack.js.org repo][wjo-url] and open a
325pull request.
326
327From there, we encourage users to visit the [webpack Gitter chat][chat-url] and
328talk to the fine folks there. If your quest for answers comes up dry in chat,
329head over to [StackOverflow][stack-url] and do a quick search or open a new
330question. Remember; It's always much easier to answer questions that include your
331`webpack.config.js` and relevant files!
332
333If you're twitter-savvy you can tweet [#webpack][hash-url] with your question
334and someone should be able to reach out and lend a hand.
335
336If you have discovered a :bug:, have a feature suggestion, of would like to see
337a modification, please feel free to create an issue on Github. _Note: The issue
338template isn't optional, so please be sure not to remove it, and please fill it
339out completely._
340
341## Contributing
342
343We welcome your contributions! Please have a read of [CONTRIBUTING.md](CONTRIBUTING.md) for more information on how to get involved.
344
345## Maintainers
346
347<table>
348 <tbody>
349 <tr>
350 <td align="center">
351 <img src="https://avatars.githubusercontent.com/SpaceK33z?v=4&s=150">
352 <br />
353 <a href="https://github.com/SpaceK33z">Kees Kluskens</a>
354 </td>
355 <td align="center">
356 <img src="https://i.imgur.com/4v6pgxh.png">
357 <br />
358 <a href="https://github.com/shellscape">Andrew Powell</a>
359 </td>
360 </tr>
361 </tbody>
362</table>
363
364## License
365
366#### [MIT](./LICENSE)
367
368[npm]: https://img.shields.io/npm/v/webpack-dev-middleware.svg
369[npm-url]: https://npmjs.com/package/webpack-dev-middleware
370
371[node]: https://img.shields.io/node/v/webpack-dev-middleware.svg
372[node-url]: https://nodejs.org
373
374[deps]: https://david-dm.org/webpack/webpack-dev-middleware.svg
375[deps-url]: https://david-dm.org/webpack/webpack-dev-middleware
376
377[tests]: http://img.shields.io/travis/webpack/webpack-dev-middleware.svg
378[tests-url]: https://travis-ci.org/webpack/webpack-dev-middleware
379
380[cover]: https://codecov.io/gh/webpack/webpack-dev-middleware/branch/master/graph/badge.svg
381[cover-url]: https://codecov.io/gh/webpack/webpack-dev-middleware
382
383[chat]: https://badges.gitter.im/webpack/webpack.svg
384[chat-url]: https://gitter.im/webpack/webpack
385
386[docs-url]: https://webpack.js.org/configuration/dev-middleware/#devmiddleware
387[hash-url]: https://twitter.com/search?q=webpack
388[middleware-url]: https://github.com/webpack/webpack-dev-middleware
389[stack-url]: https://stackoverflow.com/questions/tagged/webpack-dev-middleware
390[uglify-url]: https://github.com/webpack-contrib/uglifyjs-webpack-plugin
391[wjo-url]: https://github.com/webpack/webpack.js.org