Skip to content

Commit

Permalink
fix(resolve): fix resolver not following node resolve algorithm (#2718)…
Browse files Browse the repository at this point in the history
…, fix #2695
  • Loading branch information
Timsonrobl committed Mar 29, 2021
1 parent 86753d6 commit 669c591
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/playground/resolve/__tests__/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ test('implicit dir/index.js vs explicit file', async () => {
expect(await page.textContent('.dir-vs-file')).toMatch('[success]')
})

test('exact extension vs. duplicated (.js.js)', async () => {
expect(await page.textContent('.exact-extension')).toMatch('[success]')
})

test('dont add extension to directory name (./dir-with-ext.js/index.js)', async () => {
expect(await page.textContent('.dir-with-ext')).toMatch('[success]')
})

test('filename with dot', async () => {
expect(await page.textContent('.dot')).toMatch('[success]')
})
Expand Down
Empty file.
1 change: 1 addition & 0 deletions packages/playground/resolve/dir-with-ext/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file = '[success] ./dir-with-ext/index.js'
1 change: 1 addition & 0 deletions packages/playground/resolve/exact-extension/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file = '[success] file.js'
1 change: 1 addition & 0 deletions packages/playground/resolve/exact-extension/file.js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const file = 'file.js.js'
14 changes: 14 additions & 0 deletions packages/playground/resolve/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ <h2>Resolve /index.*</h2>
<h2>Resolve dir and file of the same name (should prioritize file)</h2>
<p class="dir-vs-file">fail</p>

<h2>Resolve to non-duplicated file extension</h2>
<p class="exact-extension">fail</p>

<h2>Don't add extensions to directory names</h2>
<p class="dir-with-ext">fail</p>

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

Expand Down Expand Up @@ -95,6 +101,14 @@ <h2>resolve.conditions</h2>
import { file } from './dir'
text('.dir-vs-file', file)

// // exact extension vs. duplicated (.js.js)
import { file as exactExtMsg } from './exact-extension/file.js'
text('.exact-extension', exactExtMsg)

// don't add extensions to dir name (./dir-with-ext.js/index.js)
import { file as dirWithExtMsg } from './dir-with-ext'
text('.dir-with-ext', dirWithExtMsg)

// filename with dot
import { bar } from './util/bar.util'
text('.dot', bar())
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ function tryFsResolve(
}

let res: string | undefined
if (
(res = tryResolveFile(file, postfix, options, false, options.tryPrefix))
) {
return res
}

for (const ext of options.extensions || DEFAULT_EXTENSIONS) {
if (
(res = tryResolveFile(
Expand Down Expand Up @@ -288,8 +294,6 @@ function tryResolveFile(
}
const index = tryFsResolve(file + '/index', options)
if (index) return index + postfix
} else {
return normalizePath(ensureVolumeInPath(file)) + postfix
}
}
if (tryPrefix) {
Expand Down

0 comments on commit 669c591

Please sign in to comment.