@tarojs/helper 4.0.0-beta.0 → 4.0.0-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/esbuild/index.js +2 -2
  2. package/dist/esbuild/index.js.map +1 -1
  3. package/dist/utils.js +3 -6
  4. package/dist/utils.js.map +1 -1
  5. package/package.json +4 -3
  6. package/swc/.gitkeep +0 -0
  7. package/swc/swc_plugin_compile_mode.wasm +0 -0
  8. package/swc/swc_plugin_define_config.wasm +0 -0
  9. package/swc/plugin-compile-mode/.cargo/config +0 -5
  10. package/swc/plugin-compile-mode/.editorconfig +0 -6
  11. package/swc/plugin-compile-mode/.vscode/settings.json +0 -3
  12. package/swc/plugin-compile-mode/Cargo.lock +0 -2021
  13. package/swc/plugin-compile-mode/Cargo.toml +0 -22
  14. package/swc/plugin-compile-mode/package.json +0 -15
  15. package/swc/plugin-compile-mode/src/lib.rs +0 -71
  16. package/swc/plugin-compile-mode/src/tests/attributes.rs +0 -54
  17. package/swc/plugin-compile-mode/src/tests/children.rs +0 -86
  18. package/swc/plugin-compile-mode/src/tests/condition.rs +0 -50
  19. package/swc/plugin-compile-mode/src/tests/entry.rs +0 -20
  20. package/swc/plugin-compile-mode/src/tests/harmony/attributes.rs +0 -45
  21. package/swc/plugin-compile-mode/src/tests/harmony/children.rs +0 -74
  22. package/swc/plugin-compile-mode/src/tests/harmony/condition.rs +0 -79
  23. package/swc/plugin-compile-mode/src/tests/harmony/entry.rs +0 -67
  24. package/swc/plugin-compile-mode/src/tests/harmony/looping.rs +0 -62
  25. package/swc/plugin-compile-mode/src/tests/harmony/mod.rs +0 -45
  26. package/swc/plugin-compile-mode/src/tests/looping.rs +0 -84
  27. package/swc/plugin-compile-mode/src/tests/mod.rs +0 -100
  28. package/swc/plugin-compile-mode/src/tests/shake.rs +0 -27
  29. package/swc/plugin-compile-mode/src/transform.rs +0 -474
  30. package/swc/plugin-compile-mode/src/transform_harmony.rs +0 -526
  31. package/swc/plugin-compile-mode/src/utils/constants.rs +0 -152
  32. package/swc/plugin-compile-mode/src/utils/harmony/components.rs +0 -61
  33. package/swc/plugin-compile-mode/src/utils/harmony/mod.rs +0 -1
  34. package/swc/plugin-compile-mode/src/utils/mod.rs +0 -347
  35. package/swc/plugin-compile-mode/target/wasm32-wasi/release/swc_plugin_compile_mode.wasm +0 -0
  36. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/attributes.rs/should_handle_events.js +0 -8
  37. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/attributes.rs/should_keep_static_attrs_only_in_templates.js +0 -8
  38. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/attributes.rs/should_turn_dynamic_attrs.js +0 -11
  39. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/children.rs/should_render_native_component.js +0 -7
  40. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/children.rs/should_render_react_component.js +0 -7
  41. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/children.rs/should_support_fragment.js +0 -44
  42. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/children.rs/should_support_render_fn.js +0 -8
  43. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/condition.rs/should_support_and_expr.js +0 -14
  44. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/condition.rs/should_support_conditional_expr.js +0 -15
  45. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/entry.rs/should_support_multi_compile_mode.js +0 -10
  46. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/attributes.rs/should_handle_events.js +0 -183
  47. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/attributes.rs/should_turn_dynamic_attrs.js +0 -186
  48. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/children.rs/should_render_react_component.js +0 -76
  49. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/children.rs/should_support_fragment.js +0 -341
  50. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/children.rs/should_support_render_fn.js +0 -186
  51. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/condition.rs/should_support_and_expr.js +0 -211
  52. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/condition.rs/should_support_complex_condition.js +0 -244
  53. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/condition.rs/should_support_conditional_and_unkonw_component.js +0 -139
  54. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/condition.rs/should_support_conditional_expr.js +0 -344
  55. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/entry.rs/should_support_compile_child_node.js +0 -141
  56. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/entry.rs/should_support_component_not_in_config.js +0 -91
  57. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/entry.rs/should_support_multi_compile_mode.js +0 -209
  58. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/entry.rs/should_support_single_compile_mode.js +0 -66
  59. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/looping.rs/should_loop_with_arrow_function_with_blockstmt.js +0 -76
  60. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/looping.rs/should_loop_with_arrow_function_with_blockstmt_and_set_parent_dynamic_id.js +0 -89
  61. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/harmony/looping.rs/should_loop_with_function_expr.js +0 -86
  62. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/looping.rs/should_loop_with_arrow_function_with_blockstmt.js +0 -8
  63. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/looping.rs/should_loop_with_arrow_function_with_expr.js +0 -6
  64. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/looping.rs/should_loop_with_function_expr.js +0 -20
  65. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/looping.rs/should_support_nested_loop.js +0 -15
  66. package/swc/plugin-compile-mode/tests/__swc_snapshots__/src/tests/shake.rs/should_static_jsx_being_shaked.js +0 -20
  67. package/swc/plugin-define-config/.cargo/config +0 -5
  68. package/swc/plugin-define-config/Cargo.lock +0 -2110
  69. package/swc/plugin-define-config/Cargo.toml +0 -18
  70. package/swc/plugin-define-config/src/lib.rs +0 -141
  71. package/swc/plugin-define-config/target/wasm32-wasi/release/swc_plugin_define_config.wasm +0 -0
  72. package/swc/plugin-define-config/tests/__swc_snapshots__/src/lib.rs/module_decl_default_app.js +0 -2
  73. package/swc/plugin-define-config/tests/__swc_snapshots__/src/lib.rs/module_decl_default_page.js +0 -2
  74. package/swc/plugin-define-config/tests/__swc_snapshots__/src/lib.rs/module_exports.js +0 -11
  75. package/swc/plugin-define-config/tests/__swc_snapshots__/src/lib.rs/var_decl_app.js +0 -3
  76. package/swc/plugin-define-config/tests/__swc_snapshots__/src/lib.rs/var_decl_page.js +0 -3
