-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
vue2.ts
88 lines (78 loc) 路 2.94 KB
/
vue2.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
import type {
BlockStatement, CallExpression, File, FunctionExpression, Node, ObjectProperty, VariableDeclaration,
} from '@babel/types'
import type MagicString from 'magic-string'
import type { ParseResult } from '@babel/parser'
import { importModule, isPackageExists } from 'local-pkg'
import type { ResolveResult } from '../../transformer'
/**
* get Vue 2 render function position
* @param ast
* @returns
*/
const getRenderFnStart = (ast: ParseResult<File>): number => {
const renderFn = ast.program.body.find((node): node is VariableDeclaration =>
node.type === 'VariableDeclaration'
&& node.declarations[0].id.type === 'Identifier'
&& node.declarations[0].id.name === 'render',
)
const start = (((renderFn?.declarations[0].init as FunctionExpression)?.body) as BlockStatement)?.start
if (start === null || start === undefined)
throw new Error('[unplugin-vue-components:directive] Cannot find render function position.')
return start + 1
}
export default async function resolveVue2(code: string, s: MagicString): Promise<ResolveResult[]> {
if (!isPackageExists('@babel/parser'))
throw new Error('[unplugin-vue-components:directive] To use Vue 2 directive you will need to install Babel first: "npm install -D @babel/parser"')
const { parse } = await importModule<typeof import('@babel/parser')>('@babel/parser')
const ast = parse(code, {
sourceType: 'module',
})
const nodes: CallExpression[] = []
const { walk } = await import('estree-walker')
walk(ast.program as any, {
enter(node: any) {
if ((node as Node).type === 'CallExpression')
nodes.push(node)
},
})
const results: ResolveResult[] = []
for (const node of nodes) {
const { callee, arguments: args } = node
// _c(_, {})
if (callee.type !== 'Identifier' || callee.name !== '_c' || args[1]?.type !== 'ObjectExpression')
continue
// { directives: [] }
const directives = args[1].properties.find(
(property): property is ObjectProperty =>
property.type === 'ObjectProperty'
&& property.key.type === 'Identifier'
&& property.key.name === 'directives',
)?.value
if (!directives || directives.type !== 'ArrayExpression')
continue
const renderStart = getRenderFnStart(ast)
for (const directive of directives.elements) {
if (directive?.type !== 'ObjectExpression')
continue
const nameNode = directive.properties.find(
(p): p is ObjectProperty =>
p.type === 'ObjectProperty'
&& p.key.type === 'Identifier'
&& p.key.name === 'name',
)?.value
if (nameNode?.type !== 'StringLiteral')
continue
const name = nameNode.value
if (!name || name.startsWith('_'))
continue
results.push({
rawName: name,
replace: (resolved) => {
s.prependLeft(renderStart!, `\nthis.$options.directives["${name}"] = ${resolved};`)
},
})
}
}
return results
}