Skip to content

fix: sort polyfill imports for deterministic chunk hashes#21960

Open
themavik wants to merge 1 commit intovitejs:mainfrom
themavik:fix/21905-deterministic-polyfill-chunks
Open

fix: sort polyfill imports for deterministic chunk hashes#21960
themavik wants to merge 1 commit intovitejs:mainfrom
themavik:fix/21905-deterministic-polyfill-chunks

Conversation

@themavik
Copy link

Summary

  • @vitejs/plugin-legacy produces non-deterministic polyfill chunk hashes across builds
  • The polyfill import order varies because Set iteration depends on insertion order from parallel renderChunk calls

Root Cause

In polyfillsPlugin.load(), the imports Set is spread into an array with [...imports].map(...). Since renderChunk hooks run in parallel, polyfills are added to the Set in non-deterministic order, producing different import sequences and thus different chunk content hashes.

Fix

Sort the imports array before generating the polyfill code: [...imports].sort().map(...). This ensures identical import order regardless of renderChunk execution order.

Testing

  • Verified the sort produces stable, alphabetical import order
  • No behavior change — only the order of imports in the generated polyfill chunk is stabilized

Fixes #21905

Made with Cursor

Root cause: Set iteration order depends on insertion order, which
is non-deterministic when renderChunk runs in parallel. This causes
polyfill chunk hashes to differ between builds.

Made-with: Cursor
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.

vite@8 build fail to produce identical polyfill chunks

1 participant