Repository navigation
Conversation
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.
fix #22078
Externalization decisions depend on where a package is imported from, but the current cache only uses the package ID. When the same ID resolves to different packages, the cached decision can incorrectly externalize linked source or inline a CommonJS dependency, depending on import order.
This change adds the directory
tryNodeResolveresolves bare imports from to the cache key: the importer's directory if the importer exists on disk, otherwiseroot. That rule is now a helper (getNodeResolveBasedir) shared by both, so they stay in sync. The externalization resolver also gets its ownpackageCache.Unlike #22080's file-level key, importers in the same directory share cached decisions. #22080 is slow mainly because this resolver had no package cache, so every new importer re-reads
package.json. With a cache, both keys perform about the same.Validation
main(a later call gets an earlier call's result) and pass with this change.b afails onmainwithReferenceError: module is not defined, anda bexternalizesdep@2. With this change, both orders match loading each package alone.Benchmark
Vite e954a9c, Node v24.12.0, Apple M5; 2,000 modules across 100 folders, 10 bare imports per module. Median of five cold loads.
config.packageCacheOnly #22080 without a package cache is clearly slower; the other variants are within run-to-run noise of
main.Reproduction and benchmark: https://github.com/oyobbeb/vite-22078-repro
Review notes
The dedicated
packageCacheis not cleared when apackage.jsonchanges, because the watcher only invalidatesconfig.packageCache.processedIdswas never cleared onmaineither, so this only affects (id, directory) pairs first seen after the change. I kept it separate becauseconfig.packageCachestores resolved entries withoutisProductionin the key, andisProductionis fixed tofalsehere.AI assistance
AI tools assisted with the investigation, implementation, and tests. I read how
tryNodeResolvepicks its base directory, reviewed the diff, and chose this approach after comparing the variants above.