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): transform class props #6261

Merged
merged 2 commits into from Dec 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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