From f7edb4f0bd8983f81ecbbcc5e9e22286befc67b8 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Thu, 14 Jul 2022 11:16:29 +0900 Subject: [PATCH 1/2] fix: resolve drive relative path --- packages/vite/src/node/plugins/resolve.ts | 12 ++++++ playground/resolve/__tests__/resolve.spec.ts | 6 ++- playground/resolve/drive-relative.js | 1 + playground/resolve/index.html | 4 ++ playground/resolve/vite.config.js | 39 ++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 playground/resolve/drive-relative.js diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 1ebe847359384c..af86ef76193c92 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -28,6 +28,7 @@ import { isObject, isPossibleTsOutput, isTsRequest, + isWindows, nestedResolveFrom, normalizePath, resolveFrom, @@ -243,6 +244,17 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { } } + // drive relative fs paths (only windows) + if (isWindows && id.startsWith('/')) { + const basedir = importer ? path.dirname(importer) : process.cwd() + const fsPath = path.resolve(basedir, id) + if ((res = tryFsResolve(fsPath, options))) { + isDebug && + debug(`[drive-relative] ${colors.cyan(id)} -> ${colors.dim(res)}`) + return res + } + } + // absolute fs paths if ( isNonDriveRelativeAbsolutePath(id) && diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index 79ab9dce6409c3..d00f6fb7e711de 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -1,4 +1,4 @@ -import { isBuild, page } from '~utils' +import { isBuild, isWindows, page } from '~utils' test('bom import', async () => { expect(await page.textContent('.utf8-bom')).toMatch('[success]') @@ -75,6 +75,10 @@ test('filename with dot', async () => { expect(await page.textContent('.dot')).toMatch('[success]') }) +test.runIf(isWindows)('drive-relative path', async () => { + expect(await page.textContent('.drive-relative')).toMatch('[success]') +}) + test('browser field', async () => { expect(await page.textContent('.browser')).toMatch('[success]') }) diff --git a/playground/resolve/drive-relative.js b/playground/resolve/drive-relative.js new file mode 100644 index 00000000000000..188ac36e661b35 --- /dev/null +++ b/playground/resolve/drive-relative.js @@ -0,0 +1 @@ +export default '[success] drive relative' diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 1920ebb675d24c..cda2f2f2bab315 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -55,6 +55,9 @@

Resolve file name containing dot

fail

+

Resolve drive-relative path (Windows only)

+

fail

+

Browser Field

fail

@@ -89,6 +92,7 @@

resolve package that contains # in path