Skip to content

Commit 9dbfa2c

Browse files
committed
fix(compiler-vapor): properly handle static ref in inline mode
1 parent 9ab8e4c commit 9dbfa2c

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ export function render(_ctx) {
4343
}"
4444
`;
4545

46+
exports[`compiler: template ref transform > static ref (PROD) 1`] = `
47+
"
48+
const _setTemplateRef = _createTemplateRefSetter()
49+
const n0 = t0()
50+
_setTemplateRef(n0, foo)
51+
return n0
52+
"
53+
`;
54+
55+
exports[`compiler: template ref transform > static ref (inline mode) 1`] = `
56+
"
57+
const _setTemplateRef = _createTemplateRefSetter()
58+
const n0 = t0()
59+
_setTemplateRef(n0, foo)
60+
return n0
61+
"
62+
`;
63+
4664
exports[`compiler: template ref transform > static ref 1`] = `
4765
"import { createTemplateRefSetter as _createTemplateRefSetter, template as _template } from 'vue';
4866
const t0 = _template("<div></div>", true)

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

+11
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/src/generators/templateRef.ts

+19-1
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+
}

0 commit comments

Comments
 (0)