diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 4acbfbaf816678..76fcfcbdb298f4 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -24,6 +24,7 @@ import { isDataUrl, isExternalUrl, isFileReadable, + isNonDriveRelativeAbsolutePath, isObject, isPossibleTsOutput, isTsRequest, @@ -172,8 +173,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { // relative if ( id.startsWith('.') || - (preferRelative && /^\w/.test(id)) || - importer?.endsWith('.html') + ((preferRelative || importer?.endsWith('.html')) && /^\w/.test(id)) ) { const basedir = importer ? path.dirname(importer) : process.cwd() const fsPath = path.resolve(basedir, id) @@ -239,7 +239,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { } // absolute fs paths - if (path.isAbsolute(id) && (res = tryFsResolve(id, options))) { + if ( + isNonDriveRelativeAbsolutePath(id) && + (res = tryFsResolve(id, options)) + ) { isDebug && debug(`[fs] ${colors.cyan(id)} -> ${colors.dim(res)}`) return res } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index e2c7c8862b930a..796f95a35f6667 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1063,3 +1063,14 @@ export function stripBomTag(content: string): string { } export const isTS = (filename: string): boolean => /\.[cm]?ts$/.test(filename) + +const windowsDrivePathPrefixRE = /^[A-Za-z]:[/\\]/ + +/** + * path.isAbsolute also returns true for drive relative paths on windows (e.g. /something) + * this function returns false for them but true for absolute paths (e.g. C:/something) + */ +export const isNonDriveRelativeAbsolutePath = (p: string): boolean => { + if (!isWindows) return p.startsWith('/') + return windowsDrivePathPrefixRE.test(p) +}