-
Notifications
You must be signed in to change notification settings - Fork 431
/
vue.ts
365 lines (306 loc) · 8.1 KB
/
vue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import {
ref,
ComponentPublicInstance,
ComponentOptions,
VNode,
SetupContext,
VNodeProps,
AllowedComponentProps,
ComponentCustomProps,
proxyRefs,
Prop,
ComponentObjectPropsOptions,
EmitsOptions,
} from 'vue'
import { VueWithProps } from './props'
function defineGetter<T, K extends keyof T>(
obj: T,
key: K,
getter: () => T[K]
): void {
Object.defineProperty(obj, key, {
get: getter,
enumerable: false,
configurable: true,
})
}
function defineProxy(proxy: any, key: string, target: any): void {
Object.defineProperty(proxy, key, {
get: () => target[key].value,
set: (value) => {
target[key].value = value
},
enumerable: true,
configurable: true,
})
}
function getSuper(Ctor: typeof VueImpl): typeof VueImpl | undefined {
const superProto = Object.getPrototypeOf(Ctor.prototype)
if (!superProto) {
return undefined
}
return superProto.constructor as typeof VueImpl
}
function getOwn<T extends Object, K extends keyof T>(
value: T,
key: K
): T[K] | undefined {
return value.hasOwnProperty(key) ? value[key] : undefined
}
export interface VueStatic {
// -- Class component configs
/**
* @internal
* The cache of __vccOpts
*/
__c?: ComponentOptions
/**
* @internal
* The base options specified to this class.
*/
__b?: ComponentOptions
/**
* @internal
* Component options specified with `@Options` decorator
*/
__o?: ComponentOptions
/**
* @internal
* Decorators applied to this class.
*/
__d?: ((options: ComponentOptions) => void)[]
/**
* @internal
* Registered (lifecycle) hooks that will be ported from class methods
* into component options.
*/
__h: string[]
/**
* @internal
* Final component options object that Vue core processes.
* The name must be __vccOpts since it is the contract with the Vue core.
*/
__vccOpts: ComponentOptions
// --- Vue Loader etc injections
/** @internal */
render?: () => VNode | void
/** @internal */
ssrRender?: () => void
/** @internal */
__file?: string
/** @internal */
__cssModules?: Record<string, any>
/** @internal */
__scopeId?: string
/** @internal */
__hmrId?: string
}
export type PublicProps = VNodeProps &
AllowedComponentProps &
ComponentCustomProps
export type VueBase = Vue<unknown, never[]>
export type VueMixin<V extends VueBase = VueBase> = VueStatic & {
prototype: V
}
export interface ClassComponentHooks {
// To be extended on user land
data?(): object
beforeCreate?(): void
created?(): void
beforeMount?(): void
mounted?(): void
beforeUnmount?(): void
unmounted?(): void
beforeUpdate?(): void
updated?(): void
activated?(): void
deactivated?(): void
render?(): VNode | void
errorCaptured?(err: Error, vm: Vue, info: string): boolean | undefined
serverPrefetch?(): Promise<unknown>
}
export type Vue<
Props = unknown,
Emits extends EmitsOptions = {},
DefaultProps = {}
> = ComponentPublicInstance<
Props,
{},
{},
{},
{},
Emits,
PublicProps,
DefaultProps,
true
> &
ClassComponentHooks
export interface VueConstructor<V extends VueBase = Vue> extends VueMixin<V> {
new (...args: any[]): V
// --- Public APIs
registerHooks(keys: string[]): void
with<P extends { new (): unknown }>(
Props: P
): VueConstructor<V & VueWithProps<InstanceType<P>>>
}
class VueImpl {
static __h = [
'data',
'beforeCreate',
'created',
'beforeMount',
'mounted',
'beforeUnmount',
'unmounted',
'beforeUpdate',
'updated',
'activated',
'deactivated',
'render',
'errorCaptured',
'serverPrefetch',
]
static get __vccOpts(): ComponentOptions {
// Early return if `this` is base class as it does not have any options
if (this === Vue) {
return {}
}
const Ctor = this as VueConstructor
const cache = getOwn(Ctor, '__c')
if (cache) {
return cache
}
// If the options are provided via decorator use it as a base
const options: ComponentOptions = { ...getOwn(Ctor, '__o') }
Ctor.__c = options
// Handle super class options
const Super = getSuper(Ctor)
if (Super) {
options.extends = Super.__vccOpts
}
// Inject base options as a mixin
const base = getOwn(Ctor, '__b')
if (base) {
options.mixins = options.mixins || []
options.mixins.unshift(base)
}
options.methods = { ...options.methods }
options.computed = { ...options.computed }
const proto = Ctor.prototype
Object.getOwnPropertyNames(proto).forEach((key) => {
if (key === 'constructor') {
return
}
// hooks
if (Ctor.__h.indexOf(key) > -1) {
;(options as any)[key] = (proto as any)[key]
return
}
const descriptor = Object.getOwnPropertyDescriptor(proto, key)!
// methods
if (typeof descriptor.value === 'function') {
;(options.methods as any)[key] = descriptor.value
return
}
// computed properties
if (descriptor.get || descriptor.set) {
;(options.computed as any)[key] = {
get: descriptor.get,
set: descriptor.set,
}
return
}
})
options.setup = function (props: Record<string, any>, ctx: SetupContext) {
const data: any = new Ctor(props, ctx)
const dataKeys = Object.keys(data)
const plainData: any = {}
let promise: Promise<any> | null = null
// Initialize reactive data and convert constructor `this` to a proxy
dataKeys.forEach((key) => {
// Skip if the value is undefined not to make it reactive.
// If the value has `__s`, it's a value from `setup` helper, proceed it later.
if (data[key] === undefined || (data[key] && data[key].__s)) {
return
}
plainData[key] = ref(data[key])
defineProxy(data, key, plainData)
})
// Invoke composition functions
dataKeys.forEach((key) => {
if (data[key] && data[key].__s) {
const setupState = data[key].__s()
if (setupState instanceof Promise) {
if (!promise) {
promise = Promise.resolve(plainData)
}
promise = promise.then(() => {
return setupState.then((value) => {
plainData[key] = proxyRefs(value)
return plainData
})
})
} else {
plainData[key] = proxyRefs(setupState)
}
}
})
return promise ?? plainData
}
const decorators = getOwn(Ctor, '__d')
if (decorators) {
decorators.forEach((fn) => fn(options))
}
// from Vue Loader
const injections = [
'render',
'ssrRender',
'__file',
'__cssModules',
'__scopeId',
'__hmrId',
]
injections.forEach((key) => {
if ((Ctor as any)[key]) {
options[key] = (Ctor as any)[key]
}
})
return options
}
static registerHooks(keys: string[]): void {
this.__h.push(...keys)
}
static with(Props: { new (): unknown }): VueConstructor {
const propsMeta = new Props() as Record<string, Prop<any> | undefined>
const props: ComponentObjectPropsOptions = {}
Object.keys(propsMeta).forEach((key) => {
const meta = propsMeta[key]
props[key] = meta ?? null
})
class PropsMixin extends this {
static __b: ComponentOptions = {
props,
}
}
return PropsMixin as VueConstructor
}
$props!: Record<string, any>
$emit!: (event: string, ...args: any[]) => void
$attrs!: ComponentPublicInstance['$attrs']
$slots!: ComponentPublicInstance['$slots']
constructor(props: Record<string, any>, ctx: SetupContext) {
defineGetter(this, '$props', () => props)
defineGetter(this, '$attrs', () => ctx.attrs)
defineGetter(this, '$slots', () => ctx.slots)
defineGetter(this, '$emit', () => ctx.emit)
Object.keys(props).forEach((key) => {
Object.defineProperty(this, key, {
enumerable: false,
configurable: true,
writable: true,
value: (props as any)[key],
})
})
}
}
export const Vue: VueConstructor = VueImpl as VueConstructor