Skip to content

Commit fee67e5

Browse files
committed
chore: Merge branch 'vapor' into edison/fix/cacheSameExpr
2 parents f77fd2f + 66f16ee commit fee67e5

File tree

9 files changed

+109
-47
lines changed

9 files changed

+109
-47
lines changed

packages/compiler-sfc/src/compileScript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ export function compileScript(
984984
ctx.s.prependLeft(
985985
startOffset,
986986
`\n${genDefaultAs} /*@__PURE__*/${ctx.helper(
987-
vapor ? `defineVaporComponent` : `defineComponent`,
987+
vapor && !ssr ? `defineVaporComponent` : `defineComponent`,
988988
)}({${def}${runtimeOptions}\n ${
989989
hasAwait ? `async ` : ``
990990
}setup(${args}) {\n${exposeCall}`,

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformTemplateRef.spec.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ export function render(_ctx) {
6363
}"
6464
`;
6565

66+
exports[`compiler: template ref transform > static ref (inline mode) 1`] = `
67+
"
68+
const _setTemplateRef = _createTemplateRefSetter()
69+
const n0 = t0()
70+
_setTemplateRef(n0, foo)
71+
return n0
72+
"
73+
`;
74+
6675
exports[`compiler: template ref transform > static ref 1`] = `
6776
"import { createTemplateRefSetter as _createTemplateRefSetter, template as _template } from 'vue';
6877
const t0 = _template("<div></div>", true)

packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ export function render(_ctx) {
128128
}"
129129
`;
130130

131+
exports[`cache multiple access > optional chaining 1`] = `
132+
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
133+
const t0 = _template("<div></div>", true)
134+
135+
export function render(_ctx) {
136+
const n0 = t0()
137+
_renderEffect(() => {
138+
const _obj = _ctx.obj
139+
_setProp(n0, "id", _obj?.foo + _obj?.bar)
140+
})
141+
return n0
142+
}"
143+
`;
144+
131145
exports[`cache multiple access > repeated expression in expressions 1`] = `
132146
"import { setProp as _setProp, renderEffect as _renderEffect, template as _template } from 'vue';
133147
const t0 = _template("<div></div>")

packages/compiler-vapor/__tests__/transforms/transformTemplateRef.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BindingTypes } from '@vue/compiler-dom'
12
import {
23
DynamicFlag,
34
type ForIRNode,
@@ -48,6 +49,16 @@ describe('compiler: template ref transform', () => {
4849
expect(code).contains('_setTemplateRef(n0, "foo")')
4950
})
5051

52+
test('static ref (inline mode)', () => {
53+
const { code } = compileWithTransformRef(`<div ref="foo" />`, {
54+
inline: true,
55+
bindingMetadata: { foo: BindingTypes.SETUP_REF },
56+
})
57+
expect(code).matchSnapshot()
58+
// pass the actual ref
59+
expect(code).contains('_setTemplateRef(n0, foo)')
60+
})
61+
5162
test('dynamic ref', () => {
5263
const { ir, code } = compileWithTransformRef(`<div :ref="foo" />`)
5364

packages/compiler-vapor/__tests__/transforms/vBind.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,13 @@ describe('cache multiple access', () => {
813813
expect(code).contains('_setStyle(n0, {color: _color})')
814814
})
815815

816+
test('optional chaining', () => {
817+
const { code } = compileWithVBind(`<div :id="obj?.foo + obj?.bar"></div>`)
818+
expect(code).matchSnapshot()
819+
expect(code).contains('const _obj = _ctx.obj')
820+
expect(code).contains('_setProp(n0, "id", _obj?.foo + _obj?.bar)')
821+
})
822+
816823
test('not cache variable only used in property shorthand', () => {
817824
const { code } = compileWithVBind(`
818825
<div :style="{color}" />

packages/compiler-vapor/src/generators/expression.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ function extractMemberExpression(
656656
case 'CallExpression': // foo[bar(baz)]
657657
return `${extractMemberExpression(exp.callee, onIdentifier)}(${exp.arguments.map(arg => extractMemberExpression(arg, onIdentifier)).join(', ')})`
658658
case 'MemberExpression': // foo[bar.baz]
659+
case 'OptionalMemberExpression': // foo?.bar
659660
const object = extractMemberExpression(exp.object, onIdentifier)
660661
const prop = exp.computed
661662
? `[${extractMemberExpression(exp.property, onIdentifier)}]`

packages/compiler-vapor/src/generators/templateRef.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { genExpression } from './expression'
22
import type { CodegenContext } from '../generate'
33
import type { DeclareOldRefIRNode, SetTemplateRefIRNode } from '../ir'
44
import { type CodeFragment, NEWLINE, genCall } from './utils'
5+
import { BindingTypes, type SimpleExpressionNode } from '@vue/compiler-dom'
56

67
export const setTemplateRefIdent = `_setTemplateRef`
78

@@ -15,7 +16,7 @@ export function genSetTemplateRef(
1516
...genCall(
1617
setTemplateRefIdent, // will be generated in root scope
1718
`n${oper.element}`,
18-
genExpression(oper.value, context),
19+
genRefValue(oper.value, context),
1920
oper.effect ? `r${oper.element}` : oper.refFor ? 'void 0' : undefined,
2021
oper.refFor && 'true',
2122
),
@@ -25,3 +26,20 @@ export function genSetTemplateRef(
2526
export function genDeclareOldRef(oper: DeclareOldRefIRNode): CodeFragment[] {
2627
return [NEWLINE, `let r${oper.id}`]
2728
}
29+
30+
function genRefValue(value: SimpleExpressionNode, context: CodegenContext) {
31+
// in inline mode there is no setupState object, so we can't use string
32+
// keys to set the ref. Instead, we need to transform it to pass the
33+
// actual ref instead.
34+
if (!__BROWSER__ && value && context.options.inline) {
35+
const binding = context.options.bindingMetadata[value.content]
36+
if (
37+
binding === BindingTypes.SETUP_LET ||
38+
binding === BindingTypes.SETUP_REF ||
39+
binding === BindingTypes.SETUP_MAYBE_REF
40+
) {
41+
return [value.content]
42+
}
43+
}
44+
return genExpression(value, context)
45+
}

packages/runtime-vapor/src/vdomInterop.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
isEmitListener,
1717
onScopeDispose,
1818
renderSlot,
19+
shallowReactive,
1920
shallowRef,
2021
simpleSetCurrentInstance,
2122
} from '@vue/runtime-dom'
@@ -163,7 +164,8 @@ function createVDOMComponent(
163164

164165
// overwrite how the vdom instance handles props
165166
vnode.vi = (instance: ComponentInternalInstance) => {
166-
instance.props = wrapper.props
167+
// ensure props are shallow reactive to align with VDOM behavior.
168+
instance.props = shallowReactive(wrapper.props)
167169

168170
const attrs = (instance.attrs = createInternalObject())
169171
for (const key in wrapper.attrs) {

pnpm-lock.yaml

Lines changed: 44 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)