Skip to content

Commit

Permalink
fix: wrongly resolve to optimized doppelganger (vitejs#11290)
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 committed Jan 5, 2023
1 parent 896475d commit 82ad4bf
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,13 +888,15 @@ export async function tryOptimizedResolve(

const metadata = depsOptimizer.metadata

const depInfo = optimizedDepInfoFromId(metadata, id)
if (depInfo) {
return depsOptimizer.getOptimizedDepId(depInfo)
if (!importer) {
// no importer. try our best to find an optimized dep
const depInfo = optimizedDepInfoFromId(metadata, id)
if (depInfo) {
return depsOptimizer.getOptimizedDepId(depInfo)
}
return
}

if (!importer) return

// further check if id is imported by nested dependency
let resolvedSrc: string | undefined

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'
import { page } from '~utils'

test('resolve-optimized-dup-deps', async () => {
expect(await page.textContent('.a')).toBe('test-package-a:test-package-b-v2')
expect(await page.textContent('.b')).toBe('test-package-b-v1')
})
17 changes: 17 additions & 0 deletions playground/resolve-optimized-dup-deps/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h2>direct dependency A</h2>
<pre class="a"></pre>

<h2>direct dependency B</h2>
<pre class="b"></pre>

<script type="module">
import A from '@vitejs/test-resolve-optimized-dup-deps-package-a'
import B from '@vitejs/test-resolve-optimized-dup-deps-package-b'

text('.a', A)
text('.b', B)

function text(sel, text) {
document.querySelector(sel).textContent = text
}
</script>
6 changes: 6 additions & 0 deletions playground/resolve-optimized-dup-deps/package-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import b from '@vitejs/test-resolve-optimized-dup-deps-package-b'

// should get test-package-a:test-package-b-v2
const result = 'test-package-a:' + b

export default result
9 changes: 9 additions & 0 deletions playground/resolve-optimized-dup-deps/package-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps-package-a",
"private": true,
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@vitejs/test-resolve-optimized-dup-deps-package-b": "file:../package-b-v2"
}
}
3 changes: 3 additions & 0 deletions playground/resolve-optimized-dup-deps/package-b-v1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// test-package-b-v1 is install and imported by user
// it is written in cjs and should be optimized
module.exports = 'test-package-b-v1'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps-package-b",
"private": true,
"version": "1.0.0",
"main": "index.js"
}
3 changes: 3 additions & 0 deletions playground/resolve-optimized-dup-deps/package-b-v2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// test-package-b-v2 is install and imported by test-package-a
// it is written in esm. it is not optimized
export default 'test-package-b-v2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps-package-b",
"private": true,
"version": "2.0.0",
"main": "index.js"
}
15 changes: 15 additions & 0 deletions playground/resolve-optimized-dup-deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@vitejs/test-resolve-optimized-dup-deps",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"@vitejs/test-resolve-optimized-dup-deps-package-a": "file:./package-a",
"@vitejs/test-resolve-optimized-dup-deps-package-b": "file:./package-b-v1"
}
}
8 changes: 8 additions & 0 deletions playground/resolve-optimized-dup-deps/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @type {import('vite').UserConfig}
*/
module.exports = {
optimizeDeps: {
exclude: ['@vitejs/test-resolve-optimized-dup-deps-package-a'],
},
}
40 changes: 40 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 82ad4bf

Please sign in to comment.