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.compileTemplateForCml = 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 | if (type === 'web') {
|
35 | source = compileWebTemplate(source, type, options).code;
|
36 | }
|
37 | if (type === 'weex') {
|
38 | source = compileWeexTemplate(source, type, options).code;
|
39 | }
|
40 | if (type === 'wx') {
|
41 | source = compileWxTemplate(source, type, options).code;
|
42 | }
|
43 | if (type === 'qq') {
|
44 | source = compileQqTemplate(source, type, options).code;
|
45 | }
|
46 | if (type === 'alipay') {
|
47 | source = compileAliPayTemplate(source, type, options).code;
|
48 | }
|
49 | if (type === 'baidu') {
|
50 | source = compileBaiduTemplate(source, type, options).code;
|
51 | }
|
52 |
|
53 | source = processTemplate.postParseOriginTag(source)
|
54 |
|
55 | source = processTemplate.postParseMustache(source)
|
56 |
|
57 | source = processTemplate.postParseUnicode(source);
|
58 |
|
59 | source = processTemplate.transformNativeEvent(source)
|
60 | return {
|
61 | source,
|
62 | usedBuildInTagMap: options.usedBuildInTagMap
|
63 | }
|
64 | }
|
65 | function compileWebTemplate(source, type, options) {
|
66 |
|
67 | const ast = babylon.parse(source, {
|
68 | plugins: ['jsx']
|
69 | })
|
70 | traverse(ast, {
|
71 | enter(path) {
|
72 | parseTemplate.parseClassStatement(path, type, options);
|
73 | parseTemplate.parseTagForSlider(path, type, options);
|
74 |
|
75 | parseTemplate.parseBuildTag(path, type, options)
|
76 |
|
77 | parseTemplate.parseTag(path, type, options);
|
78 |
|
79 | parseTemplate.parseAnimationStatement(path, type, options);
|
80 | parseTemplate.parseConditionalStatement(path, type, options);
|
81 | parseTemplate.parseEventListener(path, type, options)
|
82 | parseTemplate.parseIterationStatement(path, type, options);
|
83 |
|
84 |
|
85 | parseTemplate.parseDirectiveStatement(path, type, options);
|
86 |
|
87 | parseTemplate.parseStyleStatement(path, type, options);
|
88 |
|
89 | parseTemplate.parseAttributeStatement(path, type, options);
|
90 |
|
91 | parseTemplate.afterParseTag(path, type, options);
|
92 | }
|
93 | })
|
94 | return generate(ast);
|
95 | }
|
96 | function compileWeexTemplate(source, type, options) {
|
97 |
|
98 | const ast = babylon.parse(source, {
|
99 | plugins: ['jsx']
|
100 | })
|
101 | traverse(ast, {
|
102 | enter(path) {
|
103 | parseTemplate.parseClassStatement(path, type, options);
|
104 |
|
105 |
|
106 | parseTemplate.parseBuildTag(path, type, options)
|
107 | parseTemplate.parseTag(path, type, options);
|
108 |
|
109 | parseTemplate.parseAnimationStatement(path, type, options);
|
110 | parseTemplate.parseConditionalStatement(path, type, options);
|
111 | parseTemplate.parseEventListener(path, type, options)
|
112 | parseTemplate.parseIterationStatement(path, type, options);
|
113 |
|
114 | parseTemplate.parseDirectiveStatement(path, type, options);
|
115 |
|
116 | parseTemplate.parseStyleStatement(path, type, options)
|
117 |
|
118 | parseTemplate.parseAttributeStatement(path, type, options);
|
119 | parseTemplate.afterParseTag(path, type, options);
|
120 | }
|
121 | })
|
122 | return generate(ast);
|
123 | }
|
124 | function compileWxTemplate(source, type, options) {
|
125 |
|
126 | const ast = babylon.parse(source, {
|
127 | plugins: ['jsx']
|
128 | })
|
129 | traverse(ast, {
|
130 | enter(path) {
|
131 | parseTemplate.parseClassStatement(path, type, options);
|
132 |
|
133 |
|
134 | parseTemplate.parseTagForSlider(path, type, options);
|
135 |
|
136 | parseTemplate.parseRefStatement(path, type, options)
|
137 |
|
138 | parseTemplate.parseBuildTag(path, type, options)
|
139 |
|
140 |
|
141 | parseTemplate.parseTag(path, type, options);
|
142 |
|
143 | parseTemplate.parseAnimationStatement(path, type, options);
|
144 |
|
145 | parseTemplate.afterParseTag(path, type, options);
|
146 | parseTemplate.parseConditionalStatement(path, type, options);
|
147 | parseTemplate.parseEventListener(path, type, options);
|
148 |
|
149 |
|
150 | parseTemplate.parseDirectiveStatement(path, type, options);
|
151 |
|
152 | parseTemplate.parseIterationStatement(path, type, options);
|
153 |
|
154 | parseTemplate.parseStyleStatement(path, type, options);
|
155 |
|
156 | parseTemplate.parseVue2WxStatement(path, type, options);
|
157 | }
|
158 | })
|
159 | return generate(ast);
|
160 | }
|
161 | function compileQqTemplate(source, type, options) {
|
162 |
|
163 | const ast = babylon.parse(source, {
|
164 | plugins: ['jsx']
|
165 | })
|
166 | traverse(ast, {
|
167 | enter(path) {
|
168 | parseTemplate.parseClassStatement(path, type, options);
|
169 |
|
170 |
|
171 | parseTemplate.parseTagForSlider(path, type, options);
|
172 |
|
173 | parseTemplate.parseRefStatement(path, type, options)
|
174 |
|
175 | parseTemplate.parseBuildTag(path, type, options)
|
176 |
|
177 |
|
178 | parseTemplate.parseTag(path, type, options);
|
179 |
|
180 | parseTemplate.parseAnimationStatement(path, type, options);
|
181 |
|
182 | parseTemplate.afterParseTag(path, type, options);
|
183 | parseTemplate.parseConditionalStatement(path, type, options);
|
184 | parseTemplate.parseEventListener(path, type, options);
|
185 |
|
186 |
|
187 | parseTemplate.parseDirectiveStatement(path, type, options);
|
188 |
|
189 | parseTemplate.parseIterationStatement(path, type, options);
|
190 |
|
191 | parseTemplate.parseStyleStatement(path, type, options);
|
192 |
|
193 | parseTemplate.parseVue2WxStatement(path, type, options);
|
194 | }
|
195 | })
|
196 | return generate(ast);
|
197 | }
|
198 | function compileAliPayTemplate(source, type, options) {
|
199 | const ast = babylon.parse(source, {
|
200 | plugins: ['jsx']
|
201 | })
|
202 | traverse(ast, {
|
203 | enter(path) {
|
204 | parseTemplate.parseClassStatement(path, type, options);
|
205 |
|
206 |
|
207 | parseTemplate.parseTagForSlider(path, type, options);
|
208 |
|
209 | parseTemplate.parseRefStatement(path, type, options)
|
210 |
|
211 | parseTemplate.parseBuildTag(path, type, options)
|
212 |
|
213 |
|
214 | parseTemplate.parseTag(path, type, options);
|
215 |
|
216 | parseTemplate.parseAnimationStatement(path, type, options);
|
217 |
|
218 | parseTemplate.afterParseTag(path, type, options);
|
219 | parseTemplate.parseConditionalStatement(path, type, options);
|
220 | parseTemplate.parseEventListener(path, type, options);
|
221 |
|
222 |
|
223 | parseTemplate.parseDirectiveStatement(path, type, options);
|
224 |
|
225 | parseTemplate.parseIterationStatement(path, type, options);
|
226 |
|
227 | parseTemplate.parseStyleStatement(path, type, options);
|
228 |
|
229 | parseTemplate.parseVue2WxStatement(path, type, options);
|
230 | }
|
231 | })
|
232 | return generate(ast);
|
233 | }
|
234 | function compileBaiduTemplate(source, type, options) {
|
235 | const ast = babylon.parse(source, {
|
236 | plugins: ['jsx']
|
237 | })
|
238 | traverse(ast, {
|
239 | enter(path) {
|
240 | parseTemplate.parseClassStatement(path, type, options);
|
241 |
|
242 |
|
243 | parseTemplate.parseTagForSlider(path, type, options);
|
244 |
|
245 | parseTemplate.parseRefStatement(path, type, options)
|
246 |
|
247 | parseTemplate.parseBuildTag(path, type, options)
|
248 |
|
249 |
|
250 | parseTemplate.parseTag(path, type, options);
|
251 |
|
252 | parseTemplate.parseAnimationStatement(path, type, options);
|
253 |
|
254 | parseTemplate.afterParseTag(path, type, options);
|
255 | parseTemplate.parseConditionalStatement(path, type, options);
|
256 | parseTemplate.parseEventListener(path, type, options);
|
257 |
|
258 |
|
259 | parseTemplate.parseDirectiveStatement(path, type, options);
|
260 |
|
261 | parseTemplate.parseIterationStatement(path, type, options);
|
262 |
|
263 | parseTemplate.parseStyleStatement(path, type, options);
|
264 |
|
265 | parseTemplate.parseVue2WxStatement(path, type, options);
|
266 | }
|
267 | })
|
268 | return generate(ast);
|
269 | }
|