From 6697400243be2f9efe490f377e02bc8ba4a4005c Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 12 Nov 2024 15:46:26 +0800 Subject: [PATCH 1/3] feat: support filter writeToDisk by compilation name --- src/index.js | 2 +- src/utils/setupWriteToDisk.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index b9d85cd..de24ab6 100644 --- a/src/index.js +++ b/src/index.js @@ -100,7 +100,7 @@ const noop = () => {}; * @template {IncomingMessage} [RequestInternal = IncomingMessage] * @template {ServerResponse} [ResponseInternal = ServerResponse] * @typedef {Object} Options - * @property {boolean | ((targetPath: string) => boolean)} [writeToDisk] + * @property {boolean | ((targetPath: string, compilationName?: string) => boolean)} [writeToDisk] * @property {NonNullable["publicPath"]} [publicPath] * @property {boolean | string} [index] * @property {boolean} [lastModified] diff --git a/src/utils/setupWriteToDisk.js b/src/utils/setupWriteToDisk.js index e70becf..fcb0ae8 100644 --- a/src/utils/setupWriteToDisk.js +++ b/src/utils/setupWriteToDisk.js @@ -29,11 +29,11 @@ function setupWriteToDisk(context) { compiler.hooks.assetEmitted.tapAsync( "DevMiddleware", - (file, info, callback) => { - const { targetPath, content } = info; + (_file, info, callback) => { + const { targetPath, content, compilation } = info; const { writeToDisk: filter } = context.options; const allowWrite = - filter && typeof filter === "function" ? filter(targetPath) : true; + filter && typeof filter === "function" ? filter(targetPath, compilation.name) : true; if (!allowWrite) { return callback(); From 8de7d96097884653f044a28b289e7e2f7b8c264d Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 12 Nov 2024 15:55:57 +0800 Subject: [PATCH 2/3] fix: lint --- src/utils/setupWriteToDisk.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/setupWriteToDisk.js b/src/utils/setupWriteToDisk.js index fcb0ae8..b4b6c60 100644 --- a/src/utils/setupWriteToDisk.js +++ b/src/utils/setupWriteToDisk.js @@ -33,7 +33,9 @@ function setupWriteToDisk(context) { const { targetPath, content, compilation } = info; const { writeToDisk: filter } = context.options; const allowWrite = - filter && typeof filter === "function" ? filter(targetPath, compilation.name) : true; + filter && typeof filter === "function" + ? filter(targetPath, compilation.name) + : true; if (!allowWrite) { return callback(); From dabdba80922980f8b817c9163872ec12ef6e6a09 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 12 Nov 2024 16:02:15 +0800 Subject: [PATCH 3/3] fix: update type --- types/index.d.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index ad7c127..f79640e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -75,7 +75,7 @@ export = wdm; * @template {IncomingMessage} [RequestInternal = IncomingMessage] * @template {ServerResponse} [ResponseInternal = ServerResponse] * @typedef {Object} Options - * @property {boolean | ((targetPath: string) => boolean)} [writeToDisk] + * @property {boolean | ((targetPath: string, compilationName?: string) => boolean)} [writeToDisk] * @property {NonNullable["publicPath"]} [publicPath] * @property {boolean | string} [index] * @property {boolean} [lastModified] @@ -256,7 +256,10 @@ type Options< RequestInternal extends IncomingMessage = import("http").IncomingMessage, ResponseInternal extends ServerResponse = ServerResponse, > = { - writeToDisk?: boolean | ((targetPath: string) => boolean) | undefined; + writeToDisk?: + | boolean + | ((targetPath: string, compilationName?: string) => boolean) + | undefined; publicPath?: NonNullable["publicPath"]; index?: string | boolean | undefined; lastModified?: boolean | undefined;