Skip to content

Commit

Permalink
refactor(turbopack-core): Change OutputAssets type to use ResolvedVc (
Browse files Browse the repository at this point in the history
vercel#72041)

A hand refactor of the `OutputAssets` type in `turbopack-core` to use `ResolvedVc`. This is one of the more common types passed around turbopack, so this touches a lot of code.

Closes PACK-3358
  • Loading branch information
bgw authored and stipsan committed Nov 6, 2024
1 parent 144b9f7 commit abac25a
Show file tree
Hide file tree
Showing 66 changed files with 571 additions and 439 deletions.
90 changes: 56 additions & 34 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,15 +994,17 @@ impl AppEndpoint {
.collect(),
};
let manifest_path_prefix = &app_entry.original_name;
let app_build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
let app_build_manifest_output = VirtualOutputAsset::new(
node_root.join(
format!("server/app{manifest_path_prefix}/app-build-manifest.json",).into(),
),
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_build_manifest)?).into(),
),
));
server_assets.insert(app_build_manifest_output);
)
.to_resolved()
.await?;
server_assets.insert(ResolvedVc::upcast(app_build_manifest_output));

// polyfill-nomodule.js is a pre-compiled asset distributed as part of next,
// load it as a RawModule.
Expand All @@ -1013,13 +1015,15 @@ impl AppEndpoint {
let polyfill_output_path =
client_chunking_context.chunk_path(polyfill_source.ident(), ".js".into());
let polyfill_output_asset =
RawOutput::new(polyfill_output_path, Vc::upcast(polyfill_source));
RawOutput::new(polyfill_output_path, Vc::upcast(polyfill_source))
.to_resolved()
.await?;
let polyfill_client_path = client_relative_path_ref
.get_path_to(&*polyfill_output_path.await?)
.context("failed to resolve client-relative path to polyfill")?
.into();
let polyfill_client_paths = vec![polyfill_client_path];
client_assets.insert(Vc::upcast(polyfill_output_asset));
client_assets.insert(ResolvedVc::upcast(polyfill_output_asset));

if *this
.app_project
Expand All @@ -1029,30 +1033,34 @@ impl AppEndpoint {
{
let webpack_stats =
generate_webpack_stats(app_entry.original_name.clone(), &client_assets).await?;
let stats_output: Vc<Box<dyn OutputAsset>> = Vc::upcast(VirtualOutputAsset::new(
let stats_output = VirtualOutputAsset::new(
node_root.join(
format!("server/app{manifest_path_prefix}/webpack-stats.json",).into(),
),
AssetContent::file(
File::from(serde_json::to_string_pretty(&webpack_stats)?).into(),
),
));
server_assets.insert(Vc::upcast(stats_output));
)
.to_resolved()
.await?;
server_assets.insert(ResolvedVc::upcast(stats_output));
}

let build_manifest = BuildManifest {
root_main_files: client_shared_chunks_paths,
polyfill_files: polyfill_client_paths,
..Default::default()
};
let build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
let build_manifest_output = VirtualOutputAsset::new(
node_root
.join(format!("server/app{manifest_path_prefix}/build-manifest.json",).into()),
AssetContent::file(
File::from(serde_json::to_string_pretty(&build_manifest)?).into(),
),
));
server_assets.insert(build_manifest_output);
)
.to_resolved()
.await?;
server_assets.insert(ResolvedVc::upcast(build_manifest_output));

if runtime == NextRuntime::Edge {
// as the edge runtime doesn't support chunk loading we need to add all client
Expand Down Expand Up @@ -1132,14 +1140,16 @@ impl AppEndpoint {
ssr_chunking_context,
this.app_project.project().next_config(),
runtime,
);
)
.to_resolved()
.await?;
server_assets.insert(entry_manifest);
if runtime == NextRuntime::Edge {
middleware_assets.push(entry_manifest);
}
}

let client_assets = OutputAssets::new(client_assets.iter().cloned().collect::<Vec<_>>());
let client_assets = OutputAssets::new(client_assets.iter().map(|asset| **asset).collect());

let next_font_manifest_output = create_font_manifest(
this.app_project.project().client_root(),
Expand Down Expand Up @@ -1219,23 +1229,28 @@ impl AppEndpoint {
..Default::default()
};
let manifest_path_prefix = &app_entry.original_name;
let middleware_manifest_v2 = Vc::upcast(VirtualOutputAsset::new(
node_root.join(
format!("server/app{manifest_path_prefix}/middleware-manifest.json",)
.into(),
),
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&middleware_manifest_v2,
)?))
.cell(),
),
));
let middleware_manifest_v2 = ResolvedVc::upcast(
VirtualOutputAsset::new(
node_root.join(
format!("server/app{manifest_path_prefix}/middleware-manifest.json",)
.into(),
),
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&middleware_manifest_v2,
)?))
.cell(),
),
)
.to_resolved()
.await?,
);
server_assets.insert(middleware_manifest_v2);

// create app paths manifest
let app_paths_manifest_output =
create_app_paths_manifest(node_root, &app_entry.original_name, entry_file)?;
create_app_paths_manifest(node_root, &app_entry.original_name, entry_file)
.await?;
server_assets.insert(app_paths_manifest_output);

// create react-loadable-manifest for next/dynamic
Expand Down Expand Up @@ -1284,7 +1299,8 @@ impl AppEndpoint {
.get_path_to(&*rsc_chunk.ident().path().await?)
.context("RSC chunk path should be within app paths manifest directory")?
.into(),
)?;
)
.await?;
server_assets.insert(app_paths_manifest_output);

