From c741872e6ca975f507ec89581b742a1da19a0cb0 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 4 Feb 2021 10:04:40 -0500 Subject: [PATCH] fix(resolve): prioritize file over dir with same name for resolve fix #1871 --- .../resolve/__tests__/resolve.spec.ts | 6 ++- packages/playground/resolve/dir.js | 1 + packages/playground/resolve/dir/index.js | 1 + packages/playground/resolve/index.html | 7 +++ packages/vite/src/node/plugins/resolve.ts | 48 +++++++++---------- 5 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 packages/playground/resolve/dir.js create mode 100644 packages/playground/resolve/dir/index.js diff --git a/packages/playground/resolve/__tests__/resolve.spec.ts b/packages/playground/resolve/__tests__/resolve.spec.ts index ebb81ba2e98c72..69858977fa8f68 100644 --- a/packages/playground/resolve/__tests__/resolve.spec.ts +++ b/packages/playground/resolve/__tests__/resolve.spec.ts @@ -34,10 +34,14 @@ test('Respect production/development conditionals', async () => { ) }) -test('omitted index/*', async () => { +test('implicit dir/index.js', async () => { expect(await page.textContent('.index')).toMatch('[success]') }) +test('implicit dir/index.js vs explicit file', async () => { + expect(await page.textContent('.dir-vs-file')).toMatch('[success]') +}) + test('filename with dot', async () => { expect(await page.textContent('.dot')).toMatch('[success]') }) diff --git a/packages/playground/resolve/dir.js b/packages/playground/resolve/dir.js new file mode 100644 index 00000000000000..96d7982ecd16fb --- /dev/null +++ b/packages/playground/resolve/dir.js @@ -0,0 +1 @@ +export const file = '[success] dir.js' diff --git a/packages/playground/resolve/dir/index.js b/packages/playground/resolve/dir/index.js new file mode 100644 index 00000000000000..604301f196eb9f --- /dev/null +++ b/packages/playground/resolve/dir/index.js @@ -0,0 +1 @@ +export const file = 'dir/index.js' diff --git a/packages/playground/resolve/index.html b/packages/playground/resolve/index.html index 1ddfb90704c511..4e0a654ce33c87 100644 --- a/packages/playground/resolve/index.html +++ b/packages/playground/resolve/index.html @@ -21,6 +21,9 @@

Exports field env priority

Resolve /index.*

fail

+

Resolve dir and file of the same name (should prioritize file)

+

fail

+

Resolve file name containing dot

fail

@@ -79,6 +82,10 @@

Inline package

import { foo } from './util' text('.index', foo()) + // implicit dir index vs. file + import { file } from './dir' + text('.dir-vs-file', file) + // filename with dot import { bar } from './util/bar.util' text('.dot', bar()) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 91dac741817a02..222f2a4b44b533 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -246,25 +246,13 @@ export function resolvePlugin({ export function tryFsResolve( fsPath: string, isProduction: boolean, - tryIndex: boolean | string = true, + tryIndex: boolean = true, tryPrefix: string | undefined = undefined, extensions = SUPPORTED_EXTS ): string | undefined { const [file, q] = fsPath.split(`?`, 2) const query = q ? `?${q}` : `` let res: string | undefined - if ( - (res = tryResolveFile( - file, - query, - isProduction, - tryIndex, - tryPrefix, - extensions - )) - ) { - return res - } for (const ext of extensions) { if ( (res = tryResolveFile( @@ -279,35 +267,45 @@ export function tryFsResolve( return res } } + if ( + (res = tryResolveFile( + file, + query, + isProduction, + tryIndex, + tryPrefix, + extensions + )) + ) { + return res + } } function tryResolveFile( file: string, query: string, isProduction: boolean, - tryIndex: boolean | string, + tryIndex: boolean, tryPrefix: string | undefined, extensions: string[] ): string | undefined { if (fs.existsSync(file)) { const isDir = fs.statSync(file).isDirectory() - if (isDir) { + if (isDir && tryIndex) { const pkgPath = file + '/package.json' if (fs.existsSync(pkgPath)) { // path points to a node package const pkg = loadPackageData(pkgPath) return resolvePackageEntry(file, pkg, isProduction) } - if (tryIndex) { - const index = tryFsResolve( - file + '/index', - isProduction, - false, - tryPrefix, - extensions - ) - if (index) return index + query - } + const index = tryFsResolve( + file + '/index', + isProduction, + false, + tryPrefix, + extensions + ) + if (index) return index + query } else { return normalizePath(ensureVolumeInPath(file)) + query }