diff --git a/bindings/binding_core_wasm/src/lib.rs b/bindings/binding_core_wasm/src/lib.rs index 0ec735ff5a95..0ecf9f981e46 100644 --- a/bindings/binding_core_wasm/src/lib.rs +++ b/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::{ @@ -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)] @@ -88,8 +96,9 @@ pub fn minify_sync(s: JsString, opts: JsValue) -> Result { 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)) @@ -137,8 +146,9 @@ pub fn parse_sync(s: JsString, opts: JsValue) -> Result { 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)) }) }) }) @@ -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))) diff --git a/crates/binding_macros/src/wasm.rs b/crates/binding_macros/src/wasm.rs index e68fd608850e..f857b01655a7 100644 --- a/crates/binding_macros/src/wasm.rs +++ b/crates/binding_macros/src/wasm.rs @@ -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::{ @@ -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 { console_error_panic_hook::set_once(); @@ -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)) }) }, ) @@ -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)) }) }, ) @@ -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)) }) }, ) @@ -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)) }) }, )