Skip to content

Commit

Permalink
perf(resolve): skip absolute paths in root as url checks
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 18, 2023
1 parent 07aa155 commit 6f9a4dc
Showing 1 changed file with 11 additions and 1 deletion.
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 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',

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

0 comments on commit 6f9a4dc

Please sign in to comment.