Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack: reduce tasks needed for emitting #62291

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use indexmap::{map::Entry, IndexMap};
use next_core::{
all_assets_from_entries,
app_structure::find_app_dir,
emit_assets, get_edge_chunking_context, get_edge_compile_time_info,
get_edge_resolve_options_context,
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_context,
instrumentation::instrumentation_files,
middleware::middleware_files,
mode::NextMode,
Expand Down Expand Up @@ -798,17 +797,14 @@ impl Project {
let client_relative_path = self.client_relative_path();
let node_root = self.node_root();

self.await?
.versioned_content_map
.insert_output_assets(all_output_assets, client_relative_path, node_root)
.await?;

Ok(emit_assets(
*all_output_assets.await?,
self.node_root(),
let completion = self.await?.versioned_content_map.insert_output_assets(
all_output_assets,
node_root,
client_relative_path,
node_root,
))
);

Ok(completion)
}
.instrument(span)
.await
Expand Down
15 changes: 10 additions & 5 deletions packages/next-swc/crates/next-api/src/versioned_content_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use anyhow::{bail, Result};
use next_core::emit_client_assets;
use next_core::emit_assets;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, Completion, State, TryFlatJoinIterExt,
Expand Down Expand Up @@ -64,13 +64,18 @@ impl VersionedContentMap {
pub async fn insert_output_assets(
self: Vc<Self>,
assets_operation: Vc<OutputAssetsOperation>,
node_root: Vc<FileSystemPath>,
client_relative_path: Vc<FileSystemPath>,
client_output_path: Vc<FileSystemPath>,
) -> Result<()> {
) -> Result<Vc<Completion>> {
let assets_operation = *assets_operation.await?;
// Make sure all written client assets are up-to-date
let emit_operation =
emit_client_assets(assets_operation, client_relative_path, client_output_path);
let emit_operation = emit_assets(
assets_operation.resolve().await?,
node_root,
client_relative_path,
client_output_path,
);
let assets = assets_operation.await?;
let entries: Vec<_> = assets
.iter()
Expand All @@ -89,7 +94,7 @@ impl VersionedContentMap {
map.extend(entries);
true
});
Ok(())
Ok(emit_operation)
}

#[turbo_tasks::function]
Expand Down
92 changes: 24 additions & 68 deletions packages/next-swc/crates/next-core/src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use tracing::Instrument;
use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal},
Completion, Completions, TryFlatJoinIterExt, Vc,
Completion, Completions, TryFlatJoinIterExt, ValueToString, Vc,
};
use turbo_tasks_fs::{rebase, FileSystemPath};
use turbopack_binding::turbopack::core::{
Expand Down Expand Up @@ -47,69 +48,24 @@ pub async fn emit_assets(
.iter()
.copied()
.map(|asset| async move {
if asset
.ident()
.path()
.await?
.is_inside_ref(&*node_root.await?)
{
return Ok(Some(emit(asset)));
} else if asset
.ident()
.path()
.await?
.is_inside_ref(&*client_relative_path.await?)
{
// Client assets are emitted to the client output path, which is prefixed with
// _next. We need to rebase them to remove that prefix.
return Ok(Some(emit_rebase(
asset,
client_relative_path,
client_output_path,
)));
let asset = asset.resolve().await?;
let path = asset.ident().path();
let span = tracing::info_span!("emit asset", name = &*path.to_string().await?);
async move {
let path = path.await?;
Ok(if path.is_inside_ref(&*node_root.await?) {
Some(emit(asset))
} else if path.is_inside_ref(&*client_relative_path.await?) {
// Client assets are emitted to the client output path, which is prefixed
// with _next. We need to rebase them to remove that
// prefix.
Some(emit_rebase(asset, client_relative_path, client_output_path))
} else {
None
})
}

Ok(None)
})
.try_flat_join()
.await?,
)
.completed())
}

/// Emits all assets transitively reachable from the given chunks, that are
/// inside the client root.
///
/// Assets inside the given client root are rebased to the given client output
/// path.
#[turbo_tasks::function]
pub async fn emit_client_assets(
assets: Vc<OutputAssets>,
client_relative_path: Vc<FileSystemPath>,
client_output_path: Vc<FileSystemPath>,
) -> Result<Vc<Completion>> {
Ok(Vc::<Completions>::cell(
assets
.await?
.iter()
.copied()
.map(|asset| async move {
if asset
.ident()
.path()
.await?
.is_inside_ref(&*client_relative_path.await?)
{
// Client assets are emitted to the client output path, which is prefixed with
// _next. We need to rebase them to remove that prefix.
return Ok(Some(emit_rebase(
asset,
client_relative_path,
client_output_path,
)));
}

Ok(None)
.instrument(span)
.await
})
.try_flat_join()
.await?,
Expand All @@ -123,14 +79,14 @@ fn emit(asset: Vc<Box<dyn OutputAsset>>) -> Vc<Completion> {
}

#[turbo_tasks::function]
fn emit_rebase(
async fn emit_rebase(
asset: Vc<Box<dyn OutputAsset>>,
from: Vc<FileSystemPath>,
to: Vc<FileSystemPath>,
) -> Vc<Completion> {
asset
.content()
.write(rebase(asset.ident().path(), from, to))
) -> Result<Vc<Completion>> {
let path = rebase(asset.ident().path(), from, to);
let content = asset.content();
Ok(content.resolve().await?.write(path.resolve().await?))
}

/// Walks the asset graph from multiple assets and collect all referenced
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod util;
pub use app_segment_config::{
parse_segment_config_from_loader_tree, parse_segment_config_from_source,
};
pub use emit::{all_assets_from_entries, emit_all_assets, emit_assets, emit_client_assets};
pub use emit::{all_assets_from_entries, emit_all_assets, emit_assets};
pub use next_edge::context::{
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_context,
};
Expand Down