Skip to content

Commit 514e9b3

Browse files
authored
Turbopack: inline minify into code generation and make it a plain function instead of a turbo tasks function (#76628)
### What? This avoids celling the input code for minification and passes is directly to magnification
1 parent b52716b commit 514e9b3

File tree

15 files changed

+60
-70
lines changed

15 files changed

+60
-70
lines changed

turbopack/crates/turbopack-browser/src/ecmascript/content.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ impl EcmascriptBrowserChunkContent {
6565
async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
6666
let this = self.await?;
6767
let output_root = this.chunking_context.output_root().await?;
68-
let source_maps = this
68+
let source_maps = *this
6969
.chunking_context
70-
.reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk));
70+
.reference_chunk_source_maps(*ResolvedVc::upcast(this.chunk))
71+
.await?;
7172
let chunk_path_vc = this.chunk.path();
7273
let chunk_path = chunk_path_vc.await?;
7374
let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
@@ -108,7 +109,7 @@ impl EcmascriptBrowserChunkContent {
108109

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

111-
if *source_maps.await? && code.has_source_map() {
112+
if source_maps && code.has_source_map() {
112113
let filename = chunk_path.file_name();
113114
write!(
114115
code,
@@ -119,13 +120,13 @@ impl EcmascriptBrowserChunkContent {
119120
)?;
120121
}
121122

122-
let code = code.build().cell();
123+
let mut code = code.build();
123124

124125
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
125-
return Ok(minify(chunk_path_vc, code, source_maps, mangle));
126+
code = minify(&*chunk_path_vc.await?, &code, source_maps, mangle)?;
126127
}
127128

128-
Ok(code)
129+
Ok(code.cell())
129130
}
130131
}
131132

turbopack/crates/turbopack-browser/src/ecmascript/evaluate/chunk.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ impl EcmascriptBrowserEvaluateChunk {
7676

7777
let output_root = this.chunking_context.output_root().await?;
7878
let output_root_to_root_path = this.chunking_context.output_root_to_root_path();
79-
let source_maps = this
79+
let source_maps = *this
8080
.chunking_context
81-
.reference_chunk_source_maps(Vc::upcast(self));
81+
.reference_chunk_source_maps(Vc::upcast(self))
82+
.await?;
8283
let chunk_path_vc = self.path();
8384
let chunk_path = chunk_path_vc.await?;
8485
let chunk_public_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
@@ -175,7 +176,7 @@ impl EcmascriptBrowserEvaluateChunk {
175176
}
176177
}
177178

178-
if *source_maps.await? && code.has_source_map() {
179+
if source_maps && code.has_source_map() {
179180
let filename = chunk_path.file_name();
180181
write!(
181182
code,
@@ -186,13 +187,13 @@ impl EcmascriptBrowserEvaluateChunk {
186187
)?;
187188
}
188189

189-
let code = code.build().cell();
190+
let mut code = code.build();
190191

191192
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
192-
return Ok(minify(chunk_path_vc, code, source_maps, mangle));
193+
code = minify(&*chunk_path_vc.await?, &code, source_maps, mangle)?;
193194
}
194195

195-
Ok(code)
196+
Ok(code.cell())
196197
}
197198
}
198199

turbopack/crates/turbopack-css/src/module_asset.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,10 @@ impl EcmascriptChunkItem for ModuleChunkItem {
403403
// We generate a minimal map for runtime code so that the filename is
404404
// displayed in dev tools.
405405
source_map: if source_map {
406-
Some(
407-
generate_minimal_source_map(
408-
self.module.ident().to_string().await?.to_string(),
409-
code,
410-
)
411-
.await?,
412-
)
406+
Some(generate_minimal_source_map(
407+
self.module.ident().to_string().await?.to_string(),
408+
code,
409+
)?)
413410
} else {
414411
None
415412
},
@@ -419,7 +416,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
419416
}
420417
}
421418

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

