11use anyhow:: { Context , Error } ;
2+ use js_sys:: JsString ;
23use next_swc:: { custom_before_pass, TransformOptions } ;
34use once_cell:: sync:: Lazy ;
45use std:: sync:: Arc ;
56use swc:: { config:: JsMinifyOptions , config:: ParseOptions , try_with_handler, Compiler } ;
67use swc_common:: { comments:: Comments , errors:: ColorConfig , FileName , FilePathMapping , SourceMap } ;
78use swc_ecmascript:: transforms:: pass:: noop;
8- use wasm_bindgen:: prelude:: * ;
9+ use wasm_bindgen:: { prelude:: * , JsCast } ;
10+ use wasm_bindgen_futures:: future_to_promise;
911
1012fn convert_err ( err : Error ) -> JsValue {
1113 format ! ( "{:?}" , err) . into ( )
1214}
1315
1416#[ wasm_bindgen( js_name = "minifySync" ) ]
15- pub fn minify_sync ( s : & str , opts : JsValue ) -> Result < JsValue , JsValue > {
17+ pub fn minify_sync ( s : JsString , opts : JsValue ) -> Result < JsValue , JsValue > {
1618 console_error_panic_hook:: set_once ( ) ;
1719
1820 let c = compiler ( ) ;
@@ -37,8 +39,15 @@ pub fn minify_sync(s: &str, opts: JsValue) -> Result<JsValue, JsValue> {
3739 . map_err ( convert_err)
3840}
3941
42+ #[ wasm_bindgen( js_name = "minify" ) ]
43+ pub fn minify ( s : JsString , opts : JsValue ) -> js_sys:: Promise {
44+ // TODO: This'll be properly scheduled once wasm have standard backed thread
45+ // support.
46+ future_to_promise ( async { minify_sync ( s, opts) } )
47+ }
48+
4049#[ wasm_bindgen( js_name = "transformSync" ) ]
41- pub fn transform_sync ( s : & str , opts : JsValue ) -> Result < JsValue , JsValue > {
50+ pub fn transform_sync ( s : JsValue , opts : JsValue ) -> Result < JsValue , JsValue > {
4251 console_error_panic_hook:: set_once ( ) ;
4352
4453 let c = compiler ( ) ;
@@ -52,37 +61,55 @@ pub fn transform_sync(s: &str, opts: JsValue) -> Result<JsValue, JsValue> {
5261 |handler| {
5362 let opts: TransformOptions = opts. into_serde ( ) . context ( "failed to parse options" ) ?;
5463
55- let fm = c. cm . new_source_file (
56- if opts. swc . filename . is_empty ( ) {
57- FileName :: Anon
58- } else {
59- FileName :: Real ( opts. swc . filename . clone ( ) . into ( ) )
60- } ,
61- s. into ( ) ,
62- ) ;
63- let cm = c. cm . clone ( ) ;
64- let file = fm. clone ( ) ;
65- let out = c
66- . process_js_with_custom_pass (
67- fm,
68- None ,
69- handler,
70- & opts. swc ,
71- |_, comments| {
72- custom_before_pass ( cm, file, & opts, comments. clone ( ) , Default :: default ( ) )
73- } ,
74- |_, _| noop ( ) ,
75- )
76- . context ( "failed to process js file" ) ?;
64+ let s = s. dyn_into :: < js_sys:: JsString > ( ) ;
65+ let out = match s {
66+ Ok ( s) => {
67+ let fm = c. cm . new_source_file (
68+ if opts. swc . filename . is_empty ( ) {
69+ FileName :: Anon
70+ } else {
71+ FileName :: Real ( opts. swc . filename . clone ( ) . into ( ) )
72+ } ,
73+ s. into ( ) ,
74+ ) ;
75+ let cm = c. cm . clone ( ) ;
76+ let file = fm. clone ( ) ;
77+ c. process_js_with_custom_pass (
78+ fm,
79+ None ,
80+ handler,
81+ & opts. swc ,
82+ |_, comments| {
83+ custom_before_pass (
84+ cm,
85+ file,
86+ & opts,
87+ comments. clone ( ) ,
88+ Default :: default ( ) ,
89+ )
90+ } ,
91+ |_, _| noop ( ) ,
92+ )
93+ . context ( "failed to process js file" ) ?
94+ }
95+ Err ( v) => c. process_js ( handler, v. into_serde ( ) . expect ( "" ) , & opts. swc ) ?,
96+ } ;
7797
7898 JsValue :: from_serde ( & out) . context ( "failed to serialize json" )
7999 } ,
80100 )
81101 . map_err ( convert_err)
82102}
83103
104+ #[ wasm_bindgen( js_name = "transform" ) ]
105+ pub fn transform ( s : JsValue , opts : JsValue ) -> js_sys:: Promise {
106+ // TODO: This'll be properly scheduled once wasm have standard backed thread
107+ // support.
108+ future_to_promise ( async { transform_sync ( s, opts) } )
109+ }
110+
84111#[ wasm_bindgen( js_name = "parseSync" ) ]
85- pub fn parse_sync ( s : & str , opts : JsValue ) -> Result < JsValue , JsValue > {
112+ pub fn parse_sync ( s : JsString , opts : JsValue ) -> Result < JsValue , JsValue > {
86113 console_error_panic_hook:: set_once ( ) ;
87114
88115 let c = swc:: Compiler :: new ( Arc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ) ;
@@ -124,6 +151,13 @@ pub fn parse_sync(s: &str, opts: JsValue) -> Result<JsValue, JsValue> {
124151 . map_err ( convert_err)
125152}
126153
154+ #[ wasm_bindgen( js_name = "parse" ) ]
155+ pub fn parse ( s : JsString , opts : JsValue ) -> js_sys:: Promise {
156+ // TODO: This'll be properly scheduled once wasm have standard backed thread
157+ // support.
158+ future_to_promise ( async { parse_sync ( s, opts) } )
159+ }
160+
127161/// Get global sourcemap
128162fn compiler ( ) -> Arc < Compiler > {
129163 static C : Lazy < Arc < Compiler > > = Lazy :: new ( || {
0 commit comments