Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove idToPkgMap #12564

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
res = ensureVersionQuery(res, id, options, depsOptimizer)
isDebug &&
debug(`[relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)
const pkg = importer != null && idToPkgMap.get(importer)
const pkg =
importer &&
findNearestPackageData(path.dirname(importer), options.packageCache)
if (pkg) {
idToPkgMap.set(res, pkg)
return {
id: res,
moduleSideEffects: pkg.hasSideEffects(res),
Expand Down Expand Up @@ -664,8 +665,6 @@ export type InternalResolveOptionsWithOverrideConditions =
overrideConditions?: string[]
}

export const idToPkgMap = new Map<string, PackageData>()

export function tryNodeResolve(
id: string,
importer: string | null | undefined,
Expand Down Expand Up @@ -774,8 +773,6 @@ export function tryNodeResolve(
return { ...resolved, id: resolvedId, external: true }
}

// link id to pkg for browser field mapping check
idToPkgMap.set(resolved, pkg)
if ((isBuild && !depsOptimizer) || externalize) {
// Resolve package side effects for build so that rollup can better
// perform tree-shaking
Expand Down Expand Up @@ -1202,8 +1199,7 @@ function tryResolveBrowserMapping(
let res: string | undefined
const pkg =
importer &&
(idToPkgMap.get(importer) ||
findNearestPackageData(path.dirname(importer), options.packageCache))
findNearestPackageData(path.dirname(importer), options.packageCache)
if (pkg && isObject(pkg.data.browser)) {
const mapId = isFilePath ? './' + slash(path.relative(pkg.dir, id)) : id
const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser)
Expand All @@ -1215,7 +1211,6 @@ function tryResolveBrowserMapping(
) {
isDebug &&
debug(`[browser mapped] ${colors.cyan(id)} -> ${colors.dim(res)}`)
idToPkgMap.set(res, pkg)
const result = {
id: res,
moduleSideEffects: pkg.hasSideEffects(res),
Expand Down