From 515caa5b42aa1ef43b4b7c20791240ecc001175d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 30 Nov 2022 07:01:50 +0900 Subject: [PATCH] fix: Dev SSR dep optimization + respect optimizeDeps.include (#11123) --- packages/vite/src/node/optimizer/index.ts | 3 ++- packages/vite/src/node/plugins/resolve.ts | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 7f904bc17bccc6..8bf99736c7ae62 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -707,7 +707,8 @@ export async function addManuallyIncludedOptimizeDeps( const resolve = config.createResolver({ asSrc: false, scan: true, - ssrOptimizeCheck: ssr + ssrOptimizeCheck: ssr, + ssrConfig: config.ssr }) for (const id of [...optimizeDepsInclude, ...extra]) { // normalize 'foo >bar` as 'foo > bar' to prevent same id being added diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index c436f0837386c1..ae612148769960 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -800,11 +800,11 @@ export function tryNodeResolve( : OPTIMIZABLE_ENTRY_RE.test(resolved) let exclude = depsOptimizer?.options.exclude - let include = depsOptimizer?.options.exclude + let include = depsOptimizer?.options.include if (options.ssrOptimizeCheck) { // we don't have the depsOptimizer exclude = options.ssrConfig?.optimizeDeps?.exclude - include = options.ssrConfig?.optimizeDeps?.exclude + include = options.ssrConfig?.optimizeDeps?.include } const skipOptimization = @@ -813,7 +813,10 @@ export function tryNodeResolve( exclude?.includes(pkgId) || exclude?.includes(nestedPath) || SPECIAL_QUERY_RE.test(resolved) || - (!isBuild && ssr) || + // During dev SSR, we don't have a way to reload the module graph if + // a non-optimized dep is found. So we need to skip optimization here. + // The only optimized deps are the ones explicitly listed in the config. + (!options.ssrOptimizeCheck && !isBuild && ssr) || // Only optimize non-external CJS deps during SSR by default (ssr && !isCJS &&