@tarojs/helper 3.6.9-alpha.7 → 3.6.9-alpha.8

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,19 @@
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.79.*", features = ["ecma_plugin_transform"] }
15
+ swc_ecma_utils = "0.120.1"
23
16
 
24
17
  # .cargo/config defines few alias to build plugin.
25
18
  # cargo build-wasi generates wasm-wasi32 binary
26
19
  # cargo build-wasm32 generates wasm32-unknown-unknown binary.
27
-
@@ -1,102 +1,75 @@
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
+ },
7
+ common::{
8
+ DUMMY_SP,
9
+ SyntaxContext
10
+ },
5
11
  };
6
- use swc_core::common::{DUMMY_SP, SyntaxContext};
12
+ use swc_ecma_utils::{quote_ident, FunctionFactory};
7
13
  use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
8
14
 
9
- struct DefineConfig {
10
- found: bool,
11
- fn_name: String,
12
- ctxt: SyntaxContext
13
- }
15
+ pub struct TransformVisitor;
14
16
 
15
- impl DefineConfig {
16
- fn new() -> Self {
17
- Self {
18
- found: false,
19
- fn_name: String::from(""),
20
- ctxt: SyntaxContext::empty()
21
- }
22
- }
17
+ struct DefineFoler {
18
+ name: Option<String>,
19
+ ctxt: Option<SyntaxContext>,
23
20
  }
24
21
 
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;
36
- }
22
+ impl VisitMut for DefineFoler {
23
+ fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
24
+ expr.visit_mut_children_with(self);
25
+ if let Callee::Expr(expr) = &expr.callee {
26
+ if let Expr::Ident(ident) = &**expr {
27
+ if &*ident.sym == "defineAppConfig" || &*ident.sym == "definePageConfig" {
28
+ self.name = Some(String::from(&*ident.sym));
29
+ self.ctxt = Some(ident.span.ctxt);
30
+ }
31
+ }
37
32
  }
38
- }
39
33
  }
40
34
  }
41
35
 
42
- pub struct TransformVisitor;
43
-
44
36
  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);
47
-
48
- let mut define_config = DefineConfig::new();
37
+ fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
38
+ let mut folder = DefineFoler { name: None, ctxt: None };
49
39
  let mut line_to_be_insert: usize = 0;
50
40
 
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
- }
41
+ for (index, item) in items.iter_mut().enumerate() {
42
+ item.visit_mut_with(&mut folder);
43
+ if folder.name.is_some() {
44
+ line_to_be_insert = index;
45
+ break
59
46
  }
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
- _ => ()
68
- }
69
47
  }
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 {
48
+ if let Some(name) = &folder.name {
49
+ let func_name = quote_ident!(DUMMY_SP.with_ctxt(folder.ctxt.unwrap()), name.as_str());
50
+ let func = Function {
78
51
  span: DUMMY_SP,
79
52
  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));
53
+ params: vec![Param {
54
+ span: DUMMY_SP,
55
+ decorators: Default::default(),
56
+ pat: Pat::Ident(quote_ident!("config").into()),
57
+ }],
58
+ body: Some(BlockStmt {
59
+ span: DUMMY_SP,
60
+ stmts: vec![Stmt::Return(ReturnStmt {
61
+ span: DUMMY_SP,
62
+ arg: Some(Box::new(quote_ident!("config").into())),
63
+ })],
64
+ }),
65
+ is_async: false,
66
+ is_generator: false,
67
+ type_params: None,
68
+ return_type: None,
69
+ };
70
+ items.insert(line_to_be_insert, ModuleItem::Stmt(Stmt::Decl(Decl::Fn(func.into_fn_decl(func_name)))));
98
71
  }
99
- }
72
+ }
100
73
  }
101
74
 
102
75
  /// An example plugin function with macro support.
@@ -116,7 +89,7 @@ impl VisitMut for TransformVisitor {
116
89
  /// Refer swc_plugin_macro to see how does it work internally.
117
90
  #[plugin_transform]
118
91
  pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
119
- program.fold_with(&mut as_folder(TransformVisitor))
92
+ program.fold_with(&mut as_folder(TransformVisitor))
120
93
  }
