webpack-dev-middleware
Last updated
Last updated
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:
No files are written to disk, rather it handles files in memory
If files changed in watch mode, the middleware delays requests until compiling has completed.
Supports hot module reload (HMR).
This module requires a minimum of Node v6.9.0 and Webpack v4.0.0, and must be used with a server that accepts express-style middleware.
First thing's first, install the module:
Note: We do not recommend installing this module globally.
The middleware accepts an options
Object. The following is a property reference for the Object.
Note: The publicPath
property is required, whereas all other options are optional
Type: Array
Default: [ 'GET', 'HEAD' ]
This property allows a user to pass the list of HTTP request methods accepted by the server.
Type: Object
Default: undefined
This property allows a user to pass custom HTTP headers on each request. eg. { "X-Custom-Header": "yes" }
Type: String
Default: undefined
"index.html", // The index path for web server, defaults to "index.html". // If falsy (but not undefined), the server will not respond to requests to the root URL.
Type: Boolean
Default: undefined
This option instructs the module to operate in 'lazy' mode, meaning that it won't recompile when files change, but rather on each request.
Type: Object
Default: webpack-log
In the rare event that a user would like to provide a custom logging interface, this property allows the user to assign one. The module leverages webpack-log
for creating the loglevelnext
logging management by default. Any custom logger must adhere to the same exports for compatibility. Specifically, all custom loggers must have the following exported methods at a minimum:
log.trace
log.debug
log.info
log.warn
log.error
Please see the documentation for loglevel
for more information.
Type: String
Default: 'info'
This property defines the level of messages that the module will log. Valid levels include:
trace
debug
info
warn
error
silent
Setting a log level means that all other levels below it will be visible in the console. Setting logLevel: 'silent'
will hide all console output. The module leverages webpack-log
for logging management, and more information can be found on its page.
Type: Boolean
Default: false
If true
the log output of the module will be prefixed by a timestamp in the HH:mm:ss
format.
Type: Object
Default: null
This property allows a user to register custom mime types or extension mappings. eg. mimeTypes: { 'text/html': [ 'phtml' ] }
.
By default node-mime will throw an error if you try to map a type to an extension that is already assigned to another type. Passing force: true
will suppress this behavior (overriding any previous mapping). eg. mimeTypes: { typeMap: { 'text/html': [ 'phtml' ] } }, force: true }
.
Please see the documentation for node-mime
for more information.
Type: String
Required
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: Object
Default: undefined
Allows users to provide a custom reporter to handle logging within the module. Please see the default reporter for an example.
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: Object
Default: { context: process.cwd() }
Options for formatting statistics displayed during and after compile. For more information and property details, please see the webpack documentation.
Type: Object
Default: { aggregateTimeout: 200 }
The module accepts an Object
containing options for file watching, which is passed directly to the compiler provided. For more information on watch options please see the webpack documentation
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.
Type: Object
Default: MemoryFileSystem
Set the default file system which will be used by webpack as primary destination of generated files. Default is set to webpack's default file system: memory-fs. This option isn't affected by the writeToDisk option.
Note: As of 3.5.x version of the middleware you have to provide .join()
method to the fs
instance manually. This can be done simply by using path.join
:
webpack-dev-middleware
also provides convenience methods that can be use to interact with the middleware at runtime:
close(callback)
Instructs a webpack-dev-middleware instance to stop watching for file changes.
callback
Type: Function
A function executed once the middleware has stopped watching.
invalidate()
Instructs a webpack-dev-middleware instance to recompile the bundle. e.g. after a change to the configuration.
waitUntilValid(callback)
Executes a callback function when the compiler bundle is valid, typically after compilation.
callback
Type: Function
A function executed when the bundle becomes valid. If the bundle is valid at the time of calling, the callback is executed immediately.
Watching (by means of lazy: false
) will frequently cause multiple compilations as the bundle changes during compilation. This is due in part to cross-platform differences in file watchers, so that webpack doesn't loose file changes when watched files change rapidly. If you run into this situation, please make use of the TimeFixPlugin
.
Note: this feature is experimental and may be removed or changed completely in the future.
In order to develop an app using server-side rendering, we need access to the stats
, which is generated with each build.
With server-side rendering enabled, webpack-dev-middleware
sets the stat
to res.locals.webpackStats
and the memory filesystem to res.locals.fs
before invoking the next middleware, allowing a developer to render the page body and manage the response to clients.
Note: Requests for bundle files will still be handled by webpack-dev-middleware
and all requests will be pending until the build process is finished with server-side rendering enabled.
Example Implementation:
We do our best to keep Issues in the repository focused on bugs, features, and needed modifications to the code for the module. Because of that, we ask users with general support, "how-to", or "why isn't this working" questions to try one of the other support channels that are available.
Your first-stop-shop for support for webpack-dev-server should by the excellent documentation for the module. If you see an opportunity for improvement of those docs, please head over to the webpack.js.org repo and open a pull request.
From there, we encourage users to visit the webpack Gitter chat and talk to the fine folks there. If your quest for answers comes up dry in chat, head over to StackOverflow and do a quick search or open a new question. Remember; It's always much easier to answer questions that include your webpack.config.js
and relevant files!
If you're twitter-savvy you can tweet #webpack with your question and someone should be able to reach out and lend a hand.
If you have discovered a 🐛, have a feature suggestion, or would like to see a modification, please feel free to create an issue on Github. Note: The issue template isn't optional, so please be sure not to remove it, and please fill it out completely.
Please take a moment to read our contributing guidelines if you haven't yet done so.