@@ -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+ ) => {
74+ const pascalTag = kebabToPascalCase ( kebabTag )
75+ return ( type : any , stubs : Record < string , boolean | Component > ) => {
76+ if ( predicate ( type ) && ( pascalTag in stubs || kebabTag in stubs ) ) {
77+ if ( kebabTag in stubs && stubs [ kebabTag ] === false ) return type
78+ if ( pascalTag in stubs && stubs [ pascalTag ] === false ) return type
79+
80+ return createStub ( {
81+ name : kebabTag ,
82+ type,
83+ renderStubDefaultSlot : true
84+ } )
85+ }
86+ }
87+ }
88+
5989export const createStub = ( {
6090 name,
6191 type,
@@ -134,60 +164,12 @@ 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 defaultStub = createDefaultStub (
169+ tag ,
170+ DEFAULT_STUBS [ tag as keyof typeof DEFAULT_STUBS ]
171+ ) ( type , stubs )
172+ if ( defaultStub ) return defaultStub
191173 }
192174
193175 // Don't stub root components
0 commit comments