Skip to content

Do not hook assetEmitted when writeToDisk is true#2277

Merged
alexander-akait merged 1 commit intowebpack:mainfrom
AdrianBannister:main
Mar 10, 2026
Merged

Do not hook assetEmitted when writeToDisk is true#2277
alexander-akait merged 1 commit intowebpack:mainfrom
AdrianBannister:main

Conversation

@AdrianBannister
Copy link
Copy Markdown
Contributor

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
Copy Markdown

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
Copy Markdown
Contributor Author

This closes #2270

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.88%. Comparing base (112de09) to head (9b31ac8).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2277   +/-   ##
=======================================
  Coverage   95.88%   95.88%           
=======================================
  Files          13       13           
  Lines         900      900           
  Branches      270      270           
=======================================
  Hits          863      863           
  Misses         37       37           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@alexander-akait alexander-akait merged commit 0c3b296 into webpack:main Mar 10, 2026
17 checks passed
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.

2 participants