From 6f9a4dcda895f5b45b4d855feb483afa41c63f68 Mon Sep 17 00:00:00 2001 From: patak Date: Sat, 18 Mar 2023 21:12:16 +0100 Subject: [PATCH 1/2] perf(resolve): skip absolute paths in root as url checks --- packages/vite/src/node/plugins/resolve.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 4cdd2043cc41b7..6b5b6bf0b6485b 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -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 resulting in failed checks before falling back to checking the path + // as absolute (/root/root/path-to-file). 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', @@ -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)}`) From 7e81316cb79b774f2042d86cf313c3d9278b6a02 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 19 Mar 2023 09:21:50 +0100 Subject: [PATCH 2/2] chore: reword comment --- packages/vite/src/node/plugins/resolve.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 6b5b6bf0b6485b..87485deb814a82 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -127,8 +127,8 @@ 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 resulting in failed checks before falling back to checking the path - // as absolute (/root/root/path-to-file). If /root/root isn't a valid path, we can + // 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 =