rspack-dev-middleware

raw JSON →
0.4.0 verified Fri May 01 auth: no javascript

A Koa 2 middleware for Rspack development and hot reloading. Current stable version 0.4.0, released under Apache-2.0 license with weekly releases. Supports ESM-only with TypeScript types. Key differentiators: designed specifically for Koa 2, integrates with memfs for in-memory file systems, and provides built-in hot reloading without Webpack dependencies. Requires Node.js >=18 and peer deps @rspack/core >=1.0.7 and koa >=2.0.0. Tree-shakable with no side effects.

error ERR_REQUIRE_ESM: require() of ES Module not supported
cause Using CommonJS require() on an ESM-only package.
fix
Change to dynamic import or set type: 'module' in package.json.
error TypeError: compiler is not an object
cause Passing an array of compilers instead of a single compiler instance.
fix
Pass a single compiler object, e.g., rspack({...}).
error Error: Cannot find module 'koa'
cause Missing peer dependency koa.
fix
Install koa: npm install koa.
breaking Package is ESM-only and cannot be required via CommonJS require().
fix Use import syntax or dynamic import().
deprecated Option `writeToDisk` is removed; use `fs` to customize output instead.
fix Pass a custom `fs` object (e.g., memfs) to middleware options.
gotcha Passing an array of compilers is not supported; use a single compiler instance.
fix Wrap multiple compilers in a multi-compiler if needed, but note that rspack-dev-middleware only accepts a single compiler.
gotcha Middleware must be awaited; `app.use(dev(...))` without await may cause errors.
fix Use `app.use(await dev(compiler, options))`.
npm install rspack-dev-middleware
yarn add rspack-dev-middleware
pnpm add rspack-dev-middleware

Sets up a Koa server with Rspack dev middleware, using memfs for in-memory file output and hot reloading.

import Koa from 'koa';
import rspack from '@rspack/core';
import { Volume, createFsFromVolume } from 'memfs';
import { server as dev } from 'rspack-dev-middleware';

const compiler = rspack({
  mode: 'development',
  entry: './src/index.tsx',
  output: { path: '/dist' },
});

const app = new Koa();
const fs = createFsFromVolume(new Volume());

app.use(await dev(compiler, { fs, headers: { 'Cache-Control': 'no-cache' } }));

app.listen(3000);