Skip to content

Commit

Permalink
Merge pull request #54 from chenjiahan/enhance-writeToDisk
Browse files Browse the repository at this point in the history
feat: support filter writeToDisk by compilation name
  • Loading branch information
9aoy authored Nov 12, 2024
2 parents ef2c70b + dabdba8 commit 063b95a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration["output"]>["publicPath"]} [publicPath]
* @property {boolean | string} [index]
* @property {boolean} [lastModified]
Expand Down
8 changes: 5 additions & 3 deletions src/utils/setupWriteToDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ 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();
Expand Down
7 changes: 5 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Configuration["output"]>["publicPath"]} [publicPath]
* @property {boolean | string} [index]
* @property {boolean} [lastModified]
Expand Down Expand Up @@ -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<Configuration["output"]>["publicPath"];
index?: string | boolean | undefined;
lastModified?: boolean | undefined;
Expand Down

0 comments on commit 063b95a

Please sign in to comment.