@@ -6,24 +6,61 @@ use anyhow::Error;
6
6
#[ doc( hidden) ]
7
7
pub use js_sys;
8
8
use once_cell:: sync:: Lazy ;
9
- use swc:: { config:: ErrorFormat , Compiler } ;
9
+ #[ doc( hidden) ]
10
+ pub use serde_wasm_bindgen;
11
+ use serde_wasm_bindgen:: Serializer ;
12
+ use swc:: { config:: ErrorFormat , Compiler , HandlerOpts } ;
10
13
#[ doc( hidden) ]
11
14
pub use swc:: {
12
15
config:: { Options , ParseOptions , SourceMapsConfig } ,
13
16
try_with_handler,
14
17
} ;
15
18
#[ doc( hidden) ]
16
- pub use swc_common:: { comments, FileName } ;
17
- use swc_common:: { FilePathMapping , SourceMap } ;
19
+ pub use swc_common:: {
20
+ comments:: { self , SingleThreadedComments } ,
21
+ errors:: Handler ,
22
+ FileName , Mark , GLOBALS ,
23
+ } ;
24
+ use swc_common:: { sync:: Lrc , FilePathMapping , SourceMap } ;
18
25
#[ doc( hidden) ]
19
26
pub use swc_ecma_ast:: { EsVersion , Program } ;
20
27
#[ doc( hidden) ]
21
- pub use swc_ecma_transforms:: pass:: noop;
28
+ pub use swc_ecma_transforms:: { pass:: noop, resolver} ;
29
+ #[ doc( hidden) ]
30
+ pub use swc_ecma_visit:: VisitMutWith ;
22
31
#[ doc( hidden) ]
23
32
pub use wasm_bindgen:: { JsCast , JsValue } ;
24
33
#[ doc( hidden) ]
25
34
pub use wasm_bindgen_futures:: future_to_promise;
26
35
36
+ // A serializer with options to provide backward compat for the input / output
37
+ // from the bindgen generated swc interfaces.
38
+ #[ doc( hidden) ]
39
+ pub fn compat_serializer ( ) -> Arc < Serializer > {
40
+ static V : Lazy < Arc < Serializer > > = Lazy :: new ( || {
41
+ let s = Serializer :: new ( )
42
+ . serialize_maps_as_objects ( true )
43
+ . serialize_missing_as_null ( true ) ;
44
+ Arc :: new ( s)
45
+ } ) ;
46
+
47
+ V . clone ( )
48
+ }
49
+
50
+ #[ doc( hidden) ]
51
+ pub fn try_with_handler_globals < F , Ret > (
52
+ cm : Lrc < SourceMap > ,
53
+ config : HandlerOpts ,
54
+ op : F ,
55
+ ) -> Result < Ret , Error >
56
+ where
57
+ F : FnOnce ( & Handler ) -> Result < Ret , Error > ,
58
+ {
59
+ GLOBALS . set ( & Default :: default ( ) , || {
60
+ swc:: try_with_handler ( cm, config, op)
61
+ } )
62
+ }
63
+
27
64
/// Get global sourcemap
28
65
pub fn compiler ( ) -> Arc < Compiler > {
29
66
console_error_panic_hook:: set_once ( ) ;
@@ -56,23 +93,28 @@ macro_rules! build_minify_sync {
56
93
( $( #[ $m: meta] ) * , $opt: expr) => {
57
94
$( #[ $m] ) *
58
95
pub fn minify_sync( s: $crate:: wasm:: js_sys:: JsString , opts: $crate:: wasm:: JsValue ) -> Result <$crate:: wasm:: JsValue , $crate:: wasm:: JsValue > {
96
+ use serde:: Serialize ;
97
+
59
98
let c = $crate:: wasm:: compiler( ) ;
60
99
61
- $crate:: wasm:: try_with_handler (
100
+ $crate:: wasm:: try_with_handler_globals (
62
101
c. cm. clone( ) ,
63
102
$opt,
64
103
|handler| {
65
104
c. run( || {
66
105
let opts = if opts. is_null( ) || opts. is_undefined( ) {
67
106
Default :: default ( )
68
107
} else {
69
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" ) ?
108
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts)
109
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
70
110
} ;
71
111
72
112
let fm = c. cm. new_source_file( $crate:: wasm:: FileName :: Anon , s. into( ) ) ;
73
113
let program = $crate:: wasm:: anyhow:: Context :: context( c. minify( fm, handler, & opts) , "failed to minify file" ) ?;
74
114
75
- $crate:: wasm:: anyhow:: Context :: context( $crate:: wasm:: JsValue :: from_serde( & program) , "failed to serialize json" )
115
+ program
116
+ . serialize( $crate:: wasm:: compat_serializer( ) . as_ref( ) )
117
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize program: {}" , e) )
76
118
} )
77
119
} ,
78
120
)
@@ -105,17 +147,21 @@ macro_rules! build_parse_sync {
105
147
( $( #[ $m: meta] ) * , $opt: expr) => {
106
148
$( #[ $m] ) *
107
149
pub fn parse_sync( s: $crate:: wasm:: js_sys:: JsString , opts: $crate:: wasm:: JsValue ) -> Result <$crate:: wasm:: JsValue , $crate:: wasm:: JsValue > {
150
+ use serde:: Serialize ;
151
+ use $crate:: wasm:: VisitMutWith ;
152
+
108
153
let c = $crate:: wasm:: compiler( ) ;
109
154
110
- $crate:: wasm:: try_with_handler (
155
+ $crate:: wasm:: try_with_handler_globals (
111
156
c. cm. clone( ) ,
112
157
$opt,
113
158
|handler| {
114
159
c. run( || {
115
160
let opts: $crate:: wasm:: ParseOptions = if opts. is_null( ) || opts. is_undefined( ) {
116
161
Default :: default ( )
117
162
} else {
118
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" ) ?
163
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts)
164
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
119
165
} ;
120
166
121
167
let fm = c. cm. new_source_file( $crate:: wasm:: FileName :: Anon , s. into( ) ) ;
@@ -127,9 +173,8 @@ macro_rules! build_parse_sync {
127
173
None
128
174
} ;
129
175
130
- let program = $crate:: wasm:: anyhow:: Context :: context(
131
- c
132
- . parse_js(
176
+ let mut program = $crate:: wasm:: anyhow:: Context :: context(
177
+ c. parse_js(
133
178
fm,
134
179
handler,
135
180
opts. target,
@@ -140,7 +185,15 @@ macro_rules! build_parse_sync {
140
185
"failed to parse code"
141
186
) ?;
142
187
143
- $crate:: wasm:: anyhow:: Context :: context( $crate:: wasm:: JsValue :: from_serde( & program) , "failed to serialize json" )
188
+ program. visit_mut_with( & mut $crate:: wasm:: resolver(
189
+ $crate:: wasm:: Mark :: new( ) ,
190
+ $crate:: wasm:: Mark :: new( ) ,
191
+ opts. syntax. typescript( ) ,
192
+ ) ) ;
193
+
194
+ program
195
+ . serialize( $crate:: wasm:: compat_serializer( ) . as_ref( ) )
196
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize program: {}" , e) )
144
197
} )
145
198
} ,
146
199
)
@@ -175,18 +228,20 @@ macro_rules! build_print_sync {
175
228
pub fn print_sync( s: $crate:: wasm:: JsValue , opts: $crate:: wasm:: JsValue ) -> Result <$crate:: wasm:: JsValue , $crate:: wasm:: JsValue > {
176
229
let c = $crate:: wasm:: compiler( ) ;
177
230
178
- $crate:: wasm:: try_with_handler (
231
+ $crate:: wasm:: try_with_handler_globals (
179
232
c. cm. clone( ) ,
180
233
$opt,
181
234
|_handler| {
182
235
c. run( || {
183
236
let opts: $crate:: wasm:: Options = if opts. is_null( ) || opts. is_undefined( ) {
184
237
Default :: default ( )
185
238
} else {
186
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" ) ?
239
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts)
240
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to parse options: {}" , e) ) ?
187
241
} ;
188
242
189
- let program: $crate:: wasm:: Program = $crate:: wasm:: anyhow:: Context :: context( s. into_serde( ) , "failed to deserialize program" ) ?;
243
+ let program: $crate:: wasm:: Program = $crate:: wasm:: serde_wasm_bindgen:: from_value( s)
244
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to deserialize program: {}" , e) ) ?;
190
245
let s = $crate:: wasm:: anyhow:: Context :: context( c
191
246
. print(
192
247
& program,
@@ -205,7 +260,8 @@ macro_rules! build_print_sync {
205
260
false ,
206
261
) , "failed to print code" ) ?;
207
262
208
- $crate:: wasm:: anyhow:: Context :: context( JsValue :: from_serde( & s) , "failed to serialize json" )
263
+ serde_wasm_bindgen:: to_value( & s)
264
+ . map_err( |e| anyhow:: anyhow!( "failed to serialize json: {}" , e) )
209
265
} )
210
266
} ,
211
267
)
@@ -234,7 +290,7 @@ macro_rules! build_print {
234
290
#[ macro_export]
235
291
macro_rules! build_transform_sync {
236
292
( $( #[ $m: meta] ) * ) => {
237
- build_transform_sync!( $( #[ $m] ) * , |_, _ | $crate:: wasm:: noop( ) , |_ , _| $crate:: wasm:: noop( ) , Default :: default ( ) ) ;
293
+ build_transform_sync!( $( #[ $m] ) * , |_| $crate:: wasm:: noop( ) , |_| $crate:: wasm:: noop( ) , Default :: default ( ) ) ;
238
294
} ;
239
295
( $( #[ $m: meta] ) * , $before_pass: expr, $after_pass: expr) => {
240
296
build_transform_sync!( $( #[ $m] ) * , $before_pass, $after_pass, Default :: default ( ) ) ;
@@ -247,6 +303,8 @@ macro_rules! build_transform_sync {
247
303
opts: $crate:: wasm:: JsValue ,
248
304
experimental_plugin_bytes_resolver: $crate:: wasm:: JsValue ,
249
305
) -> Result <$crate:: wasm:: JsValue , $crate:: wasm:: JsValue > {
306
+ use serde:: Serialize ;
307
+
250
308
let c = $crate:: wasm:: compiler( ) ;
251
309
252
310
#[ cfg( feature = "plugin" ) ]
@@ -281,9 +339,7 @@ macro_rules! build_transform_sync {
281
339
buffer
282
340
} ;
283
341
284
- let bytes: Vec <u8 > = data
285
- . into_serde( )
286
- . expect( "Could not read byte from plugin resolver" ) ;
342
+ let bytes: Vec <u8 > = $crate:: wasm:: serde_wasm_bindgen:: from_value( data) . expect( "Could not read byte from plugin resolver" ) ;
287
343
288
344
// In here we 'inject' externally loaded bytes into the cache, so
289
345
// remaining plugin_runner execution path works as much as
@@ -296,13 +352,11 @@ macro_rules! build_transform_sync {
296
352
let opts: $crate:: wasm:: Options = if opts. is_null( ) || opts. is_undefined( ) {
297
353
Default :: default ( )
298
354
} else {
299
- $crate:: wasm:: anyhow:: Context :: context( opts. into_serde( ) , "failed to parse options" )
300
- . map_err( |e| $crate:: wasm:: convert_err( e, None ) ) ?
355
+ $crate:: wasm:: serde_wasm_bindgen:: from_value( opts) ?
301
356
} ;
302
357
303
358
let error_format = opts. experimental. error_format. unwrap_or_default( ) ;
304
-
305
- $crate:: wasm:: try_with_handler(
359
+ $crate:: wasm:: try_with_handler_globals(
306
360
c. cm. clone( ) ,
307
361
$opt,
308
362
|handler| {
@@ -320,24 +374,25 @@ macro_rules! build_transform_sync {
320
374
) ;
321
375
let cm = c. cm. clone( ) ;
322
376
let file = fm. clone( ) ;
323
-
377
+ let comments = $crate :: wasm :: SingleThreadedComments :: default ( ) ;
324
378
$crate:: wasm:: anyhow:: Context :: context(
325
379
c. process_js_with_custom_pass(
326
380
fm,
327
381
None ,
328
382
handler,
329
383
& opts,
330
- Default :: default ( ) ,
384
+ comments ,
331
385
$before_pass,
332
386
$after_pass,
333
387
) , "failed to process js file"
334
388
) ?
335
389
}
336
- Err ( v) => unsafe { c. process_js( handler, v . into_serde ( ) . expect( "" ) , & opts) ? } ,
390
+ Err ( v) => unsafe { c. process_js( handler, $crate :: wasm :: serde_wasm_bindgen :: from_value ( v ) . expect( "" ) , & opts) ? } ,
337
391
} ;
338
392
339
- $crate:: wasm:: anyhow:: Context :: context( $crate:: wasm:: JsValue :: from_serde( & out) ,
340
- "failed to serialize json" )
393
+ out
394
+ . serialize( $crate:: wasm:: compat_serializer( ) . as_ref( ) )
395
+ . map_err( |e| $crate:: wasm:: anyhow:: anyhow!( "failed to serialize transform result: {}" , e) )
341
396
} )
342
397
} ,
343
398
)
1 commit comments
github-actions[bot] commentedon Nov 18, 2022
Benchmark
es/full/bugs-1
333261
ns/iter (± 24615
)366800
ns/iter (± 31418
)0.91
es/full/minify/libraries/antd
1807198979
ns/iter (± 32721704
)2055494042
ns/iter (± 78757627
)0.88
es/full/minify/libraries/d3
419103916
ns/iter (± 12419303
)477783305
ns/iter (± 18919141
)0.88
es/full/minify/libraries/echarts
1566847566
ns/iter (± 49474633
)1779525769
ns/iter (± 46840644
)0.88
es/full/minify/libraries/jquery
99481084
ns/iter (± 2778702
)118346314
ns/iter (± 7646826
)0.84
es/full/minify/libraries/lodash
127093735
ns/iter (± 10497244
)137623358
ns/iter (± 6206468
)0.92
es/full/minify/libraries/moment
66319902
ns/iter (± 6021505
)69789977
ns/iter (± 1720809
)0.95
es/full/minify/libraries/react
22117838
ns/iter (± 1405033
)23433969
ns/iter (± 1581414
)0.94
es/full/minify/libraries/terser
325721497
ns/iter (± 6132262
)372333562
ns/iter (± 12267596
)0.87
es/full/minify/libraries/three
585693790
ns/iter (± 5794116
)649739339
ns/iter (± 19737265
)0.90
es/full/minify/libraries/typescript
3471689839
ns/iter (± 78001210
)3799607078
ns/iter (± 61154067
)0.91
es/full/minify/libraries/victory
860902587
ns/iter (± 21743934
)934895778
ns/iter (± 30254734
)0.92
es/full/minify/libraries/vue
168050579
ns/iter (± 11482018
)182105144
ns/iter (± 11566172
)0.92
es/full/codegen/es3
33755
ns/iter (± 937
)34522
ns/iter (± 3486
)0.98
es/full/codegen/es5
33929
ns/iter (± 604
)34487
ns/iter (± 5155
)0.98
es/full/codegen/es2015
34413
ns/iter (± 6277
)35078
ns/iter (± 3853
)0.98
es/full/codegen/es2016
33957
ns/iter (± 979
)34826
ns/iter (± 2698
)0.98
es/full/codegen/es2017
34391
ns/iter (± 1993
)34187
ns/iter (± 2043
)1.01
es/full/codegen/es2018
34562
ns/iter (± 13329
)34358
ns/iter (± 2770
)1.01
es/full/codegen/es2019
35699
ns/iter (± 5306
)34475
ns/iter (± 3493
)1.04
es/full/codegen/es2020
34602
ns/iter (± 4503
)35314
ns/iter (± 3217
)0.98
es/full/all/es3
219672312
ns/iter (± 33052956
)223676981
ns/iter (± 15577815
)0.98
es/full/all/es5
183535112
ns/iter (± 14564518
)205150591
ns/iter (± 12701799
)0.89
es/full/all/es2015
146592478
ns/iter (± 12870827
)159631999
ns/iter (± 10881445
)0.92
es/full/all/es2016
146346454
ns/iter (± 12561533
)161724721
ns/iter (± 11560641
)0.90
es/full/all/es2017
147970126
ns/iter (± 15861961
)159447439
ns/iter (± 14104711
)0.93
es/full/all/es2018
146178569
ns/iter (± 11174568
)159974078
ns/iter (± 12840273
)0.91
es/full/all/es2019
144069288
ns/iter (± 12461896
)159551306
ns/iter (± 13664315
)0.90
es/full/all/es2020
139428674
ns/iter (± 8948386
)154461090
ns/iter (± 10076296
)0.90
es/full/parser
725785
ns/iter (± 32380
)772838
ns/iter (± 66860
)0.94
es/full/base/fixer
26742
ns/iter (± 897
)28855
ns/iter (± 3848
)0.93
es/full/base/resolver_and_hygiene
92772
ns/iter (± 2698
)99021
ns/iter (± 16403
)0.94
serialization of ast node
219
ns/iter (± 4
)220
ns/iter (± 26
)1.00
serialization of serde
222
ns/iter (± 7
)251
ns/iter (± 36
)0.88
This comment was automatically generated by workflow using github-action-benchmark.