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

This commit is contained in:
André Costa 2023-10-02 01:51:40 -07:00 committed by GitHub
parent cb3ab5f465
commit f80ff77a43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 2 deletions

View File

@ -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}`,

View File

@ -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')
})

View File

@ -0,0 +1,7 @@
export function callItself() {
return '[success]'
}
export async function callPeerDepSubmodule() {
return await import('foobar/baz')
}

View File

@ -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
}
}
}

View File

@ -65,6 +65,9 @@
<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>
@ -205,6 +208,16 @@
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

View File

@ -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",

View File

@ -864,6 +864,9 @@ importers:
'@vitejs/test-dep-with-optional-peer-dep':
specifier: file:./dep-with-optional-peer-dep
version: file:playground/optimize-deps/dep-with-optional-peer-dep
'@vitejs/test-dep-with-optional-peer-dep-submodule':
specifier: file:./dep-with-optional-peer-dep-submodule
version: file:playground/optimize-deps/dep-with-optional-peer-dep-submodule
'@vitejs/test-nested-exclude':
specifier: file:./nested-exclude
version: file:playground/optimize-deps/nested-exclude
@ -970,6 +973,8 @@ importers:
playground/optimize-deps/dep-with-optional-peer-dep: {}
playground/optimize-deps/dep-with-optional-peer-dep-submodule: {}
playground/optimize-deps/nested-exclude:
dependencies:
'@vitejs/test-nested-include':
@ -10330,6 +10335,16 @@ packages:
optional: true
dev: false
file:playground/optimize-deps/dep-with-optional-peer-dep-submodule:
resolution: {directory: playground/optimize-deps/dep-with-optional-peer-dep-submodule, type: directory}
name: '@vitejs/test-dep-with-optional-peer-dep-submodule'
peerDependencies:
foobar: 0.0.0
peerDependenciesMeta:
foobar:
optional: true
dev: false
file:playground/optimize-deps/nested-exclude:
resolution: {directory: playground/optimize-deps/nested-exclude, type: directory}
name: '@vitejs/test-nested-exclude'