Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export interface InternalResolveOptions extends Required<ResolveOptions> {
shouldExternalize?: (id: string) => boolean | undefined
}

const resolvedCache = new WeakMap<
InternalResolveOptions,
Map<string, string | PartialResolvedId>
>()

export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
const {
root,
Expand All @@ -126,6 +131,8 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
} = resolveOptions

const { target: ssrTarget, noExternal: ssrNoExternal } = ssrConfig ?? {}
const resolvedIdCache = new Map<string, string | PartialResolvedId>()
resolvedCache.set(resolveOptions, resolvedIdCache)

return {
name: 'vite:resolve',
Expand Down Expand Up @@ -338,13 +345,29 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
// bare package imports, perform node resolve
if (bareImportRE.test(id)) {
const external = options.shouldExternalize?.(id)
const key = JSON.stringify({
id,
external,
asSrc,
depsOptimizer: !!depsOptimizer,
targetWeb,
...options,
ssr,
})
const cache = resolvedIdCache.get(key)

if (cache) {
return cache
}

if (
!external &&
asSrc &&
depsOptimizer &&
!options.scan &&
(res = await tryOptimizedResolve(depsOptimizer, id, importer))
) {
resolvedIdCache.set(key, res)
return res
}

Expand All @@ -359,6 +382,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
external,
))
) {
resolvedIdCache.set(key, res)
return res
}

Expand All @@ -373,6 +397,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
external,
))
) {
resolvedIdCache.set(key, res)
return res
}

Expand Down