Skip to content

Commit 5396d38

Browse files
committed
feat(napi): add tracing
1 parent 068aeda commit 5396d38

File tree

6 files changed

+139
-6
lines changed

6 files changed

+139
-6
lines changed

Cargo.lock

Lines changed: 97 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ e.g.
167167

168168
The input values are `options`, `path` and `specifier`, the returned value is `ret`.
169169

170+
### NAPI
171+
172+
```
173+
OXC_LOG=DEBUG your_program
174+
```
175+
170176
### Rolldown
171177

172178
```bash

examples/resolver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ fn main() {
1717
let options = ResolveOptions {
1818
alias_fields: vec![vec!["browser".into()]],
1919
alias: vec![("asdf".into(), vec![AliasValue::from("./test.js")])],
20+
extensions: vec![".js".into(), ".ts".into()],
21+
extension_alias: vec![(".js".into(), vec![".ts".into()])],
2022
..ResolveOptions::default()
2123
};
2224

napi/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ test = false
1111
doctest = false
1212

1313
[dependencies]
14-
oxc_resolver = { path = ".." }
15-
napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] }
16-
napi-derive = { version = "2.16.6" }
17-
tokio = "1.38.0"
14+
oxc_resolver = { path = ".." }
15+
napi = { version = "2.16.7", default-features = false, features = ["napi3", "serde-json", "async"] }
16+
napi-derive = { version = "2.16.6" }
17+
tokio = "1.38.0"
18+
tracing-subscriber = { version = "0.3.18", features = [] } # Omit the `regex` feature
1819

1920
[build-dependencies]
2021
napi-build = "2.1.3"

napi/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ use std::{
1010
use napi_derive::napi;
1111
use oxc_resolver::{ResolveOptions, Resolver};
1212

13-
use self::options::{NapiResolveOptions, StrOrStrList};
13+
use self::{
14+
options::{NapiResolveOptions, StrOrStrList},
15+
tracing::init_tracing,
16+
};
1417

1518
mod options;
19+
mod tracing;
1620

1721
#[napi(object)]
1822
pub struct ResolveResult {
@@ -47,6 +51,7 @@ pub struct ResolverFactory {
4751
impl ResolverFactory {
4852
#[napi(constructor)]
4953
pub fn new(options: NapiResolveOptions) -> Self {
54+
init_tracing();
5055
Self { resolver: Arc::new(Resolver::new(Self::normalize_options(options))) }
5156
}
5257

napi/src/tracing.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::sync::OnceLock;
2+
3+
use tracing_subscriber::{filter::Targets, prelude::*};
4+
5+
/// To debug `oxc_resolver`:
6+
/// `OXC_LOG=DEBUG your program`
7+
pub fn init_tracing() {
8+
static TRACING: OnceLock<()> = OnceLock::new();
9+
TRACING.get_or_init(|| {
10+
// Usage without the `regex` feature.
11+
// <https://github.com/tokio-rs/tracing/issues/1436#issuecomment-918528013>
12+
tracing_subscriber::registry()
13+
.with(std::env::var("OXC_LOG").map_or_else(
14+
|_| Targets::new(),
15+
|env_var| {
16+
use std::str::FromStr;
17+
Targets::from_str(&env_var).unwrap()
18+
},
19+
))
20+
.with(tracing_subscriber::fmt::layer())
21+
.init();
22+
});
23+
}

0 commit comments

Comments
 (0)