Koa Webpack Dev Middleware

2.0.2 · maintenance · verified Wed Apr 22

This package provides a middleware to integrate `webpack-dev-middleware` within Koa 2.x applications, enabling the serving of webpack-bundled assets directly from memory during development. This avoids disk writes and speeds up the development cycle, often paired with `webpack-hot-middleware` for Hot Module Replacement. The current stable version, 2.0.2, appears to be designed for the Koa 2.x ecosystem and Node.js versions 7.6.0 and above. Its release cadence seems to be inactive, with no recent updates catering to newer Koa versions (e.g., Koa 3+) or the latest webpack iterations. As a result, it is primarily relevant for maintaining legacy Koa 2.x projects. Key differentiators include its direct API compatibility with Koa's middleware signature, abstracting the underlying `webpack-dev-middleware` for Koa users.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a basic Koa 2.x application with `koa-webpack-dev-middleware` to serve webpack-bundled JavaScript files during development, illustrating how to pass a webpack compiler instance and configure middleware options.

const Koa = require('koa');
const webpack = require('webpack');
const webpackMiddleware = require('koa-webpack-dev-middleware');

const app = new Koa();

const webpackConfig = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        path: '/', // Required for webpack-dev-middleware, no real path needed
        publicPath: '/assets/'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: { presets: ['@babel/preset-env'] }
                }
            }
        ]
    }
};

const compiler = webpack(webpackConfig);

app.use(webpackMiddleware(compiler, {
    noInfo: false,
    quiet: false,
    lazy: false,
    watchDelay: 300,
    publicPath: '/assets/', // Must match webpackConfig.output.publicPath
    headers: { 'X-Custom-Header': 'yes' },
    stats: { colors: true }
}));

// Add a simple route to verify the server is running
app.use(async ctx => {
    if (ctx.path === '/') {
        ctx.body = '<html><body><h1>Koa Webpack Dev Middleware Example</h1><script src="/assets/main.js"></script></body></html>';
    }
});

app.listen(3000, () => {
    console.log('Koa server with webpack dev middleware listening on port 3000');
    console.log('Access: http://localhost:3000');
});

// Create a dummy src/index.js if it doesn't exist
// In a real project, this would be your main entry point.
// For demonstration purposes, we ensure the file exists.
const fs = require('fs');
const path = require('path');
const entryPath = path.resolve(__dirname, 'src/index.js');
if (!fs.existsSync(path.dirname(entryPath))) {
    fs.mkdirSync(path.dirname(entryPath), { recursive: true });
}
if (!fs.existsSync(entryPath)) {
    fs.writeFileSync(entryPath, 'console.log("Hello from webpack-bundled client code!");');
}

view raw JSON →