@@ -477,6 +477,58 @@ instance.waitUntilValid(() => {
477477});
478478```
479479
480+ ### ` plugin(compiler, options) `
481+
482+ Creates middleware instance in plugin mode.
483+
484+ In plugin mode, stats output is written through custom code (i.e. in callback for ` watch ` or where you are calling ` stats.toString(options) ` ) instead of ` console.log ` .
485+ In this case, the ` stats ` option is not supported because ` webpack-dev-middleware ` does not have access to the code where the stats will be output.
486+ You will also need to manually run the ` watch ` method.
487+
488+ Why do you need this mode? In some cases, you may want to have multiple dev servers or run only one dev server when you have multiple configurations, and this is suitable for you.
489+
490+ ``` js
491+ const webpack = require (" webpack" );
492+ const middleware = require (" webpack-dev-middleware" );
493+
494+ const compiler = webpack ({
495+ plugins: [
496+ {
497+ apply (compiler ) {
498+ const devMiddleware = middleware (
499+ compiler,
500+ {
501+ /* webpack-dev-middleware options */
502+ },
503+ true ,
504+ );
505+ },
506+ },
507+ ],
508+ /* Webpack configuration */
509+ });
510+
511+ compiler .watch ((err , stats ) => {
512+ if (err) {
513+ console .error (err);
514+ return ;
515+ }
516+
517+ console .log (stats .toString ());
518+ });
519+ ```
520+
521+ ### Plugin wrappers
522+
523+ The following wrappers enable plugin mode for framework integrations:
524+
525+ - ` middleware(compiler, options, true) ` (connect/express like middleware)
526+ - ` middleware.koaWrapper(compiler, options, true) `
527+ - ` middleware.hapiWrapper(true) `
528+ - ` middleware.honoWrapper(compiler, options, true) `
529+
530+ They are equivalent to ` koaWrapper ` /` hapiWrapper ` /` honoWrapper ` , but use plugin mode logging behavior.
531+
480532### ` forwardError `
481533
482534Type: ` boolean `
@@ -722,6 +774,8 @@ const devMiddlewareOptions = {
722774const app = new Koa ();
723775
724776app .use (middleware .koaWrapper (compiler, devMiddlewareOptions));
777+ // Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
778+ // app.use(middleware.koaWrapper(compiler, devMiddlewareOptions, true));
725779
726780app .listen (3000 );
727781```
@@ -740,14 +794,24 @@ const devMiddlewareOptions = {};
740794const server = Hapi .server ({ port: 3000 , host: " localhost" });
741795
742796await server .register ({
743- plugin: devMiddleware .hapiPlugin (),
797+ plugin: devMiddleware .hapiWrapper (),
744798 options: {
745799 // The `compiler` option is required
746800 compiler,
747801 ... devMiddlewareOptions,
748802 },
749803});
750804
805+ // Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
806+ // await server.register({
807+ // plugin: devMiddleware.hapiWrapper(true),
808+ // options: {
809+ // // The `compiler` option is required
810+ // compiler,
811+ // ...devMiddlewareOptions,
812+ // },
813+ // });
814+
751815await server .start ();
752816
753817console .log (" Server running on %s" , server .info .uri );
@@ -796,6 +860,9 @@ const app = new Hono();
796860
797861app .use (devMiddleware .honoWrapper (compiler, devMiddlewareOptions));
798862
863+ // Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
864+ // const honoDevMiddleware = devMiddleware.honoWrapper(compiler, devMiddlewareOptions, true)
865+
799866serve (app);
800867```
801868
0 commit comments