Skip to content

Commit

Permalink
split tryNodeResolve into tryNodeResolveCore
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 committed Dec 18, 2022
1 parent e25d63f commit 90a9767
Showing 1 changed file with 66 additions and 20 deletions.
86 changes: 66 additions & 20 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,26 +593,28 @@ export type InternalResolveOptionsWithOverrideConditions =

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

export function tryNodeResolve(
export type TryNodeResolveCoreResult =
| {
resultType: 'success'
resolved: string
// nestedPath: string
pkg: PackageData
pkgId: string
nearestPkg: PackageData
isDeepImport: boolean
}
| { resultType: 'optional-peer-dep'; resolved: string }
| { resultType: 'fail' }

export function tryNodeResolveCore(
id: string,
importer: string | null | undefined,
options: InternalResolveOptionsWithOverrideConditions,
targetWeb: boolean,
depsOptimizer?: DepsOptimizer,
ssr?: boolean,
externalize?: boolean,
allowLinkedExternal: boolean = true,
): PartialResolvedId | undefined {
const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options
): TryNodeResolveCoreResult {
const { root, dedupe, preserveSymlinks, packageCache } = options

ssr ??= false

// split id by last '>' for nested selected packages, for example:
// 'foo > bar > baz' => 'foo > bar' & 'baz'
// 'foo' => '' & 'foo'
const lastArrowIndex = id.lastIndexOf('>')
const nestedRoot = id.substring(0, lastArrowIndex).trim()
const nestedPath = id.substring(lastArrowIndex + 1).trim()
const { nestedRoot, nestedPath } = parseNestedId(id)

const possiblePkgIds: string[] = []
for (let prevSlashIndex = -1; ; ) {
Expand Down Expand Up @@ -708,12 +710,13 @@ export function tryNodeResolve(
mainPkg.peerDependenciesMeta?.[nestedPath]?.optional
) {
return {
id: `${optionalPeerDepId}:${nestedPath}:${mainPkg.name}`,
resultType: 'optional-peer-dep',
resolved: `${optionalPeerDepId}:${nestedPath}:${mainPkg.name}`,
}
}
}
}
return
return { resultType: 'fail' }
}

let resolveId = resolvePackageEntry
Expand Down Expand Up @@ -741,9 +744,42 @@ export function tryNodeResolve(
})
}
if (!resolved) {
return
return { resultType: 'fail' }
}

// link id to pkg for browser field mapping check
idToPkgMap.set(resolved, pkg)

return {
resultType: 'success',
resolved,
pkg,
pkgId,
nearestPkg,
isDeepImport,
}
}

export function tryNodeResolve(
id: string,
importer: string | null | undefined,
options: InternalResolveOptionsWithOverrideConditions,
targetWeb: boolean,
depsOptimizer?: DepsOptimizer,
ssr?: boolean,
externalize?: boolean,
allowLinkedExternal: boolean = true,
): PartialResolvedId | undefined {
const coreResult = tryNodeResolveCore(id, importer, options, targetWeb)
if (coreResult.resultType !== 'success') return

const { pkg, pkgId, nearestPkg, isDeepImport } = coreResult
let { resolved } = coreResult
// const {} = pkg
const { isBuild } = options
ssr ??= false
const { nestedPath } = parseNestedId(id)

const processResult = (resolved: PartialResolvedId) => {
if (!externalize) {
return resolved
Expand Down Expand Up @@ -775,8 +811,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 @@ -866,6 +900,18 @@ export function tryNodeResolve(
}
}

/**
* split id by last '>' for nested selected packages, for example:
* 'foo > bar > baz' => 'foo > bar' & 'baz'
* 'foo' => '' & 'foo'
*/
function parseNestedId(id: string) {
const lastArrowIndex = id.lastIndexOf('>')
const nestedRoot = id.substring(0, lastArrowIndex).trim()
const nestedPath = id.substring(lastArrowIndex + 1).trim()
return { nestedRoot, nestedPath }
}

export async function tryOptimizedResolve(
id: string,
importer: string | null | undefined,
Expand Down

0 comments on commit 90a9767

Please sign in to comment.