Skip to content

Commit

Permalink
fix(resolve): prioritize file over dir with same name for resolve
Browse files Browse the repository at this point in the history
fix #1871
  • Loading branch information
yyx990803 committed Feb 4, 2021
1 parent fd33436 commit c741872
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
6 changes: 5 additions & 1 deletion packages/playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
})
Expand Down
1 change: 1 addition & 0 deletions packages/playground/resolve/dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file = '[success] dir.js'
1 change: 1 addition & 0 deletions packages/playground/resolve/dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file = 'dir/index.js'
7 changes: 7 additions & 0 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ <h2>Exports field env priority</h2>
<h2>Resolve /index.*</h2>
<p class="index">fail</p>

<h2>Resolve dir and file of the same name (should prioritize file)</h2>
<p class="dir-vs-file">fail</p>

<h2>Resolve file name containing dot</h2>
<p class="dot">fail</p>

Expand Down Expand Up @@ -79,6 +82,10 @@ <h2>Inline package</h2>
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())
Expand Down
48 changes: 23 additions & 25 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
Expand Down

0 comments on commit c741872

Please sign in to comment.