-
Notifications
You must be signed in to change notification settings - Fork 234
/
directives.ts
73 lines (62 loc) · 1.65 KB
/
directives.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
import {
transformVNodeArgs,
Directive,
ComponentInternalInstance,
VNodeTypes
} from 'vue'
import { isObject } from './utils'
interface CreateDirectivesTransformerConfig {
directives?: Record<string, Directive>
}
type VNodeArgsTransformer = NonNullable<
Parameters<typeof transformVNodeArgs>[0]
>
export function createDirectivesTransformer({
directives = {}
}: CreateDirectivesTransformerConfig): VNodeArgsTransformer {
const directivesTransformerCache: WeakMap<
VNodeTypes & object,
VNodeTypes & object
> = new WeakMap()
if (Object.keys(directives).length === 0) {
return (args) => args
}
return function directivesTransformer(
args,
instance: ComponentInternalInstance | null
) {
// We care about
// * object components
// * functional components
// * legacy components
// * legacy functional components
// * class components (sigh)
const [nodeType, props, children, patchFlag, dynamicProps] = args
if (!isObject(nodeType)) {
return args
}
if ((nodeType as any).__PATCHED) {
console.log('wtf')
}
const cachedTransformation = directivesTransformerCache.get(nodeType)
if (cachedTransformation) {
return [cachedTransformation, props, children, patchFlag, dynamicProps]
}
const nodeTypeWithDirectives = {
__PATCHED: true,
...nodeType,
directives: {
...((nodeType as any)?.directives ?? {}),
...directives
}
}
directivesTransformerCache.set(nodeType, nodeTypeWithDirectives as any)
return [
nodeTypeWithDirectives as any,
props,
children,
patchFlag,
dynamicProps
]
}
}