Skip to content

fix(build): hash chunk import map preload dependencies (fix #23225) - #23431

Closed
Desarso wants to merge 3 commits into
vitejs:mainfrom
Whagons-International:fix/chunk-importmap-preload-hash-v2
Closed

Desarso wants to merge 3 commits into
vitejs:mainfrom
Whagons-International:fix/chunk-importmap-preload-hash-v2

Conversation

@Desarso

@Desarso Desarso commented Sep 4, 2026

Copy link
Copy Markdown

Description

With build.chunkImportMap, Rolldown finalizes chunk hashes before vite:build-import-analysis replaces dynamic import preload markers in generateBundle. A change to a dynamic import static dependency closure can therefore change the emitted __vite__mapDeps arrays without changing the content-hashed filename.

Record the rendered chunk graph and add each dynamic import exact preload dependency signature to augmentChunkHash. The signature uses the same facade, unique chunk name, and first module identities as the Rolldown chunk import map. It also accounts for extracted CSS membership and avoids pulling nested dynamic imports into the parent hash.

This is a rebased continuation of #23278. The original implementation commit retains @pooridev authorship. The failing playground test from that PR used an export that the bundler removed as unused, so this branch replaces it with a unit regression matching the graph from the issue reproduction.

Fixes #23225

Tests

  • pnpm build
  • pnpm test-unit (977 passed, 3 skipped)
  • pnpm test-build chunk-importmap (13 passed)
  • antur84/vite-chunkimportmap-stale-hash-repro against the locally built package (0 same-name/different-bytes collisions)

pooridev and others added 3 commits September 3, 2026 20:01
… is on

generateBundle substitutes `__VITE_PRELOAD__` after hashing, so a graph change that only alters `__vite__mapDeps` reused a content-hashed filename for different bytes.

Co-authored-by: Cursor <[email protected]>
@AndreasOhlsson

Copy link
Copy Markdown

I ran into #23225 with build.chunkImportMap and an immutable /assets/* cache, and tried this PR's approach. To do that I copied its renderChunk/augmentChunkHash logic into an enforce: 'post' plugin and ran it against a small fixture on Vite 8.2.0 / rolldown 1.2.1, and again on 8.3.4 / 1.2.13. It fixes JS and CSS membership changes. I found two gaps.

1. A shared chunk that becomes CSS-only still keeps its importers' names

The fixture:

  • main.js dynamically imports lazy, other and third;
  • lazy → dep-a + shared-style;
  • other → dep-a + dep-b;
  • third → dep-b + shared-style;
  • shared-style.js contains import './shared.css' and a console.log.

Removing the console.log turns shared-style into a pure-CSS chunk. After that change:

  • css-post's generateBundle rewrites the static import "./shared-style-….js" in lazy and third to /* empty css */ (replaceEmptyChunk);
  • import analysis drops the chunk's JS key from the entry's __vite__mapDeps.

Both edits happen after hashing. With this PR's logic, index, lazy and third still keep their names with different bytes (3 collisions, the same as without the fix). This variant is worse than the plain preload-list case.

A browser holding the old lazy still has import "./shared-style-<key>.js". The new import map still lists that key, because of the leftover map entry for stripped CSS-only chunks, but it now points at a JS file that is never emitted. So a static import 404s and the route chunk fails to evaluate.

What closed it for me: also put into the hash each statically or dynamically imported chunk's "is pure CSS" fact. That fact is its rendered code being empty with only styles imported, which is the condition css-post uses. With that addition, all four graph changes I tried showed 0 collisions:

  • a route gains a JS chunk;
  • a route gains its own CSS;
  • a shared chunk gains CSS;
  • a shared chunk becomes CSS-only.

A content-only change still renamed only the changed chunk.

2. Duplicate chunk names (from reading the source, not reproduced)

rolldown's chunk-import-map plugin keys the first same-named non-facade chunk, in render order, by its name, and later duplicates by their smallest module id (crates/rolldown_plugin_chunk_import_map/src/lib.rs around L43–66 at v1.2.1, unchanged at v1.2.13). This PR keys every duplicate by module id (nameCounts.get(name) === 1 ? name : module). If render order changes which duplicate comes first, the real key changes but the hashed signature doesn't. Mirroring the first-occurrence rule with meta.chunks order, which is insertion-ordered from rolldown's FxIndexMap, seems to match.

Results per graph change (same-name/different-bytes files, base vs variant in the same folder):

                  js-membership  own-css  dep-css  pure-css-flip  content-only
chunkImportMap    1              1        1        3              0
+ #23431 logic    0              0        0        3              0
+ pure-css input  0              0        0        0              0

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.

build.chunkImportMap reuses a content-hashed file name for different bytes

3 participants