1
1
use anyhow:: Error ;
2
+ use serde:: Serialize ;
3
+ use serde_wasm_bindgen:: Serializer ;
2
4
use swc_core:: {
3
5
base:: HandlerOpts ,
4
6
binding_macros:: wasm:: {
@@ -21,6 +23,12 @@ use swc_core::{
21
23
use wasm_bindgen:: { prelude:: * , JsCast } ;
22
24
mod types;
23
25
26
+ // A serializer with options to provide backward compat for the input / output
27
+ // from the bindgen generated swc interfaces.
28
+ const COMPAT_SERIALIZER : Serializer = Serializer :: new ( )
29
+ . serialize_maps_as_objects ( true )
30
+ . serialize_missing_as_null ( true ) ;
31
+
24
32
/// Custom interface definitions for the @swc/wasm's public interface instead of
25
33
/// auto generated one, which is not reflecting most of types in detail.
26
34
#[ wasm_bindgen( typescript_custom_section) ]
@@ -81,12 +89,16 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
81
89
let opts = if opts. is_null ( ) || opts. is_undefined ( ) {
82
90
Default :: default ( )
83
91
} else {
84
- anyhow:: Context :: context ( opts. into_serde ( ) , "failed to parse options" ) ?
92
+ serde_wasm_bindgen:: from_value ( opts)
93
+ . map_err ( |e| anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
85
94
} ;
86
95
let fm = c. cm . new_source_file ( FileName :: Anon , s. into ( ) ) ;
87
96
let program =
88
97
anyhow:: Context :: context ( c. minify ( fm, handler, & opts) , "failed to minify file" ) ?;
89
- anyhow:: Context :: context ( JsValue :: from_serde ( & program) , "failed to serialize json" )
98
+
99
+ program
100
+ . serialize ( & COMPAT_SERIALIZER )
101
+ . map_err ( |e| anyhow:: anyhow!( "failed to serialize program: {}" , e) )
90
102
} )
91
103
} )
92
104
. map_err ( |e| convert_err ( e, None ) )
@@ -106,7 +118,8 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
106
118
let opts: ParseOptions = if opts. is_null ( ) || opts. is_undefined ( ) {
107
119
Default :: default ( )
108
120
} else {
109
- anyhow:: Context :: context ( opts. into_serde ( ) , "failed to parse options" ) ?
121
+ serde_wasm_bindgen:: from_value ( opts)
122
+ . map_err ( |e| anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
110
123
} ;
111
124
let fm = c. cm . new_source_file ( FileName :: Anon , s. into ( ) ) ;
112
125
let cmts = c. comments ( ) . clone ( ) ;
@@ -133,7 +146,9 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
133
146
opts. syntax . typescript ( ) ,
134
147
) ) ;
135
148
136
- anyhow:: Context :: context ( JsValue :: from_serde ( & program) , "failed to serialize json" )
149
+ program
150
+ . serialize ( & COMPAT_SERIALIZER )
151
+ . map_err ( |e| anyhow:: anyhow!( "failed to serialize program: {}" , e) )
137
152
} )
138
153
} )
139
154
} )
@@ -160,9 +175,9 @@ pub fn transform_sync(
160
175
let opts: Options = if opts. is_null ( ) || opts. is_undefined ( ) {
161
176
Default :: default ( )
162
177
} else {
163
- anyhow:: Context :: context ( opts. into_serde ( ) , "failed to parse options" )
164
- . map_err ( |e| convert_err ( e, None ) ) ?
178
+ serde_wasm_bindgen:: from_value ( opts) ?
165
179
} ;
180
+
166
181
let error_format = opts. experimental . error_format . unwrap_or_default ( ) ;
167
182
try_with_handler ( c. cm . clone ( ) , Default :: default ( ) , |handler| {
168
183
c. run ( || {
@@ -193,9 +208,13 @@ pub fn transform_sync(
193
208
"failed to process js file" ,
194
209
) ?
195
210
}
196
- Err ( v) => unsafe { c. process_js ( handler, v. into_serde ( ) . expect ( "" ) , & opts) ? } ,
211
+ Err ( v) => {
212
+ c. process_js ( handler, serde_wasm_bindgen:: from_value ( v) . expect ( "Should able to deserialize into program" ) , & opts) ?
213
+ }
197
214
} ;
198
- anyhow:: Context :: context ( JsValue :: from_serde ( & out) , "failed to serialize json" )
215
+
216
+ out. serialize ( & COMPAT_SERIALIZER )
217
+ . map_err ( |e| anyhow:: anyhow!( "failed to serialize transform result: {}" , e) )
199
218
} )
200
219
} )
201
220
. map_err ( |e| convert_err ( e, Some ( error_format) ) )
@@ -218,10 +237,13 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
218
237
let opts: Options = if opts. is_null ( ) || opts. is_undefined ( ) {
219
238
Default :: default ( )
220
239
} else {
221
- anyhow:: Context :: context ( opts. into_serde ( ) , "failed to parse options" ) ?
240
+ serde_wasm_bindgen:: from_value ( opts)
241
+ . map_err ( |e| anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
222
242
} ;
223
- let program: Program =
224
- anyhow:: Context :: context ( s. into_serde ( ) , "failed to deserialize program" ) ?;
243
+
244
+ let program: Program = serde_wasm_bindgen:: from_value ( s)
245
+ . map_err ( |e| anyhow:: anyhow!( "failed to deserialize program: {}" , e) ) ?;
246
+
225
247
let s = anyhow:: Context :: context (
226
248
c. print (
227
249
& program,
@@ -241,7 +263,9 @@ pub fn print_sync(s: JsValue, opts: JsValue) -> Result<JsValue, JsValue> {
241
263
) ,
242
264
"failed to print code" ,
243
265
) ?;
244
- anyhow:: Context :: context ( JsValue :: from_serde ( & s) , "failed to serialize json" )
266
+
267
+ serde_wasm_bindgen:: to_value ( & s)
268
+ . map_err ( |e| anyhow:: anyhow!( "failed to serialize json: {}" , e) )
245
269
} )
246
270
} )
247
271
. map_err ( |e| convert_err ( e, None ) )
0 commit comments