Skip to content

Commit 211afd8

Browse files
alecgibsoncexbrayat
authored andcommitted
chore: DRY up stub component transformer
1 parent d057da9 commit 211afd8

File tree

1 file changed

+34
-54
lines changed

1 file changed

+34
-54
lines changed

src/vnodeTransformers/stubComponentsTransformer.ts

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ const normalizeStubProps = (props: ComponentPropsOptions) => {
5656
}, {})
5757
}
5858

59+
const clearAndUpper = (text: string) => text.replace(/-/, '').toUpperCase()
60+
const kebabToPascalCase = (tag: string) =>
61+
tag.replace(/(^\w|-\w)/g, clearAndUpper)
62+
63+
const DEFAULT_STUBS = {
64+
teleport: isTeleport,
65+
'keep-alive': isKeepAlive,
66+
transition: (type: any) => type === Transition || type === BaseTransition,
67+
'transition-group': (type: any) => type === TransitionGroup
68+
}
69+
70+
const createDefaultStub = (
71+
kebabTag: string,
72+
predicate: (type: any) => boolean,
73+
type: any,
74+
stubs: Record<string, boolean | Component>
75+
) => {
76+
const pascalTag = kebabToPascalCase(kebabTag)
77+
if (predicate(type) && (pascalTag in stubs || kebabTag in stubs)) {
78+
if (kebabTag in stubs && stubs[kebabTag] === false) return type
79+
if (pascalTag in stubs && stubs[pascalTag] === false) return type
80+
81+
return createStub({
82+
name: kebabTag,
83+
type,
84+
renderStubDefaultSlot: true
85+
})
86+
}
87+
}
88+
5989
export const createStub = ({
6090
name,
6191
type,
@@ -134,60 +164,10 @@ export function createStubComponentsTransformer({
134164
renderStubDefaultSlot = false
135165
}: CreateStubComponentsTransformerConfig): VTUVNodeTypeTransformer {
136166
return function componentsTransformer(type, instance) {
137-
// stub teleport by default via config.global.stubs
138-
if (isTeleport(type) && ('teleport' in stubs || 'Teleport' in stubs)) {
139-
if ('teleport' in stubs && stubs['teleport'] === false) return type
140-
if ('Teleport' in stubs && stubs['Teleport'] === false) return type
141-
142-
return createStub({
143-
name: 'teleport',
144-
type,
145-
renderStubDefaultSlot: true
146-
})
147-
}
148-
149-
// stub keep-alive/KeepAlive by default via config.global.stubs
150-
if (isKeepAlive(type) && ('keep-alive' in stubs || 'KeepAlive' in stubs)) {
151-
if ('keep-alive' in stubs && stubs['keep-alive'] === false) return type
152-
if ('KeepAlive' in stubs && stubs['KeepAlive'] === false) return type
153-
154-
return createStub({
155-
name: 'keep-alive',
156-
type,
157-
renderStubDefaultSlot: true
158-
})
159-
}
160-
161-
// stub transition by default via config.global.stubs
162-
if (
163-
(type === Transition || (type as any) === BaseTransition) &&
164-
('transition' in stubs || 'Transition' in stubs)
165-
) {
166-
if ('transition' in stubs && stubs['transition'] === false) return type
167-
if ('Transition' in stubs && stubs['Transition'] === false) return type
168-
169-
return createStub({
170-
name: 'transition',
171-
type,
172-
renderStubDefaultSlot: true
173-
})
174-
}
175-
176-
// stub transition-group by default via config.global.stubs
177-
if (
178-
(type as any) === TransitionGroup &&
179-
('transition-group' in stubs || 'TransitionGroup' in stubs)
180-
) {
181-
if ('transition-group' in stubs && stubs['transition-group'] === false)
182-
return type
183-
if ('TransitionGroup' in stubs && stubs['TransitionGroup'] === false)
184-
return type
185-
186-
return createStub({
187-
name: 'transition-group',
188-
type,
189-
renderStubDefaultSlot: true
190-
})
167+
for (const tag in DEFAULT_STUBS) {
168+
const predicate = DEFAULT_STUBS[tag as keyof typeof DEFAULT_STUBS]
169+
const defaultStub = createDefaultStub(tag, predicate, type, stubs)
170+
if (defaultStub) return defaultStub
191171
}
192172

193173
// Don't stub root components

0 commit comments

Comments
 (0)