-
Notifications
You must be signed in to change notification settings - Fork 1
/
vueparser2.js
223 lines (189 loc) · 5.02 KB
/
vueparser2.js
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
const compiler = require('vue-template-compiler')
const parser = require('@babel/parser')
const traverse = require('@babel/traverse').default
const generator = require('@babel/generator').default
const t = require('@babel/types')
const core = require('@babel/core')
const template = require('@babel/template').default
const generate = require('@babel/generator').default
const source = `
<template>
<div class="home">
<x-header title="aaa"></x-header>
<group title="components">
<cell title="number">1</cell>
<cell title="link" is-link>2</cell>
<popup-picker v-model="picker1" title="picker" :data="[['1', '2', '3']]"></popup-picker>
<x-input title="XInput" placeholder="please input"></x-input>
</group>
<group title="x-icon">
<x-icon type="ios-search" size="30"></x-icon>
<x-icon type="ios-search-strong" size="30"></x-icon>
<x-icon type="ios-star" size="30"></x-icon>
<x-icon type="ios-star-half" size="30"></x-icon>
<x-icon type="ios-star-outline" size="30"></x-icon>
<x-icon type="ios-heart" size="30"></x-icon>
<x-icon type="ios-heart-outline" size="30"></x-icon>
<x-button type="primary">XButton</x-button>
</group>
</div>
</template>
<script>
import Vue from 'vue'
import Component from 'vue-class-component'
import eztalkApi from 'm/api/eztalk'
import config from 'm/config'
@Component({
name: 'qd-text-spot-message',
components: {
QdMessage,
QdImageSpot
},
props: {
own: {
type: Boolean,
default: false
},
zoomImg: {
type: [Boolean, Number],
default: false
}
},
mixins: [routeMixin],
watch: {
'readModal.visible': function (visible) {
if (!visible) {
this.readModal.current = -1
}
}
}
})
export default class Login extends Vue {
phone = '我是中文222'
code = '22'
helloMsg = 'Hello, ' + this.phone
container = {
width: 0,
height: 0
}
get count () {
return this.countDown
}
get say () {
return this.countDown
}
created () {
console.log(1)
}
mounted () {
console.log(1)
}
@log()
getVerifycode () {}
login () {}
}
</script>
<style lang="less" scoped>
.home {
background: red;
}
</style>
<style>
.test {font-size: 14px;}
</style>
`
let vuecontent = compiler.parseComponent(source)
let ast = parser.parse(vuecontent.script.content, {
sourceType: 'module',
plugins: [
'classProperties',
'methodDefinition',
'decorators-legacy'
]
})
let dataArr = []
let methodsArr = []
let importArr = []
let lifeCycle = []
let definedLifeCycle = [
'beforeCreate',
'created',
'beforeMount',
'mounted',
'beforeUpdate',
'updated',
'activated',
'deactivated',
'beforeDestroy',
'destroyed',
'errorCaptured'
]
let computedArr = []
let componentsArr = null
traverse(ast, {
ClassProperty (p) { // 属性获取
dataArr.push(t.objectProperty(p.node.key, p.node.value))
},
ClassMethod (p) {
if (definedLifeCycle.includes(p.node.key.name)) { // 生命周期方法
lifeCycle.push(t.objectMethod(p.node.kind, p.node.key, p.node.params, p.node.body))
} else if (p.node.kind === 'get') { // 获取computed
computedArr.push(t.objectMethod('method', p.node.key, p.node.params, p.node.body))
} else {
methodsArr.push(t.objectMethod(p.node.kind, p.node.key, p.node.params, p.node.body))
}
},
ImportDeclaration (p) {
let node = p.node
if (node.specifiers.length === 1
&& (
(node.specifiers[0].local.name === 'Vue' && node.source.value === 'vue')
|| (node.specifiers[0].local.name === 'Component' && node.source.value === 'vue-class-component')
)) {
// console.log('vue')
} else {
importArr.push(t.importDeclaration(node.specifiers, node.source))
}
},
Decorator (p) {
let expression = p.node.expression
if (expression.callee.name === 'Component') {
componentsArr = expression.arguments[0].properties
}
}
})
// let sourcecode = generator(ast).code
// vue 里面的 data
let data = t.objectMethod('method', t.identifier('data'), [], t.blockStatement([
t.returnStatement(t.objectExpression(dataArr))
]))
let methods = t.objectProperty(t.identifier('methods'), t.objectExpression(methodsArr))
let computed = t.objectProperty(t.identifier('computed'), t.objectExpression(computedArr))
let reconstructor = [...lifeCycle, data, computed, methods]
if (componentsArr) {
reconstructor.unshift(...componentsArr)
}
let ExportDefaultDeclaration = t.exportDefaultDeclaration(
t.objectExpression(reconstructor)
)
// 构造正常结构
const code = ''
const ast2 = parser.parse(code)
ast2.program.body.push(...importArr)
ast2.program.body.push(ExportDefaultDeclaration)
const output = generate(ast2, {}, code)
// console.log(output.code)
// console.log(vuecontent.styles[0])
// console.log(vuecontent.template.content)
const allTpl = `
<template>
${vuecontent.template.content}
</template>
<script>
${output.code}
</script>
<style lang="less" scoped>
${vuecontent.styles[0].content}
</style>
`
console.log(allTpl)