Skip to content

Commit 2e3fe59

Browse files
authored
fix(ssr): transform class props (vitejs#6261)
1 parent 1a6e2da commit 2e3fe59

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,34 @@ objRest()
498498
"
499499
`)
500500
})
501+
502+
test('class props', async () => {
503+
expect(
504+
(
505+
await ssrTransform(
506+
`
507+
import { remove, add } from 'vue'
508+
509+
class A {
510+
remove = 1
511+
add = null
512+
}
513+
`,
514+
null,
515+
null
516+
)
517+
).code
518+
).toMatchInlineSnapshot(`
519+
"
520+
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
521+
522+
523+
const add = __vite_ssr_import_0__.add;
524+
const remove = __vite_ssr_import_0__.remove;
525+
class A {
526+
remove = 1
527+
add = null
528+
}
529+
"
530+
`)
531+
})

packages/vite/src/node/ssr/ssrTransform.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export async function ssrTransform(
180180
// 3. convert references to import bindings & import.meta references
181181
walk(ast, {
182182
onIdentifier(id, parent, parentStack) {
183+
const grandparent = parentStack[parentStack.length - 2]
183184
const binding = idToImportMap.get(id.name)
184185
if (!binding) {
185186
return
@@ -195,8 +196,9 @@ export async function ssrTransform(
195196
s.appendLeft(id.end, `: ${binding}`)
196197
}
197198
} else if (
198-
parent.type === 'ClassDeclaration' &&
199-
id === parent.superClass
199+
(parent.type === 'PropertyDefinition' &&
200+
grandparent?.type === 'ClassBody') ||
201+
(parent.type === 'ClassDeclaration' && id === parent.superClass)
200202
) {
201203
if (!declaredConst.has(id.name)) {
202204
declaredConst.add(id.name)

0 commit comments

Comments
 (0)