Skip to content

fix: merge __vitePreload deps for conditional dynamic imports (fix #23221) - #23253

Closed
Sskift wants to merge 1 commit into
vitejs:mainfrom
Sskift:fix/23221
Closed

Sskift wants to merge 1 commit into
vitejs:mainfrom
Sskift:fix/23221

Conversation

@Sskift

@Sskift Sskift commented Aug 13, 2026

Copy link
Copy Markdown

Description

Fixes #23221: two mutually-exclusive __vitePreload calls (conditional dynamic imports) get merged by minification into a single call whose first argument becomes a ternary, but whose second argument (the deps array) keeps only one branch's deps — silently dropping the other branch's preloaded assets (JS chunk preload + CSS).

Root cause

The native vite:build-import-analysis plugin wraps every bare dynamic import() as:

__vitePreload(() => import("x"), __VITE_PRELOAD__)

All markers are the identical string __VITE_PRELOAD__. When two such calls sit in mutually-exclusive branches, the minifier folds them into one call:

__vitePreload(cond ? () => import("./a") : () => import("./b"), __vite__mapDeps([…one branch…]))

The first arg becomes a correct ternary, but the second arg is a side-effect-free array literal that the minifier keeps from only one branch. The other branch's CSS and JS-chunk preloads are silently dropped at runtime.

Fix

In importAnalysisBuild.ts renderChunk, uniquify each __VITE_PRELOAD__ marker into __VITE_PRELOAD_<n>__ before minification. Distinct marker strings make the two __vitePreload(...) calls non-mergeable, so each branch's deps array is preserved.

Supporting changes:

  • preloadMarkerRE now matches both the plain and uniquified markers; matchImportsToPreloadMarkers / generateBundle marker scanning and rewriting use the full marker span (preloadMarkerEnd).
  • renderChunk now emits a real sourcemap (previously map: null) so async chunks that carry markers (e.g. nested dynamic imports) keep their .map files.

Testing

  • Added a regression test conditional dynamic imports keep both branches preload deps (#23221) in packages/vite/src/node/__tests__/build.spec.ts, which asserts the preload deps registry contains both branches' JS + CSS (4 entries, 2 CSS).
  • RED (before fix): expected [ …(2) ] to have a length of 4 but got 2.
  • GREEN (after fix): passes.
  • Regression: pnpm vitest run packages/vite/src/node/__tests__/ → 44 files / 650 tests passed. oxfmt and eslint clean.

Notes

The report is against vite 8.0.16; this root cause was independently reproduced on the repo's current main (8.2.1 / rolldown 1.2.1) using the repo's own build() with a virtual-module fixture. No changeset added — Vite generates its changelog from the PR title.

Closes #23221

@sapphi-red

Copy link
Copy Markdown
Member

fixed by #24028 and rolldown/rolldown#11157

@sapphi-red sapphi-red closed this Nov 11, 2026
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.

Conditional dynamic imports share one __vitePreload deps array, dropping CSS/preloads for one branch

2 participants