@@ -6,6 +6,8 @@ use anyhow::Error;
6
6
#[ doc( hidden) ]
7
7
pub use js_sys;
8
8
use once_cell:: sync:: Lazy ;
9
+ use serde:: Serialize ;
10
+ use serde_wasm_bindgen:: Serializer ;
9
11
use swc:: { config:: ErrorFormat , Compiler } ;
10
12
#[ doc( hidden) ]
11
13
pub use swc:: {
@@ -24,6 +26,12 @@ pub use wasm_bindgen::{JsCast, JsValue};
24
26
#[ doc( hidden) ]
25
27
pub use wasm_bindgen_futures:: future_to_promise;
26
28
29
+ // A serializer with options to provide backward compat for the input / output
30
+ // from the bindgen generated swc interfaces.
31
+ const COMPAT_SERIALIZER : Serializer = Serializer :: new ( )
32
+ . serialize_maps_as_objects ( true )
33
+ . serialize_missing_as_null ( true ) ;
34
+
27
35
/// Get global sourcemap
28
36
pub fn compiler ( ) -> Arc < Compiler > {
29
37
console_error_panic_hook:: set_once ( ) ;
@@ -66,13 +74,16 @@ macro_rules! build_minify_sync {
66
74
let opts = if opts. is_null( ) || opts. is_undefined( ) {
67
75
Default :: default ( )
68
76
} else {
69
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" ) ?
77
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts)
78
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
70
79
} ;
71
80
72
81
let fm = c. cm. new_source_file( $crate:: wasm:: FileName :: Anon , s. into( ) ) ;
73
82
let program = $crate:: wasm:: anyhow:: Context :: context( c. minify( fm, handler, & opts) , "failed to minify file" ) ?;
74
83
75
- $crate:: wasm:: anyhow:: Context :: context( $crate:: wasm:: JsValue :: from_serde( & program) , "failed to serialize json" )
84
+ program
85
+ . serialize( & COMPAT_SERIALIZER )
86
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize program: {}" , e) )
76
87
} )
77
88
} ,
78
89
)
@@ -115,7 +126,8 @@ macro_rules! build_parse_sync {
115
126
let opts: $crate:: wasm:: ParseOptions = if opts. is_null( ) || opts. is_undefined( ) {
116
127
Default :: default ( )
117
128
} else {
118
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" ) ?
129
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts)
130
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
119
131
} ;
120
132
121
133
let fm = c. cm. new_source_file( $crate:: wasm:: FileName :: Anon , s. into( ) ) ;
@@ -140,7 +152,9 @@ macro_rules! build_parse_sync {
140
152
"failed to parse code"
141
153
) ?;
142
154
143
- $crate:: wasm:: anyhow:: Context :: context( $crate:: wasm:: JsValue :: from_serde( & program) , "failed to serialize json" )
155
+ program
156
+ . serialize( & COMPAT_SERIALIZER )
157
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize program: {}" , e) )
144
158
} )
145
159
} ,
146
160
)
@@ -183,10 +197,12 @@ macro_rules! build_print_sync {
183
197
let opts: $crate:: wasm:: Options = if opts. is_null( ) || opts. is_undefined( ) {
184
198
Default :: default ( )
185
199
} else {
186
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" ) ?
200
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts)
201
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
187
202
} ;
188
203
189
- let program: $crate:: wasm:: Program = $crate:: wasm:: anyhow:: Context :: context( s. into_serde( ) , "failed to deserialize program" ) ?;
204
+ let program: $crate:: wasm:: Program = $crate:: wasm:: serde_wasm_bindgen:: from_value( s)
205
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to deserialize program: {}" , e) ) ?;
190
206
let s = $crate:: wasm:: anyhow:: Context :: context( c
191
207
. print(
192
208
& program,
@@ -205,7 +221,9 @@ macro_rules! build_print_sync {
205
221
false ,
206
222
) , "failed to print code" ) ?;
207
223
208
- $crate:: wasm:: anyhow:: Context :: context( JsValue :: from_serde( & s) , "failed to serialize json" )
224
+ program
225
+ . serialize( & COMPAT_SERIALIZER )
226
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize program: {}" , e) )
209
227
} )
210
228
} ,
211
229
)
@@ -281,9 +299,7 @@ macro_rules! build_transform_sync {
281
299
buffer
282
300
} ;
283
301
284
- let bytes: Vec <u8 > = data
285
- . into_serde( )
286
- . expect( "Could not read byte from plugin resolver" ) ;
302
+ let bytes: Vec <u8 > = $crate:: wasm:: serde_wasm_bindgen:: from_value( data) . expect( "Could not read byte from plugin resolver" ) ;
287
303
288
304
// In here we 'inject' externally loaded bytes into the cache, so
289
305
// remaining plugin_runner execution path works as much as
@@ -296,8 +312,7 @@ macro_rules! build_transform_sync {
296
312
let opts: $crate:: wasm:: Options = if opts. is_null( ) || opts. is_undefined( ) {
297
313
Default :: default ( )
298
314
} else {
299
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" )
300
- . map_err( |e| $crate:: wasm:: convert_err( e, None ) ) ?
315
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts) ?
301
316
} ;
302
317
303
318
let error_format = opts. experimental. error_format. unwrap_or_default( ) ;
@@ -333,11 +348,12 @@ macro_rules! build_transform_sync {
333
348
) , "failed to process js file"
334
349
) ?
335
350
}
336
- Err ( v) => unsafe { c. process_js( handler, v . into_serde ( ) . expect( "" ) , & opts) ? } ,
351
+ Err ( v) => unsafe { c. process_js( handler, $crate :: wasm :: serde_wasm_bindgen :: from_value ( v ) . expect( "" ) , & opts) ? } ,
337
352
} ;
338
353
339
- $crate:: wasm:: anyhow:: Context :: context( $crate:: wasm:: JsValue :: from_serde( & out) ,
340
- "failed to serialize json" )
354
+ out
355
+ . serialize( & COMPAT_SERIALIZER )
356
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize transform result: {}" , e) )
341
357
} )
342
358
} ,
343
359
)
1 commit comments
github-actions[bot] commentedon Nov 15, 2022
Benchmark
es/full/bugs-1
359764
ns/iter (± 35638
)347052
ns/iter (± 20512
)1.04
es/full/minify/libraries/antd
1859260645
ns/iter (± 50064656
)1923725849
ns/iter (± 40306160
)0.97
es/full/minify/libraries/d3
407449725
ns/iter (± 22362328
)439671932
ns/iter (± 31729477
)0.93
es/full/minify/libraries/echarts
1612858764
ns/iter (± 52492529
)1643646581
ns/iter (± 58778615
)0.98
es/full/minify/libraries/jquery
100909663
ns/iter (± 11908356
)114105698
ns/iter (± 7923322
)0.88
es/full/minify/libraries/lodash
134324510
ns/iter (± 10291658
)133984446
ns/iter (± 21300946
)1.00
es/full/minify/libraries/moment
67175152
ns/iter (± 8010933
)60560367
ns/iter (± 3894269
)1.11
es/full/minify/libraries/react
21767308
ns/iter (± 633949
)20593417
ns/iter (± 2786835
)1.06
es/full/minify/libraries/terser
318945153
ns/iter (± 12126350
)345732662
ns/iter (± 19628473
)0.92
es/full/minify/libraries/three
575525366
ns/iter (± 33154379
)582607608
ns/iter (± 29822859
)0.99
es/full/minify/libraries/typescript
3525404089
ns/iter (± 93756360
)3587011200
ns/iter (± 259198665
)0.98
es/full/minify/libraries/victory
854009311
ns/iter (± 18508124
)945429374
ns/iter (± 75184631
)0.90
es/full/minify/libraries/vue
186769157
ns/iter (± 10795873
)179476890
ns/iter (± 14293493
)1.04
es/full/codegen/es3
35987
ns/iter (± 2947
)35156
ns/iter (± 3929
)1.02
es/full/codegen/es5
35271
ns/iter (± 2919
)34958
ns/iter (± 2512
)1.01
es/full/codegen/es2015
35398
ns/iter (± 2191
)34488
ns/iter (± 1564
)1.03
es/full/codegen/es2016
35354
ns/iter (± 3173
)34127
ns/iter (± 1825
)1.04
es/full/codegen/es2017
35284
ns/iter (± 2622
)34442
ns/iter (± 3106
)1.02
es/full/codegen/es2018
35578
ns/iter (± 4212
)34504
ns/iter (± 4328
)1.03
es/full/codegen/es2019
35800
ns/iter (± 3202
)34753
ns/iter (± 3736
)1.03
es/full/codegen/es2020
35466
ns/iter (± 3811
)34691
ns/iter (± 5722
)1.02
es/full/all/es3
217580563
ns/iter (± 21596484
)232540296
ns/iter (± 30431077
)0.94
es/full/all/es5
185495295
ns/iter (± 15440090
)216932331
ns/iter (± 44851067
)0.86
es/full/all/es2015
162503849
ns/iter (± 17551828
)168650590
ns/iter (± 28210961
)0.96
es/full/all/es2016
155546701
ns/iter (± 21213081
)184085282
ns/iter (± 33544797
)0.84
es/full/all/es2017
165784208
ns/iter (± 18397331
)176378828
ns/iter (± 32859530
)0.94
es/full/all/es2018
160782944
ns/iter (± 18813240
)170432604
ns/iter (± 15447414
)0.94
es/full/all/es2019
149664700
ns/iter (± 11898224
)173107005
ns/iter (± 26194868
)0.86
es/full/all/es2020
147589823
ns/iter (± 12281102
)176772997
ns/iter (± 24962885
)0.83
es/full/parser
752944
ns/iter (± 74353
)793736
ns/iter (± 112062
)0.95
es/full/base/fixer
27952
ns/iter (± 3623
)30184
ns/iter (± 5813
)0.93
es/full/base/resolver_and_hygiene
95506
ns/iter (± 8627
)95874
ns/iter (± 19630
)1.00
serialization of ast node
215
ns/iter (± 3
)221
ns/iter (± 20
)0.97
serialization of serde
228
ns/iter (± 24
)235
ns/iter (± 28
)0.97
This comment was automatically generated by workflow using github-action-benchmark.