@tarojs/helper 3.8.0-canary.0 → 4.0.0-alpha.2

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 (38) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +25 -0
  3. package/dist/constants.d.ts +5 -3
  4. package/dist/constants.js +24 -22
  5. package/dist/constants.js.map +1 -1
  6. package/dist/dotenv.js.map +1 -1
  7. package/dist/esbuild/index.js +7 -5
  8. package/dist/esbuild/index.js.map +1 -1
  9. package/dist/npm.d.ts +2 -2
  10. package/dist/npm.js +6 -6
  11. package/dist/npm.js.map +1 -1
  12. package/dist/swcRegister.d.ts +0 -6
  13. package/dist/swcRegister.js +0 -15
  14. package/dist/swcRegister.js.map +1 -1
  15. package/dist/utils.d.ts +33 -1
  16. package/dist/utils.js +83 -33
  17. package/dist/utils.js.map +1 -1
  18. package/package.json +24 -18
  19. package/swc/.gitkeep +0 -0
  20. package/swc/swc_plugin_compile_mode.wasm +0 -0
  21. package/swc/swc_plugin_define_config.wasm +0 -0
  22. package/swc/plugin-compile-mode/Cargo.lock +0 -2086
  23. package/swc/plugin-compile-mode/Cargo.toml +0 -20
  24. package/swc/plugin-compile-mode/src/lib.rs +0 -52
  25. package/swc/plugin-compile-mode/src/tests/attributes.rs +0 -85
  26. package/swc/plugin-compile-mode/src/tests/condition.rs +0 -71
  27. package/swc/plugin-compile-mode/src/tests/entry.rs +0 -32
  28. package/swc/plugin-compile-mode/src/tests/looping.rs +0 -134
  29. package/swc/plugin-compile-mode/src/tests/mod.rs +0 -98
  30. package/swc/plugin-compile-mode/src/tests/shake.rs +0 -41
  31. package/swc/plugin-compile-mode/src/transform.rs +0 -466
  32. package/swc/plugin-compile-mode/src/utils/constants.rs +0 -11
  33. package/swc/plugin-compile-mode/src/utils/mod.rs +0 -236
  34. package/swc/plugin-compile-mode/target/wasm32-wasi/release/swc_plugin_compile_mode.wasm +0 -0
  35. package/swc/plugin-define-config/Cargo.lock +0 -2034
  36. package/swc/plugin-define-config/Cargo.toml +0 -27
  37. package/swc/plugin-define-config/src/lib.rs +0 -188
  38. package/swc/plugin-define-config/target/wasm32-wasi/release/swc_plugin_define_config.wasm +0 -0
