Express-style development middleware for webpack that serves files from memory and handles compilation in watch mode
npx @tessl/cli install tessl/npm-webpack-dev-middleware@7.4.0webpack-dev-middleware is an Express-style development middleware for webpack that serves files directly from memory without writing to disk. It enables development workflows by handling webpack bundles in memory, automatically delaying requests until compilation completes when files change in watch mode, and supporting hot module reload (HMR).
npm install webpack-dev-middleware --save-devconst webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");For ES modules:
import webpack from "webpack";
import webpackDevMiddleware from "webpack-dev-middleware";const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const express = require("express");
// Create webpack compiler
const compiler = webpack({
// webpack configuration
entry: "./src/index.js",
output: {
path: "/",
filename: "bundle.js",
},
});
// Create Express app and use middleware
const app = express();
app.use(webpackDevMiddleware(compiler, {
publicPath: "/",
stats: "minimal",
}));
app.listen(3000);webpack-dev-middleware is built around several key components:
Primary middleware functionality for serving webpack bundles from memory with Express-style interface.
function webpackDevMiddleware<RequestInternal, ResponseInternal>(
compiler: Compiler | MultiCompiler,
options?: Options<RequestInternal, ResponseInternal>
): API<RequestInternal, ResponseInternal>;
interface API<RequestInternal, ResponseInternal> extends Middleware<RequestInternal, ResponseInternal> {
getFilenameFromUrl(url: string, extra?: Extra): string | undefined;
waitUntilValid(callback?: Callback): void;
invalidate(callback?: Callback): void;
close(callback?: (err: Error | null | undefined) => void): void;
context: Context<RequestInternal, ResponseInternal>;
}
type Middleware<RequestInternal, ResponseInternal> = (
req: RequestInternal,
res: ResponseInternal,
next: NextFunction
) => Promise<void>;Framework-specific middleware adapters for Hapi.js, Koa.js, and Hono that provide native integration patterns.
// Hapi.js plugin wrapper
function hapiWrapper<HapiServer, HapiOptionsInternal>(): HapiPlugin<HapiServer, HapiOptionsInternal>;
// Koa.js middleware wrapper
function koaWrapper<RequestInternal, ResponseInternal>(
compiler: Compiler | MultiCompiler,
options?: Options<RequestInternal, ResponseInternal>
): (ctx: any, next: Function) => Promise<void> | void;
// Hono middleware wrapper
function honoWrapper<RequestInternal, ResponseInternal>(
compiler: Compiler | MultiCompiler,
options?: Options<RequestInternal, ResponseInternal>
): (ctx: any, next: Function) => Promise<void> | void;Comprehensive configuration system for controlling file serving, caching, HTTP headers, and development features.
interface Options<RequestInternal, ResponseInternal> {
mimeTypes?: { [key: string]: string };
mimeTypeDefault?: string;
writeToDisk?: boolean | ((targetPath: string) => boolean);
methods?: string[];
headers?: Headers<RequestInternal, ResponseInternal>;
publicPath?: string | Function;
stats?: Configuration["stats"];
serverSideRender?: boolean;
outputFileSystem?: OutputFileSystem;
index?: boolean | string;
modifyResponseData?: ModifyResponseData<RequestInternal, ResponseInternal>;
etag?: "weak" | "strong";
lastModified?: boolean;
cacheControl?: boolean | number | string | CacheControlObject;
cacheImmutable?: boolean;
}