diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 3206e5e6a7cba4..165620722cb2fc 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -12,6 +12,7 @@ import { isExternalUrl, isObject, normalizePath, + fsPathFromId, resolveFrom } from '../utils' import { ViteDevServer } from '..' @@ -60,8 +61,7 @@ export function resolvePlugin({ // explicit fs paths that starts with /@fs/* if (asSrc && id.startsWith(FS_PREFIX)) { - let fsPath = id.slice(FS_PREFIX.length - 1) - if (fsPath.startsWith('//')) fsPath = fsPath.slice(1) + const fsPath = fsPathFromId(id) res = tryFsResolve(fsPath, false) isDebug && debug(`[@fs] ${chalk.cyan(id)} -> ${chalk.dim(res)}`) // always return here even if res doesn't exist since /@fs/ is explicit diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index f2b617de8d04f1..1518b5c8d38a88 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -59,6 +59,11 @@ export function normalizePath(id: string): string { return path.posix.normalize(id) } +export function fsPathFromId(id: string): string { + const fsPath = normalizePath(id.slice(FS_PREFIX.length)) + return fsPath.startsWith('/') ? fsPath : `/${fsPath}` +} + export const queryRE = /\?.*$/ export const hashRE = /#.*$/