From 7aada5a0f71f60c8ebda6db0342978a2e03b5df8 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 22 Dec 2021 03:16:38 +0800 Subject: [PATCH 1/3] test: failed tests --- .../node/ssr/__tests__/ssrTransform.spec.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index cdce4cc4f629f8..9fb8c198851542 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -406,3 +406,28 @@ function c({ _ = bar() + foo() }) {} " `) }) + +test('object destructure alias', async () => { + expect( + ( + await ssrTransform( + ` +import { n } from 'foo' +const a = () => { + const { type: n = 'bar' } = {} +} +`, + null, + null + ) + ).code + ).toMatchInlineSnapshot(` + " + const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\"); + + const a = () => { + const { type: __vite_ssr_import_0__.n = 'bar' } = {} + } + " + `) +}) From cd5f428e11441d16498d31d6a5fb23c7e588c82b Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 22 Dec 2021 03:32:21 +0800 Subject: [PATCH 2/3] fix(ssr): handle object destructure alias, close #6222 --- packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts | 4 +++- packages/vite/src/node/ssr/ssrTransform.ts | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 9fb8c198851542..7a1979e9778844 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -415,6 +415,7 @@ test('object destructure alias', async () => { import { n } from 'foo' const a = () => { const { type: n = 'bar' } = {} + console.log(n) } `, null, @@ -426,7 +427,8 @@ const a = () => { const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\"); const a = () => { - const { type: __vite_ssr_import_0__.n = 'bar' } = {} + const { type: n = 'bar' } = {} + console.log(n) } " `) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 1931f0b4758314..d2c64b8bc010d9 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -343,6 +343,8 @@ 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) } From 83b016ef3626737e2c8fd72d74371dcf1e7bb256 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 22 Dec 2021 03:38:01 +0800 Subject: [PATCH 3/3] chore: thanks prettier --- packages/vite/src/node/ssr/ssrTransform.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index d2c64b8bc010d9..df68675c0847cd 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -344,7 +344,10 @@ function walk( 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) + setScope( + parentFunction, + (property.value.left as Identifier).name + ) } else { setScope(parentFunction, (property.value as Identifier).name) }