Skip to content

Commit

Permalink
fix(ssr): should correctly transfrom identifier in ssr (#6548)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bigfish8 committed Jan 18, 2022
1 parent 155fd11 commit 15cd975
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -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 }; }`,
Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -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)
Expand Down

0 comments on commit 15cd975

Please sign in to comment.