forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
225 lines (202 loc) · 6.24 KB
/
index.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
224
225
import fs from 'fs'
import path from 'path'
import * as Vue from '../../../packages/weex-vue-framework'
import { compile } from '../../../packages/weex-template-compiler'
import WeexRuntime from 'weex-js-runtime'
import styler from 'weex-styler'
const styleRE = /<\s*style\s*\w*>([^(<\/)]*)<\/\s*style\s*>/g
const scriptRE = /<\s*script.*>([^]*)<\/\s*script\s*>/
const templateRE = /<\s*template\s*([^>]*)>([^]*)<\/\s*template\s*>/
export function readFile (filename) {
return fs.readFileSync(path.resolve(__dirname, '../cases/', filename), 'utf8')
}
export function readObject (filename) {
return (new Function(`return ${readFile(filename)}`))()
}
console.debug = () => {}
// http://stackoverflow.com/a/35478115
const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g
export function strToRegExp (str) {
return new RegExp(str.replace(matchOperatorsRe, '\\$&'))
}
function parseStatic (fns) {
return '[' + fns.map(fn => `function () { ${fn} }`).join(',') + ']'
}
export function compileAndStringify (template) {
const { render, staticRenderFns } = compile(template)
return {
render: `function () { ${render} }`,
staticRenderFns: parseStatic(staticRenderFns)
}
}
/**
* Compile *.vue file into js code
* @param {string} source raw text of *.vue file
* @param {string} componentName whether compile to a component
*/
export function compileVue (source, componentName) {
return new Promise((resolve, reject) => {
if (!templateRE.test(source)) {
return reject('No Template!')
}
const scriptMatch = scriptRE.exec(source)
const script = scriptMatch ? scriptMatch[1] : ''
const templateMatch = templateRE.exec(source)
const compileOptions = {}
if (/\s*recyclable\=?/i.test(templateMatch[1])) {
compileOptions.recyclable = true
}
const res = compile(templateMatch[2], compileOptions)
const name = 'test_case_' + (Math.random() * 99999999).toFixed(0)
const generateCode = styles => (`
var ${name} = Object.assign({
style: ${JSON.stringify(styles)},
render: function () { ${res.render} },
${res['@render'] ? ('"@render": function () {' + res['@render'] + '},') : ''}
staticRenderFns: ${parseStatic(res.staticRenderFns)},
}, (function(){
var module = { exports: {} };
${script};
return module.exports;
})());
` + (componentName
? `Vue.component('${componentName}', ${name});\n`
: `${name}.el = 'body';new Vue(${name});`)
)
let cssText = ''
let styleMatch = null
while ((styleMatch = styleRE.exec(source))) {
cssText += `\n${styleMatch[1]}\n`
}
styler.parse(cssText, (error, result) => {
if (error) {
return reject(error)
}
resolve(generateCode(result.jsonStyle))
})
resolve(generateCode({}))
})
}
export function compileWithDeps (entryPath, deps) {
return new Promise((resolve, reject) => {
if (Array.isArray(deps)) {
Promise.all(deps.map(dep => {
return compileVue(readFile(dep.path), dep.name).catch(reject)
})).then(depCodes => {
compileVue(readFile(entryPath)).then(entryCode => {
resolve(depCodes.join('\n') + entryCode)
}).catch(reject)
}).catch(reject)
}
})
}
function isObject (object) {
return object !== null && typeof object === 'object'
}
function isEmptyObject (object) {
return isObject(object) && Object.keys(object).length < 1
}
function omitUseless (object) {
if (isObject(object)) {
delete object.ref
for (const key in object) {
if (key.charAt(0) !== '@' && (isEmptyObject(object[key]) || object[key] === undefined)) {
delete object[key]
}
omitUseless(object[key])
}
}
return object
}
export function getRoot (instance) {
return omitUseless(instance.$getRoot())
}
// Get all binding events in the instance
export function getEvents (instance) {
const events = []
const recordEvent = node => {
if (!node) { return }
if (Array.isArray(node.event)) {
node.event.forEach(type => {
events.push({ ref: node.ref, type })
})
}
if (Array.isArray(node.children)) {
node.children.forEach(recordEvent)
}
}
recordEvent(instance.$getRoot())
return events
}
export function fireEvent (instance, ref, type, event = {}) {
const el = instance.document.getRef(ref)
if (el) {
instance.document.fireEvent(el, type, event = {})
}
}
export function createInstance (id, code, ...args) {
WeexRuntime.config.frameworks = { Vue }
const context = WeexRuntime.init(WeexRuntime.config)
context.registerModules({
timer: ['setTimeout', 'setInterval']
})
const instance = context.createInstance(id, `// { "framework": "Vue" }\n${code}`, ...args) || {}
instance.document = context.getDocument(id)
instance.$getRoot = () => context.getRoot(id)
instance.$refresh = (data) => context.refreshInstance(id, data)
instance.$destroy = () => {
delete instance.document
context.destroyInstance(id)
}
instance.$triggerHook = (id, hook, args) => {
instance.document.taskCenter.triggerHook(id, 'lifecycle', hook, { args })
}
return instance
}
export function compileAndExecute (template, additional = '') {
return new Promise(resolve => {
const id = String(Date.now() * Math.random())
const { render, staticRenderFns } = compile(template)
const instance = createInstance(id, `
new Vue({
el: '#whatever',
render: function () { ${render} },
staticRenderFns: ${parseStatic(staticRenderFns)},
${additional}
})
`)
setTimeout(() => resolve(instance), 10)
})
}
export function syncPromise (arr) {
let p = Promise.resolve()
arr.forEach(item => {
p = p.then(item)
})
return p
}
export function checkRefresh (instance, data, checker) {
return () => new Promise(res => {
instance.$refresh(data)
setTimeout(() => {
checker(getRoot(instance))
res()
})
})
}
export function addTaskHook (hook) {
global.callNative = function callNative (id, tasks) {
if (Array.isArray(tasks) && typeof hook === 'function') {
tasks.forEach(task => {
hook(id, {
module: task.module,
method: task.method,
args: Array.from(task.args)
})
})
}
}
}
export function resetTaskHook () {
delete global.callNative
}