Skip to content

Commit

Permalink
feat(stylex_compiler_rs): add debug logging for file transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstyles committed Jan 22, 2025
1 parent 04e520a commit 2f39e33
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/stylex-rs-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
3 changes: 3 additions & 0 deletions crates/stylex-rs-compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod structs;
mod utils;
use log::info;
use napi::{Env, Result};
use std::panic;
use std::{env, sync::Arc};
Expand Down Expand Up @@ -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<SourceMap> = Default::default();
let filename = FileName::Real(filename.into());
Expand Down
45 changes: 44 additions & 1 deletion crates/stylex-rs-compiler/src/structs/mod.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -23,7 +24,6 @@ pub enum SourceMaps {
}

#[napi(object)]
#[derive(Debug)]
pub struct StyleXOptions {
pub style_resolution: Option<String>,
pub use_rem_for_font_size: Option<bool>,
Expand All @@ -45,6 +45,49 @@ pub struct StyleXOptions {
pub source_map: Option<SourceMaps>,
}

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])[]")]
Expand Down
12 changes: 3 additions & 9 deletions crates/stylex-shared/src/shared/structures/state_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();

Expand Down
7 changes: 6 additions & 1 deletion crates/stylex-shared/src/shared/utils/js/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2f39e33

Please sign in to comment.