Skip to content

Commit 8ce49f3

Browse files
refactor: code
1 parent a4db208 commit 8ce49f3

6 files changed

Lines changed: 79 additions & 122 deletions

File tree

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,13 @@ const compiler = webpack({
492492
plugins: [
493493
{
494494
apply(compiler) {
495-
const devMiddleware = middleware.plugin(compiler, {
496-
/* webpack-dev-middleware options */
497-
});
495+
const devMiddleware = middleware(
496+
compiler,
497+
{
498+
/* webpack-dev-middleware options */
499+
},
500+
true,
501+
);
498502
},
499503
},
500504
],
@@ -515,12 +519,12 @@ compiler.watch((err, stats) => {
515519

516520
The following wrappers enable plugin mode for framework integrations:
517521

518-
- `middleware.koaPluginWrapper(compiler, options)`
519-
- `middleware.hapiPluginWrapper()`
520-
- `middleware.honoPluginWrapper(compiler, options)`
522+
- `middleware(compiler, options, true)` (connect/express like middleware)
523+
- `middleware.koaWrapper(compiler, options, true)`
524+
- `middleware.hapiWrapper(true)`
525+
- `middleware.honoWrapper(compiler, options, true)`
521526

522-
They are equivalent to `koaWrapper`/`hapiWrapper`/`honoWrapper`, but use plugin
523-
mode logging behavior.
527+
They are equivalent to `koaWrapper`/`hapiWrapper`/`honoWrapper`, but use plugin mode logging behavior.
524528

525529
### `forwardError`
526530

@@ -768,7 +772,7 @@ const app = new Koa();
768772

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

773777
app.listen(3000);
774778
```
@@ -797,7 +801,7 @@ await server.register({
797801

798802
// Alternative usage (when you want to use as a plugin, i.e. all stats will be printed by other code):
799803
// await server.register({
800-
// plugin: devMiddleware.hapiPluginWrapper(),
804+
// plugin: devMiddleware.hapiWrapper(true),
801805
// options: {
802806
// // The `compiler` option is required
803807
// compiler,
@@ -854,7 +858,7 @@ const app = new Hono();
854858
app.use(devMiddleware.honoWrapper(compiler, devMiddlewareOptions));
855859

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

859863
serve(app);
860864
```

src/index.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ 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
9493
*/
9594

9695
/**
@@ -226,10 +225,9 @@ function wdm(compiler, options = {}, isPlugin = false) {
226225
options,
227226
compiler,
228227
logger: compiler.getInfrastructureLogger("webpack-dev-middleware"),
229-
isPlugin: false,
230228
};
231229

232-
setupHooks(context);
230+
setupHooks(context, isPlugin);
233231

234232
if (options.writeToDisk) {
235233
setupWriteToDisk(context);
@@ -342,11 +340,6 @@ function hapiWrapper(usePlugin = false) {
342340

343341
const devMiddleware = wdm(compiler, rest, usePlugin);
344342

345-
if (usePlugin) {
346-
// Use logger when used as webpack plugin
347-
devMiddleware.context.isPlugin = true;
348-
}
349-
350343
// @ts-expect-error
351344
if (!server.decorations.server.includes("webpackDevMiddleware")) {
352345
// @ts-expect-error
@@ -410,10 +403,6 @@ wdm.hapiWrapper = hapiWrapper;
410403
function koaWrapper(compiler, options, usePlugin) {
411404
const devMiddleware = wdm(compiler, options, usePlugin);
412405

413-
if (usePlugin) {
414-
devMiddleware.context.isPlugin = true;
415-
}
416-
417406
/**
418407
* @param {{ req: RequestInternal, res: ResponseInternal & import("./utils/compatibleAPI").ExpectedServerResponse, status: number, body: string | Buffer | import("fs").ReadStream | { message: string }, state: object }} ctx context
419408
* @param {EXPECTED_FUNCTION} next next

src/utils/setupHooks.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* @template {IncomingMessage} Request
1515
* @template {ServerResponse} Response
1616
* @param {import("../index.js").WithOptional<import("../index.js").Context<Request, Response>, "watching" | "outputFileSystem">} context context
17+
* @param {boolean=} isPlugin true when it is a plugin usage, otherwise false
1718
*/
18-
function setupHooks(context) {
19+
function setupHooks(context, isPlugin) {
1920
/**
2021
* @returns {void}
2122
*/
@@ -66,7 +67,7 @@ function setupHooks(context) {
6667
}
6768

6869
// For plugin support we should print nothing, because webpack/webpack-cli/webpack-dev-server will print them on using `stats.toString()`
69-
if (!context.isPlugin) {
70+
if (!isPlugin) {
7071
logger.log("Compilation finished");
7172

7273
const isMultiCompilerMode = Boolean(

0 commit comments

Comments
 (0)