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

perf(resolve): improve file existence check #11436

Merged
merged 6 commits into from
Jan 4, 2023
Merged
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
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 })
Copy link
Contributor

@MoustaphaDev MoustaphaDev Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know vite a lot but this not being in a try catch could be an issue as it would throw an error for virtual modules
See this error an Astro user encountered, seems like caused by this change

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 &&
bluwy marked this conversation as resolved.
Show resolved Hide resolved
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