diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 26c30b9a9c9edc..5932b720875bef 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -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}`, diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index 1e54aa1bd700b5..a42e8838288e9e 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -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') }) diff --git a/playground/optimize-deps/dep-with-optional-peer-dep-submodule/index.js b/playground/optimize-deps/dep-with-optional-peer-dep-submodule/index.js new file mode 100644 index 00000000000000..d2ace777ebf930 --- /dev/null +++ b/playground/optimize-deps/dep-with-optional-peer-dep-submodule/index.js @@ -0,0 +1,7 @@ +export function callItself() { + return '[success]' +} + +export async function callPeerDepSubmodule() { + return await import('foobar/baz') +} diff --git a/playground/optimize-deps/dep-with-optional-peer-dep-submodule/package.json b/playground/optimize-deps/dep-with-optional-peer-dep-submodule/package.json new file mode 100644 index 00000000000000..82dcdff5dea262 --- /dev/null +++ b/playground/optimize-deps/dep-with-optional-peer-dep-submodule/package.json @@ -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 + } + } +} diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 6cf34f1f4c3469..af31bcff8a4d29 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -65,6 +65,9 @@

Import from dependency with dynamic import

Import from dependency with optional peer dep

+

Import from dependency with optional peer dep submodule

+
+

Externalize known non-js files in optimize included dep

@@ -205,6 +208,16 @@

Pre bundle css modules require

callPeerDep() + +