diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 59c45c93b16a9c..6eac047bdcc97f 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -421,6 +421,18 @@ test('rewrite variables in default value of destructuring params', async () => { expect(result?.deps).toEqual(['vue']) }) +// #23232 +test('rewrite imported binding used as a computed key of a defaulted destructuring param', async () => { + const result = await ssrTransformSimple( + `import { fn } from 'vue';function A({ [fn]: foo } = {}){ return { [fn]: foo }; }`, + ) + expect(result?.code).toMatchInlineSnapshot(` + "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]}); + function A({ [__vite_ssr_import_0__.fn]: foo } = {}){ return { [__vite_ssr_import_0__.fn]: foo }; }" + `) + expect(result?.deps).toEqual(['vue']) +}) + test('do not rewrite when function declaration is in scope', async () => { const result = await ssrTransformSimple( `import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`, diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 65a5c67da7905d..6d1781f5b63fb8 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -592,8 +592,17 @@ function walk( return this.skip() } if (child.type !== 'Identifier') return - // do not record as scope variable if is a destructuring keyword - if (isStaticPropertyKey(child, parent)) return + // do not record as scope variable if it is a destructuring key + // (static or computed) rather than the bound value; a computed + // key like `{ [IMPORTED]: v } = {}` references an outer binding + // and must stay rewritable + if ( + parent?.type === 'Property' && + parent.key === child && + parent.value !== child + ) { + return + } // do not record if this is a default value // assignment of a destructuring variable if (