Skip to content

Commit

Permalink
perf(resolve): improve file existence check (#11436)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jan 4, 2023
1 parent d3bed53 commit 4a12b89
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
isBuiltin,
isDataUrl,
isExternalUrl,
isFileReadable,
isNonDriveRelativeAbsolutePath,
isObject,
isOptimizable,
Expand Down Expand Up @@ -472,6 +471,7 @@ function tryFsResolve(
targetWeb,
options.tryPrefix,
options.skipPackageJson,
false,
))
) {
return res
Expand All @@ -486,12 +486,16 @@ function tryFsResolve(
targetWeb,
options.tryPrefix,
options.skipPackageJson,
false,
))
) {
return res
}
}

// if `tryIndex` false, skip as we've already tested above
if (!tryIndex) return

if (
postfix &&
(res = tryResolveFile(
Expand Down Expand Up @@ -530,9 +534,11 @@ function tryResolveFile(
targetWeb: boolean,
tryPrefix?: string,
skipPackageJson?: boolean,
skipTsExtension?: boolean,
): string | undefined {
if (isFileReadable(file)) {
if (!fs.statSync(file).isDirectory()) {
const stat = fs.statSync(file, { throwIfNoEntry: false })
if (stat) {
if (!stat.isDirectory()) {
return getRealPath(file, options.preserveSymlinks) + postfix
} else if (tryIndex) {
if (!skipPackageJson) {
Expand All @@ -553,8 +559,12 @@ function tryResolveFile(
}
}

const tryTsExtension = options.isFromTsImporter && isPossibleTsOutput(file)
if (tryTsExtension) {
// try resolve .js import to typescript file
if (
!skipTsExtension &&
options.isFromTsImporter &&
isPossibleTsOutput(file)
) {
const tsSrcPaths = getPotentialTsSrcPaths(file)
for (const srcPath of tsSrcPaths) {
const res = tryResolveFile(
Expand All @@ -565,6 +575,7 @@ function tryResolveFile(
targetWeb,
tryPrefix,
skipPackageJson,
true,
)
if (res) return res
}
Expand Down

0 comments on commit 4a12b89

Please sign in to comment.