diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index a25336b425657d..f91444b49636a1 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -231,6 +231,20 @@ test('rewrite variable in string interpolation in function nested arguments', as expect(result.deps).toEqual(['vue']) }) +// #6520 +test('rewrite variables in default value of destructuring params', async () => { + const result = await ssrTransform( + `import { fn } from 'vue';function A({foo = fn}){ return {}; }`, + null, + null + ) + expect(result.code).toMatchInlineSnapshot(` + "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\"); + function A({foo = __vite_ssr_import_0__.fn}){ return {}; }" + `) + expect(result.deps).toEqual(['vue']) +}) + test('do not rewrite when function declaration is in scope', async () => { const result = await ssrTransform( `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 63bbada1ab1ecd..c27804e87299e1 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -350,14 +350,19 @@ function walk( } ;(eswalk as any)(p.type === 'AssignmentPattern' ? p.left : p, { enter(child: Node, parent: Node) { + // skip params default value of destructure + if ( + parent?.type === 'AssignmentPattern' && + parent?.right === child + ) { + 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 if this is a default value // assignment of a destructuring variable if ( - (parent?.type === 'AssignmentPattern' && - parent?.right === child) || (parent?.type === 'TemplateLiteral' && parent?.expressions.includes(child)) || (parent?.type === 'CallExpression' && parent?.callee === child)