-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathdefine-component.ts
67 lines (59 loc) · 1.71 KB
/
define-component.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
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import { transformAST as addImport } from './add-import'
import { transformAST as removeExtraneousImport } from './remove-extraneous-import'
type Params = {
useCompositionApi: boolean
}
export const transformAST: ASTTransformation<Params | undefined> = (
context,
{ useCompositionApi }: Params = {
useCompositionApi: false,
}
) => {
const { root, j, filename } = context
const importDefineComponent = () =>
addImport(context, {
specifier: {
type: 'named',
imported: 'defineComponent',
},
source: useCompositionApi ? '@vue/composition-api' : 'vue',
})
const vueExtend = root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
name: 'Vue',
},
property: {
name: 'extend',
},
},
})
if (vueExtend.length) {
importDefineComponent()
vueExtend.forEach(({ node }) => {
node.callee = j.identifier('defineComponent')
})
removeExtraneousImport(context, { localBinding: 'Vue' })
}
if (filename && filename.endsWith('.vue')) {
const defaultExport = root.find(j.ExportDefaultDeclaration)
if (!defaultExport.length) {
return
}
const declarationNode = defaultExport.nodes()[0].declaration
if (!j.ObjectExpression.check(declarationNode)) {
return
}
importDefineComponent()
defaultExport.nodes()[0].declaration = j.callExpression(
j.identifier('defineComponent'),
[declarationNode]
)
removeExtraneousImport(context, { localBinding: 'Vue' })
}
}
export default wrap(transformAST)
export const parser = 'babylon'