Skip to content

Commit

Permalink
fix(compiler-sfc): fix using imported ref as template ref during dev (#…
Browse files Browse the repository at this point in the history
…7593)

close #7567
  • Loading branch information
vaakian authored Jul 11, 2023
1 parent 201c46d commit 776ebf2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,21 @@ return { get FooBaz() { return FooBaz }, get Last() { return Last } }
})"
`;
exports[`SFC compile <script setup> > dev mode import usage check > template ref 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { foo, bar, Baz } from './foo'
export default /*#__PURE__*/_defineComponent({
setup(__props, { expose: __expose }) {
__expose();
return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }
}
})"
`;
exports[`SFC compile <script setup> > dev mode import usage check > vue interpolations 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { x, y, z, x$y } from './x'
Expand Down
17 changes: 17 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,23 @@ describe('SFC compile <script setup>', () => {
</template>
`)
})

test('template ref', () => {
const { content } = compile(`
<script setup lang="ts">
import { foo, bar, Baz } from './foo'
</script>
<template>
<div ref="foo"></div>
<div ref=""></div>
<Baz ref="bar" />
</template>
`)
expect(content).toMatch(
'return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }'
)
assertCode(content)
})
})

describe('inlineTemplate mode', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler-sfc/src/script/importUsageCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
)}`
}
}
if (prop.type === NodeTypes.ATTRIBUTE && prop.name === 'ref' && prop.value?.content) {
code += `,${prop.value.content}`
}
}
} else if (node.type === NodeTypes.INTERPOLATION) {
code += `,${processExp(
Expand Down

0 comments on commit 776ebf2

Please sign in to comment.