-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathnew-vue-to-create-app.ts
185 lines (167 loc) · 5.05 KB
/
new-vue-to-create-app.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
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import type * as N from 'jscodeshift'
type Params = {
// if false, do not take expressions like `new HelloWorld().$mount` or
// `new HelloWorld({ el: '#app' })`into account
includeMaybeComponents?: boolean
}
// Limitations: cannot transform expressions like `new HelloWorld()`
// FIXME: for ES modules, should use `createApp` instead of `Vue.createApp`
// because the latter makes it difficult to tree-shake the vue module
// FIXME: need to ensure there will be a Vue import if needed.
export const transformAST: ASTTransformation<Params | void> = (
context,
params: Params = {
includeMaybeComponents: true,
}
) => {
const { j, root } = context
const { includeMaybeComponents = true } = params
const newVue = root.find(j.NewExpression, {
callee: {
type: 'Identifier',
name: 'Vue',
},
})
// new Vue() -> Vue.createApp()
newVue.replaceWith(({ node }) => {
const rootProps = node.arguments[0]
return j.callExpression(
j.memberExpression(j.identifier('Vue'), j.identifier('createApp')),
[rootProps]
)
})
const vueCreateApp = newVue
// Vue.createApp().$mount() -> Vue.createApp().mount()
vueCreateApp.forEach((path) => {
const parentNode = path.parent.node
if (
j.MemberExpression.check(parentNode) &&
parentNode.object === path.node &&
j.Identifier.check(parentNode.property) &&
parentNode.property.name === '$mount'
) {
parentNode.property.name = 'mount'
}
})
// Vue.createApp({ el: '#app' }) -> Vue.createApp().mount('#app')
vueCreateApp.replaceWith(({ node }) => {
if (
node.arguments.length !== 1 ||
!j.ObjectExpression.check(node.arguments[0])
) {
return node
}
const rootProps = node.arguments[0]
const elIndex = rootProps.properties.findIndex(
(p) =>
j.ObjectProperty.check(p) &&
j.Identifier.check(p.key) &&
p.key.name === 'el'
)
if (elIndex === -1) {
return node
}
const elProperty = rootProps.properties.splice(
elIndex,
1
)[0] as N.ObjectProperty
const elExpr = elProperty.value
return j.callExpression(
j.memberExpression(node, j.identifier('mount')),
// @ts-ignore I'm not sure what the edge cases are
[elExpr]
)
})
if (includeMaybeComponents) {
// new My().$mount
const new$mount = root.find(j.CallExpression, (n: N.CallExpression) => {
return (
j.MemberExpression.check(n.callee) &&
j.NewExpression.check(n.callee.object) &&
j.Identifier.check(n.callee.property) &&
n.callee.property.name === '$mount'
)
})
new$mount.replaceWith(({ node }) => {
const el = node.arguments[0]
const instance = (node.callee as N.MemberExpression)
.object as N.NewExpression
const ctor = instance.callee
return j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(j.identifier('Vue'), j.identifier('createApp')),
[
ctor,
...instance.arguments, // additional props
]
),
j.identifier('mount')
),
[el]
)
})
// vm.$mount
const $mount = root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
property: {
type: 'Identifier',
name: '$mount',
},
},
arguments: (args: Array<any>) => args.length === 1,
})
$mount.forEach(({ node }) => {
// @ts-ignore
node.callee.property.name = 'mount'
})
// new My({ el })
const newWithEl = root.find(j.NewExpression, (n: N.NewExpression) => {
return (
n.arguments.length === 1 &&
j.ObjectExpression.check(n.arguments[0]) &&
n.arguments[0].properties.some(
(prop) =>
j.ObjectProperty.check(prop) &&
j.Identifier.check(prop.key) &&
prop.key.name === 'el'
)
)
})
newWithEl.replaceWith(({ node }) => {
const rootProps = node.arguments[0] as N.ObjectExpression
const elIndex = rootProps.properties.findIndex(
(p) =>
j.ObjectProperty.check(p) &&
j.Identifier.check(p.key) &&
p.key.name === 'el'
)
const elProperty = rootProps.properties.splice(
elIndex,
1
)[0] as N.ObjectProperty
const elExpr = elProperty.value
const ctor = node.callee
return j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(j.identifier('Vue'), j.identifier('createApp')),
[
ctor,
// additional props, and skip empty objects
...(rootProps.properties.length > 0 ? [rootProps] : []),
]
),
j.identifier('mount')
),
// @ts-ignore I'm not sure what the edge cases are
[elExpr]
)
})
}
}
export default wrap(transformAST)
export const parser = 'babylon'