Repository navigation
fix(optimizer): resolve excluded deps from importer (fix #21088) - #22970
Open
ayaangazali wants to merge 1 commit into
Open
ayaangazali wants to merge 1 commit into
ayaangazali wants to merge 1 commit into
Conversation
Excluded deps were externalized as bare specifiers in pre-bundled chunks. Serve-time resolution then starts from node_modules/.vite/deps, which fails on non-hoisted layouts (pnpm) when the import lives in a shared chunk, since the importAnalysis remap only covers entry chunks. Externalize to the path resolved from the actual importer instead.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a pre-bundled dependency imports a dependency listed in
optimizeDeps.exclude, the optimizer keeps the excluded dep as a bare specifier in the output chunk. At serve time that bare specifier gets resolved with the chunk (node_modules/.vite/deps/...) as the importer, so on non-hoisted layouts like pnpm's, where the excluded dep only exists inside the importing package's own node_modules, the import fails with "Failed to resolve import ... Does the file exist?" (#21088).There is already a serve-time remap for this in
importAnalysis.ts(normalizeUrl, the loop that swaps the importer for the optimized module's originalsrc), but it explicitly skips chunks (if (!optimizedModule.src) continue). So the bug only bites when the excluded import lives in a code-split shared chunk. That also explains the "weird thing" @brillout noticed in the issue thread: importing@brillout/picocolorsfrom client-only or server-only files worked (entry chunks, remap applies), while the import inutils/assert.tsshared by both entries broke (shared chunk, remap skipped).This PR fixes it at the source instead: in
rolldownDepPlugin, an excluded dep is now resolved from its actual importer at pre-bundle time, and the resolved absolute path is what gets externalized into the chunk. Absolute paths are importer-independent, so entry chunks and shared chunks behave the same and the serve-time resolution can't pick the wrong base anymore. When resolution does not produce an absolute path (no importer, optional peer dep markers, browser-external markers, builtins like the excludedstreamin the playground, or an uninstalled dep), it falls back to the current bare-id behavior, so those cases are unchanged. The plain-import branch inrolldownCjsExternalPluginnow defers tovite:dep-pre-bundlefor the same reason; therequire()facade path is untouched.Two behavior notes to be transparent about:
resolve.deduperemains the escape hatch for singleton packages.require()of an excluded dep in browser platform still goes through the facade with a bare id, so the non-hoisted case there is not covered by this PR. The reported bug (ESM imports) is.Testing
Added a regression test in
playground/optimize-deps:@vitejs/test-dep-importing-excludedhas two entries sharing a util that imports@vitejs/test-excluded-not-hoisted(declaredfile:../excluded-not-hoisted, so pnpm keeps it non-hoisted). Without the fix, the shared chunk 500s in serve mode and the spec fails (along with a cascade of others, since the page dies). With the fix, serve and build modes pass. Also verified end to end against the built package with a standalone repro mirroring the issue layout. Ranpnpm run build,test-serve optimize-deps,test-build optimize-deps,lintandtypechecklocally, all green.fixes #21088
small disclosure: college freshman here, contributing for the greater good and learning a ton :) Claude Code and I dug through this one together (it traced the shared-chunk gap in the importAnalysis remap and we shaped the fix and the playground test from there, all gates run locally by me). if anything looks off I'd honestly love the feedback.