121
94
 
122
95
  // An example to test plugin transform.
@@ -124,65 +97,98 @@ pub fn process_transform(program: Program, _metadata: TransformPluginProgramMeta
124
97
  // the Visitor's behavior, instead of trying to run `process_transform` with mocks
125
98
  // unless explicitly required to do so.
126
99
  test!(
127
- Default::default(),
128
- |_| as_folder(TransformVisitor),
129
- 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
- "#
139
- );
140
-
141
- test!(
142
- Default::default(),
143
- |_| as_folder(TransformVisitor),
144
- module_decl_default_page,
145
- // Input codes
146
- r#"
147
- export default definePageConfig({})
148
- "#,
149
- // Output codes after transformed with plugin
150
- r#"
151
- function definePageConfig(config) {return config}
152
- export default definePageConfig({})
153
- "#
100
+ Default::default(),
101
+ |_| as_folder(TransformVisitor),
102
+ module_decl_default_app,
103
+ // Input codes
104
+ r#"
105
+ export default defineAppConfig({})
106
+ "#,
107
+ // Output codes after transformed with plugin
108
+ r#"
109
+ function defineAppConfig(config) {return config}
110
+ export default defineAppConfig({})
111
+ "#
154
112
  );
155
113
 
156
114
  test!(
157
115
  Default::default(),
158
116
  |_| as_folder(TransformVisitor),
159
- var_decl_app,
117
+ module_decl_default_page,
160
118
  // Input codes
161
119
  r#"
162
- const config = defineAppConfig({})
163
- export default config
120
+ export default definePageConfig({})
164
121
  "#,
165
122
  // Output codes after transformed with plugin
166
123
  r#"
167
- function defineAppConfig(config) {return config}
168
- const config = defineAppConfig({})
169
- export default config
124
+ function definePageConfig(config) {return config}
125
+ export default definePageConfig({})
170
126
  "#
171
127
  );
172
128
 
129
+ test!(
130
+ Default::default(),
131
+ |_| as_folder(TransformVisitor),
132
+ var_decl_app,
133
+ // Input codes
134
+ r#"
135
+ const config = defineAppConfig({})
136
+ export default config
137
+ "#,
138
+ // Output codes after transformed with plugin
139
+ r#"
140
+ function defineAppConfig(config) {return config}
141
+ const config = defineAppConfig({})
142
+ export default config
143
+ "#
144
+ );
145
+
146
+ test!(
147
+ Default::default(),
148
+ |_| as_folder(TransformVisitor),
149
+ var_decl_page,
150
+ // Input codes
151
+ r#"
152
+ const config = definePageConfig({})
153
+ export default config
154
+ "#,
155
+ // Output codes after transformed with plugin
156
+ r#"
157
+ function definePageConfig(config) {return config}
158
+ const config = definePageConfig({})
159
+ export default config
160
+ "#
161
+ );
162
+
173
163
  test!(
174
164
  Default::default(),
175
165
  |_| as_folder(TransformVisitor),
176
- var_decl_page,
166
+ module_exports,
177
167
  // Input codes
178
168
  r#"
179
- const config = definePageConfig({})
180
- export default config
169
+ var require_index_config = __commonJS({
170
+ "src/pages/index/index.config.ts": function(exports1, module) {
171
+ var config = definePageConfig({
172
+ navigationBarTitleText: "首页",
173
+ usingComponents: {}
174
+ });
175
+ module.exports = config;
176
+ }
177
+ });
178
+ var _default = require_index_config();
181
179
  "#,
182
180
  // Output codes after transformed with plugin
183
181
  r#"
184
182
  function definePageConfig(config) {return config}
185
- const config = definePageConfig({})
186
- export default config
183
+ var require_index_config = __commonJS({
184
+ "src/pages/index/index.config.ts": function(exports1, module) {
185
+ var config = definePageConfig({
186
+ navigationBarTitleText: "首页",
187
+ usingComponents: {}
188
+ });
189
+ module.exports = config;
190
+ }
191
+ });
192
+ var _default = require_index_config();
187
193
  "#
188
194
  );