Skip to content

Commit

Permalink
fix(binding/wasm): provides backward compat output
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Nov 14, 2022
1 parent c6423bf commit e301cf5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
26 changes: 18 additions & 8 deletions bindings/binding_core_wasm/src/lib.rs
@@ -1,4 +1,6 @@
use anyhow::Error;
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use swc_core::{
base::HandlerOpts,
binding_macros::wasm::{
Expand All @@ -21,6 +23,12 @@ use swc_core::{
use wasm_bindgen::{prelude::*, JsCast};
mod types;

// A serializer with options to provide backward compat for the input / output
// from the bindgen generated swc interfaces.
const COMPAT_SERIALIZER: Serializer = Serializer::new()
.serialize_maps_as_objects(true)
.serialize_missing_as_null(true);

/// Custom interface definitions for the @swc/wasm's public interface instead of
/// auto generated one, which is not reflecting most of types in detail.
#[wasm_bindgen(typescript_custom_section)]
Expand Down Expand Up @@ -88,8 +96,9 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
let program =
anyhow::Context::context(c.minify(fm, handler, &opts), "failed to minify file")?;

serde_wasm_bindgen::to_value(&program)
.map_err(|e| anyhow::anyhow!("failed to deserialize program: {}", e))
program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
})
})
.map_err(|e| convert_err(e, None))
Expand Down Expand Up @@ -137,8 +146,9 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result<JsValue, JsValue> {
opts.syntax.typescript(),
));

serde_wasm_bindgen::to_value(&program)
.map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| anyhow::anyhow!("failed to serialize program: {}", e))
})
})
})
Expand Down Expand Up @@ -199,12 +209,12 @@ pub fn transform_sync(
)?
}
Err(v) => {
c.process_js(handler, serde_wasm_bindgen::from_value(v).expect(""), &opts)?
},
c.process_js(handler, serde_wasm_bindgen::from_value(v).expect("Should able to deserialize into program"), &opts)?
}
};

serde_wasm_bindgen::to_value(&out)
.map_err(|e| anyhow::anyhow!("failed to serialize json: {}", e))
out.serialize(&COMPAT_SERIALIZER)
.map_err(|e| anyhow::anyhow!("failed to serialize transform result: {}", e))
})
})
.map_err(|e| convert_err(e, Some(error_format)))
Expand Down
28 changes: 20 additions & 8 deletions crates/binding_macros/src/wasm.rs
Expand Up @@ -6,6 +6,8 @@ use anyhow::Error;
#[doc(hidden)]
pub use js_sys;
use once_cell::sync::Lazy;
use serde::Serialize;
use serde_wasm_bindgen::Serializer;
use swc::{config::ErrorFormat, Compiler};
#[doc(hidden)]
pub use swc::{
Expand All @@ -24,6 +26,12 @@ pub use wasm_bindgen::{JsCast, JsValue};
#[doc(hidden)]
pub use wasm_bindgen_futures::future_to_promise;

// A serializer with options to provide backward compat for the input / output
// from the bindgen generated swc interfaces.
const COMPAT_SERIALIZER: Serializer = Serializer::new()
.serialize_maps_as_objects(true)
.serialize_missing_as_null(true);

/// Get global sourcemap
pub fn compiler() -> Arc<Compiler> {
console_error_panic_hook::set_once();
Expand Down Expand Up @@ -73,8 +81,9 @@ macro_rules! build_minify_sync {
let fm = c.cm.new_source_file($crate::wasm::FileName::Anon, s.into());
let program = $crate::wasm::anyhow::Context::context(c.minify(fm, handler, &opts), "failed to minify file")?;

$crate::wasm::serde_wasm_bindgen::to_value(&program)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to deserialize program: {}", e))
program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
})
},
)
Expand Down Expand Up @@ -143,8 +152,9 @@ macro_rules! build_parse_sync {
"failed to parse code"
)?;

$crate::wasm::serde_wasm_bindgen::to_value(&program)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to deserialize program: {}", e))
program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
})
},
)
Expand Down Expand Up @@ -211,8 +221,9 @@ macro_rules! build_print_sync {
false,
),"failed to print code")?;

$crate::wasm::serde_wasm_bindgen::to_value(&s)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize json: {}", e))
program
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize program: {}", e))
})
},
)
Expand Down Expand Up @@ -340,8 +351,9 @@ macro_rules! build_transform_sync {
Err(v) => unsafe { c.process_js(handler, $crate::wasm::serde_wasm_bindgen::from_value(v).expect(""), &opts)? },
};

$crate::wasm::serde_wasm_bindgen::to_value(&out)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize json: {}", e))
out
.serialize(&COMPAT_SERIALIZER)
.map_err(|e| $crate::wasm::anyhow::anyhow!("failed to serialize transform result: {}", e))
})
},
)
Expand Down

0 comments on commit e301cf5

Please sign in to comment.