diff --git a/.changeset/shiny-bears-thank.md b/.changeset/shiny-bears-thank.md new file mode 100644 index 000000000000..89ad6739465e --- /dev/null +++ b/.changeset/shiny-bears-thank.md @@ -0,0 +1,6 @@ +--- +swc: patch +swc_core: patch +--- + +feat(es/plugin): Introduce `manual-tokio-runtmie` to `swc` crate diff --git a/crates/swc/Cargo.toml b/crates/swc/Cargo.toml index 5e41138edc1c..1fab8a93ea26 100644 --- a/crates/swc/Cargo.toml +++ b/crates/swc/Cargo.toml @@ -47,6 +47,8 @@ plugin_transform_host_js = ["swc_plugin_runner/plugin_transform_host_js"] plugin_transform_host_native = [ "swc_plugin_runner/plugin_transform_host_native", ] +# Do not inject tokio runtime while running plugin transforms +manual-tokio-runtmie = [] [dependencies] anyhow = { workspace = true } diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index 0afba9e4ef1a..1cc5ea7c219a 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -55,19 +55,19 @@ impl RustPlugins { return Ok(n); } - let fut = async move { - self.apply_inner(n).with_context(|| { - format!( - "failed to invoke plugin on '{:?}'", - self.metadata_context.filename - ) - }) - }; - if let Ok(handle) = tokio::runtime::Handle::try_current() { - handle.block_on(fut) + let filename = self.metadata_context.filename.clone(); + + if cfg!(feature = "manual-tokio-runtmie") { + self.apply_inner(n) } else { - tokio::runtime::Runtime::new().unwrap().block_on(fut) + let fut = async move { self.apply_inner(n) }; + if let Ok(handle) = tokio::runtime::Handle::try_current() { + handle.block_on(fut) + } else { + tokio::runtime::Runtime::new().unwrap().block_on(fut) + } } + .with_context(|| format!("failed to invoke plugin on '{filename:?}'")) } #[tracing::instrument(level = "info", skip_all, name = "apply_plugins")]