Skip to content

Commit

Permalink
perf(resolve): skip absolute paths in root as url checks (#12476)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 21, 2023
1 parent 7288a24 commit 8d2931b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -127,6 +127,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 @@ -256,7 +266,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

0 comments on commit 8d2931b

Please sign in to comment.