1 |
|
2 | const babylon = require('babylon');
|
3 | const traverse = require('@babel/traverse')["default"];
|
4 | const generate = require('@babel/generator')["default"];
|
5 |
|
6 | const parseTemplate = require('./parser/index.js');
|
7 |
|
8 | const processTemplate = require('./common/process-template.js')
|
9 |
|
10 | const cliUtils = require('chameleon-tool-utils');
|
11 | exports.compileTemplateForVue = function (source, type, options) {
|
12 | let errorInfo = processTemplate.preCheckTemplateSyntax(source, type, options)
|
13 | if (errorInfo) {
|
14 |
|
15 | cliUtils.log.warn(`${errorInfo} in ${options.filePath}`);
|
16 | }
|
17 |
|
18 |
|
19 | source = processTemplate.preDisappearAnnotation(source);
|
20 |
|
21 | source = processTemplate.preParseGtLt(source);
|
22 | source = processTemplate.preParseDiffPlatformTag(source, type);
|
23 |
|
24 | source = processTemplate.preParseBindAttr(source);
|
25 |
|
26 | source = processTemplate.preParseVueEvent(source);
|
27 |
|
28 | source = processTemplate.preParseMustache(source);
|
29 |
|
30 | source = processTemplate.postParseLtGt(source);
|
31 |
|
32 | source = processTemplate.preParseAnimation(source, type);
|
33 | source = processTemplate.preParseAliComponent(source, type, options);
|
34 |
|
35 | if (type === 'web') {
|
36 | source = compileWebTemplate(source, type, options).code;
|
37 | }
|
38 | if (type === 'weex') {
|
39 | source = compileWeexTemplate(source, type, options).code;
|
40 | }
|
41 | if (type === 'wx') {
|
42 | source = compileWxTemplate(source, type, options).code;
|
43 | }
|
44 | if (type === 'qq') {
|
45 | source = compileQqTemplate(source, type, options).code;
|
46 | }
|
47 | if (type === 'alipay') {
|
48 | source = compileAliPayTemplate(source, type, options).code;
|
49 | }
|
50 | if (type === 'baidu') {
|
51 | source = compileBaiduTemplate(source, type, options).code;
|
52 | }
|
53 |
|
54 | source = processTemplate.postParseOriginTag(source)
|
55 |
|
56 | source = processTemplate.postParseMustache(source)
|
57 |
|
58 | source = processTemplate.postParseUnicode(source);
|
59 |
|
60 | source = processTemplate.transformNativeEvent(source)
|
61 | return {
|
62 | source,
|
63 | usedBuildInTagMap: options.usedBuildInTagMap
|
64 | }
|
65 | }
|
66 | function compileWebTemplate(source, type, options) {
|
67 |
|
68 | const ast = babylon.parse(source, {
|
69 | plugins: ['jsx']
|
70 | })
|
71 | traverse(ast, {
|
72 | enter(path) {
|
73 | parseTemplate.parseClassStatement(path, type, options);
|
74 |
|
75 | parseTemplate.parseTagForSlider(path, type, options);
|
76 | parseTemplate.parseBuildTag(path, type, options)
|
77 |
|
78 | parseTemplate.parseTag(path, type, options);
|
79 |
|
80 | parseTemplate.parseAnimationStatement(path, type, options);
|
81 |
|
82 | parseTemplate.parseEventListener(path, type, options)
|
83 |
|
84 |
|
85 | parseTemplate.parseDirectiveStatement(path, type, options);
|
86 | parseTemplate.parseStyleStatement(path, type, options)
|
87 |
|
88 | parseTemplate.parseAttributeStatement(path, type, options);
|
89 |
|
90 | parseTemplate.afterParseTag(path, type, options);
|
91 | }
|
92 | })
|
93 | return generate(ast);
|
94 | }
|
95 | function compileWeexTemplate(source, type, options) {
|
96 |
|
97 | const ast = babylon.parse(source, {
|
98 | plugins: ['jsx']
|
99 | })
|
100 | traverse(ast, {
|
101 | enter(path) {
|
102 | parseTemplate.parseClassStatement(path, type, options);
|
103 |
|
104 |
|
105 | parseTemplate.parseBuildTag(path, type, options)
|
106 | parseTemplate.parseTag(path, type, options);
|
107 |
|
108 | parseTemplate.parseAnimationStatement(path, type, options);
|
109 |
|
110 | parseTemplate.parseEventListener(path, type, options)
|
111 |
|
112 | parseTemplate.parseDirectiveStatement(path, type, options);
|
113 |
|
114 | parseTemplate.parseStyleStatement(path, type, options)
|
115 |
|
116 | parseTemplate.parseAttributeStatement(path, type, options);
|
117 | parseTemplate.afterParseTag(path, type, options);
|
118 | }
|
119 | })
|
120 | return generate(ast);
|
121 | }
|
122 | function compileWxTemplate(source, type, options) {
|
123 |
|
124 | const ast = babylon.parse(source, {
|
125 | plugins: ['jsx']
|
126 | })
|
127 | traverse(ast, {
|
128 | enter(path) {
|
129 | parseTemplate.parseClassStatement(path, type, options);
|
130 |
|
131 | parseTemplate.parseTagForSlider(path, type, options);
|
132 |
|
133 | parseTemplate.parseRefStatement(path, type, options)
|
134 | parseTemplate.parseBuildTag(path, type, options)
|
135 | parseTemplate.parseTag(path, type, options);
|
136 |
|
137 | parseTemplate.parseAnimationStatement(path, type, options);
|
138 | parseTemplate.afterParseTag(path, type, options);
|
139 | parseTemplate.parseEventListener(path, type, options);
|
140 |
|
141 | parseTemplate.parseDirectiveStatement(path, type, options);
|
142 |
|
143 | parseTemplate.parseStyleStatement(path, type, options);
|
144 |
|
145 | parseTemplate.parseVue2WxStatement(path, type, options);
|
146 | }
|
147 | })
|
148 | return generate(ast);
|
149 | }
|
150 | function compileQqTemplate(source, type, options) {
|
151 |
|
152 | const ast = babylon.parse(source, {
|
153 | plugins: ['jsx']
|
154 | })
|
155 | traverse(ast, {
|
156 | enter(path) {
|
157 | parseTemplate.parseClassStatement(path, type, options);
|
158 |
|
159 | parseTemplate.parseTagForSlider(path, type, options);
|
160 |
|
161 | parseTemplate.parseRefStatement(path, type, options)
|
162 | parseTemplate.parseBuildTag(path, type, options)
|
163 | parseTemplate.parseTag(path, type, options);
|
164 |
|
165 | parseTemplate.parseAnimationStatement(path, type, options);
|
166 | parseTemplate.afterParseTag(path, type, options);
|
167 | parseTemplate.parseEventListener(path, type, options);
|
168 |
|
169 | parseTemplate.parseDirectiveStatement(path, type, options);
|
170 |
|
171 | parseTemplate.parseStyleStatement(path, type, options);
|
172 |
|
173 | parseTemplate.parseVue2WxStatement(path, type, options);
|
174 | }
|
175 | })
|
176 | return generate(ast);
|
177 | }
|
178 | function compileAliPayTemplate(source, type, options) {
|
179 | const ast = babylon.parse(source, {
|
180 | plugins: ['jsx']
|
181 | })
|
182 | traverse(ast, {
|
183 | enter(path) {
|
184 | parseTemplate.parseClassStatement(path, type, options);
|
185 |
|
186 | parseTemplate.parseTagForSlider(path, type, options);
|
187 |
|
188 | parseTemplate.parseRefStatement(path, type, options)
|
189 | parseTemplate.parseBuildTag(path, type, options)
|
190 | parseTemplate.parseTag(path, type, options);
|
191 |
|
192 | parseTemplate.parseAnimationStatement(path, type, options);
|
193 | parseTemplate.afterParseTag(path, type, options);
|
194 | parseTemplate.parseEventListener(path, type, options);
|
195 |
|
196 | parseTemplate.parseDirectiveStatement(path, type, options);
|
197 | parseTemplate.parseStyleStatement(path, type, options);
|
198 |
|
199 | parseTemplate.parseVue2WxStatement(path, type, options);
|
200 | }
|
201 | })
|
202 | return generate(ast);
|
203 | }
|
204 | function compileBaiduTemplate(source, type, options) {
|
205 | const ast = babylon.parse(source, {
|
206 | plugins: ['jsx']
|
207 | })
|
208 | traverse(ast, {
|
209 | enter(path) {
|
210 | parseTemplate.parseClassStatement(path, type, options);
|
211 |
|
212 | parseTemplate.parseTagForSlider(path, type, options);
|
213 |
|
214 | parseTemplate.parseRefStatement(path, type, options)
|
215 | parseTemplate.parseBuildTag(path, type, options)
|
216 | parseTemplate.parseTag(path, type, options);
|
217 |
|
218 | parseTemplate.parseAnimationStatement(path, type, options);
|
219 | parseTemplate.afterParseTag(path, type, options);
|
220 | parseTemplate.parseEventListener(path, type, options);
|
221 |
|
222 | parseTemplate.parseDirectiveStatement(path, type, options);
|
223 | parseTemplate.parseStyleStatement(path, type, options);
|
224 |
|
225 | parseTemplate.parseVue2WxStatement(path, type, options);
|
226 | }
|
227 | })
|
228 | return generate(ast);
|
229 | }
|