feat(es/plugin): Introduce manual-tokio-runtmie to swc crate (#9701)

**Description:**

This can cause a problem for Wasm plugin runtimes.
This commit is contained in:
Donny/강동윤 2024-11-01 16:34:33 +09:00 committed by GitHub
parent 1c3eaf684a
commit 97298c4e36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 11 deletions

View File

@ -0,0 +1,6 @@
---
swc: patch
swc_core: patch
---
feat(es/plugin): Introduce `manual-tokio-runtmie` to `swc` crate

View File

@ -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 }

View File

@ -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")]