Repository navigation
feat: add compilation.hooks.chunkHashDependencies - #22810
Conversation
🦋 Changeset detectedLatest commit: a1866dd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Change: Feature Merge Risk: ⚪ Minimal · up to
|
e9c4ce1 to
e4d731a
Compare
|
This PR is packaged and the instant preview of its latest commit is available. Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@22810
yarn add -D webpack@https://pkg.pr.new/webpack@22810
pnpm add -D webpack@https://pkg.pr.new/webpack@22810 |
Types CoverageCoverage after merging feat/chunk-hash-dependencies into main will be
Coverage Report |
Generated code sizeComparing base (
Read No asset that both runs emit changed size — everything below is new or deleted. 14 asset(s) this pull request adds
No runtime that both runs build changed which runtime modules it carries. 5 runtime(s) this pull request adds or no longer builds
Built |
Merging this PR will improve performance by 54.75%2 warnings, 1 note
Performance Changes
Tip Curious why performance improved? Comment Comparing |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #22810 +/- ##
===========================================
+ Coverage 77.15% 96.54% +19.39%
===========================================
Files 601 778 +177
Lines 133153 141164 +8011
Branches 46098 48311 +2213
===========================================
+ Hits 102733 136289 +33556
+ Misses 30420 4875 -25545
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Cases for a chunk naming another chunk's file: a runtime chunk renamed by the compilation hash, a plugin declaring the dependency, the hashing order and the warnings for dependencies that cannot be honored.
A chunk declares the chunks whose hashes its output reads. createHash hashes them first, in a shared order RuntimeTemplate reads too, and JavascriptModulesPlugin mixes their hashes into the chunk's. The ESM and CommonJS chunk formats declare what their startup imports or requires.
Positions run across the hashing rounds, so RuntimeTemplate compares them instead of classifying rounds itself; the walk positions chunks as it goes and JavascriptModulesPlugin mixes dependency hashes in one tap. A watch case covers HTML, CSS and worker chunks naming other chunks' files.
Recomputing the round per dependency edge walks the chunk's groups in canBeInitial; a map filled while sorting chunks into rounds is cheaper.
node-commonjs externals import createRequire, which Node 10 lacks.
Same change as #22875; main's lint:types-test fails without it. Claude-Session: https://claude.ai/code/session_01GVKiUEUTFEzHrHQoE1ryyx
90a7e89 to
a1866dd
Compare
Summary
Stacked on #22804; review that first, and this diff shows only the hook.
#19439 and #22803 were the same bug: a chunk names another chunk's hashed file, but nothing makes its hash follow that chunk's. Each fix patched one format and relied on
createHash's order without saying so. This PR adds one hook that declares the dependency instead:Compilation#createHashhashes declared dependencies first within each round, and warns on a cycle or on a dependency hashed in a later round. A chunk that depends on one rehashed after the compilation hash is rehashed after it too.RuntimeTemplate._hashRoundBeforereads that same order instead of assuming id order. fix(esm): rehash entry chunks when a chunk they import changes #22804 had already made that assumption wrong fordependOn.JavascriptModulesPluginmixes the dependencies' hashes intochunk.hashandcontentHash.javascript.runtimeChunk.hashspecial case from Main chunk points to the wrong runtime chunk when using Module Federation #19439 and the extra tap from fix(esm): rehash entry chunks when a chunk they import changes #22804 are removed.Compilationno longer knows about any output format.This also fixes a stale import, in ESM and CommonJS, when the runtime chunk's name depends on the compilation hash (
runtimeChunk: "multiple",publicPath: "/[fullhash]/", then change another entry):Cost, measured against the base branch on a stress fixture (300 ESM/CommonJS entries in
dependOnchains, shared split chunks, async imports; 6 builds per process,--cpu-profat 100 µs):createHashtime per buildruntimeChunk: "single"createHashruntimeChunk: "single"Build times overlap across arms (596–1,829 ms per build). ESM output is byte-identical to the base branch. CommonJS entry chunks get new hashes once, because the runtime chunk's hash now enters through the hook. With
realContentHash: false, and withpublicPath: "/[fullhash]/"on top, every one of the 590 names in the runtime's filename map and every staticrequireresolves to an emitted file.What kind of change does this PR introduce?
feat
Did you add tests for your changes?
Yes. Each one fails when its part of the change is disabled:
test/watchCases/cache/entry-full-hash-runtime/: ESM and CommonJS ×realContentHash, with a runtime chunk renamed by[fullhash](late rehash).test/watchCases/cache/chunk-hash-dependencies-plugin/: a third-party plugin names another chunk's file and only declares the dependency (ordering and hash mixing).test/configCases/hash/chunk-hash-dependencies-order/: exact hashing order for entry and runtime chunks,getChunkHashDependencies, andRuntimeTemplateagreeing withcreateHashfor every pair in a round.test/configCases/hash/chunk-hash-dependencies-warnings/: cycle warning and later-round warning.test/watchCases/cache/esm-entry-imported-chunk-hash/from fix(esm): rehash entry chunks when a chunk they import changes #22804 still passes on the hook.Does this PR introduce a breaking change?
No. CommonJS and ESM entry chunks may get new hashes once.
If relevant, what needs to be documented once your changes are merged or what have you already documented?
The new hook
compilation.hooks.chunkHashDependencies(SyncHook<[Chunk, Set<Chunk>]>) andcompilation.getChunkHashDependencies(chunk)belong in the compilation hooks page on webpack.js.org.Use of AI
Claude Code designed the hook with the requester, wrote the tests first, implemented the change, found the
[fullhash]runtime case and theRuntimeTemplateorder mismatch, checked that each test fails when its mechanism is disabled, and measured CPU and memory. The requester reviewed the work.Generated by Claude Code
Summary by CodeRabbit