Skip to content

Commit

Permalink
span for minify
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Feb 3, 2025
1 parent 6b21740 commit f0fe6c0
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions turbopack/crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, sync::Arc};
use std::{borrow::Cow, io::Write, sync::Arc};

use anyhow::{bail, Context, Result};
use swc_core::{
Expand All @@ -19,7 +19,8 @@ use swc_core::{
transforms::base::fixer::paren_remover,
},
};
use turbo_tasks::{ResolvedVc, Vc};
use tracing::{field::Empty, Instrument};
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
code_builder::{Code, CodeBuilder},
Expand All @@ -34,6 +35,22 @@ pub async fn minify(
code: Vc<Code>,
source_maps: Vc<bool>,
) -> Result<Vc<Code>> {
let code_ref = code.await?;
let code_str = code_ref.source_code().to_str()?;
let span = tracing::info_span!("minify", path = %path.to_string().await?, input_len = code_str.len(), output_len = Empty);
let code = async move { minify_inner(path, code, code_str, source_maps).await }
.instrument(span.clone())
.await?;
span.record("output_len", code.source_code().len());
Ok(code.cell())
}

pub async fn minify_inner(
path: Vc<FileSystemPath>,
code: Vc<Code>,
code_str: Cow<'_, str>,
source_maps: Vc<bool>,
) -> Result<Code> {
let path = path.await?;
let source_maps = source_maps.await?.then(|| code.generate_source_map());
let code = code.await?;
Expand All @@ -43,7 +60,7 @@ pub async fn minify(
let compiler = Arc::new(Compiler::new(cm.clone()));
let fm = compiler.cm.new_source_file(
FileName::Custom(path.path.to_string()).into(),
code.source_code().to_str()?.into_owned(),
code_str.into_owned(),
);

let lexer = Lexer::new(
Expand Down Expand Up @@ -134,7 +151,7 @@ pub async fn minify(
} else {
builder.push_source(&src.into(), None);
}
Ok(builder.build().cell())
Ok(builder.build())
}

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

0 comments on commit f0fe6c0

Please sign in to comment.