From 9a46c76687fe44c49ec326b88aac4998b2364771 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Mon, 27 Jun 2022 12:06:27 +0900 Subject: [PATCH 1/3] fix: incorrectly resolving `knownJsSrcRE` files from root --- packages/vite/src/node/plugins/resolve.ts | 9 ++++++--- packages/vite/src/node/utils.ts | 6 ++++++ 2 files changed, 12 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..92d9aa0fa8f9d2 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1063,3 +1063,9 @@ export function stripBomTag(content: string): string { } export const isTS = (filename: string): boolean => /\.[cm]?ts$/.test(filename) + +export const isNonDriveRelativeAbsolutePath = (p: string): boolean => { + if (!isWindows) return p.startsWith('/') + + return /^[A-Za-z]:[/\\]/.test(p) +} From afb69dfc67bc2ad93eb5d58585f3e71b5f49a134 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Mon, 27 Jun 2022 14:05:23 +0900 Subject: [PATCH 2/3] chore: add comment about isNonDriveRelativeAbsolutePath --- packages/vite/src/node/utils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 92d9aa0fa8f9d2..56f7493885eea8 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1064,8 +1064,11 @@ export function stripBomTag(content: string): string { export const isTS = (filename: string): boolean => /\.[cm]?ts$/.test(filename) +/** + * 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 /^[A-Za-z]:[/\\]/.test(p) } From 56a77de97285f8e5a7fc4b0658923ea6e3d0faf8 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Tue, 28 Jun 2022 12:43:00 +0900 Subject: [PATCH 3/3] refactor: extract regex --- packages/vite/src/node/utils.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 56f7493885eea8..796f95a35f6667 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1064,11 +1064,13 @@ 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 /^[A-Za-z]:[/\\]/.test(p) + return windowsDrivePathPrefixRE.test(p) }