Skip to content

Commit e5a4412

Browse files
committed
feat(sfc): support namespaced component tags when using <script setup>
1 parent a8edf2b commit e5a4412

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

+54
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,60 @@ describe('compiler: element transform', () => {
9595
expect(node.tag).toBe(`$setup["Example"]`)
9696
})
9797

98+
test('resolve component from setup bindings (inline)', () => {
99+
const { root, node } = parseWithElementTransform(`<Example/>`, {
100+
inline: true,
101+
bindingMetadata: {
102+
Example: BindingTypes.SETUP_MAYBE_REF
103+
}
104+
})
105+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
106+
expect(node.tag).toBe(`_unref(Example)`)
107+
})
108+
109+
test('resolve component from setup bindings (inline const)', () => {
110+
const { root, node } = parseWithElementTransform(`<Example/>`, {
111+
inline: true,
112+
bindingMetadata: {
113+
Example: BindingTypes.SETUP_CONST
114+
}
115+
})
116+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
117+
expect(node.tag).toBe(`Example`)
118+
})
119+
120+
test('resolve namespaced component from setup bindings', () => {
121+
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
122+
bindingMetadata: {
123+
Foo: BindingTypes.SETUP_MAYBE_REF
124+
}
125+
})
126+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
127+
expect(node.tag).toBe(`$setup["Foo"].Example`)
128+
})
129+
130+
test('resolve namespaced component from setup bindings (inline)', () => {
131+
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
132+
inline: true,
133+
bindingMetadata: {
134+
Foo: BindingTypes.SETUP_MAYBE_REF
135+
}
136+
})
137+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
138+
expect(node.tag).toBe(`_unref(Foo).Example`)
139+
})
140+
141+
test('resolve namespaced component from setup bindings (inline const)', () => {
142+
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
143+
inline: true,
144+
bindingMetadata: {
145+
Foo: BindingTypes.SETUP_CONST
146+
}
147+
})
148+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
149+
expect(node.tag).toBe(`Foo.Example`)
150+
})
151+
98152
test('do not resolve component from non-script-setup bindings', () => {
99153
const bindingMetadata = {
100154
Example: BindingTypes.SETUP_MAYBE_REF

packages/compiler-core/src/transforms/transformElement.ts

+7
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ export function resolveComponentType(
301301
if (fromSetup) {
302302
return fromSetup
303303
}
304+
const dotIndex = tag.indexOf('.')
305+
if (dotIndex > 0) {
306+
const ns = resolveSetupReference(tag.slice(0, dotIndex), context)
307+
if (ns) {
308+
return ns + tag.slice(dotIndex)
309+
}
310+
}
304311
}
305312

306313
// 4. Self referencing component (inferred from filename)

0 commit comments

Comments
 (0)