Unverified Commit 514e9b35 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: inline minify into code generation and make it a plain function…

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
parent b52716be
...@@ -65,9 +65,10 @@ impl EcmascriptBrowserChunkContent { ...@@ -65,9 +65,10 @@ impl EcmascriptBrowserChunkContent {
async fn code(self: Vc<Self>) -> Result<Vc<Code>> { async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
let this = self.await?; let this = self.await?;
let output_root = this.chunking_context.output_root().await?; let output_root = this.chunking_context.output_root().await?;
let source_maps = this let source_maps = *this
.chunking_context .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_vc = this.chunk.path();
let chunk_path = chunk_path_vc.await?; let chunk_path = chunk_path_vc.await?;
let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
...@@ -108,7 +109,7 @@ impl EcmascriptBrowserChunkContent { ...@@ -108,7 +109,7 @@ impl EcmascriptBrowserChunkContent {
write!(code, "\n}}]);")?; write!(code, "\n}}]);")?;
if *source_maps.await? && code.has_source_map() { if source_maps && code.has_source_map() {
let filename = chunk_path.file_name(); let filename = chunk_path.file_name();
write!( write!(
code, code,
...@@ -119,13 +120,13 @@ impl EcmascriptBrowserChunkContent { ...@@ -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() { 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())
} }
} }
......
...@@ -76,9 +76,10 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -76,9 +76,10 @@ impl EcmascriptBrowserEvaluateChunk {
let output_root = this.chunking_context.output_root().await?; let output_root = this.chunking_context.output_root().await?;
let output_root_to_root_path = this.chunking_context.output_root_to_root_path(); let output_root_to_root_path = this.chunking_context.output_root_to_root_path();
let source_maps = this let source_maps = *this
.chunking_context .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_vc = self.path();
let chunk_path = chunk_path_vc.await?; let chunk_path = chunk_path_vc.await?;
let chunk_public_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_public_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
...@@ -175,7 +176,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -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(); let filename = chunk_path.file_name();
write!( write!(
code, code,
...@@ -186,13 +187,13 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -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() { 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())
} }
} }
......
...@@ -403,13 +403,10 @@ impl EcmascriptChunkItem for ModuleChunkItem { ...@@ -403,13 +403,10 @@ impl EcmascriptChunkItem for ModuleChunkItem {
// We generate a minimal map for runtime code so that the filename is // We generate a minimal map for runtime code so that the filename is
// displayed in dev tools. // displayed in dev tools.
source_map: if source_map { source_map: if source_map {
Some( Some(generate_minimal_source_map(
generate_minimal_source_map( self.module.ident().to_string().await?.to_string(),
self.module.ident().to_string().await?.to_string(), code,
code, )?)
)
.await?,
)
} else { } else {
None None
}, },
...@@ -419,7 +416,7 @@ impl EcmascriptChunkItem for ModuleChunkItem { ...@@ -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![]; let mut mappings = vec![];
// Start from 1 because 0 is reserved for dummy spans in SWC. // Start from 1 because 0 is reserved for dummy spans in SWC.
let mut pos = 1; let mut pos = 1;
...@@ -435,7 +432,7 @@ async fn generate_minimal_source_map(filename: String, source: String) -> Result ...@@ -435,7 +432,7 @@ async fn generate_minimal_source_map(filename: String, source: String) -> Result
} }
let sm: Arc<SourceMap> = Default::default(); let sm: Arc<SourceMap> = Default::default();
sm.new_source_file(FileName::Custom(filename).into(), source); 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) Ok(map)
} }
......
...@@ -20,7 +20,7 @@ pub async fn get_browser_runtime_code( ...@@ -20,7 +20,7 @@ pub async fn get_browser_runtime_code(
chunk_base_path: Vc<Option<RcStr>>, chunk_base_path: Vc<Option<RcStr>>,
runtime_type: Value<RuntimeType>, runtime_type: Value<RuntimeType>,
output_root_to_root_path: Vc<RcStr>, output_root_to_root_path: Vc<RcStr>,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(environment).await?; let asset_context = get_runtime_asset_context(environment).await?;
......
...@@ -23,7 +23,7 @@ pub fn embed_file_path(path: RcStr) -> Vc<FileSystemPath> { ...@@ -23,7 +23,7 @@ pub fn embed_file_path(path: RcStr) -> Vc<FileSystemPath> {
pub fn embed_static_code( pub fn embed_static_code(
asset_context: Vc<Box<dyn AssetContext>>, asset_context: Vc<Box<dyn AssetContext>>,
path: RcStr, path: RcStr,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Vc<Code> { ) -> Vc<Code> {
StaticEcmascriptCode::new(asset_context, embed_file_path(path), generate_source_map).code() StaticEcmascriptCode::new(asset_context, embed_file_path(path), generate_source_map).code()
} }
...@@ -11,7 +11,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod ...@@ -11,7 +11,7 @@ use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_cod
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_nodejs_runtime_code( pub async fn get_nodejs_runtime_code(
environment: Vc<Environment>, environment: Vc<Environment>,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(environment).await?; let asset_context = get_runtime_asset_context(environment).await?;
......
...@@ -292,7 +292,7 @@ pub trait EcmascriptAnalyzable { ...@@ -292,7 +292,7 @@ pub trait EcmascriptAnalyzable {
/// transforming code that is not a module, e.g. runtime code. /// transforming code that is not a module, e.g. runtime code.
async fn module_content_without_analysis( async fn module_content_without_analysis(
self: Vc<Self>, self: Vc<Self>,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Result<Vc<EcmascriptModuleContent>>; ) -> Result<Vc<EcmascriptModuleContent>>;
async fn module_content( async fn module_content(
...@@ -396,7 +396,7 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset { ...@@ -396,7 +396,7 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn module_content_without_analysis( async fn module_content_without_analysis(
self: Vc<Self>, self: Vc<Self>,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Result<Vc<EcmascriptModuleContent>> { ) -> Result<Vc<EcmascriptModuleContent>> {
let this = self.await?; let this = self.await?;
...@@ -423,7 +423,9 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset { ...@@ -423,7 +423,9 @@ impl EcmascriptAnalyzable for EcmascriptModuleAsset {
let analyze_ref = analyze.await?; let analyze_ref = analyze.await?;
let module_type_result = *self.determine_module_type().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( Ok(EcmascriptModuleContent::new(
EcmascriptModuleContentOptions { EcmascriptModuleContentOptions {
...@@ -766,7 +768,7 @@ pub struct EcmascriptModuleContentOptions { ...@@ -766,7 +768,7 @@ pub struct EcmascriptModuleContentOptions {
esm_references: Vc<EsmAssetReferences>, esm_references: Vc<EsmAssetReferences>,
code_generation: Vc<CodeGens>, code_generation: Vc<CodeGens>,
async_module: Vc<OptionAsyncModule>, async_module: Vc<OptionAsyncModule>,
generate_source_map: Vc<bool>, generate_source_map: bool,
original_source_map: ResolvedVc<OptionStringifiedSourceMap>, original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
exports: Vc<EcmascriptExports>, exports: Vc<EcmascriptExports>,
async_module_info: Option<Vc<AsyncModuleInfo>>, async_module_info: Option<Vc<AsyncModuleInfo>>,
...@@ -856,7 +858,7 @@ impl EcmascriptModuleContent { ...@@ -856,7 +858,7 @@ impl EcmascriptModuleContent {
parsed: Vc<ParseResult>, parsed: Vc<ParseResult>,
ident: Vc<AssetIdent>, ident: Vc<AssetIdent>,
specified_module_type: SpecifiedModuleType, specified_module_type: SpecifiedModuleType,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Result<Vc<Self>> { ) -> Result<Vc<Self>> {
gen_content_with_code_gens( gen_content_with_code_gens(
parsed.to_resolved().await?, parsed.to_resolved().await?,
...@@ -875,7 +877,7 @@ async fn gen_content_with_code_gens( ...@@ -875,7 +877,7 @@ async fn gen_content_with_code_gens(
ident: Vc<AssetIdent>, ident: Vc<AssetIdent>,
specified_module_type: SpecifiedModuleType, specified_module_type: SpecifiedModuleType,
code_gens: impl IntoIterator<Item = &CodeGeneration>, code_gens: impl IntoIterator<Item = &CodeGeneration>,
generate_source_map: Vc<bool>, generate_source_map: bool,
original_source_map: ResolvedVc<OptionStringifiedSourceMap>, original_source_map: ResolvedVc<OptionStringifiedSourceMap>,
) -> Result<Vc<EcmascriptModuleContent>> { ) -> Result<Vc<EcmascriptModuleContent>> {
let parsed = parsed.final_read_hint().await?; let parsed = parsed.final_read_hint().await?;
...@@ -937,8 +939,6 @@ async fn gen_content_with_code_gens( ...@@ -937,8 +939,6 @@ async fn gen_content_with_code_gens(
let mut mappings = vec![]; let mut mappings = vec![];
let generate_source_map = *generate_source_map.await?;
{ {
let comments = match comments { let comments = match comments {
Either::Left(comments) => Either::Left(comments.into_consumable()), Either::Left(comments) => Either::Left(comments.into_consumable()),
...@@ -965,14 +965,11 @@ async fn gen_content_with_code_gens( ...@@ -965,14 +965,11 @@ async fn gen_content_with_code_gens(
} }
let source_map = if generate_source_map { let source_map = if generate_source_map {
Some( Some(generate_js_source_map(
generate_js_source_map( source_map.clone(),
source_map.clone(), mappings,
mappings, original_source_map.await?.as_ref(),
original_source_map.await?.as_ref(), )?)
)
.await?,
)
} else { } else {
None None
}; };
......
...@@ -19,23 +19,13 @@ use swc_core::{ ...@@ -19,23 +19,13 @@ use swc_core::{
transforms::base::fixer::paren_remover, transforms::base::fixer::paren_remover,
}, },
}; };
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::code_builder::{Code, CodeBuilder}; use turbopack_core::code_builder::{Code, CodeBuilder};
use crate::parse::generate_js_source_map; use crate::parse::generate_js_source_map;
#[turbo_tasks::function] pub fn minify(path: &FileSystemPath, code: &Code, source_maps: bool, mangle: bool) -> Result<Code> {
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?;
let source_maps = source_maps let source_maps = source_maps
.await?
.then(|| code.generate_source_map_ref()) .then(|| code.generate_source_map_ref())
.transpose()?; .transpose()?;
...@@ -121,7 +111,7 @@ pub async fn minify( ...@@ -121,7 +111,7 @@ pub async fn minify(
src_map_buf.shrink_to_fit(); src_map_buf.shrink_to_fit();
builder.push_source( builder.push_source(
&src.into(), &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!( write!(
...@@ -134,7 +124,7 @@ pub async fn minify( ...@@ -134,7 +124,7 @@ pub async fn minify(
} else { } else {
builder.push_source(&src.into(), None); 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 // From https://github.com/swc-project/swc/blob/11efd4e7c5e8081f8af141099d3459c3534c1e1d/crates/swc/src/lib.rs#L523-L560
......
...@@ -74,7 +74,7 @@ impl PartialEq for ParseResult { ...@@ -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>, files_map: Arc<swc_core::common::SourceMap>,
mappings: Vec<(BytePos, LineCol)>, mappings: Vec<(BytePos, LineCol)>,
original_source_map: Option<&Rope>, original_source_map: Option<&Rope>,
......
...@@ -50,8 +50,9 @@ impl EcmascriptChunkItem for EcmascriptModuleLocalsChunkItem { ...@@ -50,8 +50,9 @@ impl EcmascriptChunkItem for EcmascriptModuleLocalsChunkItem {
.module_options(async_module_info); .module_options(async_module_info);
let module_type_result = *original_module.determine_module_type().await?; let module_type_result = *original_module.determine_module_type().await?;
let generate_source_map = let generate_source_map = *chunking_context
chunking_context.reference_module_source_maps(*ResolvedVc::upcast(self.module)); .reference_module_source_maps(*ResolvedVc::upcast(self.module))
.await?;
let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions { let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions {
parsed, parsed,
......
...@@ -18,7 +18,7 @@ use crate::EcmascriptAnalyzable; ...@@ -18,7 +18,7 @@ use crate::EcmascriptAnalyzable;
pub struct StaticEcmascriptCode { pub struct StaticEcmascriptCode {
asset_context: ResolvedVc<Box<dyn AssetContext>>, asset_context: ResolvedVc<Box<dyn AssetContext>>,
asset: ResolvedVc<Box<dyn EcmascriptAnalyzable>>, asset: ResolvedVc<Box<dyn EcmascriptAnalyzable>>,
generate_source_map: ResolvedVc<bool>, generate_source_map: bool,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -28,7 +28,7 @@ impl StaticEcmascriptCode { ...@@ -28,7 +28,7 @@ impl StaticEcmascriptCode {
pub async fn new( pub async fn new(
asset_context: ResolvedVc<Box<dyn AssetContext>>, asset_context: ResolvedVc<Box<dyn AssetContext>>,
asset_path: ResolvedVc<FileSystemPath>, asset_path: ResolvedVc<FileSystemPath>,
generate_source_map: ResolvedVc<bool>, generate_source_map: bool,
) -> Result<Vc<Self>> { ) -> Result<Vc<Self>> {
let module = asset_context let module = asset_context
.process( .process(
...@@ -54,7 +54,7 @@ impl StaticEcmascriptCode { ...@@ -54,7 +54,7 @@ impl StaticEcmascriptCode {
pub async fn code(&self) -> Result<Vc<Code>> { pub async fn code(&self) -> Result<Vc<Code>> {
let runtime_base_content = self let runtime_base_content = self
.asset .asset
.module_content_without_analysis(*self.generate_source_map) .module_content_without_analysis(self.generate_source_map)
.await?; .await?;
let mut code = CodeBuilder::default(); let mut code = CodeBuilder::default();
code.push_source( code.push_source(
......
...@@ -67,7 +67,7 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset { ...@@ -67,7 +67,7 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
fn module_content_without_analysis( fn module_content_without_analysis(
&self, &self,
generate_source_map: Vc<bool>, generate_source_map: bool,
) -> Vc<EcmascriptModuleContent> { ) -> Vc<EcmascriptModuleContent> {
self.full_module self.full_module
.module_content_without_analysis(generate_source_map) .module_content_without_analysis(generate_source_map)
......
...@@ -56,9 +56,10 @@ impl EcmascriptChunkItem for EcmascriptModulePartChunkItem { ...@@ -56,9 +56,10 @@ impl EcmascriptChunkItem for EcmascriptModulePartChunkItem {
let async_module_options = analyze_ref.async_module.module_options(async_module_info); 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 module_type_result = *module.full_module.determine_module_type().await?;
let generate_source_map = self let generate_source_map = *self
.chunking_context .chunking_context
.reference_module_source_maps(*ResolvedVc::upcast(self.module)); .reference_module_source_maps(*ResolvedVc::upcast(self.module))
.await?;
let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions { let content = EcmascriptModuleContent::new(EcmascriptModuleContentOptions {
parsed, parsed,
......
...@@ -45,9 +45,10 @@ impl EcmascriptBuildNodeChunkContent { ...@@ -45,9 +45,10 @@ impl EcmascriptBuildNodeChunkContent {
async fn code(self: Vc<Self>) -> Result<Vc<Code>> { async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
use std::io::Write; use std::io::Write;
let this = self.await?; let this = self.await?;
let source_maps = this let source_maps = *this
.chunking_context .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_vc = this.chunk.path();
let chunk_path = chunk_path_vc.await?; let chunk_path = chunk_path_vc.await?;
...@@ -73,7 +74,7 @@ impl EcmascriptBuildNodeChunkContent { ...@@ -73,7 +74,7 @@ impl EcmascriptBuildNodeChunkContent {
write!(code, "\n}};")?; write!(code, "\n}};")?;
if *source_maps.await? && code.has_source_map() { if source_maps && code.has_source_map() {
let filename = chunk_path.file_name(); let filename = chunk_path.file_name();
write!( write!(
code, code,
...@@ -82,13 +83,13 @@ impl EcmascriptBuildNodeChunkContent { ...@@ -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() { 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] #[turbo_tasks::function]
......
...@@ -38,9 +38,10 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -38,9 +38,10 @@ impl EcmascriptBuildNodeRuntimeChunk {
let output_root_to_root_path = this.chunking_context.output_root_to_root_path().await?; 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 output_root = this.chunking_context.output_root().await?;
let generate_source_map = this let generate_source_map = *this
.chunking_context .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_path = self.path().await?;
let runtime_public_path = if let Some(path) = output_root.get_path_to(&runtime_path) { let runtime_public_path = if let Some(path) = output_root.get_path_to(&runtime_path) {
path path
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment