A development middleware for webpack
[![npm][npm]][npm-url] [![node][node]][node-url] [![tests][tests]][tests-url] [![coverage][cover]][cover-url] [![discussion][discussion]][discussion-url] [![size][size]][size-url]
An express-style development middleware for use with webpack bundles and allows for serving of the files emitted from webpack. This should be used for development only.
Some of the benefits of using this middleware include:
First thing's first, install the module:
npm install webpack-dev-middleware --save-dev[!WARNING]
We do not recommend installing this module globally.
const express = require("express");
const webpack = require("webpack");
const middleware = require("webpack-dev-middleware");
const compiler = webpack({
// webpack options
});
const app = express();
app.use(
middleware(compiler, {
// webpack-dev-middleware options
}),
);
app.listen(3000, () => console.log("Example app listening on port 3000!"));See below for an example of use with fastify.
| Name | Type | Default | Description |
|---|---|---|---|
methods |
Array |
[ 'GET', 'HEAD' ] |
Allows to pass the list of HTTP request methods accepted by the middleware |
headers |
Array|Object|Function |
undefined |
Allows to pass custom HTTP headers on each request. |
index |
boolean|string |
index.html |
If false (but not undefined), the server will not respond to requests to the root URL. |
mimeTypes |
Object |
undefined |
Allows to register custom mime types or extension mappings. |
mimeTypeDefault |
string |
undefined |
Allows to register a default mime type when we can't determine the content type. |
etag |
boolean| "weak"| "strong" |
undefined |
Enable or disable etag generation. |
lastModified |
boolean |
undefined |
Enable or disable Last-Modified header. Uses the file system's last modified value. |
cacheControl |
boolean|number|string|Object |
undefined |
Enable or disable setting Cache-Control response header. |
cacheImmutable |
boolean |
undefined |
Enable or disable setting Cache-Control: public, max-age=31536000, immutable response header for immutable assets. |
publicPath |
string |
undefined |
The public path that the middleware is bound to. |
stats |
boolean|string|Object |
stats (from a configuration) |
Stats options object or preset name. |
serverSideRender |
boolean |
undefined |
Instructs the module to enable or disable the server-side rendering mode. |
writeToDisk |
boolean|Function |
false |
Instructs the module to write files to the configured location on disk as specified in your webpack configuration. |
outputFileSystem |
Object |
memfs |
Set the default file system which will be used by webpack as primary destination of generated files. |
modifyResponseData |
Function |
undefined |
Allows to set up a callback to change the response data. |
hot |
boolean|Object |
false |
Enables a Server-Sent Events endpoint that drives the browser HMR client. |
forwardError |
boolean |
false |
Enable or disable forwarding errors to the next middleware. |
The middleware accepts an options Object. The following is a property reference for the Object.
Type: Array
Default: [ 'GET', 'HEAD' ]
This property allows a user to pass the list of HTTP request methods accepted by the middleware**.
Type: Array|Object|Function
Default: undefined
This property allows a user to pass custom HTTP headers on each request.
eg. { "X-Custom-Header": "yes" }
or
webpackDevMiddleware(compiler, {
headers: () => ({
"Last-Modified": new Date(),
}),
});or
webpackDevMiddleware(compiler, {
headers: (req, res, context) => {
res.setHeader("Last-Modified", new Date());
},
});or
webpackDevMiddleware(compiler, {
headers: [
{
key: "X-custom-header",
value: "foo",
},
{
key: "Y-custom-header",
value: "bar",
},
],
});or
webpackDevMiddleware(compiler, {
headers: () => [
{
key: "X-custom-header",
value: "foo",
},
{
key: "Y-custom-header",
value: "bar",
},
],
});Type: Boolean|String
Default: index.html
If false (but not undefined), the server will not respond to requests to the root URL.
Type: Object
Default: undefined
This property allows a user to register custom mime types or extension mappings.
eg. mimeTypes: { phtml: 'text/html' }.
Please see the documentation for mime-types for more information.
Type: String
Default: undefined
This property allows a user to register a default mime type when we can't determine the content type.
Type: "weak" | "strong"
Default: undefined
Enable or disable etag generation. Boolean value use
Type: Boolean
Default: undefined
Enable or disable Last-Modified header. Uses the file system's last modified value.
Type: Boolean | Number | String | { maxAge?: number, immutable?: boolean }
Default: undefined
Depending on the setting, the following headers will be generated:
Boolean - Cache-Control: public, max-age=31536000000Number - Cache-Control: public, max-age=YOUR_NUMBERString - Cache-Control: YOUR_STRING{ maxAge?: number, immutable?: boolean } - Cache-Control: public, max-age=YOUR_MAX_AGE_or_31536000000, also , immutable can be added if you set the immutable option to trueEnable or disable setting Cache-Control response header.
Type: Boolean
Default: undefined
Enable or disable setting Cache-Control: public, max-age=31536000, immutable response header for immutable assets (i.e. asset with a hash like image.a4c12bde.jpg).
Immutable assets are assets that have their hash in the file name therefore they can be cached, because if you change their contents the file name will be changed.
Take preference over the cacheControl option if the asset was defined as immutable.
Type: String
Default: output.publicPath (from a configuration)
The public path that the middleware is bound to.
Best Practice: use the same publicPath defined in your webpack config. For more information about publicPath, please see the webpack documentation.
Type: Boolean|String|Object
Default: stats (from a configuration)
Stats options object or preset name.
Type: Boolean
Default: undefined
Instructs the module to enable or disable the server-side rendering mode. Please see Server-Side Rendering for more information.
Type: Boolean|Function
Default: false
If true, the option will instruct the module to write files to the configured location on disk as specified in your webpack config file.
Setting writeToDisk: true won't change the behavior of the webpack-dev-middleware, and bundle files accessed through the browser will still be served from memory.
This option provides the same capabilities as the WriteFilePlugin.
This option also accepts a Function value, which can be used to filter which files are written to disk.
The function follows the same premise as Array#filter in which a return value of false will not write the file, and a return value of true will write the file to disk. eg.
const webpack = require("webpack");
const configuration = {/* Webpack configuration */};
const compiler = webpack(configuration);
middleware(compiler, {
writeToDisk: (filePath) => /superman\.css$/.test(filePath),
});Type: Object
Default: memfs
Set the default file system which will be used by webpack as primary destination of generated files. This option isn't affected by the writeToDisk option.
You have to provide .join() and mkdirp method to the outputFileSystem instance manually for compatibility with webpack@4.
This can be done simply by using path.join:
const path = require("node:path");
const mkdirp = require("mkdirp");
const myOutputFileSystem = require("my-fs");
const webpack = require("webpack");
myOutputFileSystem.join = path.join.bind(path); // no need to bind
myOutputFileSystem.mkdirp = mkdirp.bind(mkdirp); // no need to bind
const compiler = webpack({/* Webpack configuration */});
middleware(compiler, { outputFileSystem: myOutputFileSystem });Allows to set up a callback to change the response data.
const webpack = require("webpack");
const configuration = {/* Webpack configurationNo open issues yet, or sync has not completed.