From e1e426e45de51660a48e832775007e7731db07f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Tue, 28 Jun 2022 15:38:31 +0900 Subject: [PATCH] fix: incorrectly resolving `knownJsSrcRE` files from root (fixes #4161) (#8808) --- packages/vite/src/node/plugins/resolve.ts | 9 ++++++--- packages/vite/src/node/utils.ts | 11 +++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) 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) +}