Skip to content

Commit 8d2931b

Browse files
authored
perf(resolve): skip absolute paths in root as url checks (#12476)
1 parent 7288a24 commit 8d2931b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/vite/src/node/plugins/resolve.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
127127

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

130+
// In unix systems, absolute paths inside root first needs to be checked as an
131+
// absolute URL (/root/root/path-to-file) resulting in failed checks before falling
132+
// back to checking the path as absolute. If /root/root isn't a valid path, we can
133+
// avoid these checks. Absolute paths inside root are common in user code as many
134+
// paths are resolved by the user. For example for an alias.
135+
const rootInRoot =
136+
fs
137+
.statSync(path.join(root, root), { throwIfNoEntry: false })
138+
?.isDirectory() ?? false
139+
130140
return {
131141
name: 'vite:resolve',
132142

@@ -256,7 +266,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
256266

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

0 commit comments

Comments
 (0)