Skip to content

Commit dce99c1

Browse files
committed
fix(compiler-sfc): fix dev regression for dot / namespace component usage
close #9947
1 parent 63c3e62 commit dce99c1

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { r
7979
})"
8080
`;
8181

82+
exports[`import namespace 1`] = `
83+
"import { defineComponent as _defineComponent } from 'vue'
84+
import * as Foo from './foo'
85+
86+
export default /*#__PURE__*/_defineComponent({
87+
setup(__props, { expose: __expose }) {
88+
__expose();
89+
90+
91+
return { get Foo() { return Foo } }
92+
}
93+
94+
})"
95+
`;
96+
8297
exports[`js template string interpolations 1`] = `
8398
"import { defineComponent as _defineComponent } from 'vue'
8499
import { VAR, VAR2, VAR3 } from './x'

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

+14
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,17 @@ test('property access (whitespace)', () => {
219219
expect(content).toMatch('return { get Foo() { return Foo } }')
220220
assertCode(content)
221221
})
222+
223+
// #9974
224+
test('namespace / dot component usage', () => {
225+
const { content } = compile(`
226+
<script setup lang="ts">
227+
import * as Foo from './foo'
228+
</script>
229+
<template>
230+
<Foo.Bar />
231+
</template>
232+
`)
233+
expect(content).toMatch('return { get Foo() { return Foo } }')
234+
assertCode(content)
235+
})

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
1616
* when not using inline mode.
1717
*/
1818
export function isImportUsed(local: string, sfc: SFCDescriptor): boolean {
19-
return resolveTemplateUsageCheckString(sfc).has(local)
19+
return resolveTemplateUsedIdentifiers(sfc).has(local)
2020
}
2121

2222
const templateUsageCheckCache = createCache<Set<string>>()
2323

24-
function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
24+
function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
2525
const { content, ast } = sfc.template!
2626
const cached = templateUsageCheckCache.get(content)
2727
if (cached) {
@@ -35,12 +35,14 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
3535
function walk(node: TemplateChildNode) {
3636
switch (node.type) {
3737
case NodeTypes.ELEMENT:
38+
let tag = node.tag
39+
if (tag.includes('.')) tag = tag.split('.')[0].trim()
3840
if (
39-
!parserOptions.isNativeTag!(node.tag) &&
40-
!parserOptions.isBuiltInComponent!(node.tag)
41+
!parserOptions.isNativeTag!(tag) &&
42+
!parserOptions.isBuiltInComponent!(tag)
4143
) {
42-
ids.add(camelize(node.tag))
43-
ids.add(capitalize(camelize(node.tag)))
44+
ids.add(camelize(tag))
45+
ids.add(capitalize(camelize(tag)))
4446
}
4547
for (let i = 0; i < node.props.length; i++) {
4648
const prop = node.props[i]

0 commit comments

Comments
 (0)