Skip to content

Commit

Permalink
inline minify into code generation and make it a plain function inste…
Browse files Browse the repository at this point in the history
…ad of a turbo tasks function
  • Loading branch information
sokra committed Feb 27, 2025
1 parent 0e32f84 commit 4b1223a
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 70 deletions.
13 changes: 7 additions & 6 deletions turbopack/crates/turbopack-browser/src/ecmascript/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ impl EcmascriptBrowserChunkContent {
async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
let this = self.await?;
let output_root = this.chunking_context.output_root().await?;
let source_maps = this
let source_maps = *this
.chunking_context
.reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk));
.reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk))
.await?;
let chunk_path_vc = this.chunk.path();
let chunk_path = chunk_path_vc.await?;
let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
Expand Down Expand Up @@ -108,7 +109,7 @@ impl EcmascriptBrowserChunkContent {

write!(code, "\n}}]);")?;

if *source_maps.await? && code.has_source_map() {
if source_maps && code.has_source_map() {
let filename = chunk_path.file_name();
write!(
code,
Expand All @@ -119,13 +120,13 @@ impl EcmascriptBrowserChunkContent {
)?;
}

let code = code.build().cell();
let mut code = code.build();

if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
return Ok(minify(chunk_path_vc, code, source_maps, mangle));
code = minify(&*chunk_path_vc.await?, &code, source_maps, mangle)?;
}

Ok(code)
Ok(code.cell())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ impl EcmascriptBrowserEvaluateChunk {

let output_root = this.chunking_context.output_root().await?;
let output_root_to_root_path = this.chunking_context.output_root_to_root_path();
let source_maps = this
let source_maps = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self));
.reference_chunk_source_maps(Vc::upcast(self))
.await?;
let chunk_path_vc = self.path();
let chunk_path = chunk_path_vc.await?;
let chunk_public_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
Expand Down Expand Up @@ -175,7 +176,7 @@ impl EcmascriptBrowserEvaluateChunk {
}
}

if *source_maps.await? && code.has_source_map() {
if source_maps && code.has_source_map() {
let filename = chunk_path.file_name();
write!(
code,
Expand All @@ -186,13 +187,13 @@ impl EcmascriptBrowserEvaluateChunk {
)?;
}

let code = code.build().cell();
let mut code = code.build();

if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
return Ok(minify(chunk_path_vc, code, source_maps, mangle));
code = minify(&*chunk_path_vc.await?, &code, source_maps, mangle)?;
}

Ok(code)
Ok(code.cell())
}
}

Expand Down
15 changes: 6 additions & 9 deletions turbopack/crates/turbopack-css/src/module_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,10 @@ impl EcmascriptChunkItem for ModuleChunkItem {
// We generate a minimal map for runtime code so that the filename is
// displayed in dev tools.
source_map: if source_map {
Some(
generate_minimal_source_map(
self.module.ident().to_string().await?.to_string(),
code,
)
.await?,
)
Some(generate_minimal_source_map(
self.module.ident().to_string().await?.to_string(),
code,
)?)
} else {
None
},
Expand All @@ -419,7 +416,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
}
}

