1
1
import * as Vue from '../../../packages/weex-vue-framework'
2
2
import { compile } from '../../../packages/weex-template-compiler'
3
3
import WeexRuntime from 'weex-js-runtime'
4
+ import styler from 'weex-styler'
5
+
6
+ const styleRE = / < \s * s t y l e \s * \w * > ( [ ^ ( < \/ ) ] * ) < \/ \s * s t y l e \s * > / g
7
+ const scriptRE = / < \s * s c r i p t .* > ( [ ^ ] * ) < \/ \s * s c r i p t \s * > /
8
+ const templateRE = / < \s * t e m p l a t e \s * > ( [ ^ ] * ) < \/ \s * t e m p l a t e \s * > /
4
9
5
10
console . debug = ( ) => { }
6
11
@@ -10,6 +15,10 @@ export function strToRegExp (str) {
10
15
return new RegExp ( str . replace ( matchOperatorsRe , '\\$&' ) )
11
16
}
12
17
18
+ function parseStatic ( fns ) {
19
+ return '[' + fns . map ( fn => `function () { ${ fn } }` ) . join ( ',' ) + ']'
20
+ }
21
+
13
22
export function compileAndStringify ( template ) {
14
23
const { render, staticRenderFns } = compile ( template )
15
24
return {
@@ -18,8 +27,48 @@ export function compileAndStringify (template) {
18
27
}
19
28
}
20
29
21
- function parseStatic ( fns ) {
22
- return '[' + fns . map ( fn => `function () { ${ fn } }` ) . join ( ',' ) + ']'
30
+ /**
31
+ * Compile *.vue file into js code
32
+ * @param {string } source raw text of *.vue file
33
+ * @param {string } componentName whether compile to a component
34
+ */
35
+ export function compileVue ( source , componentName ) {
36
+ return new Promise ( ( resolve , reject ) => {
37
+ if ( ! templateRE . test ( source ) ) {
38
+ return reject ( 'No Template!' )
39
+ }
40
+ const scriptMatch = scriptRE . exec ( source )
41
+ const script = scriptMatch ? scriptMatch [ 1 ] : ''
42
+ const { render, staticRenderFns } = compile ( templateRE . exec ( source ) [ 1 ] )
43
+
44
+ const generateCode = styles => ( `
45
+ var test_case = Object.assign({
46
+ style: ${ JSON . stringify ( styles ) } ,
47
+ render: function () { ${ render } },
48
+ staticRenderFns: ${ parseStatic ( staticRenderFns ) } ,
49
+ }, (function(){
50
+ var module = { exports: {} };
51
+ ${ script } ;
52
+ return module.exports;
53
+ })());
54
+ ` + ( componentName
55
+ ? `Vue.component('${ componentName } ', test_case);\n`
56
+ : `test_case.el = 'body';new Vue(test_case);` )
57
+ )
58
+
59
+ let cssText = ''
60
+ let styleMatch = null
61
+ while ( ( styleMatch = styleRE . exec ( source ) ) ) {
62
+ cssText += `\n${ styleMatch [ 1 ] } \n`
63
+ }
64
+ styler . parse ( cssText , ( error , result ) => {
65
+ if ( error ) {
66
+ return reject ( error )
67
+ }
68
+ resolve ( generateCode ( result . jsonStyle ) )
69
+ } )
70
+ resolve ( generateCode ( { } ) )
71
+ } )
23
72
}
24
73
25
74
function isObject ( object ) {
@@ -47,6 +96,24 @@ export function getRoot (instance) {
47
96
return omitUseless ( instance . document . body . toJSON ( ) )
48
97
}
49
98
99
+ // Get all binding events in the instance
100
+ export function getEvents ( instance ) {
101
+ const events = [ ]
102
+ const recordEvent = node => {
103
+ if ( ! node ) { return }
104
+ if ( Array . isArray ( node . event ) ) {
105
+ node . event . forEach ( type => {
106
+ events . push ( { ref : node . ref , type } )
107
+ } )
108
+ }
109
+ if ( Array . isArray ( node . children ) ) {
110
+ node . children . forEach ( recordEvent )
111
+ }
112
+ }
113
+ recordEvent ( instance . document . body . toJSON ( ) )
114
+ return events
115
+ }
116
+
50
117
export function fireEvent ( instance , ref , type , event = { } ) {
51
118
const el = instance . document . getRef ( ref )
52
119
if ( el ) {
0 commit comments