Skip to content

Commit

Permalink
fix(ssr): transform class props (vitejs#6261)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 26, 2021
1 parent 1a6e2da commit 2e3fe59
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Expand Up @@ -498,3 +498,34 @@ objRest()
"
`)
})

test('class props', async () => {
expect(
(
await ssrTransform(
`
import { remove, add } from 'vue'
class A {
remove = 1
add = null
}
`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
const add = __vite_ssr_import_0__.add;
const remove = __vite_ssr_import_0__.remove;
class A {
remove = 1
add = null
}
"
`)
})
6 changes: 4 additions & 2 deletions packages/vite/src/node/ssr/ssrTransform.ts
Expand Up @@ -180,6 +180,7 @@ export async function ssrTransform(
// 3. convert references to import bindings & import.meta references
walk(ast, {
onIdentifier(id, parent, parentStack) {
const grandparent = parentStack[parentStack.length - 2]
const binding = idToImportMap.get(id.name)
if (!binding) {
return
Expand All @@ -195,8 +196,9 @@ export async function ssrTransform(
s.appendLeft(id.end, `: ${binding}`)
}
} else if (
parent.type === 'ClassDeclaration' &&
id === parent.superClass
(parent.type === 'PropertyDefinition' &&
grandparent?.type === 'ClassBody') ||
(parent.type === 'ClassDeclaration' && id === parent.superClass)
) {
if (!declaredConst.has(id.name)) {
declaredConst.add(id.name)
Expand Down

0 comments on commit 2e3fe59

Please sign in to comment.