// create react-loadable-manifest for next/dynamic
Expand Down Expand Up @@ -1476,11 +1492,11 @@ impl AppEndpoint {
}
}

fn create_app_paths_manifest(
async fn create_app_paths_manifest(
node_root: Vc<FileSystemPath>,
original_name: &str,
filename: RcStr,
) -> Result<Vc<Box<dyn OutputAsset>>> {
) -> Result<ResolvedVc<Box<dyn OutputAsset>>> {
let manifest_path_prefix = original_name;
let path =
node_root.join(format!("server/app{manifest_path_prefix}/app-paths-manifest.json",).into());
Expand All @@ -1490,10 +1506,16 @@ fn create_app_paths_manifest(
},
..Default::default()
};
Ok(Vc::upcast(VirtualOutputAsset::new(
path,
AssetContent::file(File::from(serde_json::to_string_pretty(&app_paths_manifest)?).into()),
)))
Ok(ResolvedVc::upcast(
VirtualOutputAsset::new(
path,
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_paths_manifest)?).into(),
),
)
.to_resolved()
.await?,
))
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -1596,7 +1618,7 @@ impl Endpoint for AppEndpoint {
#[turbo_tasks::value]
enum AppEndpointOutput {
NodeJs {
rsc_chunk: Vc<Box<dyn OutputAsset>>,
rsc_chunk: ResolvedVc<Box<dyn OutputAsset>>,
server_assets: Vc<OutputAssets>,
client_assets: Vc<OutputAssets>,
},
Expand Down
18 changes: 12 additions & 6 deletions crates/next-api/src/font.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use next_core::{all_assets_from_entries, next_manifests::NextFontManifest};
use turbo_tasks::{RcStr, ValueToString, Vc};
use turbo_tasks::{RcStr, ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
Expand All @@ -19,7 +19,7 @@ pub(crate) async fn create_font_manifest(
pathname: &str,
client_assets: Vc<OutputAssets>,
app_dir: bool,
) -> Result<Vc<Box<dyn OutputAsset>>> {
) -> Result<ResolvedVc<Box<dyn OutputAsset>>> {
let all_client_output_assets = all_assets_from_entries(client_assets).await?;

// `_next` gets added again later, so we "strip" it here via
Expand Down Expand Up @@ -65,8 +65,14 @@ pub(crate) async fn create_font_manifest(
}
};

Ok(Vc::upcast(VirtualOutputAsset::new(
path,
AssetContent::file(File::from(serde_json::to_string_pretty(&next_font_manifest)?).into()),
)))
Ok(ResolvedVc::upcast(
VirtualOutputAsset::new(
path,
AssetContent::file(
File::from(serde_json::to_string_pretty(&next_font_manifest)?).into(),
),
)
.to_resolved()
.await?,
))
}
12 changes: 7 additions & 5 deletions crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl InstrumentationEndpoint {
Value::new(AvailabilityInfo::Root),
)
.await?;
Ok(chunk)
Ok(*chunk)
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -195,20 +195,22 @@ impl InstrumentationEndpoint {
instrumentation: Some(instrumentation_definition),
..Default::default()
};
let middleware_manifest_v2 = Vc::upcast(VirtualOutputAsset::new(
let middleware_manifest_v2 = VirtualOutputAsset::new(
node_root.join("server/instrumentation/middleware-manifest.json".into()),
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&middleware_manifest_v2,
)?))
.cell(),
),
));
output_assets.push(middleware_manifest_v2);
)
.to_resolved()
.await?;
output_assets.push(ResolvedVc::upcast(middleware_manifest_v2));

Ok(Vc::cell(output_assets))
} else {
Ok(Vc::cell(vec![self.node_chunk()]))
Ok(Vc::cell(vec![self.node_chunk().to_resolved().await?]))
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/next-api/src/loadable_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use anyhow::Result;
use next_core::next_manifests::LoadableManifest;
use turbo_tasks::{RcStr, TryFlatJoinIterExt, Vc};
use turbo_tasks::{RcStr, ResolvedVc, TryFlatJoinIterExt, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
Expand Down Expand Up @@ -56,16 +56,18 @@ pub async fn create_react_loadable_manifest(
}
}

let loadable_manifest = Vc::upcast(VirtualOutputAsset::new(
let loadable_manifest = VirtualOutputAsset::new(
output_path,
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&loadable_manifest,
)?))
.cell(),
),
));
)
.to_resolved()
.await?;

output.push(loadable_manifest);
output.push(ResolvedVc::upcast(loadable_manifest));
Ok(Vc::cell(output))
}
8 changes: 5 additions & 3 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,18 @@ impl MiddlewareEndpoint {
.collect(),
..Default::default()
};
let middleware_manifest_v2 = Vc::upcast(VirtualOutputAsset::new(
let middleware_manifest_v2 = VirtualOutputAsset::new(
node_root.join("server/middleware/middleware-manifest.json".into()),
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string_pretty(
&middleware_manifest_v2,
)?))
.cell(),
),
));
output_assets.push(middleware_manifest_v2);
)
.to_resolved()
.await?;
output_assets.push(ResolvedVc::upcast(middleware_manifest_v2));

Ok(Vc::cell(output_assets))
}
Expand Down
Loading

0 comments on commit abac25a

Please sign in to comment.