From 9cf13fb6608aabb795dec2d0d93f146e9208a2c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 05:53:18 +0900 Subject: [PATCH 01/26] init interop crate --- crates/swc_interop_nodejs/Cargo.toml | 8 ++++++++ crates/swc_interop_nodejs/src/lib.rs | 1 + 2 files changed, 9 insertions(+) create mode 100644 crates/swc_interop_nodejs/Cargo.toml create mode 100644 crates/swc_interop_nodejs/src/lib.rs diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml new file mode 100644 index 000000000000..994a4a54367b --- /dev/null +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = { workspace = true } +license = { workspace = true } +name = "swc_interop_nodejs" +repository = { workspace = true } +version = "0.1.0" + +[dependencies] diff --git a/crates/swc_interop_nodejs/src/lib.rs b/crates/swc_interop_nodejs/src/lib.rs new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/crates/swc_interop_nodejs/src/lib.rs @@ -0,0 +1 @@ + From cd259d10b76a8bce02309e150711ef2aa83b4a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 05:54:24 +0900 Subject: [PATCH 02/26] fixup --- crates/swc_interop_nodejs/Cargo.toml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index 994a4a54367b..21fa4592124c 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -1,8 +1,12 @@ [package] -edition = { workspace = true } -license = { workspace = true } -name = "swc_interop_nodejs" -repository = { workspace = true } -version = "0.1.0" +authors = ["강동윤 "] +description = "General interop for Node.js" +documentation = "https://rustdoc.swc.rs/swc_interop_nodejs/" +edition = { workspace = true } +license = { workspace = true } +name = "swc_interop_nodejs" +repository = { workspace = true } +version = "0.1.0" + [dependencies] From 35a65ac7e8195bcee89a002c5f99cdc0dfc4515f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 05:59:29 +0900 Subject: [PATCH 03/26] pacakges --- crates/swc_interop_babel/Cargo.toml | 13 +++++++++++++ crates/swc_interop_babel/src/lib.rs | 1 + 2 files changed, 14 insertions(+) create mode 100644 crates/swc_interop_babel/Cargo.toml create mode 100644 crates/swc_interop_babel/src/lib.rs diff --git a/crates/swc_interop_babel/Cargo.toml b/crates/swc_interop_babel/Cargo.toml new file mode 100644 index 000000000000..aade5ac980da --- /dev/null +++ b/crates/swc_interop_babel/Cargo.toml @@ -0,0 +1,13 @@ +[package] +authors = ["강동윤 "] +description = "General interop for Babel" +documentation = "https://rustdoc.swc.rs/swc_interop_babel/" +edition = { workspace = true } +license = { workspace = true } +name = "swc_interop_babel" +repository = { workspace = true } +version = "0.1.0" + + +[dependencies] +swc_interop_nodejs = { version = "0.1.0", path = "../swc_interop_nodejs" } diff --git a/crates/swc_interop_babel/src/lib.rs b/crates/swc_interop_babel/src/lib.rs new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/crates/swc_interop_babel/src/lib.rs @@ -0,0 +1 @@ + From 32d35f2db9187bc986a6b5187906cdac7afa9a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 05:59:38 +0900 Subject: [PATCH 04/26] cargo lockfile --- Cargo.lock | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 47e3d6b62f11..404cbbed2726 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6012,6 +6012,17 @@ dependencies = [ "swc_visit", ] +[[package]] +name = "swc_interop_babel" +version = "0.1.0" +dependencies = [ + "swc_interop_nodejs", +] + +[[package]] +name = "swc_interop_nodejs" +version = "0.1.0" + [[package]] name = "swc_macros_common" version = "1.0.0" From 2285c501437d66bfdcf36f668969a9e6ae931903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:04:36 +0900 Subject: [PATCH 05/26] Dep on napi --- crates/swc_interop_nodejs/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index 21fa4592124c..13a1ee3e52d8 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -10,3 +10,4 @@ version = "0.1.0" [dependencies] +napi = { workspace = true, features = ["napi4"] } From 4922fc1a1e9de412fc4e029bb340bf47b33f3e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:04:39 +0900 Subject: [PATCH 06/26] cargo lockfile --- Cargo.lock | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 404cbbed2726..bc1c16253d97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6022,6 +6022,9 @@ dependencies = [ [[package]] name = "swc_interop_nodejs" version = "0.1.0" +dependencies = [ + "napi", +] [[package]] name = "swc_macros_common" From 19dce15370ae2166bc3c21b5edf671699e44ddc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:06:25 +0900 Subject: [PATCH 07/26] feature --- crates/swc_interop_nodejs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index 13a1ee3e52d8..adef7d5237c5 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -10,4 +10,4 @@ version = "0.1.0" [dependencies] -napi = { workspace = true, features = ["napi4"] } +napi = { workspace = true, features = ["napi4", "tokio_rt"] } From f68642386d069c9cc2f6c45014703a846b5b04bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:06:29 +0900 Subject: [PATCH 08/26] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index bc1c16253d97..a558fdd8f1e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2886,6 +2886,7 @@ dependencies = [ "once_cell", "serde", "serde_json", + "tokio", ] [[package]] From f0cf38f083332ed0558be3b4b9c757eede985309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:07:04 +0900 Subject: [PATCH 09/26] Deps --- crates/swc_interop_nodejs/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index adef7d5237c5..36aa894e35fc 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -10,4 +10,6 @@ version = "0.1.0" [dependencies] -napi = { workspace = true, features = ["napi4", "tokio_rt"] } +napi = { workspace = true, features = ["napi4", "tokio_rt"] } +tokio = { workspace = true } +tracing = { workspace = true } From 040c85cbb69ffee59bbffcc11701ab8d15e414e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:07:10 +0900 Subject: [PATCH 10/26] cargo lockfile --- Cargo.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index a558fdd8f1e7..29dd40112e28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6025,6 +6025,8 @@ name = "swc_interop_nodejs" version = "0.1.0" dependencies = [ "napi", + "tokio", + "tracing", ] [[package]] From 3b650c66c3ab36b6fc5521e6176ca8b0b5512358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 06:19:47 +0900 Subject: [PATCH 11/26] JsHook --- crates/swc_interop_nodejs/src/js_hook.rs | 99 ++++++++++++++++++++++++ crates/swc_interop_nodejs/src/lib.rs | 2 +- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 crates/swc_interop_nodejs/src/js_hook.rs diff --git a/crates/swc_interop_nodejs/src/js_hook.rs b/crates/swc_interop_nodejs/src/js_hook.rs new file mode 100644 index 000000000000..f316925dc23e --- /dev/null +++ b/crates/swc_interop_nodejs/src/js_hook.rs @@ -0,0 +1,99 @@ +use std::{fmt::Debug, marker::PhantomData}; + +use napi::{ + bindgen_prelude::{FromNapiValue, Promise}, + threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction}, + Env, JsFunction, JsUnknown, +}; +use tracing::trace; + +pub trait JsInput: 'static + Send + Debug { + fn into_js(self, env: &Env) -> napi::Result; +} + +impl JsInput for String { + fn into_js(self, env: &Env) -> napi::Result { + Ok(env.create_string(&self)?.into_unknown()) + } +} + +impl JsInput for Vec { + fn into_js(self, env: &Env) -> napi::Result { + let mut arr = env.create_array_with_length(self.len())?; + + for (idx, s) in self.into_iter().enumerate() { + arr.set_element(idx as _, s.into_js(env)?)?; + } + + Ok(arr.into_unknown()) + } +} + +impl JsInput for Vec { + fn into_js(self, env: &Env) -> napi::Result { + Ok(env.create_buffer_with_data(self)?.into_unknown()) + } +} + +impl JsInput for (A, B) +where + A: JsInput, + B: JsInput, +{ + fn into_js(self, env: &Env) -> napi::Result { + let mut arr = env.create_array(2)?; + arr.set(0, self.0.into_js(env)?)?; + arr.set(1, self.1.into_js(env)?)?; + + Ok(arr.coerce_to_object()?.into_unknown()) + } +} + +/// Seems like Vec is buggy +pub trait JsOutput: 'static + FromNapiValue + Send + Debug {} + +impl JsOutput for String {} + +impl JsOutput for bool {} + +pub struct JsHook +where + I: JsInput, + O: JsOutput, +{ + f: ThreadsafeFunction, + _marker: PhantomData, +} + +impl JsHook +where + I: JsInput, + O: JsOutput, +{ + pub fn new(env: &napi::Env, f: &JsFunction) -> napi::Result { + Ok(Self { + f: env.create_threadsafe_function(f, 0, |cx: ThreadSafeCallContext| { + let arg = cx.value.into_js(&cx.env)?.into_unknown(); + + if cfg!(debug_assertions) { + trace!("Converted to js value"); + } + Ok(vec![arg]) + })?, + _marker: PhantomData, + }) + } + + #[tracing::instrument(skip_all, fields(perf = "JsHook::call"))] + pub async fn call(&self, input: I) -> napi::Result { + if cfg!(debug_assertions) { + trace!("Calling js function"); + } + + let result: Promise = self.f.call_async(Ok(input)).await?; + + let res = result.await?; + + Ok(res) + } +} diff --git a/crates/swc_interop_nodejs/src/lib.rs b/crates/swc_interop_nodejs/src/lib.rs index 8b137891791f..84cd4b1705e5 100644 --- a/crates/swc_interop_nodejs/src/lib.rs +++ b/crates/swc_interop_nodejs/src/lib.rs @@ -1 +1 @@ - +pub mod js_hook; From 14a63a1e60224292497de18da55e632768bc6d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:01:40 +0900 Subject: [PATCH 12/26] Dep --- crates/swc_interop_nodejs/Cargo.toml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index 36aa894e35fc..cc262f1e8b93 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -10,6 +10,8 @@ version = "0.1.0" [dependencies] -napi = { workspace = true, features = ["napi4", "tokio_rt"] } -tokio = { workspace = true } -tracing = { workspace = true } +napi = { workspace = true, features = ["napi4", "tokio_rt"] } +serde = { workspace = true } +serde_json = { workspace = true } +tokio = { workspace = true } +tracing = { workspace = true } From d406bf85b5d6cf1e2bc7869299529723cc4fece3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:07:43 +0900 Subject: [PATCH 13/26] `AsJson` --- crates/swc_interop_nodejs/src/json.rs | 39 +++++++++++++++++++++++++++ crates/swc_interop_nodejs/src/lib.rs | 1 + 2 files changed, 40 insertions(+) create mode 100644 crates/swc_interop_nodejs/src/json.rs diff --git a/crates/swc_interop_nodejs/src/json.rs b/crates/swc_interop_nodejs/src/json.rs new file mode 100644 index 000000000000..39faac451ab1 --- /dev/null +++ b/crates/swc_interop_nodejs/src/json.rs @@ -0,0 +1,39 @@ +use std::fmt::Debug; + +use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown}; +use serde::{de::DeserializeOwned, Serialize}; + +use crate::js_hook::{JsInput, JsOutput}; + +/// Note: This type stringifies the output json string, because it's faster. +#[derive(Debug, Default)] +pub struct AsJson(pub T) +where + T: 'static + Send + Debug; + +impl FromNapiValue for AsJson +where + T: 'static + Send + Debug + DeserializeOwned, +{ + unsafe fn from_napi_value( + env_raw: napi::sys::napi_env, + napi_val: napi::sys::napi_value, + ) -> napi::Result { + let env = Env::from_raw(env_raw); + let json: String = env.from_js_value(JsUnknown::from_napi_value(env_raw, napi_val)?)?; + let t = serde_json::from_str(&json)?; + Ok(Self(t)) + } +} + +impl JsOutput for AsJson where T: 'static + Send + Debug + DeserializeOwned {} + +impl JsInput for AsJson +where + T: 'static + Send + Debug + Serialize, +{ + fn into_js(self, env: &napi::Env) -> napi::Result { + let json = serde_json::to_string(&self.0)?; + Ok(env.create_string(json.as_str())?.into_unknown()) + } +} diff --git a/crates/swc_interop_nodejs/src/lib.rs b/crates/swc_interop_nodejs/src/lib.rs index 84cd4b1705e5..f17f40723eb3 100644 --- a/crates/swc_interop_nodejs/src/lib.rs +++ b/crates/swc_interop_nodejs/src/lib.rs @@ -1 +1,2 @@ pub mod js_hook; +pub mod json; From bd69662ba235d9d11196d18b17becc01f1951aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:09:15 +0900 Subject: [PATCH 14/26] Move & Rename --- crates/swc_interop_nodejs/src/js_hook.rs | 55 ++------------- crates/swc_interop_nodejs/src/json.rs | 39 ----------- crates/swc_interop_nodejs/src/lib.rs | 2 +- crates/swc_interop_nodejs/src/types.rs | 86 ++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 91 deletions(-) delete mode 100644 crates/swc_interop_nodejs/src/json.rs create mode 100644 crates/swc_interop_nodejs/src/types.rs diff --git a/crates/swc_interop_nodejs/src/js_hook.rs b/crates/swc_interop_nodejs/src/js_hook.rs index f316925dc23e..9e33b731cc38 100644 --- a/crates/swc_interop_nodejs/src/js_hook.rs +++ b/crates/swc_interop_nodejs/src/js_hook.rs @@ -1,60 +1,13 @@ -use std::{fmt::Debug, marker::PhantomData}; +use std::marker::PhantomData; use napi::{ - bindgen_prelude::{FromNapiValue, Promise}, + bindgen_prelude::Promise, threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction}, - Env, JsFunction, JsUnknown, + JsFunction, }; use tracing::trace; -pub trait JsInput: 'static + Send + Debug { - fn into_js(self, env: &Env) -> napi::Result; -} - -impl JsInput for String { - fn into_js(self, env: &Env) -> napi::Result { - Ok(env.create_string(&self)?.into_unknown()) - } -} - -impl JsInput for Vec { - fn into_js(self, env: &Env) -> napi::Result { - let mut arr = env.create_array_with_length(self.len())?; - - for (idx, s) in self.into_iter().enumerate() { - arr.set_element(idx as _, s.into_js(env)?)?; - } - - Ok(arr.into_unknown()) - } -} - -impl JsInput for Vec { - fn into_js(self, env: &Env) -> napi::Result { - Ok(env.create_buffer_with_data(self)?.into_unknown()) - } -} - -impl JsInput for (A, B) -where - A: JsInput, - B: JsInput, -{ - fn into_js(self, env: &Env) -> napi::Result { - let mut arr = env.create_array(2)?; - arr.set(0, self.0.into_js(env)?)?; - arr.set(1, self.1.into_js(env)?)?; - - Ok(arr.coerce_to_object()?.into_unknown()) - } -} - -/// Seems like Vec is buggy -pub trait JsOutput: 'static + FromNapiValue + Send + Debug {} - -impl JsOutput for String {} - -impl JsOutput for bool {} +use crate::types::{JsInput, JsOutput}; pub struct JsHook where diff --git a/crates/swc_interop_nodejs/src/json.rs b/crates/swc_interop_nodejs/src/json.rs deleted file mode 100644 index 39faac451ab1..000000000000 --- a/crates/swc_interop_nodejs/src/json.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::fmt::Debug; - -use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown}; -use serde::{de::DeserializeOwned, Serialize}; - -use crate::js_hook::{JsInput, JsOutput}; - -/// Note: This type stringifies the output json string, because it's faster. -#[derive(Debug, Default)] -pub struct AsJson(pub T) -where - T: 'static + Send + Debug; - -impl FromNapiValue for AsJson -where - T: 'static + Send + Debug + DeserializeOwned, -{ - unsafe fn from_napi_value( - env_raw: napi::sys::napi_env, - napi_val: napi::sys::napi_value, - ) -> napi::Result { - let env = Env::from_raw(env_raw); - let json: String = env.from_js_value(JsUnknown::from_napi_value(env_raw, napi_val)?)?; - let t = serde_json::from_str(&json)?; - Ok(Self(t)) - } -} - -impl JsOutput for AsJson where T: 'static + Send + Debug + DeserializeOwned {} - -impl JsInput for AsJson -where - T: 'static + Send + Debug + Serialize, -{ - fn into_js(self, env: &napi::Env) -> napi::Result { - let json = serde_json::to_string(&self.0)?; - Ok(env.create_string(json.as_str())?.into_unknown()) - } -} diff --git a/crates/swc_interop_nodejs/src/lib.rs b/crates/swc_interop_nodejs/src/lib.rs index f17f40723eb3..2a1ff769004e 100644 --- a/crates/swc_interop_nodejs/src/lib.rs +++ b/crates/swc_interop_nodejs/src/lib.rs @@ -1,2 +1,2 @@ pub mod js_hook; -pub mod json; +pub mod types; diff --git a/crates/swc_interop_nodejs/src/types.rs b/crates/swc_interop_nodejs/src/types.rs new file mode 100644 index 000000000000..7c0fce9eff2c --- /dev/null +++ b/crates/swc_interop_nodejs/src/types.rs @@ -0,0 +1,86 @@ +use std::fmt::Debug; + +use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown}; +use serde::{de::DeserializeOwned, Serialize}; + +pub trait JsInput: 'static + Send + Debug { + fn into_js(self, env: &Env) -> napi::Result; +} + +impl JsInput for String { + fn into_js(self, env: &Env) -> napi::Result { + Ok(env.create_string(&self)?.into_unknown()) + } +} + +impl JsInput for Vec { + fn into_js(self, env: &Env) -> napi::Result { + let mut arr = env.create_array_with_length(self.len())?; + + for (idx, s) in self.into_iter().enumerate() { + arr.set_element(idx as _, s.into_js(env)?)?; + } + + Ok(arr.into_unknown()) + } +} + +impl JsInput for Vec { + fn into_js(self, env: &Env) -> napi::Result { + Ok(env.create_buffer_with_data(self)?.into_unknown()) + } +} + +impl JsInput for (A, B) +where + A: JsInput, + B: JsInput, +{ + fn into_js(self, env: &Env) -> napi::Result { + let mut arr = env.create_array(2)?; + arr.set(0, self.0.into_js(env)?)?; + arr.set(1, self.1.into_js(env)?)?; + + Ok(arr.coerce_to_object()?.into_unknown()) + } +} + +/// Seems like Vec is buggy +pub trait JsOutput: 'static + FromNapiValue + Send + Debug {} + +impl JsOutput for String {} + +impl JsOutput for bool {} + +/// Note: This type stringifies the output json string, because it's faster. +#[derive(Debug, Default)] +pub struct AsJson(pub T) +where + T: 'static + Send + Debug; + +impl FromNapiValue for AsJson +where + T: 'static + Send + Debug + DeserializeOwned, +{ + unsafe fn from_napi_value( + env_raw: napi::sys::napi_env, + napi_val: napi::sys::napi_value, + ) -> napi::Result { + let env = Env::from_raw(env_raw); + let json: String = env.from_js_value(JsUnknown::from_napi_value(env_raw, napi_val)?)?; + let t = serde_json::from_str(&json)?; + Ok(Self(t)) + } +} + +impl JsOutput for AsJson where T: 'static + Send + Debug + DeserializeOwned {} + +impl JsInput for AsJson +where + T: 'static + Send + Debug + Serialize, +{ + fn into_js(self, env: &napi::Env) -> napi::Result { + let json = serde_json::to_string(&self.0)?; + Ok(env.create_string(json.as_str())?.into_unknown()) + } +} From 39333d1bddc46634f6dcff81548b2c28541064ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:09:31 +0900 Subject: [PATCH 15/26] Dep: serde --- crates/swc_interop_babel/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/swc_interop_babel/Cargo.toml b/crates/swc_interop_babel/Cargo.toml index aade5ac980da..affaccae11f9 100644 --- a/crates/swc_interop_babel/Cargo.toml +++ b/crates/swc_interop_babel/Cargo.toml @@ -10,4 +10,6 @@ version = "0.1.0" [dependencies] +serde = { workspace = true } +serde_json = { workspace = true } swc_interop_nodejs = { version = "0.1.0", path = "../swc_interop_nodejs" } From fc86efc1c803cd5d18856df9eb5038f64b33f8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:09:35 +0900 Subject: [PATCH 16/26] cargo lockfile --- Cargo.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 29dd40112e28..640272e27047 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6017,6 +6017,8 @@ dependencies = [ name = "swc_interop_babel" version = "0.1.0" dependencies = [ + "serde", + "serde_json", "swc_interop_nodejs", ] @@ -6025,6 +6027,8 @@ name = "swc_interop_nodejs" version = "0.1.0" dependencies = [ "napi", + "serde", + "serde_json", "tokio", "tracing", ] From 210db9d65c0a0b4c5c60f43ec03cc5ed582240c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:09:42 +0900 Subject: [PATCH 17/26] JsTransform --- crates/swc_interop_babel/src/lib.rs | 2 +- crates/swc_interop_babel/src/transform.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 crates/swc_interop_babel/src/transform.rs diff --git a/crates/swc_interop_babel/src/lib.rs b/crates/swc_interop_babel/src/lib.rs index 8b137891791f..1bba7e1508ae 100644 --- a/crates/swc_interop_babel/src/lib.rs +++ b/crates/swc_interop_babel/src/lib.rs @@ -1 +1 @@ - +pub mod transform; diff --git a/crates/swc_interop_babel/src/transform.rs b/crates/swc_interop_babel/src/transform.rs new file mode 100644 index 000000000000..1b9b7b7ed12b --- /dev/null +++ b/crates/swc_interop_babel/src/transform.rs @@ -0,0 +1,13 @@ +use serde::{Deserialize, Serialize}; +use swc_interop_nodejs::{js_hook::JsHook, types::AsJson}; + +pub struct JsTrasnform { + f: JsHook, AsJson>, +} + +#[derive(Debug, Serialize, Deserialize)] +struct TransformUnit { + code: String, + #[serde(default)] + map: Option, +} From 098601ba8346ed6f99ccc56af7e95645a7b4ac81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:12:09 +0900 Subject: [PATCH 18/26] Dep on napi --- crates/swc_interop_babel/Cargo.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/swc_interop_babel/Cargo.toml b/crates/swc_interop_babel/Cargo.toml index affaccae11f9..0a82b18d91d3 100644 --- a/crates/swc_interop_babel/Cargo.toml +++ b/crates/swc_interop_babel/Cargo.toml @@ -10,6 +10,8 @@ version = "0.1.0" [dependencies] -serde = { workspace = true } -serde_json = { workspace = true } +napi = { workspace = true, features = ["napi4"] } +serde = { workspace = true } +serde_json = { workspace = true } + swc_interop_nodejs = { version = "0.1.0", path = "../swc_interop_nodejs" } From 923c0c660b0179afd8136bd824e599014890f616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:13:23 +0900 Subject: [PATCH 19/26] Hook --- crates/swc_interop_babel/src/transform.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/swc_interop_babel/src/transform.rs b/crates/swc_interop_babel/src/transform.rs index 1b9b7b7ed12b..f2d0f9208484 100644 --- a/crates/swc_interop_babel/src/transform.rs +++ b/crates/swc_interop_babel/src/transform.rs @@ -1,13 +1,26 @@ +use napi::JsFunction; use serde::{Deserialize, Serialize}; use swc_interop_nodejs::{js_hook::JsHook, types::AsJson}; pub struct JsTrasnform { - f: JsHook, AsJson>, + f: JsHook, AsJson>, } #[derive(Debug, Serialize, Deserialize)] -struct TransformUnit { - code: String, +pub struct SourceFile { + pub code: String, #[serde(default)] - map: Option, + pub map: Option, +} + +impl JsTrasnform { + pub fn new(env: &napi::Env, f: &JsFunction) -> napi::Result { + Ok(Self { + f: JsHook::new(env, f)?, + }) + } + + pub async fn transform(&self, input: SourceFile) -> napi::Result { + Ok(self.f.call(AsJson(input)).await?.0) + } } From 4eb58dd5ecb1344f32422da0ff33d53934e78d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:13:40 +0900 Subject: [PATCH 20/26] Rename --- crates/swc_interop_babel/src/transform.rs | 6 +++--- crates/swc_interop_nodejs/src/types.rs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/swc_interop_babel/src/transform.rs b/crates/swc_interop_babel/src/transform.rs index f2d0f9208484..b64b54aa871b 100644 --- a/crates/swc_interop_babel/src/transform.rs +++ b/crates/swc_interop_babel/src/transform.rs @@ -1,9 +1,9 @@ use napi::JsFunction; use serde::{Deserialize, Serialize}; -use swc_interop_nodejs::{js_hook::JsHook, types::AsJson}; +use swc_interop_nodejs::{js_hook::JsHook, types::AsJsonString}; pub struct JsTrasnform { - f: JsHook, AsJson>, + f: JsHook, AsJsonString>, } #[derive(Debug, Serialize, Deserialize)] @@ -21,6 +21,6 @@ impl JsTrasnform { } pub async fn transform(&self, input: SourceFile) -> napi::Result { - Ok(self.f.call(AsJson(input)).await?.0) + Ok(self.f.call(AsJsonString(input)).await?.0) } } diff --git a/crates/swc_interop_nodejs/src/types.rs b/crates/swc_interop_nodejs/src/types.rs index 7c0fce9eff2c..445551938a71 100644 --- a/crates/swc_interop_nodejs/src/types.rs +++ b/crates/swc_interop_nodejs/src/types.rs @@ -54,11 +54,11 @@ impl JsOutput for bool {} /// Note: This type stringifies the output json string, because it's faster. #[derive(Debug, Default)] -pub struct AsJson(pub T) +pub struct AsJsonString(pub T) where T: 'static + Send + Debug; -impl FromNapiValue for AsJson +impl FromNapiValue for AsJsonString where T: 'static + Send + Debug + DeserializeOwned, { @@ -73,9 +73,9 @@ where } } -impl JsOutput for AsJson where T: 'static + Send + Debug + DeserializeOwned {} +impl JsOutput for AsJsonString where T: 'static + Send + Debug + DeserializeOwned {} -impl JsInput for AsJson +impl JsInput for AsJsonString where T: 'static + Send + Debug + Serialize, { From 202d6582ff298068f36732958cd0db7b2e8ef56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:13:44 +0900 Subject: [PATCH 21/26] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 640272e27047..113b663e6459 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6017,6 +6017,7 @@ dependencies = [ name = "swc_interop_babel" version = "0.1.0" dependencies = [ + "napi", "serde", "serde_json", "swc_interop_nodejs", From 87f9e6764a5071ef40366b65247c14c318ae5e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:15:38 +0900 Subject: [PATCH 22/26] Rename --- crates/swc_interop_babel/src/transform.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/swc_interop_babel/src/transform.rs b/crates/swc_interop_babel/src/transform.rs index b64b54aa871b..3f74f22e3725 100644 --- a/crates/swc_interop_babel/src/transform.rs +++ b/crates/swc_interop_babel/src/transform.rs @@ -3,11 +3,11 @@ use serde::{Deserialize, Serialize}; use swc_interop_nodejs::{js_hook::JsHook, types::AsJsonString}; pub struct JsTrasnform { - f: JsHook, AsJsonString>, + f: JsHook, AsJsonString>, } #[derive(Debug, Serialize, Deserialize)] -pub struct SourceFile { +pub struct TransformOutput { pub code: String, #[serde(default)] pub map: Option, @@ -20,7 +20,7 @@ impl JsTrasnform { }) } - pub async fn transform(&self, input: SourceFile) -> napi::Result { + pub async fn transform(&self, input: TransformOutput) -> napi::Result { Ok(self.f.call(AsJsonString(input)).await?.0) } } From ff16e456bf7d47a638208483ba8a242dbe7bb4a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:20:21 +0900 Subject: [PATCH 23/26] skip --- crates/swc_interop_babel/src/transform.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_interop_babel/src/transform.rs b/crates/swc_interop_babel/src/transform.rs index 3f74f22e3725..d92b4c6d2219 100644 --- a/crates/swc_interop_babel/src/transform.rs +++ b/crates/swc_interop_babel/src/transform.rs @@ -9,7 +9,7 @@ pub struct JsTrasnform { #[derive(Debug, Serialize, Deserialize)] pub struct TransformOutput { pub code: String, - #[serde(default)] + #[serde(default, skip_serializing_if = "Option::is_none")] pub map: Option, } From 9c7d0f5b0bacbd24454b03018a021e0af17837cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:32:23 +0900 Subject: [PATCH 24/26] Dep --- crates/swc_interop_nodejs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index cc262f1e8b93..565e79986126 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -10,7 +10,7 @@ version = "0.1.0" [dependencies] -napi = { workspace = true, features = ["napi4", "tokio_rt"] } +napi = { workspace = true, features = ["napi4", "tokio_rt", "serde_json"] } serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true } From efaf68159475906fe5abe8734da4c38ed774c2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:33:07 +0900 Subject: [PATCH 25/26] serde-json --- crates/swc_interop_nodejs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_interop_nodejs/Cargo.toml b/crates/swc_interop_nodejs/Cargo.toml index 565e79986126..40c2aeae628b 100644 --- a/crates/swc_interop_nodejs/Cargo.toml +++ b/crates/swc_interop_nodejs/Cargo.toml @@ -10,7 +10,7 @@ version = "0.1.0" [dependencies] -napi = { workspace = true, features = ["napi4", "tokio_rt", "serde_json"] } +napi = { workspace = true, features = ["napi4", "tokio_rt", "serde-json"] } serde = { workspace = true } serde_json = { workspace = true } tokio = { workspace = true } From d3d366cd5dcc0692de101f5f37b030bbe8dd32e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Thu, 30 Jan 2025 07:38:48 +0900 Subject: [PATCH 26/26] feature --- crates/swc_interop_babel/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_interop_babel/Cargo.toml b/crates/swc_interop_babel/Cargo.toml index 0a82b18d91d3..3cbc2285d4b2 100644 --- a/crates/swc_interop_babel/Cargo.toml +++ b/crates/swc_interop_babel/Cargo.toml @@ -11,7 +11,7 @@ version = "0.1.0" [dependencies] napi = { workspace = true, features = ["napi4"] } -serde = { workspace = true } +serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } swc_interop_nodejs = { version = "0.1.0", path = "../swc_interop_nodejs" }