Skip to content

Do not hook assetEmitted when writeToDisk is true#2277

Open
AdrianBannister wants to merge 1 commit intowebpack:mainfrom
AdrianBannister:main
Open

Do not hook assetEmitted when writeToDisk is true#2277
AdrianBannister wants to merge 1 commit intowebpack:mainfrom
AdrianBannister:main

Conversation

@AdrianBannister
Copy link

Summary

When writeToDisk: true is set, files were being written to disk twice — once by webpack via its native output file system (configured by setupOutputFileSystem), and again by the assetEmitted hook set up in setupWriteToDisk. This caused redundant I/O for every emitted asset.

The fix changes the call site in index.js to only invoke setupWriteToDisk when writeToDisk is a filter function. When it is true, webpack handles disk writes natively and no hook is needed.

What kind of change does this PR introduce?

fix

Did you add tests for your changes?

Yes. The existing writeToDisk: true tests in middleware.test.js already asserted on the number of assetEmitted taps — those assertions have been updated from toHaveLength(1) to toHaveLength(0) to reflect that DevMiddleware no longer taps assetEmitted when writeToDisk: true. The tests continue to verify that the bundle file is found on disk.

Does this PR introduce a breaking change?

No. Behaviour for end users is unchanged — files are still written to disk when writeToDisk: true, just without the redundant second write.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

No documentation changes needed.

Use of AI

This PR was developed with the assistance of Claude (Anthropic). The fix and tests were generated and reviewed with AI support, in accordance with the webpack AI policy.

When `writeToDisk: true`, `setupOutputFileSystem` already configures
webpack to use the native (disk) output file system, so webpack writes
all assets to disk itself.  Calling `setupWriteToDisk` on top of that
tapped `assetEmitted` and wrote every file a second time.

Only invoke `setupWriteToDisk` when `writeToDisk` is a filter function,
where the in-memory fs is still used and selective disk writes are
needed.

Fixes webpack#2270
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 9, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: AdrianBannister / name: Adrian Bannister (9b31ac8)

@AdrianBannister
Copy link
Author

This closes #2270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant