From 104971d9b4b6b3994bc8362e7ffabd7d2bf3c311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Costa?= Date: Mon, 2 Oct 2023 01:51:40 -0700 Subject: [PATCH] fix(resolve): support submodules of optional peer deps (#14489) --- packages/vite/src/node/plugins/resolve.ts | 6 ++++-- .../optimize-deps/__tests__/optimize-deps.spec.ts | 13 +++++++++++++ .../dep-with-optional-peer-dep-submodule/index.js | 7 +++++++ .../package.json | 15 +++++++++++++++ playground/optimize-deps/index.html | 13 +++++++++++++ playground/optimize-deps/package.json | 1 + pnpm-lock.yaml | 15 +++++++++++++++ 7 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 playground/optimize-deps/dep-with-optional-peer-dep-submodule/index.js create mode 100644 playground/optimize-deps/dep-with-optional-peer-dep-submodule/package.json 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() + +