11import { defineComponent } from 'vue' ;
2- import { obtainSlot } from './utils' ;
3-
4- const LifecycleNames = [
5- "beforeCreate" ,
6- "created" ,
7- "beforeMount" ,
8- "mounted" ,
9- "beforeUpdate" ,
10- "updated" ,
11- "activated" ,
12- "deactivated" ,
13- "beforeDestroy" ,
14- "beforeUnmount" ,
15- "destroyed" ,
16- "unmounted" ,
17- "renderTracked" ,
18- "renderTriggered" ,
19- "errorCaptured"
20- ]
21-
22-
23- function makeObject ( names : string [ ] , obj : any ) {
24- return names . reduce < Record < string , any > > ( ( pv , cv ) => {
25- pv [ cv ] = obj [ cv ]
26- return pv
27- } , { } )
2+ import { build as optionComputed } from './option/computed'
3+ import { build as optionData } from './option/data'
4+ import { build as optionMethodsAndLifecycle } from './option/methodsAndLifecycle'
5+ import { build as optionRef } from './option/ref'
6+ export interface OptionBuilder {
7+ data ?: Record < string , any >
8+ methods ?: Record < string , Function >
9+ lifecycle ?: Record < string , Function >
10+ computed ?: Record < string , any >
2811}
29- export function Component ( cons : { new ( ) : any , prototype : any } ) {
30- const slot = obtainSlot ( cons . prototype )
31- const sample = new cons
32- const proto = cons . prototype
33- const LifecycleFunctions :Record < string , Function > = { }
34- const methodNames = Object . getOwnPropertyNames ( proto ) . filter ( name => {
35- if ( name === 'constructor' ) {
36- return false
37- }
38- if ( typeof proto [ name ] === 'function' ) {
39- if ( LifecycleNames . includes ( name ) ) {
40- LifecycleFunctions [ name ] = proto [ name ]
41- return false
42- }
43-
44- return true
45- }
46- } )
47-
48- const dataNames = Object . getOwnPropertyNames ( sample )
49-
50- const def = defineComponent ( {
12+ export interface Cons { new ( ) : any , prototype : any }
13+ export function Component ( cons : Cons ) {
14+ const optionBuilder : OptionBuilder = { }
15+ optionComputed ( cons , optionBuilder )
16+ optionMethodsAndLifecycle ( cons , optionBuilder )
17+ optionRef ( cons , optionBuilder )
18+ const raw = {
5119 data ( ) {
52- return makeObject ( dataNames , sample )
20+ const optionBuilder : OptionBuilder = { }
21+ optionData ( cons , optionBuilder )
22+ return optionBuilder . data ?? { }
5323 } ,
54- methods : makeObject ( methodNames , proto ) ,
55- ...LifecycleFunctions
56- } )
24+ methods : optionBuilder . methods ,
25+ computed : optionBuilder . computed ,
26+ ...optionBuilder . lifecycle
27+ }
28+ const def = defineComponent ( raw )
5729 return def as any
58-
59-
6030}
0 commit comments