Skip to content

Commit

Permalink
feat(es/plugin): Introduce manual-tokio-runtmie to swc crate (#9701)
Browse files Browse the repository at this point in the history
**Description:**

This can cause a problem for Wasm plugin runtimes.
  • Loading branch information
kdy1 authored Nov 1, 2024
1 parent 1c3eaf6 commit 97298c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/shiny-bears-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc: patch
swc_core: patch
---

feat(es/plugin): Introduce `manual-tokio-runtmie` to `swc` crate
2 changes: 2 additions & 0 deletions crates/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
22 changes: 11 additions & 11 deletions crates/swc/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 97298c4

Please sign in to comment.