From 97298c4e36318674f82343b9cde2d938265ea3d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 1 Nov 2024 16:34:33 +0900 Subject: [PATCH] feat(es/plugin): Introduce `manual-tokio-runtmie` to `swc` crate (#9701) **Description:** This can cause a problem for Wasm plugin runtimes. --- .changeset/shiny-bears-thank.md | 6 ++++++ crates/swc/Cargo.toml | 2 ++ crates/swc/src/plugin.rs | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 .changeset/shiny-bears-thank.md diff --git a/.changeset/shiny-bears-thank.md b/.changeset/shiny-bears-thank.md new file mode 100644 index 00000000000..89ad6739465 --- /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 5e41138edc1..1fab8a93ea2 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 0afba9e4ef1..1cc5ea7c219 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")]