async fn generate_minimal_source_map(filename: String, source: String) -> Result<Rope> {
fn generate_minimal_source_map(filename: String, source: String) -> Result<Rope> {
let mut mappings = vec![];
// Start from 1 because 0 is reserved for dummy spans in SWC.
let mut pos = 1;
Expand All @@ -435,7 +432,7 @@ async fn generate_minimal_source_map(filename: String, source: String) -> Result
}
let sm: Arc<SourceMap> = Default::default();
sm.new_source_file(FileName::Custom(filename).into(), source);
let map = generate_js_source_map(sm, mappings, None).await?;
let map = generate_js_source_map(sm, mappings, None)?;
Ok(map)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn get_browser_runtime_code(
chunk_base_path: Vc<Option<RcStr>>,
runtime_type: Value<RuntimeType>,
output_root_to_root_path: Vc<RcStr>,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(environment).await?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn embed_file_path(path: RcStr) -> Vc<FileSystemPath> {
pub fn embed_static_code(
asset_context: Vc<Box<dyn AssetContext>>,
path: RcStr,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Vc<Code> {
StaticEcmascriptCode::new(asset_context, embed_file_path(path), generate_source_map).code()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod
#[turbo_tasks::function]
pub async fn get_nodejs_runtime_code(
environment: Vc<Environment>,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(environment).await?;

Expand Down
29 changes: 13 additions & 16 deletions turbopack/crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub trait EcmascriptAnalyzable {
/// transforming code that is not a module, e.g. runtime code.
async fn module_content_without_analysis(
self: Vc<Self>,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Result<Vc<EcmascriptModuleContent>>;

async fn module_content(
Expand Down Expand Up @@ -396,7 +396,7 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset {
#[turbo_tasks::function]
async fn module_content_without_analysis(
self: Vc<Self>,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Result<Vc<EcmascriptModuleContent>> {
let this = self.await?;

Expand All @@ -423,7 +423,9 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset {
let analyze_ref = analyze.await?;

let module_type_result = *self.determine_module_type().await?;
let generate_source_map = chunking_context.reference_module_source_maps(Vc::upcast(self));
let generate_source_map = *chunking_context
.reference_module_source_maps(Vc::upcast(self))
.await?;

Ok(EcmascriptModuleContent::new(
EcmascriptModuleContentOptions {
Expand Down Expand Up @@ -766,7 +768,7 @@ pub struct EcmascriptModuleContentOptions {
esm_references: Vc<EsmAssetReferences>,
code_generation: Vc<CodeGens>,
async_module: Vc<OptionAsyncModule>,
generate_source_map: Vc<bool>,
generate_source_map: bool,
original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
exports: Vc<EcmascriptExports>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
Expand Down Expand Up @@ -856,7 +858,7 @@ impl EcmascriptModuleContent {
parsed: Vc<ParseResult>,
ident: Vc<AssetIdent>,
specified_module_type: SpecifiedModuleType,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Result<Vc<Self>> {
gen_content_with_code_gens(
parsed.to_resolved().await?,
Expand All @@ -875,7 +877,7 @@ async fn gen_content_with_code_gens(
ident: Vc<AssetIdent>,
specified_module_type: SpecifiedModuleType,
code_gens: impl IntoIterator<Item = &CodeGeneration>,
generate_source_map: Vc<bool>,
generate_source_map: bool,
original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
) -> Result<Vc<EcmascriptModuleContent>> {
let parsed = parsed.final_read_hint().await?;
Expand Down Expand Up @@ -937,8 +939,6 @@ async fn gen_content_with_code_gens(

let mut mappings = vec![];

let generate_source_map = *generate_source_map.await?;

{
let comments = match comments {
Either::Left(comments) => Either::Left(comments.into_consumable()),
Expand All @@ -965,14 +965,11 @@ async fn gen_content_with_code_gens(
}

let source_map = if generate_source_map {
Some(
generate_js_source_map(
source_map.clone(),
mappings,
original_source_map.await?.as_ref(),
)
.await?,
)
Some(generate_js_source_map(
source_map.clone(),
mappings,
original_source_map.await?.as_ref(),
)?)
} else {
None
};
Expand Down
16 changes: 3 additions & 13 deletions turbopack/crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,13 @@ use swc_core::{
transforms::base::fixer::paren_remover,
},
};
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::code_builder::{Code, CodeBuilder};

use crate::parse::generate_js_source_map;

#[turbo_tasks::function]
pub async fn minify(
path: Vc<FileSystemPath>,
code: Vc<Code>,
source_maps: Vc<bool>,
mangle: bool,
) -> Result<Vc<Code>> {
let path = path.await?;
let code = code.await?;
pub fn minify(path: &FileSystemPath, code: &Code, source_maps: bool, mangle: bool) -> Result<Code> {
let source_maps = source_maps
.await?
.then(|| code.generate_source_map_ref())
.transpose()?;

Expand Down Expand Up @@ -121,7 +111,7 @@ pub async fn minify(
src_map_buf.shrink_to_fit();
builder.push_source(
&src.into(),
Some(generate_js_source_map(cm, src_map_buf, Some(original_map)).await?),
Some(generate_js_source_map(cm, src_map_buf, Some(original_map))?),
);

write!(
Expand All @@ -134,7 +124,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
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-ecmascript/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl PartialEq for ParseResult {
}
}

pub async fn generate_js_source_map(
pub fn generate_js_source_map(
files_map: Arc<swc_core::common::SourceMap>,
mappings: Vec<(BytePos, LineCol)>,
original_source_map: Option<&Rope>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ impl EcmascriptChunkItem for EcmascriptModuleLocalsChunkItem {
.module_options(async_module_info);

let module_type_result = *original_module.determine_module_type().await?;
let generate_source_map =
chunking_context.reference_module_source_maps(*ResolvedVc::upcast(self.module));
let generate_source_map = *chunking_context
.reference_module_source_maps(*ResolvedVc::upcast(self.module))
.await?;

let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions {
parsed,
Expand Down
6 changes: 3 additions & 3 deletions turbopack/crates/turbopack-ecmascript/src/static_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::EcmascriptAnalyzable;
pub struct StaticEcmascriptCode {
asset_context: ResolvedVc<Box<dyn AssetContext>>,
asset: ResolvedVc<Box<dyn EcmascriptAnalyzable>>,
generate_source_map: ResolvedVc<bool>,
generate_source_map: bool,
}

#[turbo_tasks::value_impl]
Expand All @@ -28,7 +28,7 @@ impl StaticEcmascriptCode {
pub async fn new(
asset_context: ResolvedVc<Box<dyn AssetContext>>,
asset_path: ResolvedVc<FileSystemPath>,
generate_source_map: ResolvedVc<bool>,
generate_source_map: bool,
) -> Result<Vc<Self>> {
let module = asset_context
.process(
Expand All @@ -54,7 +54,7 @@ impl StaticEcmascriptCode {
pub async fn code(&self) -> Result<Vc<Code>> {
let runtime_base_content = self
.asset
.module_content_without_analysis(*self.generate_source_map)
.module_content_without_analysis(self.generate_source_map)
.await?;
let mut code = CodeBuilder::default();
code.push_source(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset {
#[turbo_tasks::function]
fn module_content_without_analysis(
&self,
generate_source_map: Vc<bool>,
generate_source_map: bool,
) -> Vc<EcmascriptModuleContent> {
self.full_module
.module_content_without_analysis(generate_source_map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ impl EcmascriptChunkItem for EcmascriptModulePartChunkItem {
let async_module_options = analyze_ref.async_module.module_options(async_module_info);

let module_type_result = *module.full_module.determine_module_type().await?;
let generate_source_map = self
let generate_source_map = *self
.chunking_context
.reference_module_source_maps(*ResolvedVc::upcast(self.module));
.reference_module_source_maps(*ResolvedVc::upcast(self.module))
.await?;

let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions {
parsed,
Expand Down
13 changes: 7 additions & 6 deletions turbopack/crates/turbopack-nodejs/src/ecmascript/node/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ impl EcmascriptBuildNodeChunkContent {
async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
use std::io::Write;
let this = self.await?;
let source_maps = this
let source_maps = *this
.chunking_context
.reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk));
.reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk))
.await?;
let chunk_path_vc = this.chunk.path();
let chunk_path = chunk_path_vc.await?;

Expand All @@ -73,7 +74,7 @@ impl EcmascriptBuildNodeChunkContent {

write!(code, "\n}};")?;

if *source_maps.await? && code.has_source_map() {
if source_maps && code.has_source_map() {
let filename = chunk_path.file_name();
write!(
code,
Expand All @@ -82,13 +83,13 @@ impl EcmascriptBuildNodeChunkContent {
)?;
}

let code = code.build().cell();
let mut code = code.build();

if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
return Ok(minify(chunk_path_vc, code, source_maps, mangle));
code = minify(&*chunk_path_vc.await?, &code, source_maps, mangle)?;
}

Ok(code)
Ok(code.cell())
}

#[turbo_tasks::function]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ impl EcmascriptBuildNodeRuntimeChunk {

let output_root_to_root_path = this.chunking_context.output_root_to_root_path().await?;
let output_root = this.chunking_context.output_root().await?;
let generate_source_map = this
let generate_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self));
.reference_chunk_source_maps(Vc::upcast(self))
.await?;
let runtime_path = self.path().await?;
let runtime_public_path = if let Some(path) = output_root.get_path_to(&runtime_path) {
path
Expand Down

0 comments on commit 4b1223a

Please sign in to comment.