@tarojs/helper 3.6.9-alpha.8 → 3.6.10-alpha.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.
@@ -1,19 +1,27 @@
1
1
  [package]
2
2
  name = "swc-plugin-define-config"
3
- version = "0.2.0"
3
+ version = "0.1.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
10
12
  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"
11
19
 
12
20
  [dependencies]
13
21
  serde = "1"
14
- swc_core = { version = "0.79.*", features = ["ecma_plugin_transform"] }
15
- swc_ecma_utils = "0.120.1"
22
+ swc_core = { version = "0.40.*", features = ["plugin_transform"] }
16
23
 
17
24
  # .cargo/config defines few alias to build plugin.
18
25
  # cargo build-wasi generates wasm-wasi32 binary
19
26
  # cargo build-wasm32 generates wasm32-unknown-unknown binary.
27
+
@@ -1,75 +1,102 @@
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
- },
1
+ use swc_core::ecma::{
2
+ ast::*,
3
+ transforms::testing::test,
4
+ visit::{as_folder, FoldWith, VisitMut, VisitMutWith},
11
5
  };
12
- use swc_ecma_utils::{quote_ident, FunctionFactory};
6
+ use swc_core::common::{DUMMY_SP, SyntaxContext};
13
7
  use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
14
8
 
15
- pub struct TransformVisitor;
9
+ struct DefineConfig {
10
+ found: bool,
11
+ fn_name: String,
12
+ ctxt: SyntaxContext
13
+ }
16
14
 
17
- struct DefineFoler {
18
- name: Option<String>,
19
- ctxt: Option<SyntaxContext>,
15
+ impl DefineConfig {
16
+ fn new() -> Self {
17
+ Self {
18
+ found: false,
19
+ fn_name: String::from(""),
20
+ ctxt: SyntaxContext::empty()
21
+ }
22
+ }
20
23
  }
21
24
 
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
- }
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
+ }
32
37
  }
38
+ }
33
39
  }
34
40
  }
35
41
 
42
+ pub struct TransformVisitor;
43
+
36
44
  impl VisitMut for TransformVisitor {
37
- fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
38
- let mut folder = DefineFoler { name: None, ctxt: None };
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();
39
49
  let mut line_to_be_insert: usize = 0;
40
50
 
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
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
+ }
46
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
+ _ => ()
68
+ }
47
69
  }
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 {
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 {
51
78
  span: DUMMY_SP,
52
79
  decorators: Default::default(),
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)))));
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));
71
98
  }
72
- }
99
+ }
73
100
  }
74
101
 
75
102
  /// An example plugin function with macro support.
@@ -89,7 +116,7 @@ impl VisitMut for TransformVisitor {
89
116
  /// Refer swc_plugin_macro to see how does it work internally.
90
117
  #[plugin_transform]
91
118
  pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
92
- program.fold_with(&mut as_folder(TransformVisitor))
119
+ program.fold_with(&mut as_folder(TransformVisitor))
93
120
  }
94
121
 
95
122
  // An example to test plugin transform.
@@ -97,98 +124,65 @@ pub fn process_transform(program: Program, _metadata: TransformPluginProgramMeta
97
124
  // the Visitor's behavior, instead of trying to run `process_transform` with mocks
98
125
  // unless explicitly required to do so.
99
126
  test!(
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
- "#
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
+ "#
112
154
  );
113
155
 
114
156
  test!(
115
157
  Default::default(),
116
158
  |_| as_folder(TransformVisitor),
117
- module_decl_default_page,
159
+ var_decl_app,
118
160
  // Input codes
119
161
  r#"
120
- export default definePageConfig({})
162
+ const config = defineAppConfig({})
163
+ export default config
121
164
  "#,
122
165
  // Output codes after transformed with plugin
123
166
  r#"
124
- function definePageConfig(config) {return config}
125
- export default definePageConfig({})
167
+ function defineAppConfig(config) {return config}
168
+ const config = defineAppConfig({})
169
+ export default config
126
170
  "#
127
171
  );
128
172
 
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
-
163
173
  test!(
164
174
  Default::default(),
165
175
  |_| as_folder(TransformVisitor),
166
- module_exports,
176
+ var_decl_page,
167
177
  // Input codes
168
178
  r#"
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();
179
+ const config = definePageConfig({})
180
+ export default config
179
181
  "#,
180
182
  // Output codes after transformed with plugin
181
183
  r#"
182
184
  function definePageConfig(config) {return 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();
185
+ const config = definePageConfig({})
186
+ export default config
193
187
  "#
194
188
  );