Skip to content

Commit 776ebf2

Browse files
authored
fix(compiler-sfc): fix using imported ref as template ref during dev (#7593)
close #7567
1 parent 201c46d commit 776ebf2

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+15
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,21 @@ return { get FooBaz() { return FooBaz }, get Last() { return Last } }
775775
})"
776776
`;
777777
778+
exports[`SFC compile <script setup> > dev mode import usage check > template ref 1`] = `
779+
"import { defineComponent as _defineComponent } from 'vue'
780+
import { foo, bar, Baz } from './foo'
781+
782+
export default /*#__PURE__*/_defineComponent({
783+
setup(__props, { expose: __expose }) {
784+
__expose();
785+
786+
787+
return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }
788+
}
789+
790+
})"
791+
`;
792+
778793
exports[`SFC compile <script setup> > dev mode import usage check > vue interpolations 1`] = `
779794
"import { defineComponent as _defineComponent } from 'vue'
780795
import { x, y, z, x$y } from './x'

packages/compiler-sfc/__tests__/compileScript.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,23 @@ describe('SFC compile <script setup>', () => {
513513
</template>
514514
`)
515515
})
516+
517+
test('template ref', () => {
518+
const { content } = compile(`
519+
<script setup lang="ts">
520+
import { foo, bar, Baz } from './foo'
521+
</script>
522+
<template>
523+
<div ref="foo"></div>
524+
<div ref=""></div>
525+
<Baz ref="bar" />
526+
</template>
527+
`)
528+
expect(content).toMatch(
529+
'return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }'
530+
)
531+
assertCode(content)
532+
})
516533
})
517534

518535
describe('inlineTemplate mode', () => {

packages/compiler-sfc/src/script/importUsageCheck.ts

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
5757
)}`
5858
}
5959
}
60+
if (prop.type === NodeTypes.ATTRIBUTE && prop.name === 'ref' && prop.value?.content) {
61+
code += `,${prop.value.content}`
62+
}
6063
}
6164
} else if (node.type === NodeTypes.INTERPOLATION) {
6265
code += `,${processExp(

0 commit comments

Comments
 (0)