From 2f39e338db1f1e202e344aedd75c2bbabf8762be Mon Sep 17 00:00:00 2001 From: Matt Styles Date: Wed, 22 Jan 2025 11:37:11 +0000 Subject: [PATCH] feat(stylex_compiler_rs): add debug logging for file transforms --- Cargo.lock | 1 + crates/stylex-rs-compiler/Cargo.toml | 1 + crates/stylex-rs-compiler/src/lib.rs | 3 ++ crates/stylex-rs-compiler/src/structs/mod.rs | 45 ++++++++++++++++++- .../src/shared/structures/state_manager.rs | 12 ++--- .../src/shared/utils/js/evaluate.rs | 7 ++- 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1201d52f..cc457242 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2103,6 +2103,7 @@ name = "stylex_compiler_rs" version = "0.6.3-rc.2" dependencies = [ "color-backtrace", + "log", "napi", "napi-build", "napi-derive", diff --git a/crates/stylex-rs-compiler/Cargo.toml b/crates/stylex-rs-compiler/Cargo.toml index 02a5f8e1..35871f0b 100644 --- a/crates/stylex-rs-compiler/Cargo.toml +++ b/crates/stylex-rs-compiler/Cargo.toml @@ -27,6 +27,7 @@ swc_core = { workspace = true, features = [ ] } color-backtrace.workspace = true rustc-hash.workspace = true +log.workspace = true swc_compiler_base = { version = "*", features = ["node"] } swc_ecma_parser = { version = "*", features = ["verify"] } diff --git a/crates/stylex-rs-compiler/src/lib.rs b/crates/stylex-rs-compiler/src/lib.rs index 66c6aef0..70f7895e 100644 --- a/crates/stylex-rs-compiler/src/lib.rs +++ b/crates/stylex-rs-compiler/src/lib.rs @@ -1,5 +1,6 @@ mod structs; mod utils; +use log::info; use napi::{Env, Result}; use std::panic; use std::{env, sync::Arc}; @@ -38,6 +39,8 @@ pub fn transform( color_backtrace::install(); logger::initialize(); + info!("Transforming source file: {}", filename); + let result = panic::catch_unwind(|| { let cm: Arc = Default::default(); let filename = FileName::Real(filename.into()); diff --git a/crates/stylex-rs-compiler/src/structs/mod.rs b/crates/stylex-rs-compiler/src/structs/mod.rs index 2290aa98..65cad68b 100644 --- a/crates/stylex-rs-compiler/src/structs/mod.rs +++ b/crates/stylex-rs-compiler/src/structs/mod.rs @@ -1,6 +1,7 @@ use napi::JsObject; use napi_derive::napi; use rustc_hash::FxHashMap; +use std::fmt; use stylex_shared::shared::structures::{ named_import_source::ImportSources, stylex_options::{ModuleResolution, StyleResolution, StyleXOptionsParams}, @@ -23,7 +24,6 @@ pub enum SourceMaps { } #[napi(object)] -#[derive(Debug)] pub struct StyleXOptions { pub style_resolution: Option, pub use_rem_for_font_size: Option, @@ -45,6 +45,49 @@ pub struct StyleXOptions { pub source_map: Option, } +impl fmt::Debug for StyleXOptions { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!(f, "StyleXOptions {{")?; + writeln!(f, " dev: {:?}", self.dev)?; + writeln!( + f, + " use_rem_for_font_size: {:?}", + self.use_rem_for_font_size + )?; + writeln!(f, " runtime_injection: {:?}", self.runtime_injection)?; + writeln!( + f, + " treeshake_compensation: {:?}", + self.treeshake_compensation + )?; + + write!(f, " import_sources: [")?; + if let Some(sources) = &self.import_sources { + for (i, source) in sources.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "\"{}\"", source)?; + } + } + writeln!(f, "]")?; + + if let Some(res) = &self.unstable_module_resolution { + writeln!(f, " unstable_module_resolution: {{")?; + writeln!(f, " type: \"{}\"", res.r#type)?; + writeln!(f, " root_dir: {:?}", res.root_dir)?; + writeln!( + f, + " theme_file_extension: {:?}", + res.theme_file_extension + )?; + write!(f, " }}")?; + } + + write!(f, "}}") + } +} + #[napi(object)] pub struct StyleXMetadata { #[napi(ts_type = "([string, { ltr: string; rtl?: null | string }, number])[]")] diff --git a/crates/stylex-shared/src/shared/structures/state_manager.rs b/crates/stylex-shared/src/shared/structures/state_manager.rs index 17ce50f2..7c0e6624 100644 --- a/crates/stylex-shared/src/shared/structures/state_manager.rs +++ b/crates/stylex-shared/src/shared/structures/state_manager.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::{option::Option, rc::Rc}; use indexmap::{IndexMap, IndexSet}; -use log::{debug, error, info, warn}; +use log::debug; use stylex_path_resolver::{ package_json::{find_nearest_package_json, get_package_json, PackageJsonExtended}, resolvers::{resolve_file_path, EXTENSIONS}, @@ -380,6 +380,8 @@ impl StateManager { package_json_seen, ); + debug!(" resolved import path: {}", resolved_file_path); + let resolved_file_path = self.get_canonical_file_path(&resolved_file_path, package_json_seen); @@ -853,14 +855,6 @@ fn file_path_resolver( package_json_seen, ); - println!("Resolving path {}", source_file_path); - - // Example log messages at different levels - error!("Error err"); - warn!("warning msg"); - info!("some information: {}", "hello world"); - debug!("Variable x = {:?}", Some(42)); - if let Ok(resolved_path) = resolved_file_path { let resolved_path_str = resolved_path.display().to_string(); diff --git a/crates/stylex-shared/src/shared/utils/js/evaluate.rs b/crates/stylex-shared/src/shared/utils/js/evaluate.rs index 51c22bf5..6c96acea 100644 --- a/crates/stylex-shared/src/shared/utils/js/evaluate.rs +++ b/crates/stylex-shared/src/shared/utils/js/evaluate.rs @@ -2,7 +2,7 @@ use core::panic; use std::{borrow::Borrow, rc::Rc}; use indexmap::IndexMap; -use log::warn; +use log::{debug, warn}; use rustc_hash::{FxHashMap, FxHashSet}; use swc_core::{ atoms::Atom, @@ -1727,6 +1727,11 @@ fn _evaluate( } if let Some(import_path) = get_import_by_ident(ident, traversal_state) { + debug!( + "importing: {} from {}", + ident.sym.to_string(), + import_path.src.value + ); let (local_name, imported) = import_path .specifiers .iter()