Skip to content

Commit

Permalink
fix(resolve): support submodules of optional peer deps (#14489)
Browse files Browse the repository at this point in the history
  • Loading branch information
decs authored and bluwy committed Oct 3, 2023
1 parent 0c6d289 commit 104971d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,11 @@ export function tryNodeResolve(
) {
const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data
if (mainPkg) {
const pkgName = getNpmPackageName(id)
if (
mainPkg.peerDependencies?.[id] &&
mainPkg.peerDependenciesMeta?.[id]?.optional
pkgName != null &&
mainPkg.peerDependencies?.[pkgName] &&
mainPkg.peerDependenciesMeta?.[pkgName]?.optional
) {
return {
id: `${optionalPeerDepId}:${id}:${mainPkg.name}`,
Expand Down
13 changes: 13 additions & 0 deletions playground/optimize-deps/__tests__/optimize-deps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ test('dep with optional peer dep', async () => {
}
})

test('dep with optional peer dep submodule', async () => {
expect(
await page.textContent('.dep-with-optional-peer-dep-submodule'),
).toMatch(`[success]`)
if (isServe) {
expect(browserErrors.map((error) => error.message)).toEqual(
expect.arrayContaining([
'Could not resolve "foobar/baz" imported by "@vitejs/test-dep-with-optional-peer-dep-submodule". Is it installed?',
]),
)
}
})

test('dep with css import', async () => {
expect(await getColor('.dep-linked-include')).toBe('red')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function callItself() {
return '[success]'
}

export async function callPeerDepSubmodule() {
return await import('foobar/baz')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@vitejs/test-dep-with-optional-peer-dep-submodule",
"private": true,
"version": "0.0.0",
"main": "index.js",
"type": "module",
"peerDependencies": {
"foobar": "0.0.0"
},
"peerDependenciesMeta": {
"foobar": {
"optional": true
}
}
}
13 changes: 13 additions & 0 deletions playground/optimize-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ <h2>Import from dependency with dynamic import</h2>
<h2>Import from dependency with optional peer dep</h2>
<div class="dep-with-optional-peer-dep"></div>

<h2>Import from dependency with optional peer dep submodule</h2>
<div class="dep-with-optional-peer-dep-submodule"></div>

<h2>Externalize known non-js files in optimize included dep</h2>
<div class="externalize-known-non-js"></div>

Expand Down Expand Up @@ -205,6 +208,16 @@ <h2>Pre bundle css modules require</h2>
callPeerDep()
</script>

<script type="module">
import {
callItself,
callPeerDepSubmodule,
} from '@vitejs/test-dep-with-optional-peer-dep-submodule'
text('.dep-with-optional-peer-dep-submodule', callItself())
// expect error as optional peer dep not installed
callPeerDepSubmodule()
</script>

<script type="module">
// should error on builtin modules (named import)
// no node: protocol intentionally
Expand Down
1 change: 1 addition & 0 deletions playground/optimize-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@vitejs/test-dep-with-builtin-module-esm": "file:./dep-with-builtin-module-esm",
"@vitejs/test-dep-with-dynamic-import": "file:./dep-with-dynamic-import",
"@vitejs/test-dep-with-optional-peer-dep": "file:./dep-with-optional-peer-dep",
"@vitejs/test-dep-with-optional-peer-dep-submodule": "file:./dep-with-optional-peer-dep-submodule",
"@vitejs/test-dep-non-optimized": "file:./dep-non-optimized",
"@vitejs/test-added-in-entries": "file:./added-in-entries",
"lodash-es": "^4.17.21",
Expand Down
15 changes: 15 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 104971d

Please sign in to comment.