Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,51 @@ instance.waitUntilValid(() => {
});
```

### `plugin(compiler, options)`

Creates middleware instance in plugin mode.

In plugin mode, stats output is written through custom code (i.e. in callback for `watch` or where you are calling `stats.toString(options)`)
(`compiler.getInfrastructureLogger`) instead of `console.log`.

```js
const webpack = require("webpack");
const middleware = require("webpack-dev-middleware");

const compiler = webpack({
plugins: [
{
apply(compiler) {
const devMiddleware = middleware.plugin(compiler, {
/* webpack-dev-middleware options */
});
},
},
],
/* Webpack configuration */
});

compiler.watch((err, stats) => {
if (err) {
console.error(err);
return;
}

console.log(stats.toString());
});
```

### Plugin wrappers

The following wrappers enable plugin mode for framework integrations:

- `middleware.koaPluginWrapper(compiler, options)`
- `middleware.hapiPluginWrapper()`
- `middleware.honoPluginWrapper(compiler, options)`

They are equivalent to `koaWrapper`/`hapiWrapper`/`honoWrapper`, but use plugin
mode logging behavior.

### `forwardError`

Type: `boolean`
Expand Down Expand Up @@ -722,6 +767,8 @@ const devMiddlewareOptions = {
const app = new Koa();

app.use(middleware.koaWrapper(compiler, devMiddlewareOptions));
// Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
// app.use(middleware.koaPluginWrapper(compiler, devMiddlewareOptions));

app.listen(3000);
```
Expand All @@ -740,14 +787,24 @@ const devMiddlewareOptions = {};
const server = Hapi.server({ port: 3000, host: "localhost" });

await server.register({
plugin: devMiddleware.hapiPlugin(),
plugin: devMiddleware.hapiWrapper(),
options: {
// The `compiler` option is required
compiler,
...devMiddlewareOptions,
},
});

// Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
// await server.register({
// plugin: devMiddleware.hapiPluginWrapper(),
// options: {
// // The `compiler` option is required
// compiler,
// ...devMiddlewareOptions,
// },
// });

await server.start();

console.log("Server running on %s", server.info.uri);
Expand Down Expand Up @@ -796,6 +853,9 @@ const app = new Hono();

app.use(devMiddleware.honoWrapper(compiler, devMiddlewareOptions));

// Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
// const honoDevMiddleware = devMiddleware.honoPluginWrapper(compiler, devMiddlewareOptions)

serve(app);
```

Expand Down
34 changes: 29 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading