Navigation Menu

Skip to content

Commit

Permalink
fix: nested dependencies from sub node_modules, fix #3254 (#4091)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelmor committed Jul 14, 2021
1 parent 73344a9 commit b465d3e
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 23 deletions.
6 changes: 4 additions & 2 deletions packages/playground/nested-deps/__tests__/nested-deps.spec.ts
@@ -1,6 +1,8 @@
// TODO: Rework #3753, taking into account issues with #4005, #4012, #4014
test.skip('handle nested package', async () => {
test('handle nested package', async () => {
expect(await page.textContent('.a')).toBe('A@2.0.0')
expect(await page.textContent('.b')).toBe('B@1.0.0')
expect(await page.textContent('.nested-a')).toBe('A@1.0.0')
const c = await page.textContent('.c')
expect(c).toBe('es-C@1.0.0')
expect(await page.textContent('.side-c')).toBe(c)
})
11 changes: 11 additions & 0 deletions packages/playground/nested-deps/index.html
Expand Up @@ -7,14 +7,25 @@ <h2>direct dependency B</h2>
<h2>nested dependency A</h2>
<pre class="nested-a"></pre>

<h2>direct dependency C</h2>
<pre class="c"></pre>

<h2>side dependency C</h2>
<pre class="side-c"></pre>

<script type="module">
import A from 'test-package-a'
import B, { A as nestedA } from 'test-package-b'
import C from 'test-package-c'
import { C as sideC } from 'test-package-c/side'

text('.a', A)
text('.b', B)
text('.nested-a', nestedA)

text('.c', C)
text('.side-c', sideC)

function text(sel, text) {
document.querySelector(sel).textContent = text
}
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/nested-deps/package.json
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"test-package-a": "link:./test-package-a",
"test-package-b": "link:./test-package-b"
"test-package-b": "link:./test-package-b",
"test-package-c": "link:./test-package-c"
}
}
1 change: 1 addition & 0 deletions packages/playground/nested-deps/test-package-c/index-es.js
@@ -0,0 +1 @@
export default 'es-C@1.0.0'
2 changes: 2 additions & 0 deletions packages/playground/nested-deps/test-package-c/index.js
@@ -0,0 +1,2 @@
// this module should not be resolved
export default 'C@1.0.0'
6 changes: 6 additions & 0 deletions packages/playground/nested-deps/test-package-c/package.json
@@ -0,0 +1,6 @@
{
"name": "test-package-c",
"version": "1.0.0",
"main": "index.js",
"module": "index-es.js"
}
1 change: 1 addition & 0 deletions packages/playground/nested-deps/test-package-c/side.js
@@ -0,0 +1 @@
export { default as C } from 'test-package-c'
7 changes: 6 additions & 1 deletion packages/playground/nested-deps/vite.config.js
Expand Up @@ -3,6 +3,11 @@
*/
module.exports = {
optimizeDeps: {
include: ['test-package-a', 'test-package-b']
include: [
'test-package-a',
'test-package-b',
'test-package-c',
'test-package-c/side'
]
}
}
32 changes: 13 additions & 19 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -85,35 +85,29 @@ export function esbuildDepPlugin(
}
)

function resolveEntry(id: string, isEntry: boolean, resolveDir: string) {
function resolveEntry(id: string) {
const flatId = flattenId(id)
if (flatId in qualified) {
return isEntry
? {
path: flatId,
namespace: 'dep'
}
: {
path: require.resolve(qualified[flatId], {
paths: [resolveDir]
})
}
return {
path: flatId,
namespace: 'dep'
}
}
}

build.onResolve(
{ filter: /^[\w@][^:]/ },
async ({ path: id, importer, kind, resolveDir }) => {
const isEntry = !importer
async ({ path: id, importer, kind }) => {
// ensure esbuild uses our resolved entries
let entry
// if this is an entry, return entry namespace resolve result
if ((entry = resolveEntry(id, isEntry, resolveDir))) return entry

// check if this is aliased to an entry - also return entry namespace
const aliased = await _resolve(id, undefined, true)
if (aliased && (entry = resolveEntry(aliased, isEntry, resolveDir))) {
return entry
if (!importer) {
if ((entry = resolveEntry(id))) return entry
// check if this is aliased to an entry - also return entry namespace
const aliased = await _resolve(id, undefined, true)
if (aliased && (entry = resolveEntry(aliased))) {
return entry
}
}

// use vite's own resolver
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -7021,6 +7021,10 @@ test-exclude@^6.0.0:
version "0.0.0"
uid ""

"test-package-c@link:./packages/playground/nested-deps/test-package-c":
version "0.0.0"
uid ""

text-extensions@^1.0.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/text-extensions/-/text-extensions-1.9.0.tgz#1853e45fee39c945ce6f6c36b2d659b5aabc2a26"
Expand Down

0 comments on commit b465d3e

Please sign in to comment.