Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): handle object destructure alias, close #6222 #6224

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,30 @@ function c({ _ = bar() + foo() }) {}
"
`)
})

test('object destructure alias', async () => {
expect(
(
await ssrTransform(
`
import { n } from 'foo'
const a = () => {
const { type: n = 'bar' } = {}
console.log(n)
}
`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");

const a = () => {
const { type: n = 'bar' } = {}
console.log(n)
}
"
`)
})
5 changes: 5 additions & 0 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ function walk(
node.id.properties.forEach((property) => {
if (property.type === 'RestElement') {
setScope(parentFunction, (property.argument as Identifier).name)
} else if (property.value.type === 'AssignmentPattern') {
setScope(
parentFunction,
(property.value.left as Identifier).name
)
} else {
setScope(parentFunction, (property.value as Identifier).name)
}
Expand Down