@tarojs/helper 3.6.22-alpha.4 → 3.6.22-nightly.0

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.
@@ -0,0 +1,18 @@
1
+ [package]
2
+ name = "swc-plugin-define-config"
3
+ version = "0.2.0"
4
+ edition = "2021"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib"]
8
+
9
+ [profile.release]
10
+ lto = true
11
+
12
+ [dependencies]
13
+ serde = "1"
14
+ swc_core = { version = "0.86.*", features = ["ecma_plugin_transform", "__utils"] }
15
+
16
+ # .cargo/config defines few alias to build plugin.
17
+ # cargo build-wasi generates wasm-wasi32 binary
18
+ # cargo build-wasm32 generates wasm32-unknown-unknown binary.
@@ -0,0 +1,141 @@
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},
10
+ };
11
+
12
+ struct DefineConfigVisitor {
13
+ fn_name: Option<Ident>
14
+ }
15
+
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
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+ pub struct TransformVisitor;
30
+
31
+ impl VisitMut for TransformVisitor {
32
+ fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
33
+ let mut folder = DefineConfigVisitor { fn_name: None };
34
+
35
+ let is_found = items.iter_mut().any(|item| {
36
+ item.visit_mut_with(&mut folder);
37
+ folder.fn_name.is_some()
38
+ });
39
+
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)))));
63
+ }
64
+ }
65
+ }
66
+
67
+ /// An example plugin function with macro support.
68
+ /// `plugin_transform` macro interop pointers into deserialized structs, as well
69
+ /// as returning ptr back to host.
70
+ ///
71
+ /// It is possible to opt out from macro by writing transform fn manually
72
+ /// if plugin need to handle low-level ptr directly via
73
+ /// `__transform_plugin_process_impl(
74
+ /// ast_ptr: *const u8, ast_ptr_len: i32,
75
+ /// unresolved_mark: u32, should_enable_comments_proxy: i32) ->
76
+ /// i32 /* 0 for success, fail otherwise.
77
+ /// Note this is only for internal pointer interop result,
78
+ /// not actual transform result */`
79
+ ///
80
+ /// This requires manual handling of serialization / deserialization from ptrs.
81
+ /// Refer swc_plugin_macro to see how does it work internally.
82
+ #[plugin_transform]
83
+ pub fn process_transform(program: Program, _metadata: TransformPluginProgramMetadata) -> Program {
84
+ program.fold_with(&mut as_folder(TransformVisitor))
85
+ }
86
+
87
+ // An example to test plugin transform.
88
+ // Recommended strategy to test plugin's transform is verify
89
+ // the Visitor's behavior, instead of trying to run `process_transform` with mocks
90
+ // unless explicitly required to do so.
91
+ test!(
92
+ Default::default(),
93
+ |_| as_folder(TransformVisitor),
94
+ module_decl_default_app,
95
+ r#"export default defineAppConfig({})"#
96
+ );
97
+
98
+ test!(
99
+ Default::default(),
100
+ |_| as_folder(TransformVisitor),
101
+ module_decl_default_page,
102
+ r#"export default definePageConfig({})"#
103
+ );
104
+
105
+ test!(
106
+ Default::default(),
107
+ |_| as_folder(TransformVisitor),
108
+ var_decl_app,
109
+ r#"
110
+ const config = defineAppConfig({})
111
+ export default config
112
+ "#
113
+ );
114
+
115
+ test!(
116
+ Default::default(),
117
+ |_| as_folder(TransformVisitor),
118
+ var_decl_page,
119
+ r#"
120
+ const config = definePageConfig({})
121
+ export default config
122
+ "#
123
+ );
124
+
125
+ test!(
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
+ "#
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
package/swc/.gitkeep DELETED
File without changes
Binary file
Binary file