diff --git a/Cargo.lock b/Cargo.lock index 3ddeee2976e9..aca7a62753bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1129,6 +1129,12 @@ dependencies = [ "testing", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "lazy_static" version = "1.4.0" @@ -1447,6 +1453,8 @@ dependencies = [ "swc_node_base", "swc_node_bundler", "tracing", + "tracing-chrome", + "tracing-futures", "tracing-subscriber", ] @@ -1811,6 +1819,26 @@ dependencies = [ "siphasher", ] +[[package]] +name = "pin-project" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58ad3879ad3baf4e44784bc6a718a8698867bb991f8ce24d1bcbe2cfb4c3a75e" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744b6f092ba29c3650faf274db506afd39944f48420f6c86b17cfe0ee1cb36bb" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.8" @@ -3841,9 +3869,9 @@ checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" [[package]] name = "tracing" -version = "0.1.29" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" +checksum = "f6c650a8ef0cd2dd93736f033d21cbd1224c5a967aa0c258d00fcf7dafef9b9f" dependencies = [ "cfg-if 1.0.0", "log", @@ -3854,22 +3882,44 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e" +checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "tracing-chrome" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fa213f0cfbe503fb51065f2663726b093dac7cda39401eac060d0ebd4b8415c" +dependencies = [ + "json", + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-core" -version = "0.1.21" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" +checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23" dependencies = [ "lazy_static", + "valuable", +] + +[[package]] +name = "tracing-futures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" +dependencies = [ + "pin-project", + "tracing", ] [[package]] @@ -3885,9 +3935,9 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.5" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d81bfa81424cc98cb034b837c985b7a290f592e5b4322f353f94a0ab0f9f594" +checksum = "9e0ab7bdc962035a87fba73f3acca9b8a8d0034c2e6f60b84aeaaddddc155dce" dependencies = [ "ansi_term", "lazy_static", @@ -3967,6 +4017,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index b7e517d9efdb..ea15fdeef5a2 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -39,5 +39,7 @@ swc_ecma_loader = {path = "../swc_ecma_loader"} swc_ecma_parser = {path = "../swc_ecma_parser"} swc_node_base = {path = "../swc_node_base"} swc_node_bundler = {path = "../swc_node_bundler"} -tracing = {version = "0.1.28", features = ["release_max_level_info"]} -tracing-subscriber = {version = "0.3.4", features = ["env-filter"]} +tracing = { version = "0.1.31", features = ["release_max_level_info"] } +tracing-chrome = "0.4.0" +tracing-futures = "0.2.5" +tracing-subscriber = { version = "0.3.9", features = ["env-filter"] } diff --git a/crates/node/src/bundle.rs b/crates/node/src/bundle.rs index b7c8f1779062..32908645789a 100644 --- a/crates/node/src/bundle.rs +++ b/crates/node/src/bundle.rs @@ -186,6 +186,8 @@ pub(crate) fn bundle( conf_items: Buffer, signal: Option, ) -> napi::Result> { + crate::util::init_trace_once(false, None)?; + let c: Arc = get_compiler(); let static_items: StaticConfigItem = get_deserialized(&conf_items)?; diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index 320deec30e4c..c38a37a28f84 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -11,7 +11,6 @@ use std::{env, panic::set_hook, sync::Arc}; use backtrace::Backtrace; use swc::Compiler; use swc_common::{self, sync::Lazy, FilePathMapping, SourceMap}; -use tracing_subscriber::EnvFilter; mod bundle; mod minify; @@ -34,13 +33,6 @@ fn init() { println!("Panic: {:?}\nBacktrace: {:?}", panic_info, backtrace); })); } - - let _ = tracing_subscriber::FmtSubscriber::builder() - .without_time() - .with_target(false) - .with_ansi(true) - .with_env_filter(EnvFilter::from_env("SWC_LOG")) - .try_init(); } fn get_compiler() -> Arc { @@ -56,6 +48,7 @@ pub struct JsCompiler { impl JsCompiler { #[napi(constructor)] #[allow(clippy::new_without_default)] + #[tracing::instrument(level = "trace", skip_all)] pub fn new() -> Self { Self { _compiler: COMPILER.clone(), diff --git a/crates/node/src/minify.rs b/crates/node/src/minify.rs index b3fdde419857..b223382efe2d 100644 --- a/crates/node/src/minify.rs +++ b/crates/node/src/minify.rs @@ -71,6 +71,7 @@ impl Task for MinifyTask { #[napi] fn minify(code: Buffer, opts: Buffer, signal: Option) -> AsyncTask { + crate::util::init_trace_once(false, None).expect("Should able to init trace"); let code = String::from_utf8_lossy(code.as_ref()).to_string(); let options = String::from_utf8_lossy(opts.as_ref()).to_string(); @@ -83,6 +84,7 @@ fn minify(code: Buffer, opts: Buffer, signal: Option) -> AsyncTask< #[napi] pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result { + crate::util::init_trace_once(false, None)?; let code: MinifyTarget = get_deserialized(code)?; let opts = get_deserialized(opts)?; diff --git a/crates/node/src/parse.rs b/crates/node/src/parse.rs index e345d10bd37a..69cd01e0e14e 100644 --- a/crates/node/src/parse.rs +++ b/crates/node/src/parse.rs @@ -110,6 +110,8 @@ pub fn parse( filename: Option, signal: Option, ) -> AsyncTask { + crate::util::init_trace_once(false, None).expect("Should able to init trace"); + let c = get_compiler(); let options = String::from_utf8_lossy(options.as_ref()).to_string(); let filename = if let Some(value) = filename { @@ -131,6 +133,7 @@ pub fn parse( #[napi] pub fn parse_sync(src: String, opts: Buffer, filename: Option) -> napi::Result { + crate::util::init_trace_once(false, None)?; let c = get_compiler(); let options: ParseOptions = get_deserialized(&opts)?; @@ -160,6 +163,7 @@ pub fn parse_sync(src: String, opts: Buffer, filename: Option) -> napi:: #[napi] pub fn parse_file_sync(path: String, opts: Buffer) -> napi::Result { + crate::util::init_trace_once(false, None)?; let c = get_compiler(); let options: ParseOptions = get_deserialized(&opts)?; @@ -190,6 +194,8 @@ pub fn parse_file( options: Buffer, signal: Option, ) -> AsyncTask { + crate::util::init_trace_once(false, None).expect("Should able to init trace"); + let c = get_compiler(); let path = PathBuf::from(&path); let options = String::from_utf8_lossy(options.as_ref()).to_string(); diff --git a/crates/node/src/print.rs b/crates/node/src/print.rs index e0d8889e22ec..0bac10ee1d14 100644 --- a/crates/node/src/print.rs +++ b/crates/node/src/print.rs @@ -62,6 +62,8 @@ pub fn print( options: Buffer, signal: Option, ) -> napi::Result> { + crate::util::init_trace_once(false, None)?; + let c = get_compiler(); let options = String::from_utf8_lossy(&options).to_string(); @@ -77,6 +79,8 @@ pub fn print( #[napi] pub fn print_sync(program: String, options: Buffer) -> napi::Result { + crate::util::init_trace_once(false, None)?; + let c = get_compiler(); let program: Program = deserialize_json(program.as_str())?; diff --git a/crates/node/src/transform.rs b/crates/node/src/transform.rs index 0ab6d363f224..f7d4b90f2fea 100644 --- a/crates/node/src/transform.rs +++ b/crates/node/src/transform.rs @@ -12,6 +12,7 @@ use path_clean::clean; use swc::{config::Options, Compiler, TransformOutput}; use swc_common::FileName; use swc_ecma_ast::Program; +use tracing::instrument; use crate::{ get_compiler, @@ -40,6 +41,7 @@ impl Task for TransformTask { type JsValue = TransformOutput; type Output = TransformOutput; + #[instrument(level = "trace", skip_all)] fn compute(&mut self) -> napi::Result { let mut options: Options = serde_json::from_slice(self.options.as_ref())?; if !options.filename.is_empty() { @@ -92,12 +94,15 @@ impl Task for TransformTask { } #[napi] +#[instrument(level = "trace", skip_all)] pub fn transform( src: String, is_module: bool, options: JsBuffer, signal: Option, ) -> napi::Result> { + crate::util::init_trace_once(false, None)?; + let c = get_compiler(); let input = if is_module { @@ -115,7 +120,10 @@ pub fn transform( } #[napi] +#[instrument(level = "trace", skip_all)] pub fn transform_sync(s: String, is_module: bool, opts: Buffer) -> napi::Result { + crate::util::init_trace_once(false, None)?; + let c = get_compiler(); let mut options: Options = get_deserialized(&opts)?; @@ -147,12 +155,15 @@ pub fn transform_sync(s: String, is_module: bool, opts: Buffer) -> napi::Result< } #[napi] +#[instrument(level = "trace", skip_all)] pub fn transform_file( src: String, _is_module: bool, options: JsBuffer, signal: Option, ) -> napi::Result> { + crate::util::init_trace_once(false, None)?; + let c = get_compiler(); let path = clean(&src); @@ -170,6 +181,8 @@ pub fn transform_file_sync( is_module: bool, opts: Buffer, ) -> napi::Result { + crate::util::init_trace_once(false, None)?; + let c = get_compiler(); let mut options: Options = get_deserialized(&opts)?; diff --git a/crates/node/src/util.rs b/crates/node/src/util.rs index afe5fdd09edb..6ed0333ec366 100644 --- a/crates/node/src/util.rs +++ b/crates/node/src/util.rs @@ -1,14 +1,68 @@ use std::{ any::type_name, panic::{catch_unwind, AssertUnwindSafe}, + sync::Mutex, }; use anyhow::{anyhow, Context, Error}; use napi::Status; use serde::de::DeserializeOwned; use swc::try_with_handler; -use swc_common::{errors::Handler, sync::Lrc, SourceMap}; +use swc_common::{ + errors::Handler, + sync::{Lrc, OnceCell}, + SourceMap, +}; +use tracing::instrument; +use tracing_chrome::{ChromeLayerBuilder, FlushGuard}; +use tracing_subscriber::{ + prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter, +}; + +// Hold FlushGuard for the trace until it flushes out completely. +#[derive(Default)] +struct TraceFlushGuard { + inner: Option, +} +static FLUSH_GUARD: OnceCell> = OnceCell::new(); + +#[napi] +pub fn init_trace_once( + enable_chrome_trace: bool, + trace_out_file: Option, +) -> napi::Result<()> { + FLUSH_GUARD.get_or_init(|| { + if enable_chrome_trace { + let layer = if let Some(trace_out_file) = trace_out_file { + ChromeLayerBuilder::new().file(trace_out_file) + } else { + ChromeLayerBuilder::new() + }; + + let (chrome_layer, guard) = layer.build(); + tracing_subscriber::registry() + .with(chrome_layer) + .try_init() + .expect("Should able to register trace"); + + Mutex::new(TraceFlushGuard { inner: Some(guard) }) + } else { + tracing_subscriber::FmtSubscriber::builder() + .without_time() + .with_target(false) + .with_ansi(true) + .with_env_filter(EnvFilter::from_env("SWC_LOG")) + .try_init() + .expect("Should able to register trace"); + + Mutex::new(TraceFlushGuard { inner: None }) + } + }); + + Ok(()) +} +#[instrument(level = "trace", skip_all)] pub fn try_with(cm: Lrc, skip_filename: bool, op: F) -> Result where F: FnOnce(&Handler) -> Result, diff --git a/crates/swc/Cargo.toml b/crates/swc/Cargo.toml index 900edf04b419..925a6b9377b2 100644 --- a/crates/swc/Cargo.toml +++ b/crates/swc/Cargo.toml @@ -72,7 +72,7 @@ swc_ecmascript = {version = "0.119.0", path = "../swc_ecmascript"} swc_node_comments = {version = "0.4.0", path = "../swc_node_comments"} swc_plugin_runner = {version = "0.38.0", path = "../swc_plugin_runner", optional = true} swc_visit = {version = "0.3.0", path = "../swc_visit"} -tracing = "0.1.28" +tracing = "0.1.31" [dependencies.napi-derive] default-features = false diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index 2dc15965af5a..012c40cadf10 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -156,6 +156,7 @@ use swc_ecma_transforms::{ }; use swc_ecma_visit::{noop_visit_type, FoldWith, Visit, VisitMutWith, VisitWith}; pub use swc_node_comments::SwcComments; +use tracing::instrument; pub use crate::builder::PassBuilder; use crate::config::{ @@ -227,6 +228,7 @@ impl Write for LockedWriter { /// Try operation with a [Handler] and prints the errors as a [String] wrapped /// by [Err]. +#[instrument(level = "trace", skip_all)] pub fn try_with_handler( cm: Lrc, skip_filename: bool, diff --git a/crates/swc_bundler/Cargo.toml b/crates/swc_bundler/Cargo.toml index d21a393cd571..4929ac13ea5f 100644 --- a/crates/swc_bundler/Cargo.toml +++ b/crates/swc_bundler/Cargo.toml @@ -52,7 +52,7 @@ swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} swc_fast_graph = {version = "0.4.0", path = "../swc_fast_graph/"} swc_graph_analyzer = {version = "0.4.0", path = "../swc_graph_analyzer/"} -tracing = "0.1.28" +tracing = "0.1.31" [dev-dependencies] hex = "0.4" diff --git a/crates/swc_common/Cargo.toml b/crates/swc_common/Cargo.toml index edabd166fa65..3bd8d3c3a951 100644 --- a/crates/swc_common/Cargo.toml +++ b/crates/swc_common/Cargo.toml @@ -49,7 +49,7 @@ string_cache = "0.8.3" swc_eq_ignore_macros = {version = "0.1", path = "../swc_eq_ignore_macros"} swc_visit = {version = "0.3.0", path = "../swc_visit"} termcolor = {version = "1.0", optional = true} -tracing = "0.1.28" +tracing = "0.1.31" unicode-width = "0.1.4" url = "2.2.2" diff --git a/crates/swc_ecma_codegen/Cargo.toml b/crates/swc_ecma_codegen/Cargo.toml index 9d1b33a4f3fd..502edecc1170 100644 --- a/crates/swc_ecma_codegen/Cargo.toml +++ b/crates/swc_ecma_codegen/Cargo.toml @@ -19,7 +19,7 @@ swc_atoms = {version = "0.2", path = "../swc_atoms"} swc_common = {version = "0.17.3", path = "../swc_common"} swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast"} swc_ecma_codegen_macros = {version = "0.6.0", path = "../swc_ecma_codegen_macros"} -tracing = "0.1" +tracing = "0.1.31" [dev-dependencies] swc_common = {version = "0.17.3", path = "../swc_common", features = ["sourcemap"]} diff --git a/crates/swc_ecma_loader/Cargo.toml b/crates/swc_ecma_loader/Cargo.toml index 49620062ba30..a557b4164a26 100644 --- a/crates/swc_ecma_loader/Cargo.toml +++ b/crates/swc_ecma_loader/Cargo.toml @@ -31,7 +31,7 @@ regex = {version = "1", optional = true} serde = {version = "1", features = ["derive"]} serde_json = {version = "1.0.64", optional = true} swc_common = {version = "0.17.0", path = "../swc_common"} -tracing = "0.1.28" +tracing = "0.1.31" [dev-dependencies] diff --git a/crates/swc_ecma_minifier/Cargo.toml b/crates/swc_ecma_minifier/Cargo.toml index cf5262a5d114..2e367a7343e9 100644 --- a/crates/swc_ecma_minifier/Cargo.toml +++ b/crates/swc_ecma_minifier/Cargo.toml @@ -38,7 +38,7 @@ swc_ecma_transforms_base = {version = "0.61.0", path = "../swc_ecma_transforms_b swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} swc_timer = {version = "0.4.0", path = "../swc_timer"} -tracing = "0.1.28" +tracing = "0.1.31" unicode-xid = "0.2.2" [dev-dependencies] diff --git a/crates/swc_ecma_parser/Cargo.toml b/crates/swc_ecma_parser/Cargo.toml index 000d236da757..6c36133ebdfa 100644 --- a/crates/swc_ecma_parser/Cargo.toml +++ b/crates/swc_ecma_parser/Cargo.toml @@ -31,7 +31,7 @@ swc_atoms = {version = "0.2.3", path = "../swc_atoms"} swc_common = {version = "0.17.0", path = "../swc_common"} swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit", optional = true} -tracing = "0.1.28" +tracing = "0.1.31" typed-arena = "2.0.1" unicode-xid = "0.2" diff --git a/crates/swc_ecma_transforms_base/Cargo.toml b/crates/swc_ecma_transforms_base/Cargo.toml index 88b1b661b9fc..f596973119b9 100644 --- a/crates/swc_ecma_transforms_base/Cargo.toml +++ b/crates/swc_ecma_transforms_base/Cargo.toml @@ -27,7 +27,7 @@ swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast"} swc_ecma_parser = {version = "0.91.0", path = "../swc_ecma_parser"} swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} -tracing = "0.1" +tracing = "0.1.31" [dev-dependencies] swc_ecma_codegen = {version = "0.93.0", path = "../swc_ecma_codegen"} diff --git a/crates/swc_ecma_transforms_compat/Cargo.toml b/crates/swc_ecma_transforms_compat/Cargo.toml index a44caae95efd..c41f6b7fd957 100644 --- a/crates/swc_ecma_transforms_compat/Cargo.toml +++ b/crates/swc_ecma_transforms_compat/Cargo.toml @@ -34,7 +34,7 @@ swc_ecma_transforms_classes = {version = "0.49.0", path = "../swc_ecma_transform swc_ecma_transforms_macros = {version = "0.3.0", path = "../swc_ecma_transforms_macros"} swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} -tracing = "0.1" +tracing = "0.1.31" [dev-dependencies] serde_json = "1.0.66" diff --git a/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs b/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs index 4316b1c57d93..8540eee20de2 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/arrow.rs @@ -56,6 +56,7 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, InjectVars, VisitMut, /// }; /// console.log(bob.printFriends()); /// ``` +#[tracing::instrument(level = "trace", skip_all)] pub fn arrow() -> impl Fold + VisitMut + InjectVars { as_folder(Arrow::default()) } @@ -69,6 +70,7 @@ struct Arrow { impl VisitMut for Arrow { noop_visit_mut_type!(); + #[tracing::instrument(level = "trace", skip_all)] fn visit_mut_class(&mut self, c: &mut Class) { if c.super_class.is_some() { self.in_subclass = true; @@ -77,6 +79,7 @@ impl VisitMut for Arrow { self.in_subclass = false; } + #[tracing::instrument(level = "trace", skip_all)] fn visit_mut_constructor(&mut self, c: &mut Constructor) { c.params.visit_mut_children_with(self); @@ -105,6 +108,7 @@ impl VisitMut for Arrow { } } + #[tracing::instrument(level = "trace", skip_all)] fn visit_mut_expr(&mut self, expr: &mut Expr) { match expr { Expr::Arrow(ArrowExpr { @@ -163,10 +167,12 @@ impl VisitMut for Arrow { } } + #[tracing::instrument(level = "trace", skip_all)] fn visit_mut_function(&mut self, f: &mut Function) { f.visit_mut_children_with(self); } + #[tracing::instrument(level = "trace", skip_all)] fn visit_mut_module_items(&mut self, stmts: &mut Vec) { stmts.visit_mut_children_with(self); @@ -177,6 +183,7 @@ impl VisitMut for Arrow { } } + #[tracing::instrument(level = "trace", skip_all)] fn visit_mut_stmts(&mut self, stmts: &mut Vec) { let old_rep = self.hoister.take(); diff --git a/crates/swc_ecma_transforms_optimization/Cargo.toml b/crates/swc_ecma_transforms_optimization/Cargo.toml index f6e92aa0b2d4..27f76f884aa2 100644 --- a/crates/swc_ecma_transforms_optimization/Cargo.toml +++ b/crates/swc_ecma_transforms_optimization/Cargo.toml @@ -33,7 +33,7 @@ swc_ecma_transforms_base = {version = "0.61.0", path = "../swc_ecma_transforms_b swc_ecma_transforms_macros = {version = "0.3.0", path = "../swc_ecma_transforms_macros"} swc_ecma_utils = {version = "0.68.0", path = "../swc_ecma_utils"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} -tracing = "0.1.28" +tracing = "0.1.31" [dev-dependencies] swc_ecma_transforms_compat = {version = "0.73.0", path = "../swc_ecma_transforms_compat"} diff --git a/crates/swc_ecma_utils/Cargo.toml b/crates/swc_ecma_utils/Cargo.toml index f9ff616c0a1a..82548e149313 100644 --- a/crates/swc_ecma_utils/Cargo.toml +++ b/crates/swc_ecma_utils/Cargo.toml @@ -27,7 +27,7 @@ swc_atoms = {version = "0.2.0", path = "../swc_atoms"} swc_common = {version = "0.17.0", path = "../swc_common"} swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast"} swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} -tracing = "0.1" +tracing = "0.1.31" [dev-dependencies] swc_ecma_parser = {version = "0.91.0", path = "../swc_ecma_parser"} diff --git a/crates/swc_ecma_visit/Cargo.toml b/crates/swc_ecma_visit/Cargo.toml index f4f25d2c67d7..c2cb1acf33e5 100644 --- a/crates/swc_ecma_visit/Cargo.toml +++ b/crates/swc_ecma_visit/Cargo.toml @@ -17,4 +17,4 @@ swc_atoms = {version = "0.2", path = "../swc_atoms"} swc_common = {version = "0.17.0", path = "../swc_common"} swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast"} swc_visit = {version = "0.3.0", path = "../swc_visit"} -tracing = "0.1" +tracing = "0.1.31" diff --git a/crates/swc_graph_analyzer/Cargo.toml b/crates/swc_graph_analyzer/Cargo.toml index eea440c46999..dab2b3d4b39a 100644 --- a/crates/swc_graph_analyzer/Cargo.toml +++ b/crates/swc_graph_analyzer/Cargo.toml @@ -14,7 +14,7 @@ ahash = "0.7.6" auto_impl = "0.5.0" petgraph = "0.5.0" swc_fast_graph = {version = "0.4.0", path = "../swc_fast_graph/"} -tracing = "0.1.29" +tracing = "0.1.31" [dev-dependencies] testing = {version = "0.18.0", path = "../testing"} diff --git a/crates/swc_node_bundler/Cargo.toml b/crates/swc_node_bundler/Cargo.toml index 878a0830c1d7..35f0663675de 100644 --- a/crates/swc_node_bundler/Cargo.toml +++ b/crates/swc_node_bundler/Cargo.toml @@ -36,7 +36,7 @@ swc_ecma_transforms = {path = "../swc_ecma_transforms"} swc_ecma_utils = {path = "../swc_ecma_utils"} swc_ecma_visit = {path = "../swc_ecma_visit"} swc_node_base = {path = "../swc_node_base"} -tracing = "0.1.28" +tracing = "0.1.31" [dev-dependencies] pretty_assertions = "0.7.2" diff --git a/crates/swc_timer/Cargo.toml b/crates/swc_timer/Cargo.toml index c84941eab603..a222bd3f2c4f 100644 --- a/crates/swc_timer/Cargo.toml +++ b/crates/swc_timer/Cargo.toml @@ -11,7 +11,7 @@ version = "0.4.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tracing = "0.1" +tracing = "0.1.31" [dev-dependencies] testing = {version = "0.18.0", path = "../testing"} diff --git a/crates/swc_webpack_ast/Cargo.toml b/crates/swc_webpack_ast/Cargo.toml index ac9ff06296e7..58e485b7b2a5 100644 --- a/crates/swc_webpack_ast/Cargo.toml +++ b/crates/swc_webpack_ast/Cargo.toml @@ -24,7 +24,7 @@ swc_ecma_visit = {version = "0.54.0", path = "../swc_ecma_visit"} swc_estree_ast = {version = "0.7.0", path = "../swc_estree_ast"} swc_estree_compat = {version = "0.53.0", path = "../swc_estree_compat"} swc_timer = {version = "0.4.0", path = "../swc_timer"} -tracing = "0.1.29" +tracing = "0.1.31" [dev-dependencies] swc_ecma_transforms_testing = {version = "0.63.0", path = "../swc_ecma_transforms_testing"} diff --git a/crates/testing/Cargo.toml b/crates/testing/Cargo.toml index 64ddb97ff4b9..3e485d94a803 100644 --- a/crates/testing/Cargo.toml +++ b/crates/testing/Cargo.toml @@ -17,5 +17,5 @@ regex = "1" serde_json = "1.0.71" swc_common = {version = "0.17.0", path = "../swc_common", features = ["tty-emitter"]} testing_macros = {version = "0.2.0", path = "../testing_macros"} -tracing = "0.1.28" -tracing-subscriber = {version = "0.3.4", features = ["env-filter"]} +tracing = "0.1.31" +tracing-subscriber = { version = "0.3.9", features = ["env-filter"] } diff --git a/crates/wasm/Cargo.toml b/crates/wasm/Cargo.toml index 81365c0c2ec7..26c3261a652d 100644 --- a/crates/wasm/Cargo.toml +++ b/crates/wasm/Cargo.toml @@ -28,5 +28,5 @@ swc = {path = "../swc"} swc_common = {path = "../swc_common"} swc_ecma_lints = {path = "../swc_ecma_lints", features = ["non_critical_lints"]} swc_ecmascript = {path = "../swc_ecmascript"} -tracing = {version = "0.1.28", features = ["release_max_level_off"]} +tracing = { version = "0.1.31", features = ["release_max_level_off"] } wasm-bindgen = {version = "0.2", features = ["serde-serialize"]} diff --git a/node-swc/src/binding.d.ts b/node-swc/src/binding.d.ts index 331c9764772c..6162701d48af 100644 --- a/node-swc/src/binding.d.ts +++ b/node-swc/src/binding.d.ts @@ -4,76 +4,35 @@ /* auto-generated by NAPI-RS */ export class ExternalObject { - readonly "": { - readonly "": unique symbol; - [K: symbol]: T; - }; + readonly '': { + readonly '': unique symbol + [K: symbol]: T + } } export interface TransformOutput { - code: string; - map?: string | undefined | null; + code: string + map?: string | undefined | null } -export function bundle( - confItems: Buffer, - signal?: AbortSignal | undefined | null -): Promise<{ [index: string]: { code: string; map?: string } }>; -export function minify( - code: Buffer, - opts: Buffer, - signal?: AbortSignal | undefined | null -): Promise; -export function minifySync(code: Buffer, opts: Buffer): TransformOutput; -export function parse( - src: string, - options: Buffer, - filename?: string | undefined | null, - signal?: AbortSignal | undefined | null -): Promise; -export function parseSync( - src: string, - opts: Buffer, - filename?: string | undefined | null -): string; -export function parseFileSync(path: string, opts: Buffer): string; -export function parseFile( - path: string, - options: Buffer, - signal?: AbortSignal | undefined | null -): Promise; -export function print( - programJson: string, - options: Buffer, - signal?: AbortSignal | undefined | null -): Promise; -export function printSync(program: string, options: Buffer): TransformOutput; -export function transform( - src: string, - isModule: boolean, - options: Buffer, - signal?: AbortSignal | undefined | null -): Promise; -export function transformSync( - s: string, - isModule: boolean, - opts: Buffer -): TransformOutput; -export function transformFile( - src: string, - isModule: boolean, - options: Buffer, - signal?: AbortSignal | undefined | null -): Promise; -export function transformFileSync( - s: string, - isModule: boolean, - opts: Buffer -): TransformOutput; +export function bundle(confItems: Buffer, signal?: AbortSignal | undefined | null): Promise<{ [index: string]: { code: string, map?: string } }> +export function minify(code: Buffer, opts: Buffer, signal?: AbortSignal | undefined | null): Promise +export function minifySync(code: Buffer, opts: Buffer): TransformOutput +export function parse(src: string, options: Buffer, filename?: string | undefined | null, signal?: AbortSignal | undefined | null): Promise +export function parseSync(src: string, opts: Buffer, filename?: string | undefined | null): string +export function parseFileSync(path: string, opts: Buffer): string +export function parseFile(path: string, options: Buffer, signal?: AbortSignal | undefined | null): Promise +export function print(programJson: string, options: Buffer, signal?: AbortSignal | undefined | null): Promise +export function printSync(program: string, options: Buffer): TransformOutput +export function transform(src: string, isModule: boolean, options: Buffer, signal?: AbortSignal | undefined | null): Promise +export function transformSync(s: string, isModule: boolean, opts: Buffer): TransformOutput +export function transformFile(src: string, isModule: boolean, options: Buffer, signal?: AbortSignal | undefined | null): Promise +export function transformFileSync(s: string, isModule: boolean, opts: Buffer): TransformOutput +export function initTraceOnce(enableChromeTrace: boolean, traceOutFile?: string | undefined | null): void /** Hack for `Type Generation` */ export interface TransformOutput { - code: string; - map?: string | undefined | null; + code: string + map?: string | undefined | null } -export type JsCompiler = Compiler; +export type JsCompiler = Compiler export class Compiler { - constructor(); + constructor() } diff --git a/node-swc/src/binding.js b/node-swc/src/binding.js index 540bb9b1006d..41843f0cfbc6 100644 --- a/node-swc/src/binding.js +++ b/node-swc/src/binding.js @@ -1,275 +1,255 @@ -const { existsSync, readFileSync } = require("fs"); -const { join } = require("path"); +const { existsSync, readFileSync } = require('fs') +const { join } = require('path') -const { platform, arch } = process; +const { platform, arch } = process -let nativeBinding = null; -let localFileExisted = false; -let loadError = null; +let nativeBinding = null +let localFileExisted = false +let loadError = null function isMusl() { - // For Node 10 - if (!process.report || typeof process.report.getReport !== "function") { - try { - return readFileSync("/usr/bin/ldd", "utf8").includes("musl"); - } catch (e) { - return true; - } - } else { - const { glibcVersionRuntime } = process.report.getReport().header; - return !glibcVersionRuntime; + // For Node 10 + if (!process.report || typeof process.report.getReport !== 'function') { + try { + return readFileSync('/usr/bin/ldd', 'utf8').includes('musl') + } catch (e) { + return true } + } else { + const { glibcVersionRuntime } = process.report.getReport().header + return !glibcVersionRuntime + } } switch (platform) { - case "android": - switch (arch) { - case "arm64": - localFileExisted = existsSync( - join(__dirname, "swc.android-arm64.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.android-arm64.node"); - } else { - nativeBinding = require("@swc/core-android-arm64"); - } - } catch (e) { - loadError = e; - } - break; - case "arm": - localFileExisted = existsSync( - join(__dirname, "swc.android-arm-eabi.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.android-arm-eabi.node"); - } else { - nativeBinding = require("@swc/core-android-arm-eabi"); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on Android ${arch}`); + case 'android': + switch (arch) { + case 'arm64': + localFileExisted = existsSync(join(__dirname, 'swc.android-arm64.node')) + try { + if (localFileExisted) { + nativeBinding = require('./swc.android-arm64.node') + } else { + nativeBinding = require('@swc/core-android-arm64') + } + } catch (e) { + loadError = e + } + break + case 'arm': + localFileExisted = existsSync(join(__dirname, 'swc.android-arm-eabi.node')) + try { + if (localFileExisted) { + nativeBinding = require('./swc.android-arm-eabi.node') + } else { + nativeBinding = require('@swc/core-android-arm-eabi') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on Android ${arch}`) + } + break + case 'win32': + switch (arch) { + case 'x64': + localFileExisted = existsSync( + join(__dirname, 'swc.win32-x64-msvc.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.win32-x64-msvc.node') + } else { + nativeBinding = require('@swc/core-win32-x64-msvc') + } + } catch (e) { + loadError = e } - break; - case "win32": - switch (arch) { - case "x64": - localFileExisted = existsSync( - join(__dirname, "swc.win32-x64-msvc.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.win32-x64-msvc.node"); - } else { - nativeBinding = require("@swc/core-win32-x64-msvc"); - } - } catch (e) { - loadError = e; - } - break; - case "ia32": - localFileExisted = existsSync( - join(__dirname, "swc.win32-ia32-msvc.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.win32-ia32-msvc.node"); - } else { - nativeBinding = require("@swc/core-win32-ia32-msvc"); - } - } catch (e) { - loadError = e; - } - break; - case "arm64": - localFileExisted = existsSync( - join(__dirname, "swc.win32-arm64-msvc.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.win32-arm64-msvc.node"); - } else { - nativeBinding = require("@swc/core-win32-arm64-msvc"); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on Windows: ${arch}`); + break + case 'ia32': + localFileExisted = existsSync( + join(__dirname, 'swc.win32-ia32-msvc.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.win32-ia32-msvc.node') + } else { + nativeBinding = require('@swc/core-win32-ia32-msvc') + } + } catch (e) { + loadError = e } - break; - case "darwin": - switch (arch) { - case "x64": - localFileExisted = existsSync( - join(__dirname, "swc.darwin-x64.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.darwin-x64.node"); - } else { - nativeBinding = require("@swc/core-darwin-x64"); - } - } catch (e) { - loadError = e; - } - break; - case "arm64": - localFileExisted = existsSync( - join(__dirname, "swc.darwin-arm64.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.darwin-arm64.node"); - } else { - nativeBinding = require("@swc/core-darwin-arm64"); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on macOS: ${arch}`); + break + case 'arm64': + localFileExisted = existsSync( + join(__dirname, 'swc.win32-arm64-msvc.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.win32-arm64-msvc.node') + } else { + nativeBinding = require('@swc/core-win32-arm64-msvc') + } + } catch (e) { + loadError = e } - break; - case "freebsd": - if (arch !== "x64") { - throw new Error(`Unsupported architecture on FreeBSD: ${arch}`); + break + default: + throw new Error(`Unsupported architecture on Windows: ${arch}`) + } + break + case 'darwin': + switch (arch) { + case 'x64': + localFileExisted = existsSync(join(__dirname, 'swc.darwin-x64.node')) + try { + if (localFileExisted) { + nativeBinding = require('./swc.darwin-x64.node') + } else { + nativeBinding = require('@swc/core-darwin-x64') + } + } catch (e) { + loadError = e } - localFileExisted = existsSync(join(__dirname, "swc.freebsd-x64.node")); + break + case 'arm64': + localFileExisted = existsSync( + join(__dirname, 'swc.darwin-arm64.node') + ) try { + if (localFileExisted) { + nativeBinding = require('./swc.darwin-arm64.node') + } else { + nativeBinding = require('@swc/core-darwin-arm64') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on macOS: ${arch}`) + } + break + case 'freebsd': + if (arch !== 'x64') { + throw new Error(`Unsupported architecture on FreeBSD: ${arch}`) + } + localFileExisted = existsSync(join(__dirname, 'swc.freebsd-x64.node')) + try { + if (localFileExisted) { + nativeBinding = require('./swc.freebsd-x64.node') + } else { + nativeBinding = require('@swc/core-freebsd-x64') + } + } catch (e) { + loadError = e + } + break + case 'linux': + switch (arch) { + case 'x64': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'swc.linux-x64-musl.node') + ) + try { if (localFileExisted) { - nativeBinding = require("./swc.freebsd-x64.node"); + nativeBinding = require('./swc.linux-x64-musl.node') } else { - nativeBinding = require("@swc/core-freebsd-x64"); + nativeBinding = require('@swc/core-linux-x64-musl') } - } catch (e) { - loadError = e; + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'swc.linux-x64-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.linux-x64-gnu.node') + } else { + nativeBinding = require('@swc/core-linux-x64-gnu') + } + } catch (e) { + loadError = e + } } - break; - case "linux": - switch (arch) { - case "x64": - if (isMusl()) { - localFileExisted = existsSync( - join(__dirname, "swc.linux-x64-musl.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.linux-x64-musl.node"); - } else { - nativeBinding = require("@swc/core-linux-x64-musl"); - } - } catch (e) { - loadError = e; - } - } else { - localFileExisted = existsSync( - join(__dirname, "swc.linux-x64-gnu.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.linux-x64-gnu.node"); - } else { - nativeBinding = require("@swc/core-linux-x64-gnu"); - } - } catch (e) { - loadError = e; - } - } - break; - case "arm64": - if (isMusl()) { - localFileExisted = existsSync( - join(__dirname, "swc.linux-arm64-musl.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.linux-arm64-musl.node"); - } else { - nativeBinding = require("@swc/core-linux-arm64-musl"); - } - } catch (e) { - loadError = e; - } - } else { - localFileExisted = existsSync( - join(__dirname, "swc.linux-arm64-gnu.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.linux-arm64-gnu.node"); - } else { - nativeBinding = require("@swc/core-linux-arm64-gnu"); - } - } catch (e) { - loadError = e; - } - } - break; - case "arm": - localFileExisted = existsSync( - join(__dirname, "swc.linux-arm-gnueabihf.node") - ); - try { - if (localFileExisted) { - nativeBinding = require("./swc.linux-arm-gnueabihf.node"); - } else { - nativeBinding = require("@swc/core-linux-arm-gnueabihf"); - } - } catch (e) { - loadError = e; - } - break; - default: - throw new Error(`Unsupported architecture on Linux: ${arch}`); + break + case 'arm64': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'swc.linux-arm64-musl.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.linux-arm64-musl.node') + } else { + nativeBinding = require('@swc/core-linux-arm64-musl') + } + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'swc.linux-arm64-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.linux-arm64-gnu.node') + } else { + nativeBinding = require('@swc/core-linux-arm64-gnu') + } + } catch (e) { + loadError = e + } } - break; - default: - throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`); + break + case 'arm': + localFileExisted = existsSync( + join(__dirname, 'swc.linux-arm-gnueabihf.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./swc.linux-arm-gnueabihf.node') + } else { + nativeBinding = require('@swc/core-linux-arm-gnueabihf') + } + } catch (e) { + loadError = e + } + break + default: + throw new Error(`Unsupported architecture on Linux: ${arch}`) + } + break + default: + throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) } if (!nativeBinding) { - if (loadError) { - throw loadError; - } - throw new Error(`Failed to load native binding`); + if (loadError) { + throw loadError + } + throw new Error(`Failed to load native binding`) } -const { - bundle, - minify, - minifySync, - parse, - parseSync, - parseFileSync, - parseFile, - print, - printSync, - transform, - transformSync, - transformFile, - transformFileSync, - Compiler, -} = nativeBinding; +const { bundle, minify, minifySync, parse, parseSync, parseFileSync, parseFile, print, printSync, transform, transformSync, transformFile, transformFileSync, initTraceOnce, Compiler } = nativeBinding -module.exports.bundle = bundle; -module.exports.minify = minify; -module.exports.minifySync = minifySync; -module.exports.parse = parse; -module.exports.parseSync = parseSync; -module.exports.parseFileSync = parseFileSync; -module.exports.parseFile = parseFile; -module.exports.print = print; -module.exports.printSync = printSync; -module.exports.transform = transform; -module.exports.transformSync = transformSync; -module.exports.transformFile = transformFile; -module.exports.transformFileSync = transformFileSync; -module.exports.Compiler = Compiler; +module.exports.bundle = bundle +module.exports.minify = minify +module.exports.minifySync = minifySync +module.exports.parse = parse +module.exports.parseSync = parseSync +module.exports.parseFileSync = parseFileSync +module.exports.parseFile = parseFile +module.exports.print = print +module.exports.printSync = printSync +module.exports.transform = transform +module.exports.transformSync = transformSync +module.exports.transformFile = transformFile +module.exports.transformFileSync = transformFileSync +module.exports.initTraceOnce = initTraceOnce +module.exports.Compiler = Compiler diff --git a/node-swc/src/index.ts b/node-swc/src/index.ts index c019e0379987..40ef54501130 100644 --- a/node-swc/src/index.ts +++ b/node-swc/src/index.ts @@ -307,6 +307,31 @@ export function minifySync(src: string, opts?: JsMinifyOptions): Output { return compiler.minifySync(src, opts); } +/** + * Configure custom trace configuration runs for a process lifecycle. + * Currently only chromium's trace event format is supported. + * (https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview) + * + * This should be called before calling any binding interfaces exported in `@swc/core`, such as + * `transform*`, or `parse*` or anything. To avoid breaking changes, each binding fn internally + * sets default trace subscriber if not set. + * + * Unlike other configuration, this does not belong to individual api surface using swcrc + * or api's parameters (`transform(..., {trace})`). This is due to current tracing subscriber + * can be configured only once for the global scope. Calling `registerGlobalTraceConfig` multiple + * time won't cause error, subsequent calls will be ignored. + * + * As name implies currently this is experimental interface may change over time without semver + * major breaking changes. Please provide feedbacks, + * or bug report at https://github.com/swc-project/swc/discussions. + */ +export function __experimental_registerGlobalTraceConfig(traceConfig: { + type: 'traceEvent', + fileName?: string +}) { + bindings.initTraceOnce(traceConfig.type === 'traceEvent', traceConfig.fileName); +} + export const DEFAULT_EXTENSIONS = Object.freeze([ ".js", ".jsx",