@@ -90,6 +90,7 @@ const noop = () => {};
9090 * @property {Watching | MultiWatching | undefined } watching watching
9191 * @property {Logger } logger logger
9292 * @property {OutputFileSystem } outputFileSystem output file system
93+ * @property {boolean } isPlugin whether wdm is used as webpack plugin
9394 */
9495
9596/**
@@ -225,6 +226,7 @@ function wdm(compiler, options = {}) {
225226 options,
226227 compiler,
227228 logger : compiler . getInfrastructureLogger ( "webpack-dev-middleware" ) ,
229+ isPlugin : false ,
228230 } ;
229231
230232 setupHooks ( context ) ;
@@ -319,9 +321,10 @@ function wdm(compiler, options = {}) {
319321/**
320322 * @template HapiServer
321323 * @template {HapiOptions} HapiOptionsInternal
324+ * @param {boolean } usePlugin whether to use as webpack plugin
322325 * @returns {HapiPlugin<HapiServer, HapiOptionsInternal> } hapi wrapper
323326 */
324- function hapiWrapper ( ) {
327+ function createHapiWrapper ( usePlugin = false ) {
325328 return {
326329 pkg : {
327330 name : "webpack-dev-middleware" ,
@@ -337,6 +340,11 @@ function hapiWrapper() {
337340
338341 const devMiddleware = wdm ( compiler , rest ) ;
339342
343+ if ( usePlugin ) {
344+ // Use logger when used as webpack plugin
345+ devMiddleware . context . isPlugin = true ;
346+ }
347+
340348 // @ts -expect-error
341349 if ( ! server . decorations . server . includes ( "webpackDevMiddleware" ) ) {
342350 // @ts -expect-error
@@ -387,18 +395,42 @@ function hapiWrapper() {
387395 } ;
388396}
389397
398+ /**
399+ * @template HapiServer
400+ * @template {HapiOptions} HapiOptionsInternal
401+ * @returns {HapiPlugin<HapiServer, HapiOptionsInternal> } hapi wrapper
402+ */
403+ function hapiWrapper ( ) {
404+ return createHapiWrapper ( false ) ;
405+ }
406+
407+ /**
408+ * @template HapiServer
409+ * @template {HapiOptions} HapiOptionsInternal
410+ * @returns {HapiPlugin<HapiServer, HapiOptionsInternal> } hapi plugin wrapper
411+ */
412+ function hapiPluginWrapper ( ) {
413+ return createHapiWrapper ( true ) ;
414+ }
415+
390416wdm . hapiWrapper = hapiWrapper ;
417+ wdm . hapiPluginWrapper = hapiPluginWrapper ;
391418
392419/**
393420 * @template {IncomingMessage} [RequestInternal=IncomingMessage]
394421 * @template {ServerResponse} [ResponseInternal=ServerResponse]
395422 * @param {Compiler | MultiCompiler } compiler compiler
396423 * @param {Options<RequestInternal, ResponseInternal>= } options options
424+ * @param {boolean } usePlugin whether to use as webpack plugin
397425 * @returns {(ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise<void> | void } kow wrapper
398426 */
399- function koaWrapper ( compiler , options ) {
427+ function createKoaWrapper ( compiler , options , usePlugin = false ) {
400428 const devMiddleware = wdm ( compiler , options ) ;
401429
430+ if ( usePlugin ) {
431+ devMiddleware . context . isPlugin = true ;
432+ }
433+
402434 /**
403435 * @param {{ req: RequestInternal, res: ResponseInternal & import("./utils/compatibleAPI").ExpectedServerResponse, status: number, body: string | Buffer | import("fs").ReadStream | { message: string }, state: object } } ctx context
404436 * @param {EXPECTED_FUNCTION } next next
@@ -508,18 +540,46 @@ function koaWrapper(compiler, options) {
508540 return webpackDevMiddleware ;
509541}
510542
543+ /**
544+ * @template {IncomingMessage} [RequestInternal=IncomingMessage]
545+ * @template {ServerResponse} [ResponseInternal=ServerResponse]
546+ * @param {Compiler | MultiCompiler } compiler compiler
547+ * @param {Options<RequestInternal, ResponseInternal>= } options options
548+ * @returns {(ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise<void> | void } kow wrapper
549+ */
550+ function koaWrapper ( compiler , options ) {
551+ return createKoaWrapper ( compiler , options , false ) ;
552+ }
553+
554+ /**
555+ * @template {IncomingMessage} [RequestInternal=IncomingMessage]
556+ * @template {ServerResponse} [ResponseInternal=ServerResponse]
557+ * @param {Compiler | MultiCompiler } compiler compiler
558+ * @param {Options<RequestInternal, ResponseInternal>= } options options
559+ * @returns {(ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise<void> | void } kow plugin wrapper
560+ */
561+ function koaPluginWrapper ( compiler , options ) {
562+ return createKoaWrapper ( compiler , options , true ) ;
563+ }
564+
511565wdm . koaWrapper = koaWrapper ;
566+ wdm . koaPluginWrapper = koaPluginWrapper ;
512567
513568/**
514569 * @template {IncomingMessage} [RequestInternal=IncomingMessage]
515570 * @template {ServerResponse} [ResponseInternal=ServerResponse]
516571 * @param {Compiler | MultiCompiler } compiler compiler
517572 * @param {Options<RequestInternal, ResponseInternal>= } options options
573+ * @param {boolean } usePlugin whether to use as webpack plugin
518574 * @returns {(ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise<void> | void } hono wrapper
519575 */
520- function honoWrapper ( compiler , options ) {
576+ function createHonoWrapper ( compiler , options , usePlugin = false ) {
521577 const devMiddleware = wdm ( compiler , options ) ;
522578
579+ if ( usePlugin ) {
580+ devMiddleware . context . isPlugin = true ;
581+ }
582+
523583 /**
524584 * @param {{ env: EXPECTED_ANY, body: EXPECTED_ANY, json: EXPECTED_ANY, status: EXPECTED_ANY, set: EXPECTED_ANY, req: RequestInternal & import("./utils/compatibleAPI").ExpectedIncomingMessage & { header: (name: string) => string }, res: ResponseInternal & import("./utils/compatibleAPI").ExpectedServerResponse & { headers: EXPECTED_ANY, status: EXPECTED_ANY } } } context context
525585 * @param {EXPECTED_FUNCTION } next next function
@@ -685,6 +745,45 @@ function honoWrapper(compiler, options) {
685745 return webpackDevMiddleware ;
686746}
687747
748+ /**
749+ * @template {IncomingMessage} [RequestInternal=IncomingMessage]
750+ * @template {ServerResponse} [ResponseInternal=ServerResponse]
751+ * @param {Compiler | MultiCompiler } compiler compiler
752+ * @param {Options<RequestInternal, ResponseInternal>= } options options
753+ * @returns {(ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise<void> | void } hono wrapper
754+ */
755+ function honoWrapper ( compiler , options ) {
756+ return createHonoWrapper ( compiler , options , false ) ;
757+ }
758+
759+ /**
760+ * @template {IncomingMessage} [RequestInternal=IncomingMessage]
761+ * @template {ServerResponse} [ResponseInternal=ServerResponse]
762+ * @param {Compiler | MultiCompiler } compiler compiler
763+ * @param {Options<RequestInternal, ResponseInternal>= } options options
764+ * @returns {(ctx: EXPECTED_ANY, next: EXPECTED_FUNCTION) => Promise<void> | void } hono plugin wrapper
765+ */
766+ function honoPluginWrapper ( compiler , options ) {
767+ return createHonoWrapper ( compiler , options , true ) ;
768+ }
769+
688770wdm . honoWrapper = honoWrapper ;
771+ wdm . honoPluginWrapper = honoPluginWrapper ;
772+
773+ /**
774+ * @template {IncomingMessage} [RequestInternal=IncomingMessage]
775+ * @template {ServerResponse} [ResponseInternal=ServerResponse]
776+ * @param {Compiler | MultiCompiler } compiler compiler
777+ * @param {Options<RequestInternal, ResponseInternal>= } options options
778+ * @returns {API<RequestInternal, ResponseInternal> } webpack dev middleware
779+ */
780+ function plugin ( compiler , options = { } ) {
781+ const instance = wdm ( compiler , options ) ;
782+ // Mark that wdm is used as webpack plugin (to use logger instead of console.log)
783+ instance . context . isPlugin = true ;
784+ return instance ;
785+ }
786+
787+ wdm . plugin = plugin ;
689788
690789module . exports = wdm ;
0 commit comments