@@ -1,526 +0,0 @@
1
-
2
- use swc_core::{
3
- common::{
4
- util::take::Take,
5
- DUMMY_SP as span
6
- },
7
- ecma::{
8
- self,
9
- ast::*,
10
- atoms::Atom,
11
- visit::{VisitMut, VisitMutWith},
12
- },
13
- };
14
- use std::collections::HashMap;
15
- use std::collections::HashSet;
16
- use crate::PluginConfig;
17
- use crate::utils::{self, constants::*, harmony::components::*};
18
-
19
- pub struct PreVisitor {
20
- is_in_and_expr: bool,
21
- }
22
- impl PreVisitor {
23
- fn new () -> Self {
24
- Self {
25
- is_in_and_expr: false
26
- }
27
- }
28
- fn process_cond_expr (&self, expr: &mut CondExpr) {
29
- let test = &expr.test;
30
- let cons = &mut expr.cons;
31
- let compile_if = utils::create_jsx_expr_attr(COMPILE_IF, test.clone());
32
- let process_cond_arm = |arm: &mut Box<Expr>, attr: JSXAttrOrSpread| {
33
- match &mut **arm {
34
- Expr::JSXElement(el) => {
35
- el.opening.attrs.push(attr);
36
- },
37
- Expr::Cond(cond_expr) => {
38
- self.process_cond_expr(cond_expr);
39
- },
40
- _ => ()
41
- }
42
- };
43
- process_cond_arm(cons, compile_if);
44
- }
45
- }
46
- impl VisitMut for PreVisitor {
47
- fn visit_mut_jsx_element_child (&mut self, child: &mut JSXElementChild) {
48
- if let JSXElementChild::JSXExprContainer(JSXExprContainer { expr: JSXExpr::Expr(expr), .. }) = child {
49
- let mut is_first_and_expr = false;
50
-
51
- if let Expr::Paren(ParenExpr { expr: e, .. }) = &mut **expr {
52
- *expr = e.take();
53
- }
54
-
55
- // is_in_and_expr 为 false 时,表示为当前 expr 的第一个 && 表达式,即当出现 aa && bb && <View /> 这种表达式时,只会处理最右侧的 && 表达式
56
- match &mut **expr {
57
- // 将 aa && <B /> 转换为 aa ? <B /> : <B compileIgnore />
58
- Expr::Bin(BinExpr { op, left, right, ..}) => {
59
- // C&&A 替换为 C?A:A',原因是为了无论显示还是隐藏都保留一个元素,从而不影响兄弟节点的变量路径
60
- if *op == op!("&&") && !self.is_in_and_expr {
61
- is_first_and_expr = true;
62
- fn inject_compile_if (el: &mut Box<JSXElement>, condition: &mut Box<Expr>) -> () {
63
- el.opening.attrs.push(utils::create_jsx_expr_attr(COMPILE_IF, condition.clone()));
64
- }
65
- fn get_element_double (element_name: JSXElementName, condition: &mut Box<Expr>, right: &mut Box<Expr>) -> Expr {
66
- Expr::Cond(CondExpr {
67
- span,
68
- test: condition.take(),
69
- cons: right.take(),
70
- alt: Box::new(utils::create_self_closing_jsx_element_expr(
71
- element_name, // element 替换为同类型的元素。在显示/隐藏切换时,让运行时 diff 只更新必要属性而不是整个节点刷新
72
- Some(vec![utils::create_jsx_bool_attr(COMPILE_IGNORE)]
73
- )))
74
- })
75
- }
76
- match &mut **right {
77
- Expr::JSXElement(el) => {
78
- let element_name = el.opening.name.clone();
79
- inject_compile_if(el, left);
80
- **expr = get_element_double(element_name, left, right);
81
- },
82
- Expr::Paren(ParenExpr { expr: paren_expr, .. }) => {
83
- if paren_expr.is_jsx_element() {
84
- let el: &mut Box<JSXElement> = paren_expr.as_mut_jsx_element().unwrap();
85
- let element_name = el.opening.name.clone();
86
- inject_compile_if(el, left);
87
- **expr = get_element_double(element_name, left, paren_expr);
88
- }
89
- },
90
- Expr::Lit(_) => {
91
- **expr = Expr::Cond(CondExpr {
92
- span,
93
- test: left.take(),
94
- cons: right.take(),
95
- alt: Box::new(Expr::Lit(Lit::Str(Str { span, value: String::new().into(), raw: None })))
96
- })
97
- },
98
- _ => ()
99
- }
100
- }
101
- },
102
- Expr::Cond(cond_expr) => {
103
- self.process_cond_expr(cond_expr);
104
- },
105
- _ => (),
106
- }
107
-
108
- if is_first_and_expr {
109
- self.is_in_and_expr = true;
110
- }
111
-
112
- expr.visit_mut_children_with(self);
113
-
114
- if is_first_and_expr {
115
- self.is_in_and_expr = false;
116
- }
117
- } else {
118
- child.visit_mut_children_with(self);
119
- }
120
- }
121
- }
122
-
123
- pub struct TransformVisitor {
124
- pub config: PluginConfig,
125
- pub is_compile_mode: bool,
126
- pub node_stack: HashMap<String, Vec<i32>>,
127
- pub node_name: Vec<String>,
128
- pub component_set: HashSet<String>,
129
- pub templates: HashMap<String, String>,
130
- pub get_tmpl_name: Box<dyn FnMut() -> String>,
131
- pub node_name_vec: Vec<String>,
132
- pub get_node_name: Box<dyn FnMut() -> String>
133
- }
134
-
135
- impl TransformVisitor {
136
- pub fn new (config: PluginConfig) -> Self {
137
- let get_node_name = Box::new(utils::named_iter(String::from("node")));
138
- let get_tmpl_name = Box::new(utils::named_iter(format!("{}t", config.tmpl_prefix)));
139
-
140
- Self {
141
- config,
142
- is_compile_mode: false,
143
- node_name: vec![],
144
- node_stack: HashMap::new(),
145
- templates: HashMap::new(),
146
- get_tmpl_name,
147
- component_set: HashSet::new(),
148
- node_name_vec: vec![],
149
- get_node_name
150
- }
151
- }
152
-
153
- fn build_ets_element (&mut self, el: &mut JSXElement) -> String {
154
- // jsx 节点添加动态 id,需要判断是否存在静态节点
155
- let dynmaic_node_name: String;
156
- let mut is_node_name_created = false;
157
-
158
- // 如果是半编译状态下碰到的第一个 jsx,或者 jsx 含有动态属性,则创建新的 node_name
159
- if !self.check_jsx_is_static(el) || self.node_stack.is_empty() {
160
- dynmaic_node_name = utils::create_jsx_dynamic_id(el, self);
161
- self.node_name.push(dynmaic_node_name.clone());
162
- is_node_name_created = true;
163
- } else {
164
- dynmaic_node_name = self.get_current_node_path();
165
- }
166
-
167
- let opening_element = &mut el.opening;
168
-
169
- let child_string = match &opening_element.name {
170
- JSXElementName::Ident(ident) => {
171
- let name = utils::to_kebab_case(&ident.sym);
172
-
173
- match self.config.support_components.iter().find(|&component| component == &name) {
174
- // 内置组件
175
- Some(_) => {
176
- // 事件的处理,根据事件添加对应的 ets 事件处理函数
177
- let event_string: String = self.build_ets_event(opening_element);
178
-
179
- // 判断 el 的子元素是否只有一个循环,如果是的话,直接使用 createNode 来生成后续子节点
180
- let is_loop_exist = utils::check_jsx_element_children_exist_loop(el);
181
- let mut children = utils::create_original_node_renderer_foreach(self);
182
-
183
- if !is_loop_exist {
184
- let (ets_children, ..) = self.build_ets_children(&mut el.children, None);
185
- children = ets_children;
186
- }
187
-
188
- // 当前 node_name 节点树已全部递归完毕
189
- if is_node_name_created {
190
- self.node_name.pop();
191
- }
192
-
193
- let mut code = match name.as_str() {
194
- VIEW_TAG => {
195
- self.component_set.insert(name.clone());
196
- get_view_component_str(&dynmaic_node_name, &children)
197
- },
198
- TEXT_TAG => {
199
- self.component_set.insert(name.clone());
200
- get_text_component_str(&dynmaic_node_name)
201
- },
202
- IMAGE_TAG => {
203
- self.component_set.insert(name.clone());
204
- get_image_component_str(&dynmaic_node_name)
205
- },
206
- _ => String::new()
207
- };
208
-
209
- code.push_str(event_string.as_str());
210
- utils::add_spaces_to_lines(code.as_str())
211
- },
212
- None => {
213
- // React 组件
214
- // 原生自定义组件, 不支持自定义组件的跟节点是一个 fragment!!
215
- // 半编译暂未支持的组件
216
- utils::create_original_node_renderer(self)
217
- }
218
- }
219
- }
220
- _ => String::new()
221
- };
222
-
223
- child_string
224
- }
225
-
226
- fn build_ets_children (&mut self, children: &mut Vec<JSXElementChild>, retain_start_from: Option<i32>) -> (String, i32) {
227
- let mut children_string = String::new();
228
- let start = if retain_start_from.is_some() { retain_start_from.unwrap() } else { 0 };
229
- let mut retain_child_counter = start;
230
-
231
- // 迭代 el.children
232
- children.iter_mut().for_each(|child| {
233
- self.push_node_stack(retain_child_counter);
234
-
235
- match child {
236
- JSXElementChild::JSXElement(child_el) => {
237
- let child_string = self.build_ets_element(&mut **child_el);
238
- children_string.push_str(&child_string);
239
- retain_child_counter += 1;
240
- },
241
- JSXElementChild::JSXExprContainer(JSXExprContainer {
242
- expr: JSXExpr::Expr(jsx_expr),
243
- ..
244
- }) => {
245
- // 如果套着 (),则获取括号里面的内容
246
- if let Expr::Paren(ParenExpr { expr, .. }) = &mut **jsx_expr {
247
- *jsx_expr = expr.take();
248
- }
249
- match &mut **jsx_expr {
250
- Expr::Cond(cond_expr) => {
251
- children_string.push_str(self.build_ets_cond_expr(cond_expr).as_str());
252
- },
253
- Expr::Call(CallExpr {
254
- callee: Callee::Expr(callee_expr),
255
- ..
256
- }) => {
257
- let mut tmpl = utils::create_normal_text_template(self);
258
- if utils::is_render_fn(callee_expr) {
259
- tmpl = utils::create_original_node_renderer(self);
260
- }
261
- children_string.push_str(&tmpl);
262
- },
263
- _ => {
264
- let code = utils::create_normal_text_template(self);
265
- children_string.push_str(&code);
266
- }
267
- };
268
- retain_child_counter += 1;
269
- },
270
- JSXElementChild::JSXText(jsx_text) => {
271
- let content = utils::jsx_text_to_string(&jsx_text.value);
272
- if !content.is_empty() {
273
- let current_path = self.get_current_node_path();
274
- let code = utils::add_spaces_to_lines(get_text_component_str(&current_path).as_str());
275
-
276
- children_string.push_str(code.as_str());
277
- self.component_set.insert(TEXT_TAG.clone().to_string());
278
- retain_child_counter += 1;
279
- }
280
- },
281
- JSXElementChild::JSXFragment(child_el) => {
282
- self.pop_node_stack();
283
- let (child_string, inner_retain) = self.build_ets_children(&mut child_el.children, Some(retain_child_counter));
284
- children_string.push_str(&child_string);
285
- if inner_retain != 0 {
286
- retain_child_counter += inner_retain;
287
- }
288
- self.push_node_stack(retain_child_counter);
289
- },
290
- _ => (),
291
- }
292
-
293
- self.pop_node_stack();
294
- });
295
-
296
- (children_string, retain_child_counter - start)
297
- }
298
-
299
- fn build_ets_cond_expr (&mut self, cond_expr: &mut CondExpr) -> String {
300
- let mut children_string = String::new();
301
- let mut process_condition_expr = |arm: &mut Box<Expr>| {
302
- match &mut **arm {
303
- Expr::JSXElement(el) => {
304
- // 判断 el 的属性中是否存在 COMPILE_IGNORE,如果存在则返回空字符串
305
- if utils::check_jsx_element_has_compile_ignore(el) {
306
- String::new()
307
- } else {
308
- self.build_ets_element(el)
309
- }
310
- },
311
- Expr::Lit(_) => {
312
- // {condition1 && 'Hello'} 在预处理时会变成 {condition1 ? 'Hello' : "compileIgnore"}
313
- // 而普通文本三元则会被 block 标签包裹,因此处理后只有上述情况会存在 lit 类型的表达式
314
- // 由于这种情况没有办法使用 wx:if 来处理,需要特殊处理成 {{i.cn[3].v==="compileIgnore"?"":i.cn[3].v}} 的形式
315
- let current_path = self.get_current_node_path();
316
-
317
- self.component_set.insert(TEXT_TAG.to_string());
318
- utils::add_spaces_to_lines(get_text_component_str(&current_path).as_str())
319
- },
320
- Expr::Cond(cond_expr) => self.build_ets_cond_expr(cond_expr),
321
- _ => String::new()
322
- }
323
- };
324
- let alt_children_string = process_condition_expr(&mut cond_expr.alt);
325
- let cons_children_string = process_condition_expr(&mut cond_expr.cons as &mut Box<Expr>);
326
-
327
- children_string.push_str(format!("if ((this.{} as TaroElement)._attrs.compileIf) {{\n{}}}", self.get_current_node_path(), cons_children_string).as_str());
328
- if !alt_children_string.is_empty() {
329
- children_string.push_str(format!(" else {{\n{}}}", alt_children_string).as_str());
330
- }
331
-
332
- utils::add_spaces_to_lines(&children_string)
333
- }
334
-
335
- fn check_jsx_is_static (&self, el: &mut JSXElement) -> bool {
336
- let opening_element = &mut el.opening;
337
-
338
- for attr in opening_element.attrs.iter_mut() {
339
- if let JSXAttrOrSpread::JSXAttr(jsx_attr) = attr {
340
- if let JSXAttrName::Ident(..) = &jsx_attr.name {
341
- if let JSXAttrName::Ident(Ident { sym: name, .. }) = &jsx_attr.name {
342
- let jsx_attr_name = name.to_string();
343
- let event_name = utils::identify_jsx_event_key(&jsx_attr_name);
344
- let is_event = event_name.is_some();
345
- let is_condition = jsx_attr_name == COMPILE_IF;
346
-
347
- if let Some(value) = &jsx_attr.value {
348
- match value {
349
- JSXAttrValue::Lit(..) => (),
350
- JSXAttrValue::JSXExprContainer(JSXExprContainer{ expr, .. }) => {
351
- // jsx_attr 是事件,而且事件的 value 不是一个变量,那么当作静态属性,否则 JSXExprContainer 情况下都当作非静态属性
352
- if is_event {
353
- if let JSXExpr::Expr(expr) = &expr {
354
- if let Expr::Ident(..) = &**expr {
355
- return false
356
- }
357
- }
358
- } else if !is_condition {
359
- return false
360
- }
361
-
362
- },
363
- _ => return false
364
- }
365
- }
366
- }
367
- }
368
- }
369
- }
370
- // 判断当前 el 的 children 是否是表达式,表达式的话父节点需要标注为非静态
371
- for child in el.children.iter_mut() {
372
- if let JSXElementChild::JSXExprContainer(JSXExprContainer { .. }) = child {
373
- return false
374
- }
375
- }
376
-
377
- return true;
378
- }
379
-
380
- fn build_ets_event (&self, opening_element: &mut JSXOpeningElement) -> String {
381
- let mut event_string = String::new();
382
-
383
- // 在 transform 中使用的是 retain_mut 方法,在 retain_mut 中,如果 return false,就代表该属性不需要保留,在小程序中主要用于剪枝静态属性,鸿蒙这里不需要处理
384
- for attr in opening_element.attrs.iter_mut() {
385
- if let JSXAttrOrSpread::JSXAttr(jsx_attr) = attr {
386
- if let JSXAttrName::Ident(Ident { sym: name, .. }) = &jsx_attr.name {
387
- let mut jsx_attr_name = name.to_string();
388
- let common_event = &self.config.support_events;
389
- let is_event = utils::check_is_event_attr(&jsx_attr_name);
390
-
391
- // 如果不是事件或者不是 common_event 里的事件,则执行下一次迭代
392
- if !is_event || !common_event.iter().any(|event| event == &jsx_attr_name) {
393
- continue;
394
- }
395
-
396
- let harmony_event_name = self.config.event_adapter.get(jsx_attr_name.as_str());
397
-
398
- if harmony_event_name.is_some() {
399
- jsx_attr_name = harmony_event_name.unwrap().to_string();
400
- }
401
-
402
- event_string.push_str(&create_component_event(jsx_attr_name.as_str(), &self.get_current_node_path()))
403
- }
404
- }
405
- }
406
-
407
- event_string
408
- }
409
-
410
- pub fn get_current_node_path (&self) -> String {
411
- let current_node_name = self.node_name.last().unwrap().clone();
412
- let current_node_stack = match self.node_stack.get(&current_node_name) {
413
- Some(stack) => stack,
414
- None => return current_node_name
415
- };
416
-
417
- // return: node0.childNodes[0].childNodes[0]....
418
- current_node_stack.iter().fold(current_node_name, |mut acc, item| {
419
- acc.push_str(&format!(".childNodes[{}]", item));
420
- return acc;
421
- })
422
- }
423
-
424
- pub fn pop_node_stack (&mut self) {
425
- self.node_stack.entry(self.node_name.last().unwrap().clone()).or_insert(vec![]).pop();
426
- }
427
-
428
- pub fn push_node_stack (&mut self, index: i32) {
429
- self.node_stack.entry(self.node_name.last().unwrap().clone()).or_insert(vec![]).push(index);
430
- }
431
- }
432
-
433
- impl VisitMut for TransformVisitor {
434
- // Implement necessary visit_mut_* methods for actual custom transform.
435
- // A comprehensive list of possible visitor methods can be found here:
436
- // https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
437
- // 半编译模式入口,遍历 jsx 语法树,寻找拥有 COMPILE_MODE 属性节点,预处理节点所在的子树,并根据子树信息生成 tmpl_contents
438
- fn visit_mut_jsx_element (&mut self, el: &mut JSXElement) {
439
- let mut tmpl_name = String::new();
440
- for attr in &mut el.opening.attrs {
441
- if let JSXAttrOrSpread::JSXAttr(jsx_attr) = attr {
442
- if let JSXAttrName::Ident(jsx_attr_name) = &jsx_attr.name {
443
- if &*jsx_attr_name.sym == COMPILE_MODE {
444
- self.is_compile_mode = true;
445
- tmpl_name = (self.get_tmpl_name)();
446
- jsx_attr.value = Some(JSXAttrValue::Lit(Lit::Str(Str {
447
- span,
448
- value: tmpl_name.clone().into(),
449
- raw: None,
450
- })));
451
- break;
452
- }
453
- }
454
- }
455
- }
456
- if self.is_compile_mode {
457
- el.visit_mut_children_with(&mut PreVisitor::new());
458
-
459
- let tmpl_build_contents = format!("build() {{\n{content}}}", content = self.build_ets_element(el));
460
- let tmpl_node_declare_contents = self.node_name_vec.iter().fold(String::new(), |mut acc, item| {
461
- acc.push_str(&format!("@State {}: TaroElement = new TaroIgnoreElement()\n", item));
462
- return acc;
463
- });
464
- let tmpl_main_contents = utils::add_spaces_to_lines(&format!("{}\n{}", tmpl_node_declare_contents, tmpl_build_contents));
465
- let tmpl_contents =
466
- HARMONY_IMPORTER.to_owned() +
467
- utils::get_harmony_component_style(self).as_str() +
468
- format!(
469
- r#"@Component
470
- export default struct TARO_TEMPLATES_{name} {{
471
- nodeInfoMap: TaroAny = {{}}
472
- dynamicCenter: DynamicCenter = new DynamicCenter()
473
- @ObjectLink node: TaroViewElement
474
-
475
- aboutToAppear () {{
476
- this.dynamicCenter.bindComponentToNodeWithDFS(this.node, this)
477
- }}
478
-
479
- {content}}}
480
- "#,
481
- name = tmpl_name,
482
- content = tmpl_main_contents
483
- ).as_str();
484
-
485
- self.templates.insert(tmpl_name, format!("`{}`", tmpl_contents));
486
-
487
- // 数据清理
488
- self.node_stack.clear();
489
- self.node_name.clear();
490
- self.node_name_vec.clear();
491
- self.component_set.clear();
492
- self.is_compile_mode = false;
493
- self.get_node_name = Box::new(utils::named_iter(String::from("node")));
494
- } else {
495
- el.visit_mut_children_with(self)
496
- }
497
- }
498
-
499
- // 将生成的模板字符串以变量的形式插入在文件最上面,等待后续编译抽离
500
- fn visit_mut_module_items (&mut self, body_stmts: &mut Vec<ModuleItem>) {
501
- body_stmts.visit_mut_children_with(self);
502
-
503
- let mut keys: Vec<&String> = self.templates.keys().collect();
504
- keys.sort();
505
- let stmts_being_inserted = keys.into_iter()
506
- .map(|key| {
507
- let value = self.templates.get(key).unwrap();
508
- ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
509
- span,
510
- kind: VarDeclKind::Const,
511
- declare: false,
512
- decls: vec![VarDeclarator {
513
- span,
514
- name: Pat::Ident(Ident::new(format!("TARO_TEMPLATES_{}", key).as_str().into(), span).into()),
515
- init: Some(Box::new(Expr::Lit(Lit::Str(Str {
516
- span,
517
- value: value.as_str().into(),
518
- raw: Some(Atom::new(value.as_str()))
519
- })))),
520
- definite: false,
521
- }],
522
- }))))
523
- });
524
- ecma::utils::prepend_stmts(body_stmts, stmts_being_inserted);
525
- }
526
- }
@@ -1,152 +0,0 @@
1
- pub const COMPILE_MODE: &str = "compileMode";
2
- pub const COMPILE_IF: &str = "compileIf";
3
- pub const COMPILE_ELSE: &str = "compileElse";
4
- pub const COMPILE_IGNORE: &str = "compileIgnore";
5
- pub const COMPILE_FOR: &str = "compileFor";
6
- pub const COMPILE_FOR_KEY: &str = "compileForKey";
7
- pub const EVENT_HANDLER: &str = "eh";
8
- pub const DATA_SID: &str = "data-sid";
9
- pub const TMPL_DATA_ROOT: &str = "i.";
10
- pub const ID: &str = "id";
11
- pub const LOOP_WRAPPER_ID: i32 = -1;
12
- pub const DYNAMIC_ID: &str = "_dynamicID";
13
- pub const REACT_RESERVED: [&str; 2] = ["key", "ref"];
14
-
15
- pub const VIEW_TAG: &str = "view";
16
- pub const TEXT_TAG: &str = "text";
17
- pub const IMAGE_TAG: &str = "image";
18
-
19
- pub const HARMONY_IMPORTER: &str = "import { createNode } from '../render'
20
- import { FlexManager } from '../utils/FlexManager'
21
- import { TOUCH_EVENT_MAP } from '../utils/constant/event'
22
- import { getNodeThresholds, getNormalAttributes, getFontAttributes } from '../utils/helper'
23
- import { TaroIgnoreElement, eventHandler, getComponentEventCallback, AREA_CHANGE_EVENT_NAME, VISIBLE_CHANGE_EVENT_NAME } from '../../runtime'
24
- import { DynamicCenter } from '../utils/DynamicCenter'
25
-
26
- import type { TaroViewElement } from '../element'
27
- import type { TaroElement, TaroAny, TaroStyleType, TaroTextStyleType } from '../../runtime'
28
-
29
- ";
30
-
31
- pub const HARMONY_FLEX_STYLE_BIND: &str = r#"@Extend(Flex)
32
- function attrs (style: TaroStyleType) {
33
- .id(style.id)
34
- .key(style.id)
35
- .padding(style.padding)
36
- .margin(style.margin)
37
- .width(style.width)
38
- .height(style.height)
39
- .constraintSize(style.constraintSize)
40
- .flexGrow(style.flexGrow)
41
- .flexShrink(style.flexShrink)
42
- .flexBasis(style.flexBasis)
43
- .alignSelf(style.alignSelf)
44
- .backgroundColor(style.backgroundColor)
45
- .backgroundImage(style.backgroundImage, style.backgroundRepeat)
46
- .backgroundImageSize(style.backgroundImageSize)
47
- .backgroundImagePosition(style.backgroundImagePosition)
48
- .rotate(style.rotate)
49
- .scale(style.scale)
50
- .translate(style.translate)
51
- .transform(style.transform)
52
- .borderStyle(style.borderStyle)
53
- .borderWidth(style.borderWidth)
54
- .borderColor(style.borderColor)
55
- .borderRadius(style.borderRadius)
56
- .linearGradient(style.linearGradient)
57
- .zIndex(style.zIndex)
58
- .opacity(style.opacity)
59
- .clip(style.clip)
60
- }
61
- "#;
62
- pub const HARMONY_TEXT_STYLE_BIND: &str = r#"@Extend(Text)
63
- function textStyle (style: TaroStyleType) {
64
- .id(style.id)
65
- .key(style.id)
66
- .padding(style.padding)
67
- .margin(style.margin)
68
- .width(style.width)
69
- .height(style.height)
70
- .constraintSize(style.constraintSize)
71
- .flexGrow(style.flexGrow)
72
- .flexShrink(style.flexShrink)
73
- .flexBasis(style.flexBasis)
74
- .alignSelf(style.alignSelf)
75
- .backgroundColor(style.backgroundColor)
76
- .backgroundImage(style.backgroundImage, style.backgroundRepeat)
77
- .backgroundImageSize(style.backgroundImageSize)
78
- .backgroundImagePosition(style.backgroundImagePosition)
79
- .rotate(style.rotate)
80
- .scale(style.scale)
81
- .translate(style.translate)
82
- .transform(style.transform)
83
- .borderStyle(style.borderStyle)
84
- .borderWidth(style.borderWidth)
85
- .borderColor(style.borderColor)
86
- .borderRadius(style.borderRadius)
87
- .linearGradient(style.linearGradient)
88
- .zIndex(style.zIndex)
89
- .opacity(style.opacity)
90
- .clip(style.clip)
91
- .fontColor(style.color)
92
- .fontSize(style.fontSize)
93
- .fontWeight(style.fontWeight)
94
- .fontStyle(style.fontStyle)
95
- .fontFamily(style.fontFamily)
96
- .lineHeight(style.lineHeight)
97
- .decoration({
98
- type: style.decoration,
99
- color: style.color
100
- })
101
- }
102
-
103
- @Extend(Text)
104
- function textAttr(attr: TaroTextStyleType) {
105
- .textAlign(attr.textAlign)
106
- .textOverflow(attr.textOverflow)
107
- .maxLines(attr.maxLines)
108
- .letterSpacing(attr.letterSpacing)
109
- }
110
- "#;
111
- pub const HARMONY_IMAGE_STYLE_BIND: &str = r#"@Extend(Image)
112
- function attrsImage (style: TaroStyleType) {
113
- .id(style.id)
114
- .key(style.id)
115
- .padding(style.padding)
116
- .margin(style.margin)
117
- .width(style.width)
118
- .height(style.height)
119
- .constraintSize(style.constraintSize)
120
- .flexGrow(style.flexGrow)
121
- .flexShrink(style.flexShrink)
122
- .flexBasis(style.flexBasis)
123
- .alignSelf(style.alignSelf)
124
- .backgroundColor(style.backgroundColor)
125
- .backgroundImage(style.backgroundImage, style.backgroundRepeat)
126
- .backgroundImageSize(style.backgroundImageSize)
127
- .backgroundImagePosition(style.backgroundImagePosition)
128
- .rotate(style.rotate)
129
- .scale(style.scale)
130
- .translate(style.translate)
131
- .transform(style.transform)
132
- .borderStyle(style.borderStyle)
133
- .borderWidth(style.borderWidth)
134
- .borderColor(style.borderColor)
135
- .borderRadius(style.borderRadius)
136
- .linearGradient(style.linearGradient)
137
- .zIndex(style.zIndex)
138
- .opacity(style.opacity)
139
- .clip(style.clip)
140
- }
141
-
142
- function getImageMode (mode: string): ImageFit {
143
- switch (mode) {
144
- case 'aspectFit': return ImageFit.Contain
145
- case 'aspectFill': return ImageFit.Cover
146
- case 'scaleToFill': return ImageFit.Fill
147
- case 'widthFix': return ImageFit.Auto
148
- case 'heightFix': return ImageFit.Auto
149
- default: return ImageFit.Contain
150
- }
151
- }
152
- "#;