turbopack/crates/turbopack-ecmascript-runtime/src/browser_runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn get_browser_runtime_code(
2020
chunk_base_path: Vc<Option<RcStr>>,
2121
runtime_type: Value<RuntimeType>,
2222
output_root_to_root_path: Vc<RcStr>,
23-
generate_source_map: Vc<bool>,
23+
generate_source_map: bool,
2424
) -> Result<Vc<Code>> {
2525
let asset_context = get_runtime_asset_context(environment).await?;
2626

turbopack/crates/turbopack-ecmascript-runtime/src/embed_js.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn embed_file_path(path: RcStr) -> Vc<FileSystemPath> {
2323
pub fn embed_static_code(
2424
asset_context: Vc<Box<dyn AssetContext>>,
2525
path: RcStr,
26-
generate_source_map: Vc<bool>,
26+
generate_source_map: bool,
2727
) -> Vc<Code> {
2828
StaticEcmascriptCode::new(asset_context, embed_file_path(path), generate_source_map).code()
2929
}

turbopack/crates/turbopack-ecmascript-runtime/src/nodejs_runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod
1111
#[turbo_tasks::function]
1212
pub async fn get_nodejs_runtime_code(
1313
environment: Vc<Environment>,
14-
generate_source_map: Vc<bool>,
14+
generate_source_map: bool,
1515
) -> Result<Vc<Code>> {
1616
let asset_context = get_runtime_asset_context(environment).await?;
1717

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub trait EcmascriptAnalyzable {
292292
/// transforming code that is not a module, e.g. runtime code.
293293
async fn module_content_without_analysis(
294294
self: Vc<Self>,
295-
generate_source_map: Vc<bool>,
295+
generate_source_map: bool,
296296
) -> Result<Vc<EcmascriptModuleContent>>;
297297

298298
async fn module_content(
@@ -396,7 +396,7 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset {
396396
#[turbo_tasks::function]
397397
async fn module_content_without_analysis(
398398
self: Vc<Self>,
399-
generate_source_map: Vc<bool>,
399+
generate_source_map: bool,
400400
) -> Result<Vc<EcmascriptModuleContent>> {
401401
let this = self.await?;
402402

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

425425
let module_type_result = *self.determine_module_type().await?;
426-
let generate_source_map = chunking_context.reference_module_source_maps(Vc::upcast(self));
426+
let generate_source_map = *chunking_context
427+
.reference_module_source_maps(Vc::upcast(self))
428+
.await?;
427429

428430
Ok(EcmascriptModuleContent::new(
429431
EcmascriptModuleContentOptions {
@@ -766,7 +768,7 @@ pub struct EcmascriptModuleContentOptions {
766768
esm_references: Vc<EsmAssetReferences>,
767769
code_generation: Vc<CodeGens>,
768770
async_module: Vc<OptionAsyncModule>,
769-
generate_source_map: Vc<bool>,
771+
generate_source_map: bool,
770772
original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
771773
exports: Vc<EcmascriptExports>,
772774
async_module_info: Option<Vc<AsyncModuleInfo>>,
@@ -856,7 +858,7 @@ impl EcmascriptModuleContent {
856858
parsed: Vc<ParseResult>,
857859
ident: Vc<AssetIdent>,
858860
specified_module_type: SpecifiedModuleType,
859-
generate_source_map: Vc<bool>,
861+
generate_source_map: bool,
860862
) -> Result<Vc<Self>> {
861863
gen_content_with_code_gens(
862864
parsed.to_resolved().await?,
@@ -875,7 +877,7 @@ async fn gen_content_with_code_gens(
875877
ident: Vc<AssetIdent>,
876878
specified_module_type: SpecifiedModuleType,
877879
code_gens: impl IntoIterator<Item = &CodeGeneration>,
878-
generate_source_map: Vc<bool>,
880+
generate_source_map: bool,
879881
original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
880882
) -> Result<Vc<EcmascriptModuleContent>> {
881883
let parsed = parsed.final_read_hint().await?;
@@ -937,8 +939,6 @@ async fn gen_content_with_code_gens(
937939

938940
let mut mappings = vec![];
939941

940-
let generate_source_map = *generate_source_map.await?;
941-
942942
{
943943
let comments = match comments {
944944
Either::Left(comments) => Either::Left(comments.into_consumable()),
@@ -965,14 +965,11 @@ async fn gen_content_with_code_gens(
965965
}
966966

967967
let source_map = if generate_source_map {
968-
Some(
969-
generate_js_source_map(
970-
source_map.clone(),
971-
mappings,
972-
original_source_map.await?.as_ref(),
973-
)
974-
.await?,
975-
)
968+
Some(generate_js_source_map(
969+
source_map.clone(),
970+
mappings,
971+
original_source_map.await?.as_ref(),
972+
)?)
976973
} else {
977974
None
978975
};

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,13 @@ use swc_core::{
1919
transforms::base::fixer::paren_remover,
2020
},
2121
};
22-
use turbo_tasks::Vc;
2322
use turbo_tasks_fs::FileSystemPath;
2423
use turbopack_core::code_builder::{Code, CodeBuilder};
2524

2625
use crate::parse::generate_js_source_map;
2726

28-
#[turbo_tasks::function]
29-
pub async fn minify(
30-
path: Vc<FileSystemPath>,
31-
code: Vc<Code>,
32-
source_maps: Vc<bool>,
33-
mangle: bool,
34-
) -> Result<Vc<Code>> {
35-
let path = path.await?;
36-
let code = code.await?;
27+
pub fn minify(path: &FileSystemPath, code: &Code, source_maps: bool, mangle: bool) -> Result<Code> {
3728
let source_maps = source_maps
38-
.await?
3929
.then(|| code.generate_source_map_ref())
4030
.transpose()?;
4131

@@ -121,7 +111,7 @@ pub async fn minify(
121111
src_map_buf.shrink_to_fit();
122112
builder.push_source(
123113
&src.into(),
124-
Some(generate_js_source_map(cm, src_map_buf, Some(original_map)).await?),
114+
Some(generate_js_source_map(cm, src_map_buf, Some(original_map))?),
125115
);
126116

127117
write!(
@@ -134,7 +124,7 @@ pub async fn minify(
134124
} else {
135125
builder.push_source(&src.into(), None);
136126
}
137-
Ok(builder.build().cell())
127+
Ok(builder.build())
138128
}
139129

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl PartialEq for ParseResult {
7474
}
7575
}
7676

77-
pub async fn generate_js_source_map(
77+
pub fn generate_js_source_map(
7878
files_map: Arc<swc_core::common::SourceMap>,
7979
mappings: Vec<(BytePos, LineCol)>,
8080
original_source_map: Option<&Rope>,

turbopack/crates/turbopack-ecmascript/src/side_effect_optimization/locals/chunk_item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ impl EcmascriptChunkItem for EcmascriptModuleLocalsChunkItem {
5050
.module_options(async_module_info);
5151

5252
let module_type_result = *original_module.determine_module_type().await?;
53-
let generate_source_map =
54-
chunking_context.reference_module_source_maps(*ResolvedVc::upcast(self.module));
53+
let generate_source_map = *chunking_context
54+
.reference_module_source_maps(*ResolvedVc::upcast(self.module))
55+
.await?;
5556

5657
let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions {
5758
parsed,

0 commit comments

Comments
 (0)