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): skip absolute paths in root as url checks #12476

Merged
merged 2 commits into from
Mar 21, 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
12 changes: 11 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

const { target: ssrTarget, noExternal: ssrNoExternal } = ssrConfig ?? {}

// In unix systems, absolute paths inside root first needs to be checked as an
// absolute URL (/root/root/path-to-file) resulting in failed checks before falling
// back to checking the path as absolute. If /root/root isn't a valid path, we can
// avoid these checks. Absolute paths inside root are common in user code as many
// paths are resolved by the user. For example for an alias.
const rootInRoot =
fs
.statSync(path.join(root, root), { throwIfNoEntry: false })
?.isDirectory() ?? false

return {
name: 'vite:resolve',

Expand Down Expand Up @@ -255,7 +265,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {

// URL
// /foo -> /fs-root/foo
if (asSrc && id.startsWith('/')) {
if (asSrc && id.startsWith('/') && (rootInRoot || !id.startsWith(root))) {
const fsPath = path.resolve(root, id.slice(1))
if ((res = tryFsResolve(fsPath, options))) {
isDebug && debug(`[url] ${colors.cyan(id)} -> ${colors.dim(res)}`)
Expand Down