What is webpack-dev-middleware?
webpack-dev-middleware is a package that provides a simple way to serve and live reload webpack bundles for development purposes. It's designed to be used with a Node.js server, such as Express, and it allows developers to serve the webpack-generated files without writing them to disk, providing a faster development experience.
What are webpack-dev-middleware's main functionalities?
Serving Webpack Bundles
This code sample demonstrates how to set up webpack-dev-middleware with an Express server. It serves the files generated by webpack based on the provided configuration.
const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const config = require('./webpack.config.js');
const compiler = webpack(config);
const app = express();
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
app.listen(3000, function () {
console.log('Example app listening on port 3000!\n');
});
Enabling Hot Module Replacement (HMR)
This code sample shows how to enable Hot Module Replacement (HMR) in conjunction with webpack-dev-middleware. It requires an additional package, webpack-hot-middleware, to work.
const webpackHotMiddleware = require('webpack-hot-middleware');
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
Other packages similar to webpack-dev-middleware
browser-sync
BrowserSync is a package that allows for live reloading of web pages as files are edited and saved. It can be integrated with webpack using browser-sync-webpack-plugin. Unlike webpack-dev-middleware, BrowserSync is focused on synchronizing interactions across multiple devices/browsers during testing.
serve
Serve is a static file serving and directory listing package that can be used for quick prototyping and local development. It does not have built-in webpack integration or HMR, but it's a simple alternative for serving static files.
webpack-hot-server-middleware
webpack-hot-server-middleware is similar to webpack-dev-middleware but is specifically designed for use with server-side rendering in Node.js applications. It works in tandem with webpack-dev-middleware and webpack-hot-middleware to enable HMR for server-rendered apps.
webpack-serve
webpack-serve is a now-deprecated package that was once an alternative to webpack-dev-middleware. It provided a development server that used webpack's watch mode to observe file changes and recompile automatically. It has since been replaced by webpack-dev-server, which offers similar functionality.
webpack-dev-middleware
THIS MIDDLEWARE SHOULD ONLY USED FOR DEVELOPMENT!
DO NOT USE IT IN PRODUCTION!
What is it?
It's a simple wrapper middleware for webpack. It serves the files emitted from webpack over a connect server.
It has a few advantages over bundling it as files:
- No files are written to disk, it handle the files in memory
- If files changed in watch mode, the middleware no longer serves the old bundle, but delays requests until the compiling has finished. You don't have to wait before refreshing the page after a file modification.
- I may add some specifiy optimation in future releases.
Usage
var webpackMiddleware = require("webpack-dev-middleware");
app.use(webpackMiddleware(...));
Use the webpackMiddleware
function like the webpack function, but without callback.
Example usage:
app.use(webpackMiddleware(__dirname, "./lib/file", {
watch: true,
debug: true,
publicPrefix: "http://localhost:8080/assets/",
output: "bundle.js",
outputPostfix: ".bundle.js",
}));
options.watch = true
This is not required, but recommendated to get the best out of the middleware.
options.publicPrefix
The prefix for request that should be handled.
options.output, options.outputPostfix
The filename for the bundle.
options.verbose
Verbose output of the statistics.
All other options like webpack