Skip to content

Commit

Permalink
Revert "Turbopack: reduce tasks needed for emitting (#62291)"
Browse files Browse the repository at this point in the history
This reverts commit bf196f5.
  • Loading branch information
sokra authored Feb 21, 2024
1 parent 2a9a7a2 commit 70222c5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 42 deletions.
18 changes: 11 additions & 7 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use indexmap::{map::Entry, IndexMap};
use next_core::{
all_assets_from_entries,
app_structure::find_app_dir,
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_context,
emit_assets, 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 @@ -797,14 +798,17 @@ impl Project {
let client_relative_path = self.client_relative_path();
let node_root = self.node_root();

let completion = self.await?.versioned_content_map.insert_output_assets(
all_output_assets,
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(),
client_relative_path,
node_root,
);

Ok(completion)
))
}
.instrument(span)
.await
Expand Down
15 changes: 5 additions & 10 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_assets;
use next_core::emit_client_assets;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, Completion, State, TryFlatJoinIterExt,
Expand Down Expand Up @@ -64,18 +64,13 @@ 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<Vc<Completion>> {
) -> Result<()> {
let assets_operation = *assets_operation.await?;
// Make sure all written client assets are up-to-date
let emit_operation = emit_assets(
assets_operation.resolve().await?,
node_root,
client_relative_path,
client_output_path,
);
let emit_operation =
emit_client_assets(assets_operation, client_relative_path, client_output_path);
let assets = assets_operation.await?;
let entries: Vec<_> = assets
.iter()
Expand All @@ -94,7 +89,7 @@ impl VersionedContentMap {
map.extend(entries);
true
});
Ok(emit_operation)
Ok(())
}

#[turbo_tasks::function]
Expand Down
92 changes: 68 additions & 24 deletions packages/next-swc/crates/next-core/src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anyhow::Result;
use tracing::Instrument;
use turbo_tasks::{
graph::{AdjacencyMap, GraphTraversal},
Completion, Completions, TryFlatJoinIterExt, ValueToString, Vc,
Completion, Completions, TryFlatJoinIterExt, Vc,
};
use turbo_tasks_fs::{rebase, FileSystemPath};
use turbopack_binding::turbopack::core::{
Expand Down Expand Up @@ -48,24 +47,69 @@ pub async fn emit_assets(
.iter()
.copied()
.map(|asset| async move {
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
})
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,
)));
}
.instrument(span)
.await

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)
})
.try_flat_join()
.await?,
Expand All @@ -79,14 +123,14 @@ fn emit(asset: Vc<Box<dyn OutputAsset>>) -> Vc<Completion> {
}

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

/// 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};
pub use emit::{all_assets_from_entries, emit_all_assets, emit_assets, emit_client_assets};
pub use next_edge::context::{
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_context,
};
Expand Down

0 comments on commit 70222c5

Please sign in to comment.