Skip to content

Commit cb9d197

Browse files
committed
span for minify
1 parent d739d9d commit cb9d197

File tree

1 file changed

+21
-4
lines changed
  • turbopack/crates/turbopack-ecmascript/src

1 file changed

+21
-4
lines changed

turbopack/crates/turbopack-ecmascript/src/minify.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io::Write, sync::Arc};
1+
use std::{borrow::Cow, io::Write, sync::Arc};
22

33
use anyhow::{bail, Context, Result};
44
use swc_core::{
@@ -19,7 +19,8 @@ use swc_core::{
1919
transforms::base::fixer::paren_remover,
2020
},
2121
};
22-
use turbo_tasks::{ResolvedVc, Vc};
22+
use tracing::{field::Empty, Instrument};
23+
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
2324
use turbo_tasks_fs::FileSystemPath;
2425
use turbopack_core::{
2526
code_builder::{Code, CodeBuilder},
@@ -34,6 +35,22 @@ pub async fn minify(
3435
code: Vc<Code>,
3536
source_maps: Vc<bool>,
3637
) -> Result<Vc<Code>> {
38+
let code_ref = code.await?;
39+
let code_str = code_ref.source_code().to_str()?;
40+
let span = tracing::info_span!("minify", path = %path.to_string().await?, input_len = code_str.len(), output_len = Empty);
41+
let code = async move { minify_inner(path, code, code_str, source_maps).await }
42+
.instrument(span.clone())
43+
.await?;
44+
span.record("output_len", code.source_code().len());
45+
Ok(code.cell())
46+
}
47+
48+
pub async fn minify_inner(
49+
path: Vc<FileSystemPath>,
50+
code: Vc<Code>,
51+
code_str: Cow<'_, str>,
52+
source_maps: Vc<bool>,
53+
) -> Result<Code> {
3754
let path = path.await?;
3855
let source_maps = source_maps.await?.then(|| code.generate_source_map());
3956
let code = code.await?;
@@ -43,7 +60,7 @@ pub async fn minify(
4360
let compiler = Arc::new(Compiler::new(cm.clone()));
4461
let fm = compiler.cm.new_source_file(
4562
FileName::Custom(path.path.to_string()).into(),
46-
code.source_code().to_str()?.into_owned(),
63+
code_str.into_owned(),
4764
);
4865

4966
let lexer = Lexer::new(
@@ -134,7 +151,7 @@ pub async fn minify(
134151
} else {
135152
builder.push_source(&src.into(), None);
136153
}
137-
Ok(builder.build().cell())
154+
Ok(builder.build())
138155
}
139156

140157
// From https://github.com/swc-project/swc/blob/11efd4e7c5e8081f8af141099d3459c3534c1e1d/crates/swc/src/lib.rs#L523-L560

0 commit comments

Comments
 (0)