diff --git a/Cargo.lock b/Cargo.lock index 75f8a678ee87..99c42faacf77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3717,6 +3717,7 @@ dependencies = [ "swc_timer", "swc_visit", "testing", + "tokio", "tracing", "url", "walkdir", @@ -5629,9 +5630,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.35.1" +version = "1.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104" +checksum = "61285f6515fa018fb2d1e46eb21223fff441ee8db5d0f1435e8ab4f5cdb80931" dependencies = [ "backtrace", "bytes", diff --git a/bindings/Cargo.lock b/bindings/Cargo.lock index e0c4f5dbe3a8..79aa8568cce2 100644 --- a/bindings/Cargo.lock +++ b/bindings/Cargo.lock @@ -215,7 +215,6 @@ dependencies = [ "serde_json", "swc_core", "swc_malloc", - "tokio", "tracing", "tracing-chrome", "tracing-futures", diff --git a/bindings/binding_core_node/Cargo.toml b/bindings/binding_core_node/Cargo.toml index f3f2bca8b296..d25f90c82b0b 100644 --- a/bindings/binding_core_node/Cargo.toml +++ b/bindings/binding_core_node/Cargo.toml @@ -65,4 +65,3 @@ swc_core = { version = "0.90.17", features = [ "base_concurrent", ] } swc_malloc = "0.5.10" -tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread"] } diff --git a/bindings/binding_core_node/src/transform.rs b/bindings/binding_core_node/src/transform.rs index 00b8d19e2b45..86a8bd34cbc3 100644 --- a/bindings/binding_core_node/src/transform.rs +++ b/bindings/binding_core_node/src/transform.rs @@ -50,45 +50,40 @@ impl Task for TransformTask { let error_format = options.experimental.error_format.unwrap_or_default(); - tokio::runtime::Runtime::new() - .unwrap() - .block_on(async move { - try_with( - self.c.cm.clone(), - !options.config.error.filename.into_bool(), - error_format, - |handler| { - self.c.run(|| match &self.input { - Input::Program(ref s) => { - let program: Program = - deserialize_json(s).expect("failed to deserialize Program"); - // TODO: Source map - self.c.process_js(handler, program, &options) - } - - Input::File(ref path) => { - let fm = - self.c.cm.load_file(path).context("failed to load file")?; - self.c.process_js_file(fm, handler, &options) - } - - Input::Source { src } => { - let fm = self.c.cm.new_source_file( - if options.filename.is_empty() { - FileName::Anon - } else { - FileName::Real(options.filename.clone().into()) - }, - src.to_string(), - ); - - self.c.process_js_file(fm, handler, &options) - } - }) - }, - ) - }) - .convert_err() + try_with( + self.c.cm.clone(), + !options.config.error.filename.into_bool(), + error_format, + |handler| { + self.c.run(|| match &self.input { + Input::Program(ref s) => { + let program: Program = + deserialize_json(s).expect("failed to deserialize Program"); + // TODO: Source map + self.c.process_js(handler, program, &options) + } + + Input::File(ref path) => { + let fm = self.c.cm.load_file(path).context("failed to load file")?; + self.c.process_js_file(fm, handler, &options) + } + + Input::Source { src } => { + let fm = self.c.cm.new_source_file( + if options.filename.is_empty() { + FileName::Anon + } else { + FileName::Real(options.filename.clone().into()) + }, + src.to_string(), + ); + + self.c.process_js_file(fm, handler, &options) + } + }) + }, + ) + .convert_err() } fn resolve(&mut self, _env: Env, result: Self::Output) -> napi::Result { @@ -142,35 +137,31 @@ pub fn transform_sync(s: String, is_module: bool, opts: Buffer) -> napi::Result< let error_format = options.experimental.error_format.unwrap_or_default(); - tokio::runtime::Runtime::new() - .unwrap() - .block_on(async move { - try_with( - c.cm.clone(), - !options.config.error.filename.into_bool(), - error_format, - |handler| { - c.run(|| { - if is_module { - let program: Program = deserialize_json(s.as_str()) - .context("failed to deserialize Program")?; - c.process_js(handler, program, &options) + try_with( + c.cm.clone(), + !options.config.error.filename.into_bool(), + error_format, + |handler| { + c.run(|| { + if is_module { + let program: Program = + deserialize_json(s.as_str()).context("failed to deserialize Program")?; + c.process_js(handler, program, &options) + } else { + let fm = c.cm.new_source_file( + if options.filename.is_empty() { + FileName::Anon } else { - let fm = c.cm.new_source_file( - if options.filename.is_empty() { - FileName::Anon - } else { - FileName::Real(options.filename.clone().into()) - }, - s, - ); - c.process_js_file(fm, handler, &options) - } - }) - }, - ) - }) - .convert_err() + FileName::Real(options.filename.clone().into()) + }, + s, + ); + c.process_js_file(fm, handler, &options) + } + }) + }, + ) + .convert_err() } #[napi] @@ -212,26 +203,22 @@ pub fn transform_file_sync( let error_format = options.experimental.error_format.unwrap_or_default(); - tokio::runtime::Runtime::new() - .unwrap() - .block_on(async move { - try_with( - c.cm.clone(), - !options.config.error.filename.into_bool(), - error_format, - |handler| { - c.run(|| { - if is_module { - let program: Program = deserialize_json(s.as_str()) - .context("failed to deserialize Program")?; - c.process_js(handler, program, &options) - } else { - let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file"); - c.process_js_file(fm, handler, &options) - } - }) - }, - ) - }) - .convert_err() + try_with( + c.cm.clone(), + !options.config.error.filename.into_bool(), + error_format, + |handler| { + c.run(|| { + if is_module { + let program: Program = + deserialize_json(s.as_str()).context("failed to deserialize Program")?; + c.process_js(handler, program, &options) + } else { + let fm = c.cm.load_file(Path::new(&s)).expect("failed to load file"); + c.process_js_file(fm, handler, &options) + } + }) + }, + ) + .convert_err() } diff --git a/crates/swc/Cargo.toml b/crates/swc/Cargo.toml index 32e89b252c8e..3b4692a1772a 100644 --- a/crates/swc/Cargo.toml +++ b/crates/swc/Cargo.toml @@ -32,6 +32,7 @@ plugin = [ "swc_plugin_runner/ecma", "swc_plugin_runner/rkyv-impl", "swc_plugin_proxy/plugin-rt", + "tokio", ] plugin_transform_schema_v1 = [ "swc_common/plugin_transform_schema_v1", @@ -106,6 +107,10 @@ swc_plugin_proxy = { version = "0.41.5", path = "../swc_plugin_proxy", optional swc_plugin_runner = { version = "0.106.10", path = "../swc_plugin_runner", optional = true, default-features = false } swc_timer = { version = "0.21.20", path = "../swc_timer" } swc_visit = { version = "0.5.10", path = "../swc_visit" } +tokio = { version = "1.36.0", optional = true, features = [ + "rt", + "rt-multi-thread", +] } [dependencies.napi-derive] diff --git a/crates/swc/src/plugin.rs b/crates/swc/src/plugin.rs index 9fc4772a43ac..0cf9b37c3e7f 100644 --- a/crates/swc/src/plugin.rs +++ b/crates/swc/src/plugin.rs @@ -55,12 +55,16 @@ impl RustPlugins { return Ok(n); } - self.apply_inner(n).with_context(|| { - format!( - "failed to invoke plugin on '{:?}'", - self.metadata_context.filename - ) - }) + tokio::runtime::Runtime::new() + .unwrap() + .block_on(async move { + self.apply_inner(n).with_context(|| { + format!( + "failed to invoke plugin on '{:?}'", + self.metadata_context.filename + ) + }) + }) } #[tracing::instrument(level = "info", skip_all, name = "apply_plugins")]