Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): support submodules of optional peer deps #14489

Merged
merged 1 commit into from Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -732,9 +732,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
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
@@ -0,0 +1,7 @@
export function callItself() {
return '[success]'
}

export async function callPeerDepSubmodule() {
return await import('foobar/baz')
}
@@ -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
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
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.