@@ -1,236 +0,0 @@
1
- use swc_core::{
2
- ecma::{
3
- atoms::Atom,
4
- ast::*
5
- },
6
- common::{
7
- iter::IdentifyLast,
8
- util::take::Take,
9
- DUMMY_SP as span
10
- },
11
- };
12
- use std::collections::HashMap;
13
-
14
- use self::constants::*;
15
-
16
- pub mod constants;
17
-
18
- pub fn named_iter (str: String) -> impl FnMut() -> String {
19
- let mut count = -1;
20
- return move || {
21
- count += 1;
22
- format!("{str}{count}")
23
- };
24
- }
25
-
26
- pub fn jsx_text_to_string (atom: &Atom) -> String {
27
- let content = atom.replace("\t", " ");
28
-
29
- let res = content
30
- .lines()
31
- .enumerate()
32
- .identify_last()
33
- .fold(String::new(), |mut acc, (is_last, (index, line))| {
34
- // 首行不 trim 头
35
- let line = if index == 0 {
36
- line
37
- } else {
38
- line.trim_start()
39
- };
40
-
41
- // 尾行不 trim 尾
42
- let line = if is_last {
43
- line
44
- } else {
45
- line.trim_end()
46
- };
47
-
48
- if !acc.is_empty() && !line.is_empty() {
49
- acc.push(' ');
50
- }
51
-
52
- acc.push_str(line);
53
- acc
54
- });
55
- res
56
- }
57
-
58
- pub fn is_empty_jsx_text_line (atom: &Atom) -> bool {
59
- let str = jsx_text_to_string(atom);
60
- str.is_empty()
61
- }
62
-
63
- pub fn to_kebab_case (val: &str) -> String {
64
- let mut res = String::new();
65
- val
66
- .chars()
67
- .enumerate()
68
- .for_each(|(idx, c)| {
69
- if idx != 0 && c.is_uppercase() {
70
- res.push('-');
71
- }
72
- res.push(c.to_ascii_lowercase());
73
- });
74
- res
75
- }
76
-
77
- pub fn convert_jsx_attr_key (jsx_key: &str, adapter: &HashMap<String, String>) -> String {
78
- if jsx_key == "className" {
79
- return String::from("class");
80
- } else if
81
- jsx_key == COMPILE_IF ||
82
- jsx_key == COMPILE_ELSE ||
83
- jsx_key == COMPILE_FOR ||
84
- jsx_key == COMPILE_FOR_KEY
85
- {
86
- let expr = match jsx_key {
87
- COMPILE_IF => "if",
88
- COMPILE_ELSE => "else",
89
- COMPILE_FOR => "for",
90
- COMPILE_FOR_KEY => "key",
91
- _ => ""
92
- };
93
- let adapter = adapter.get(expr).expect(&format!("[compile mode] 模板 {} 语法未配置", expr));
94
- return adapter.clone()
95
- }
96
- to_kebab_case(jsx_key)
97
- }
98
-
99
- pub fn identify_jsx_event_key (val: &str) -> Option<String> {
100
- if
101
- val.starts_with("on") &&
102
- val.chars().nth(2).is_some_and(|x| x.is_uppercase())
103
- {
104
- let event_name = val.get(2..).unwrap().to_lowercase();
105
- let event_name = if event_name == "click" {
106
- "tap"
107
- } else {
108
- &event_name
109
- };
110
- Some(format!("bind{}", event_name))
111
- } else {
112
- return None;
113
- }
114
- }
115
-
116
- pub fn is_static_jsx (el: &Box<JSXElement>) -> bool {
117
- if el.opening.attrs.len() > 0 {
118
- return false
119
- }
120
-
121
- for child in &el.children {
122
- if let JSXElementChild::JSXText(_) = child {
123
- } else {
124
- return false
125
- }
126
- }
127
-
128
- true
129
- }
130
-
131
- pub fn create_self_closing_jsx_element_expr (name: JSXElementName, attrs: Option<Vec<JSXAttrOrSpread>>) -> Expr {
132
- Expr::JSXElement(Box::new(JSXElement {
133
- span,
134
- opening: JSXOpeningElement {
135
- name,
136
- span,
137
- attrs: attrs.unwrap_or(vec![]),
138
- self_closing: true,
139
- type_args: None
140
- },
141
- children: vec![],
142
- closing: None
143
- }))
144
- }
145
-
146
- pub fn create_jsx_expr_attr (name: &str, expr: Box<Expr>) -> JSXAttrOrSpread {
147
- JSXAttrOrSpread::JSXAttr(JSXAttr {
148
- span,
149
- name: JSXAttrName::Ident(Ident::new(name.into(), span)),
150
- value: Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
151
- span,
152
- expr: JSXExpr::Expr(expr)
153
- }))
154
- })
155
- }
156
-
157
- pub fn create_jsx_bool_attr (name: &str) -> JSXAttrOrSpread {
158
- JSXAttrOrSpread::JSXAttr(JSXAttr {
159
- span,
160
- name: JSXAttrName::Ident(Ident::new(name.into(), span)),
161
- value: None
162
- })
163
- }
164
-
165
- pub fn create_jsx_lit_attr (name: &str, lit: Lit) -> JSXAttrOrSpread {
166
- JSXAttrOrSpread::JSXAttr(JSXAttr {
167
- span,
168
- name: JSXAttrName::Ident(Ident::new(name.into(), span)),
169
- value: Some(JSXAttrValue::Lit(lit))
170
- })
171
- }
172
-
173
- /**
174
- * identify: `xx.map(function () {})` or `xx.map(() => {})`
175
- */
176
- pub fn is_call_expr_of_loop (callee_expr: &mut Box<Expr>, args: &mut Vec<ExprOrSpread>) -> bool {
177
- if let Expr::Member(MemberExpr { prop: MemberProp::Ident(Ident { sym, ..}), .. }) = &mut **callee_expr {
178
- if &**sym == "map" {
179
- if let Some(ExprOrSpread { expr, .. }) = args.get_mut(0) {
180
- return expr.is_arrow() || expr.is_fn_expr()
181
- }
182
- }
183
- }
184
- return false
185
- }
186
-
187
- pub fn extract_jsx_loop <'a> (callee_expr: &mut Box<Expr>, args: &'a mut Vec<ExprOrSpread>) -> Option<&'a mut Box<JSXElement>> {
188
- if is_call_expr_of_loop(callee_expr, args) {
189
- if let Some(ExprOrSpread { expr, .. }) = args.get_mut(0) {
190
- fn update_return_el (return_value: &mut Box<Expr>) -> Option<&mut Box<JSXElement>> {
191
- if let Expr::Paren(ParenExpr { expr, .. }) = &mut **return_value {
192
- *return_value = expr.take()
193
- }
194
- if return_value.is_jsx_element() {
195
- let el = return_value.as_mut_jsx_element().unwrap();
196
- el.opening.attrs.push(create_jsx_bool_attr(COMPILE_FOR));
197
- el.opening.attrs.push(create_jsx_lit_attr(COMPILE_FOR_KEY, Lit::Str(Str { span, value: "sid".into(), raw: None})));
198
- return Some(el)
199
- }
200
- None
201
- }
202
- match &mut **expr {
203
- Expr::Fn(FnExpr { function, .. }) => {
204
- if let Function { body: Some(BlockStmt { stmts, .. }), .. } = &mut **function {
205
- if let Some(Stmt::Return(ReturnStmt { arg: Some(return_value), .. })) = stmts.last_mut() {
206
- return update_return_el(return_value);
207
- }
208
- }
209
- },
210
- Expr::Arrow(ArrowExpr { body, .. }) => {
211
- match &mut **body {
212
- BlockStmtOrExpr::BlockStmt(BlockStmt { stmts, .. }) => {
213
- if let Some(Stmt::Return(ReturnStmt { arg: Some(return_value), .. })) = stmts.last_mut() {
214
- return update_return_el(return_value);
215
- }
216
- },
217
- BlockStmtOrExpr::Expr(return_value) => {
218
- return update_return_el(return_value);
219
- }
220
- }
221
- },
222
- _ => ()
223
- }
224
- }
225
- }
226
- None
227
- }
228
-
229
- #[test]
230
- fn test_jsx_text () {
231
- assert_eq!(" span ", jsx_text_to_string(&" span ".into()));
232
- assert_eq!(" s xxx ", jsx_text_to_string(&" s\n \n \n xxx ".into()));
233
- assert_eq!("a b", jsx_text_to_string(&" \n \n a b \n ".into()));
234
- assert_eq!("", jsx_text_to_string(&"\n\n\n\n\n\n \n\n ".into()));
235
- assert_eq!("", jsx_text_to_string(&"".into()));
236
- }