Skip to content

Commit e137601

Browse files
authored
fix(ssr): class property keys hoisting matching imports (#22199)
1 parent 15f1c15 commit e137601

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,25 +860,35 @@ test('class props', async () => {
860860
expect(
861861
await ssrTransformSimpleCode(
862862
`
863-
import { remove, add } from 'vue'
863+
import { remove, add, update, del, call } from 'vue'
864864
865865
class A {
866866
remove = 1
867867
add = null
868+
update = update
869+
del = () => del()
870+
call = call(4)
868871
}
872+
873+
remove(2);
874+
add(4);
869875
`,
870876
),
871877
).toMatchInlineSnapshot(`
872-
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add"]});
878+
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add","update","del","call"]});
873879
874880
875881
876-
const add = __vite_ssr_import_0__.add;
877-
const remove = __vite_ssr_import_0__.remove;
878882
class A {
879883
remove = 1
880884
add = null
885+
update = __vite_ssr_import_0__.update
886+
del = () => (0,__vite_ssr_import_0__.del)()
887+
call = (0,__vite_ssr_import_0__.call)(4)
881888
}
889+
890+
(0,__vite_ssr_import_0__.remove)(2);
891+
(0,__vite_ssr_import_0__.add)(4);
882892
"
883893
`)
884894
})

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ async function ssrTransformScript(
344344
}
345345
},
346346
onIdentifier(id, parent, parentStack) {
347-
const grandparent = parentStack[1]
348347
const binding = idToImportMap.get(id.name)
349348
if (!binding) {
350349
return
@@ -360,9 +359,8 @@ async function ssrTransformScript(
360359
s.appendLeft(id.end, `: ${binding}`)
361360
}
362361
} else if (
363-
(parent.type === 'PropertyDefinition' &&
364-
grandparent?.type === 'ClassBody') ||
365-
(parent.type === 'ClassDeclaration' && id === parent.superClass)
362+
parent.type === 'ClassDeclaration' &&
363+
id === parent.superClass
366364
) {
367365
if (!declaredConst.has(id.name)) {
368366
declaredConst.add(id.name)
@@ -680,6 +678,13 @@ function isRefIdentifier(
680678
return false
681679
}
682680

681+
// class property key
682+
if (parent.type === 'PropertyDefinition' && !parent.computed) {
683+
// values can still contain identifier references,
684+
// but keys cannot unless computed.
685+
return parent.value === id
686+
}
687+
683688
// property key
684689
if (isStaticPropertyKey(id, parent)) {
685690
return false

0 commit comments

Comments
 (0)