@tarojs/helper 3.7.0-beta.3 → 3.7.0-beta.4

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.
@@ -1,27 +1,18 @@
1
1
  [package]
2
2
  name = "swc-plugin-define-config"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  edition = "2021"
5
5
 
6
6
  [lib]
7
7
  crate-type = ["cdylib"]
8
8
 
9
9
  [profile.release]
10
- # This removes more dead code
11
- codegen-units = 1
12
10
  lto = true
13
- # Optimize for size
14
- opt-level = "s"
15
- # Optimize for performance, this is default so you don't need to specify it
16
- # opt-level = "z"
17
- # Strip debug symbols
18
- strip = "symbols"
19
11
 
20
12
  [dependencies]
21
13
  serde = "1"
22
- swc_core = { version = "0.40.*", features = ["plugin_transform"] }
14
+ swc_core = { version = "0.86.*", features = ["ecma_plugin_transform", "__utils"] }
23
15
 
24
16
  # .cargo/config defines few alias to build plugin.
25
17
  # cargo build-wasi generates wasm-wasi32 binary
26
18
  # cargo build-wasm32 generates wasm32-unknown-unknown binary.
27
-
@@ -1,101 +1,66 @@
1
- use swc_core::ecma::{
2
- ast::*,
3
- transforms::testing::test,
4
- visit::{as_folder, FoldWith, VisitMut, VisitMutWith},
1
+ use swc_core::{
2
+ ecma::{
3
+ ast::*,
4
+ transforms::testing::test,
5
+ visit::{as_folder, FoldWith, VisitMut, VisitMutWith},
6
+ utils::{quote_ident, FunctionFactory, prepend_stmt},
7
+ },
8
+ common::{DUMMY_SP as span, SyntaxContext },
9
+ plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
5
10
  };
6
- use swc_core::common::{DUMMY_SP, SyntaxContext};
7
- use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
8
11
 
9
- struct DefineConfig {
10
- found: bool,
11
- fn_name: String,
12
- ctxt: SyntaxContext
12
+ struct DefineConfigVisitor {
13
+ fn_name: Option<Ident>
13
14
  }
14
15
 
15
- impl DefineConfig {
16
- fn new() -> Self {
17
- Self {
18
- found: false,
19
- fn_name: String::from(""),
20
- ctxt: SyntaxContext::empty()
21
- }
22
- }
23
- }
24
-
25
- impl VisitMut for DefineConfig {
26
- fn visit_mut_call_expr(&mut self, node: &mut CallExpr) {
27
- node.visit_mut_children_with(self);
28
-
29
- let callee = &node.callee;
30
- if let Some(expr_box) = callee.as_expr() {
31
- if let Expr::Ident(ident) = &**expr_box {
32
- if &ident.sym == "defineAppConfig" || &ident.sym == "definePageConfig" {
33
- self.found = true;
34
- self.fn_name = String::from(&*ident.sym);
35
- self.ctxt = ident.span.ctxt;
16
+ impl VisitMut for DefineConfigVisitor {
17
+ fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
18
+ expr.visit_mut_children_with(self);
19
+ if let Callee::Expr(expr) = &expr.callee {
20
+ if let Expr::Ident(ident) = &**expr {
21
+ if ident.sym == "defineAppConfig" || ident.sym == "definePageConfig" {
22
+ self.fn_name = Some(ident.clone());
23
+ }
24
+ }
36
25
  }
37
- }
38
26
  }
39
- }
40
27
  }
41
28
 
42
29
  pub struct TransformVisitor;
43
30
 
44
31
  impl VisitMut for TransformVisitor {
45
- fn visit_mut_module_items(&mut self, module_item: &mut Vec<ModuleItem>) {
46
- module_item.visit_mut_children_with(self);
32
+ fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
33
+ let mut folder = DefineConfigVisitor { fn_name: None };
47
34
 
48
- let mut define_config = DefineConfig::new();
49
- let mut line_to_be_insert: usize = 0;
35
+ let is_found = items.iter_mut().any(|item| {
36
+ item.visit_mut_with(&mut folder);
37
+ folder.fn_name.is_some()
38
+ });
50
39
 
51
- for (idx, item) in module_item.iter_mut().enumerate() {
52
- match item {
53
- ModuleItem::Stmt(stmt) => {
54
- stmt.visit_mut_with(&mut define_config);
55
- if define_config.found {
56
- line_to_be_insert = idx;
57
- break;
58
- }
59
- }
60
- ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(expr)) => {
61
- expr.visit_mut_with(&mut define_config);
62
- if define_config.found {
63
- line_to_be_insert = idx;
64
- break;
65
- }
66
- }
67
- _ => ()
40
+ if is_found {
41
+ let fn_name = folder.fn_name.take().unwrap();
42
+ let func = Function {
43
+ span,
44
+ decorators: Default::default(),
45
+ params: vec![Param {
46
+ span,
47
+ decorators: Default::default(),
48
+ pat: Pat::Ident(quote_ident!("config").into()),
49
+ }],
50
+ body: Some(BlockStmt {
51
+ span,
52
+ stmts: vec![Stmt::Return(ReturnStmt {
53
+ span,
54
+ arg: Some(Box::new(quote_ident!("config").into())),
55
+ })],
56
+ }),
57
+ is_async: false,
58
+ is_generator: false,
59
+ type_params: None,
60
+ return_type: None,
61
+ };
62
+ prepend_stmt(items, ModuleItem::Stmt(Stmt::Decl(Decl::Fn(func.into_fn_decl(fn_name)))));
68
63
  }
69
- }
70
-
71
- if define_config.found {
72
- let func = Stmt::Decl(Decl::Fn(FnDecl {
73
- ident: Ident::new(define_config.fn_name.into(), DUMMY_SP.with_ctxt(define_config.ctxt)),
74
- declare: false,
75
- function: Box::new(Function {
76
- span: DUMMY_SP,
77
- params: vec![Param {
78
- span: DUMMY_SP,
79
- decorators: Default::default(),
80
- pat: Pat::Ident(BindingIdent::from(Ident::new("config".into(), DUMMY_SP))),
81
- }],
82
- body: Some(BlockStmt {
83
- span: DUMMY_SP,
84
- stmts: vec![Stmt::Return(ReturnStmt {
85
- span: DUMMY_SP,
86
- arg: Some(Box::new(Expr::Ident(Ident::new("config".into(), DUMMY_SP)))),
87
- })],
88
- }),
89
- is_generator: false,
90
- is_async: false,
91
- type_params: Default::default(),
92
- decorators: Default::default(),
93
- return_type: Default::default(),
94
- }),
95
- }));
96
-
97
- module_item.insert(line_to_be_insert, ModuleItem::Stmt(func));
98
- }
99
64
  }
100
65
  }
101
66
 
@@ -127,62 +92,50 @@ test!(
127
92
  Default::default(),
128
93
  |_| as_folder(TransformVisitor),
129
94
  module_decl_default_app,
130
- // Input codes
131
- r#"
132
- export default defineAppConfig({})
133
- "#,
134
- // Output codes after transformed with plugin
135
- r#"
136
- function defineAppConfig(config) {return config}
137
- export default defineAppConfig({})
138
- "#
95
+ r#"export default defineAppConfig({})"#
139
96
  );
140
97
 
141
98
  test!(
142
99
  Default::default(),
143
100
  |_| as_folder(TransformVisitor),
144
101
  module_decl_default_page,
145
- // Input codes
146
- r#"
147
- export default definePageConfig({})
148
- "#,
149
- // Output codes after transformed with plugin
102
+ r#"export default definePageConfig({})"#
103
+ );
104
+
105
+ test!(
106
+ Default::default(),
107
+ |_| as_folder(TransformVisitor),
108
+ var_decl_app,
150
109
  r#"
151
- function definePageConfig(config) {return config}
152
- export default definePageConfig({})
110
+ const config = defineAppConfig({})
111
+ export default config
153
112
  "#
154
113
  );
155
114
 
156
115
  test!(
157
- Default::default(),
158
- |_| as_folder(TransformVisitor),
159
- var_decl_app,
160
- // Input codes
161
- r#"
162
- const config = defineAppConfig({})
163
- export default config
164
- "#,
165
- // Output codes after transformed with plugin
166
- r#"
167
- function defineAppConfig(config) {return config}
168
- const config = defineAppConfig({})
169
- export default config
170
- "#
116
+ Default::default(),
117
+ |_| as_folder(TransformVisitor),
118
+ var_decl_page,
119
+ r#"
120
+ const config = definePageConfig({})
121
+ export default config
122
+ "#
171
123
  );
172
124
 
173
125
  test!(
174
- Default::default(),
175
- |_| as_folder(TransformVisitor),
176
- var_decl_page,
177
- // Input codes
178
- r#"
179
- const config = definePageConfig({})
180
- export default config
181
- "#,
182
- // Output codes after transformed with plugin
183
- r#"
184
- function definePageConfig(config) {return config}
185
- const config = definePageConfig({})
186
- export default config
187
- "#
126
+ Default::default(),
127
+ |_| as_folder(TransformVisitor),
128
+ module_exports,
129
+ r#"
130
+ var require_index_config = __commonJS({
131
+ "src/pages/index/index.config.ts": function(exports1, module) {
132
+ var config = definePageConfig({
133
+ navigationBarTitleText: "首页",
134
+ usingComponents: {}
135
+ });
136
+ module.exports = config;
137
+ }
138
+ });
139
+ var _default = require_index_config();
140
+ "#
188
141
  );
@@ -0,0 +1,2 @@
1
+ function defineAppConfig(config) {return config}
2
+ export default defineAppConfig({})
@@ -0,0 +1,2 @@
1
+ function definePageConfig(config) {return config}
2
+ export default definePageConfig({})
@@ -0,0 +1,11 @@
1
+ function definePageConfig(config) {return config}
2
+ var require_index_config = __commonJS({
3
+ "src/pages/index/index.config.ts": function(exports1, module) {
4
+ var config = definePageConfig({
5
+ navigationBarTitleText: "首页",
6
+ usingComponents: {}
7
+ });
8
+ module.exports = config;
9
+ }
10
+ });
11
+ var _default = require_index_config();
@@ -0,0 +1,3 @@
1
+ function defineAppConfig(config) {return config}
2
+ const config = defineAppConfig({})
3
+ export default config
@@ -0,0 +1,3 @@
1
+ function definePageConfig(config) {return config}
2
+ const config = definePageConfig({})
3
+ export default config