Unverified Commit 2dab0ed2 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbopack): Use ResolvedVc for chunk graph related types (#74507)

Allows a few more `turbo_tasks::value`s to implement `NonLocalValue`, making them safe for use with local tasks.
parent c51b5368
......@@ -77,7 +77,9 @@ impl ClientReferenceManifest {
let client_chunk_item = ecmascript_client_reference
.client_module
.as_chunk_item(Vc::upcast(client_chunking_context));
.as_chunk_item(Vc::upcast(client_chunking_context))
.to_resolved()
.await?;
let client_module_id = client_chunk_item.id().await?;
......@@ -116,16 +118,20 @@ impl ClientReferenceManifest {
if let Some(ssr_chunking_context) = ssr_chunking_context {
let ssr_chunk_item = ecmascript_client_reference
.ssr_module
.as_chunk_item(Vc::upcast(ssr_chunking_context));
.as_chunk_item(Vc::upcast(ssr_chunking_context))
.to_resolved()
.await?;
let ssr_module_id = ssr_chunk_item.id().await?;
let rsc_chunk_item: Vc<Box<dyn ChunkItem>> =
let rsc_chunk_item: ResolvedVc<Box<dyn ChunkItem>> =
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceProxyModule>(
parent_module,
)
.await?
.unwrap()
.as_chunk_item(Vc::upcast(ssr_chunking_context));
.as_chunk_item(Vc::upcast(ssr_chunking_context))
.to_resolved()
.await?;
let rsc_module_id = rsc_chunk_item.id().await?;
let (ssr_chunks_paths, ssr_is_async) = if runtime == NextRuntime::Edge {
......@@ -362,13 +368,13 @@ pub fn get_client_reference_module_key(server_path: &str, export_name: &str) ->
async fn is_item_async(
availability_info: &AvailabilityInfo,
chunk_item: Vc<Box<dyn ChunkItem>>,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<bool> {
let Some(available_chunk_items) = availability_info.available_chunk_items() else {
return Ok(false);
};
let Some(info) = &*available_chunk_items.get(chunk_item).await? else {
let Some(info) = available_chunk_items.await?.get(chunk_item).await? else {
return Ok(false);
};
......
......@@ -390,7 +390,7 @@ impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
async fn chunk_group(
self: Vc<Self>,
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
module: ResolvedVc<Box<dyn ChunkableModule>>,
availability_info: Value<AvailabilityInfo>,
......@@ -403,7 +403,7 @@ impl ChunkingContext for BrowserChunkingContext {
chunks,
availability_info,
} = make_chunk_group(
Vc::upcast(self),
ResolvedVc::upcast(self),
[ResolvedVc::upcast(module)],
input_availability_info,
)
......@@ -454,7 +454,7 @@ impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
async fn evaluated_chunk_group(
self: Vc<Self>,
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
evaluatable_assets: Vc<EvaluatableAssets>,
availability_info: Value<AvailabilityInfo>,
......@@ -476,7 +476,7 @@ impl ChunkingContext for BrowserChunkingContext {
let MakeChunkGroupResult {
chunks,
availability_info,
} = make_chunk_group(Vc::upcast(self), entries, availability_info).await?;
} = make_chunk_group(ResolvedVc::upcast(self), entries, availability_info).await?;
let mut assets: Vec<ResolvedVc<Box<dyn OutputAsset>>> = chunks
.iter()
......
......@@ -59,7 +59,11 @@ impl EcmascriptDevChunkContentEntries {
async move {
Ok((
chunk_item.id().await?,
EcmascriptDevChunkContentEntry::new(chunk_item, async_module_info).await?,
EcmascriptDevChunkContentEntry::new(
*chunk_item,
async_module_info.map(|info| *info),
)
.await?,
))
}
.instrument(info_span!(
......
......@@ -17,14 +17,14 @@ pub enum AvailabilityInfo {
}
impl AvailabilityInfo {
pub fn available_chunk_items(&self) -> Option<Vc<AvailableChunkItems>> {
pub fn available_chunk_items(&self) -> Option<ResolvedVc<AvailableChunkItems>> {
match self {
Self::Untracked => None,
Self::Root => None,
Self::Complete {
available_chunk_items,
..
} => Some(**available_chunk_items),
} => Some(*available_chunk_items),
}
}
......
......@@ -15,9 +15,6 @@ pub struct AvailableChunkItemInfo {
pub is_async: bool,
}
#[turbo_tasks::value(transparent)]
pub struct OptionAvailableChunkItemInfo(Option<AvailableChunkItemInfo>);
#[turbo_tasks::value(transparent)]
pub struct AvailableChunkItemInfoMap(
FxIndexMap<ResolvedVc<Box<dyn ChunkItem>>, AvailableChunkItemInfo>,
......@@ -48,12 +45,13 @@ impl AvailableChunkItems {
self: ResolvedVc<Self>,
chunk_items: ResolvedVc<AvailableChunkItemInfoMap>,
) -> Result<Vc<Self>> {
let this = &*self.await?;
let chunk_items = chunk_items
.await?
.into_iter()
.map(|(&chunk_item, &info)| async move {
Ok(self
.get(*chunk_item)
Ok(this
.get(chunk_item)
.await?
.is_none()
.then_some((chunk_item, info)))
......@@ -87,18 +85,24 @@ impl AvailableChunkItems {
}
Ok(Vc::cell(hasher.finish()))
}
}
#[turbo_tasks::function]
impl AvailableChunkItems {
// This is not a turbo-tasks function because:
//
// - We want to enforce that the `chunk_item` key is a `ResolvedVc`, so that it will actually
// match the entries in the `HashMap`.
// - This is a cheap operation that's unlikely to be worth caching.
pub async fn get(
&self,
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Result<Vc<OptionAvailableChunkItemInfo>> {
) -> Result<Option<AvailableChunkItemInfo>> {
if let Some(&info) = self.chunk_items.await?.get(&chunk_item) {
return Ok(Vc::cell(Some(info)));
return Ok(Some(info));
};
if let Some(parent) = self.parent {
return Ok(parent.get(*chunk_item));
return Box::pin(parent.await?.get(chunk_item)).await;
}
Ok(Vc::cell(None))
Ok(None)
}
}
......@@ -2,9 +2,7 @@ use std::collections::HashSet;
use anyhow::Result;
use auto_hash_map::AutoSet;
use turbo_tasks::{
FxIndexMap, FxIndexSet, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc,
};
use turbo_tasks::{FxIndexMap, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, Vc};
use super::{
availability_info::AvailabilityInfo, available_chunk_items::AvailableChunkItemInfo,
......@@ -22,7 +20,7 @@ pub struct MakeChunkGroupResult {
/// Creates a chunk group from a set of entries.
pub async fn make_chunk_group(
chunking_context: Vc<Box<dyn ChunkingContext>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
chunk_group_entries: impl IntoIterator<Item = ResolvedVc<Box<dyn Module>>>,
availability_info: AvailabilityInfo,
) -> Result<MakeChunkGroupResult> {
......@@ -53,7 +51,7 @@ pub async fn make_chunk_group(
.keys()
.copied()
.chain(self_async_children.into_iter())
.map(|chunk_item| (chunk_item, AutoSet::<Vc<Box<dyn ChunkItem>>>::new()))
.map(|chunk_item| (chunk_item, AutoSet::<ResolvedVc<Box<dyn ChunkItem>>>::new()))
.collect::<FxIndexMap<_, _>>();
// Propagate async inheritance
......@@ -86,7 +84,7 @@ pub async fn make_chunk_group(
let mut chunk_items = chunk_items
.into_iter()
.map(|chunk_item| (chunk_item, None))
.collect::<FxIndexMap<_, Option<Vc<AsyncModuleInfo>>>>();
.collect::<FxIndexMap<_, Option<ResolvedVc<AsyncModuleInfo>>>>();
// Insert AsyncModuleInfo for every async module
for (async_item, referenced_async_modules) in async_chunk_items {
......@@ -96,13 +94,18 @@ pub async fn make_chunk_group(
.iter()
.copied()
.filter(|item| referenced_async_modules.contains(item))
.map(|item| *item)
.collect()
} else {
Default::default()
};
chunk_items.insert(
async_item,
Some(AsyncModuleInfo::new(referenced_async_modules)),
Some(
AsyncModuleInfo::new(referenced_async_modules)
.to_resolved()
.await?,
),
);
}
......@@ -112,7 +115,7 @@ pub async fn make_chunk_group(
.iter()
.map(|(&chunk_item, async_info)| async move {
Ok((
chunk_item.to_resolved().await?,
chunk_item,
AvailableChunkItemInfo {
is_async: async_info.is_some(),
},
......@@ -130,9 +133,12 @@ pub async fn make_chunk_group(
let async_loaders = async_modules
.into_iter()
.map(|module| {
chunking_context.async_loader_chunk_item(*module, Value::new(availability_info))
chunking_context
.async_loader_chunk_item(*module, Value::new(availability_info))
.to_resolved()
})
.collect::<Vec<_>>();
.try_join()
.await?;
let has_async_loaders = !async_loaders.is_empty();
let async_loader_chunk_items = async_loaders.iter().map(|&chunk_item| (chunk_item, None));
......@@ -151,7 +157,7 @@ pub async fn make_chunk_group(
let mut referenced_output_assets = (*external_output_assets.await?).clone();
referenced_output_assets.extend(
references_to_output_assets(external_module_references)
references_to_output_assets(&external_module_references)
.await?
.await?
.iter()
......@@ -175,7 +181,7 @@ pub async fn make_chunk_group(
// Pass chunk items to chunking algorithm
let mut chunks = make_chunks(
chunking_context,
*chunking_context,
Vc::cell(chunk_items.into_iter().collect()),
"".into(),
Vc::cell(referenced_output_assets),
......@@ -188,7 +194,7 @@ pub async fn make_chunk_group(
// We want them to be separate since they are specific to this chunk group due
// to available chunk items differing
let async_loader_chunks = make_chunks(
chunking_context,
*chunking_context,
Vc::cell(async_loader_chunk_items.into_iter().collect()),
"async-loader-".into(),
async_loader_external_module_references,
......@@ -206,7 +212,7 @@ pub async fn make_chunk_group(
}
pub async fn references_to_output_assets(
references: FxIndexSet<Vc<Box<dyn ModuleReference>>>,
references: impl IntoIterator<Item = &ResolvedVc<Box<dyn ModuleReference>>>,
) -> Result<Vc<OutputAssets>> {
let output_assets = references
.into_iter()
......
......@@ -53,7 +53,9 @@ pub async fn make_chunks(
.await?
.iter()
.map(|&(chunk_item, async_info)| async move {
let chunk_item_info = chunk_item_info(chunking_context, chunk_item, async_info).await?;
let chunk_item_info =
chunk_item_info(chunking_context, *chunk_item, async_info.map(|info| *info))
.await?;
Ok((chunk_item, async_info, chunk_item_info))
})
.try_join()
......@@ -118,8 +120,8 @@ pub async fn make_chunks(
}
type ChunkItemWithInfo = (
Vc<Box<dyn ChunkItem>>,
Option<Vc<AsyncModuleInfo>>,
ResolvedVc<Box<dyn ChunkItem>>,
Option<ResolvedVc<AsyncModuleInfo>>,
usize,
ReadRef<RcStr>,
);
......
......@@ -209,14 +209,14 @@ pub trait ChunkableModuleReference: ModuleReference + ValueToString {
}
}
type AsyncInfo = FxIndexMap<Vc<Box<dyn ChunkItem>>, Vec<Vc<Box<dyn ChunkItem>>>>;
type AsyncInfo = FxIndexMap<ResolvedVc<Box<dyn ChunkItem>>, Vec<ResolvedVc<Box<dyn ChunkItem>>>>;
pub struct ChunkContentResult {
pub chunk_items: FxIndexSet<Vc<Box<dyn ChunkItem>>>,
pub chunk_items: FxIndexSet<ResolvedVc<Box<dyn ChunkItem>>>,
pub async_modules: FxIndexSet<ResolvedVc<Box<dyn ChunkableModule>>>,
pub traced_modules: FxIndexSet<ResolvedVc<Box<dyn Module>>>,
pub external_output_assets: Vc<OutputAssets>,
pub external_module_references: FxIndexSet<Vc<Box<dyn ModuleReference>>>,
pub external_output_assets: ResolvedVc<OutputAssets>,
pub external_module_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
/// A map from local module to all children from which the async module
/// status is inherited
pub forward_edges_inherit_async: AsyncInfo,
......@@ -229,7 +229,7 @@ pub struct ChunkContentResult {
}
pub async fn chunk_content(
chunking_context: Vc<Box<dyn ChunkingContext>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
chunk_entries: impl IntoIterator<Item = ResolvedVc<Box<dyn Module>>>,
availability_info: AvailabilityInfo,
) -> Result<ChunkContentResult> {
......@@ -250,25 +250,25 @@ enum InheritAsyncEdge {
AvailableAsyncModule,
}
#[derive(Eq, PartialEq, Clone, Hash, Serialize, Deserialize, TraceRawVcs, Debug)]
#[derive(Eq, PartialEq, Clone, Hash, Serialize, Deserialize, TraceRawVcs, Debug, NonLocalValue)]
enum ChunkContentGraphNode {
// A chunk item not placed in the current chunk, but whose references we will
// follow to find more graph nodes.
PassthroughChunkItem {
item: Vc<Box<dyn ChunkItem>>,
item: ResolvedVc<Box<dyn ChunkItem>>,
},
// Chunk items that are placed into the current chunk group
ChunkItem {
item: Vc<Box<dyn ChunkItem>>,
item: ResolvedVc<Box<dyn ChunkItem>>,
ident: ReadRef<RcStr>,
},
// Async module that is referenced from the chunk group
AsyncModule {
module: Vc<Box<dyn ChunkableModule>>,
module: ResolvedVc<Box<dyn ChunkableModule>>,
},
// Module that is referenced as traced and will be turned into a separate RebasedAsset
TracedModule {
module: Vc<Box<dyn Module>>,
module: ResolvedVc<Box<dyn Module>>,
},
ExternalOutputAssets(ResolvedVc<OutputAssets>),
// ModuleReferences that are not placed in the current chunk group
......@@ -276,7 +276,7 @@ enum ChunkContentGraphNode {
/// A list of directly referenced chunk items from which `is_async_module`
/// will be inherited.
InheritAsyncInfo {
item: Vc<Box<dyn ChunkItem>>,
item: ResolvedVc<Box<dyn ChunkItem>>,
references: Vec<(ResolvedVc<Box<dyn ChunkItem>>, InheritAsyncEdge)>,
},
}
......@@ -287,14 +287,14 @@ enum ChunkGraphNodeToReferences {
ChunkItem(ResolvedVc<Box<dyn ChunkItem>>),
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, TraceRawVcs)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, TraceRawVcs, NonLocalValue)]
struct ChunkGraphEdge {
key: Option<ResolvedVc<Box<dyn Module>>>,
node: ChunkContentGraphNode,
}
#[derive(Debug, Clone)]
#[turbo_tasks::value(transparent, local)]
#[turbo_tasks::value(transparent)]
struct ChunkGraphEdges(Vec<ChunkGraphEdge>);
#[turbo_tasks::function]
......@@ -307,7 +307,8 @@ async fn graph_node_to_referenced_nodes_with_available_chunk_items(
let edges_ref = edges.await?;
for (unchanged, edge) in edges_ref.iter().enumerate() {
if let ChunkContentGraphNode::ChunkItem { item, .. } = edge.node {
if let Some(info) = *available_chunk_items.get(item).await? {
let available_chunk_items = available_chunk_items.await?;
if let Some(info) = available_chunk_items.get(item).await? {
let mut new_edges = Vec::with_capacity(edges_ref.len());
new_edges.extend(edges_ref[0..unchanged].iter().cloned());
let mut available_chunk_item_info = HashMap::new();
......@@ -315,7 +316,7 @@ async fn graph_node_to_referenced_nodes_with_available_chunk_items(
for edge in edges_ref[unchanged + 1..].iter() {
match edge.node {
ChunkContentGraphNode::ChunkItem { item, .. } => {
if let Some(info) = *available_chunk_items.get(item).await? {
if let Some(info) = available_chunk_items.get(item).await? {
available_chunk_item_info.insert(item, info);
continue;
}
......@@ -406,7 +407,7 @@ async fn graph_node_to_referenced_nodes(
return Ok((
Some(ChunkGraphEdge {
key: None,
node: ChunkContentGraphNode::TracedModule { module: *module },
node: ChunkContentGraphNode::TracedModule { module },
}),
None,
));
......@@ -431,7 +432,7 @@ async fn graph_node_to_referenced_nodes(
ChunkingType::Parallel => {
let chunk_item = chunkable_module
.as_chunk_item(chunking_context)
.resolve()
.to_resolved()
.await?;
Ok((
Some(ChunkGraphEdge {
......@@ -447,7 +448,7 @@ async fn graph_node_to_referenced_nodes(
ChunkingType::ParallelInheritAsync => {
let chunk_item = chunkable_module
.as_chunk_item(chunking_context)
.resolve()
.to_resolved()
.await?;
Ok((
Some(ChunkGraphEdge {
......@@ -457,16 +458,13 @@ async fn graph_node_to_referenced_nodes(
ident: module.ident().to_string().await?,
},
}),
Some((
chunk_item.to_resolved().await?,
InheritAsyncEdge::LocalModule,
)),
Some((chunk_item, InheritAsyncEdge::LocalModule)),
))
}
ChunkingType::Passthrough => {
let chunk_item = chunkable_module
.as_chunk_item(chunking_context)
.resolve()
.to_resolved()
.await?;
Ok((
......@@ -485,7 +483,7 @@ async fn graph_node_to_referenced_nodes(
if matches!(*chunk_loading, ChunkLoading::Edge) {
let chunk_item = chunkable_module
.as_chunk_item(chunking_context)
.resolve()
.to_resolved()
.await?;
Ok((
Some(ChunkGraphEdge {
......@@ -502,7 +500,7 @@ async fn graph_node_to_referenced_nodes(
Some(ChunkGraphEdge {
key: None,
node: ChunkContentGraphNode::AsyncModule {
module: *chunkable_module,
module: chunkable_module,
},
}),
None,
......@@ -537,7 +535,7 @@ async fn graph_node_to_referenced_nodes(
graph_nodes.push(ChunkGraphEdge {
key: None,
node: ChunkContentGraphNode::InheritAsyncInfo {
item: *parent,
item: parent,
references: inherit_async_references,
},
})
......@@ -560,9 +558,9 @@ async fn graph_node_to_referenced_nodes(
}
struct ChunkContentVisit {
chunking_context: Vc<Box<dyn ChunkingContext>>,
available_chunk_items: Option<Vc<AvailableChunkItems>>,
processed_modules: HashSet<Vc<Box<dyn Module>>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
available_chunk_items: Option<ResolvedVc<AvailableChunkItems>>,
processed_modules: HashSet<ResolvedVc<Box<dyn Module>>>,
}
type ChunkItemToGraphNodesEdges = impl Iterator<Item = ChunkGraphEdge>;
......@@ -585,7 +583,7 @@ impl Visit<ChunkContentGraphNode, ()> for ChunkContentVisit {
}
};
if !self.processed_modules.insert(*module) {
if !self.processed_modules.insert(module) {
return VisitControlFlow::Skip(node);
}
......@@ -601,10 +599,10 @@ impl Visit<ChunkContentGraphNode, ()> for ChunkContentVisit {
async move {
let node = match node {
ChunkContentGraphNode::PassthroughChunkItem { item } => {
ChunkGraphNodeToReferences::PassthroughChunkItem(item.to_resolved().await?)
ChunkGraphNodeToReferences::PassthroughChunkItem(item)
}
ChunkContentGraphNode::ChunkItem { item, .. } => {
ChunkGraphNodeToReferences::ChunkItem(item.to_resolved().await?)
ChunkGraphNodeToReferences::ChunkItem(item)
}
_ => {
return Ok(None.into_iter().flatten());
......@@ -614,11 +612,11 @@ impl Visit<ChunkContentGraphNode, ()> for ChunkContentVisit {
let nodes = if let Some(available_chunk_items) = available_chunk_items {
graph_node_to_referenced_nodes_with_available_chunk_items(
node,
chunking_context,
available_chunk_items,
*chunking_context,
*available_chunk_items,
)
} else {
graph_node_to_referenced_nodes(node, chunking_context)
graph_node_to_referenced_nodes(node, *chunking_context)
}
.await?;
Ok(Some(nodes.into_iter().cloned()).into_iter().flatten())
......@@ -635,7 +633,7 @@ impl Visit<ChunkContentGraphNode, ()> for ChunkContentVisit {
}
async fn chunk_content_internal_parallel(
chunking_context: Vc<Box<dyn ChunkingContext>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
chunk_entries: impl IntoIterator<Item = ResolvedVc<Box<dyn Module>>>,
availability_info: AvailabilityInfo,
) -> Result<ChunkContentResult> {
......@@ -650,7 +648,10 @@ async fn chunk_content_internal_parallel(
Ok(Some(ChunkGraphEdge {
key: Some(entry),
node: ChunkContentGraphNode::ChunkItem {
item: chunkable_module.as_chunk_item(chunking_context),
item: chunkable_module
.as_chunk_item(*chunking_context)
.to_resolved()
.await?,
ident: chunkable_module.ident().to_string().await?,
},
}))
......@@ -686,19 +687,16 @@ async fn chunk_content_internal_parallel(
match graph_node {
ChunkContentGraphNode::PassthroughChunkItem { .. } => {}
ChunkContentGraphNode::TracedModule { module } => {
let module = module.to_resolved().await?;
traced_modules.insert(module);
}
ChunkContentGraphNode::ChunkItem { item, .. } => {
chunk_items.insert(*item.to_resolved().await?);
chunk_items.insert(item);
}
ChunkContentGraphNode::AsyncModule { module } => {
let module = module.to_resolved().await?;
async_modules.insert(module);
}
ChunkContentGraphNode::ExternalModuleReference(reference) => {
let reference = reference.resolve().await?;
external_module_references.insert(*reference);
external_module_references.insert(reference);
}
ChunkContentGraphNode::ExternalOutputAssets(reference) => {
for output_asset in reference.await? {
......@@ -709,12 +707,12 @@ async fn chunk_content_internal_parallel(
for &(reference, ty) in &references {
match ty {
InheritAsyncEdge::LocalModule => local_back_edges_inherit_async
.entry(*reference)
.entry(reference)
.or_insert_with(Vec::new)
.push(item),
InheritAsyncEdge::AvailableAsyncModule => {
available_async_modules_back_edges_inherit_async
.entry(*reference)
.entry(reference)
.or_insert_with(Vec::new)
.push(item)
}
......@@ -723,7 +721,7 @@ async fn chunk_content_internal_parallel(
forward_edges_inherit_async
.entry(item)
.or_insert_with(Vec::new)
.extend(references.into_iter().map(|(r, _)| *r));
.extend(references.into_iter().map(|(r, _)| r));
}
}
}
......@@ -732,7 +730,7 @@ async fn chunk_content_internal_parallel(
chunk_items,
async_modules,
traced_modules,
external_output_assets: Vc::cell(external_output_assets.into_iter().collect()),
external_output_assets: ResolvedVc::cell(external_output_assets.into_iter().collect()),
external_module_references,
forward_edges_inherit_async,
local_back_edges_inherit_async,
......@@ -809,23 +807,22 @@ pub struct AsyncModuleInfo {
#[turbo_tasks::value_impl]
impl AsyncModuleInfo {
#[turbo_tasks::function]
pub async fn new(referenced_async_modules: Vec<Vc<Box<dyn ChunkItem>>>) -> Result<Vc<Self>> {
let resolved_modules = referenced_async_modules
.into_iter()
.map(|m| m.to_resolved())
.try_join()
.await?;
pub async fn new(
referenced_async_modules: Vec<ResolvedVc<Box<dyn ChunkItem>>>,
) -> Result<Vc<Self>> {
Ok(Self {
referenced_async_modules: resolved_modules.into_iter().collect(),
referenced_async_modules: referenced_async_modules.into_iter().collect(),
}
.cell())
}
}
pub type ChunkItemWithAsyncModuleInfo = (Vc<Box<dyn ChunkItem>>, Option<Vc<AsyncModuleInfo>>);
pub type ChunkItemWithAsyncModuleInfo = (
ResolvedVc<Box<dyn ChunkItem>>,
Option<ResolvedVc<AsyncModuleInfo>>,
);
#[turbo_tasks::value(transparent, local)]
#[turbo_tasks::value(transparent)]
pub struct ChunkItemsWithAsyncModuleInfo(Vec<ChunkItemWithAsyncModuleInfo>);
pub trait ChunkItemExt {
......
......@@ -3,7 +3,7 @@ pub mod source_map;
use std::fmt::Write;
use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, Value, ValueDefault, ValueToString, Vc};
use turbo_tasks_fs::{rope::Rope, File, FileSystem};
......@@ -502,13 +502,10 @@ impl ChunkType for CssChunkType {
chunk_items: chunk_items
.iter()
.map(|(chunk_item, _async_info)| async move {
let Some(chunk_item) =
Vc::try_resolve_downcast::<Box<dyn CssChunkItem>>(*chunk_item).await?
else {
bail!("Chunk item is not an css chunk item but reporting chunk type css");
};
// CSS doesn't need to care about async_info, so we can discard it
chunk_item.to_resolved().await
ResolvedVc::try_downcast::<Box<dyn CssChunkItem>>(*chunk_item)
.await?
.context("Chunk item is not an css chunk item but reporting chunk type css")
})
.try_join()
.await?,
......
......@@ -34,11 +34,12 @@ impl AsyncLoaderChunkItem {
let module = self.module.await?;
if let Some(chunk_items) = module.availability_info.available_chunk_items() {
if chunk_items
.await?
.get(
module
.inner
.as_chunk_item(*ResolvedVc::upcast(self.chunking_context))
.resolve()
.to_resolved()
.await?,
)
.await?
......
use anyhow::{bail, Result};
use turbo_rcstr::RcStr;
use turbo_tasks::{TryJoinIterExt, ValueDefault, ValueToString, Vc};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueDefault, ValueToString, Vc};
use turbopack_core::{
chunk::{
round_chunk_item_size, AsyncModuleInfo, Chunk, ChunkItem, ChunkItemWithAsyncModuleInfo,
......@@ -45,9 +45,9 @@ impl ChunkType for EcmascriptChunkType {
let content = EcmascriptChunkContent {
chunk_items: chunk_items
.iter()
.map(|(chunk_item, async_info)| async move {
.map(|&(chunk_item, async_info)| async move {
let Some(chunk_item) =
Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkItem>>(*chunk_item)
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkItem>>(chunk_item)
.await?
else {
bail!(
......@@ -55,7 +55,7 @@ impl ChunkType for EcmascriptChunkType {
ecmascript"
);
};
Ok((chunk_item, *async_info))
Ok((chunk_item, async_info))
})
.try_join()
.await?,
......
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks::ResolvedVc;
use turbopack_core::{chunk::AsyncModuleInfo, output::OutputAsset};
use super::item::EcmascriptChunkItem;
type EcmascriptChunkItemWithAsyncInfo = (
Vc<Box<dyn EcmascriptChunkItem>>,
Option<Vc<AsyncModuleInfo>>,
ResolvedVc<Box<dyn EcmascriptChunkItem>>,
Option<ResolvedVc<AsyncModuleInfo>>,
);
#[turbo_tasks::value(shared, local)]
#[turbo_tasks::value(shared)]
pub struct EcmascriptChunkContent {
pub chunk_items: Vec<EcmascriptChunkItemWithAsyncInfo>,
pub referenced_output_assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>,
......
......@@ -145,9 +145,8 @@ impl Chunk for EcmascriptChunk {
Ok(ChunkItems(
chunk_items
.iter()
.map(|(item, _)| async move { Ok(ResolvedVc::upcast(item.to_resolved().await?)) })
.try_join()
.await?,
.map(|&(item, _)| ResolvedVc::upcast(item))
.collect(),
)
.cell())
}
......
......@@ -66,10 +66,11 @@ impl ManifestAsyncModule {
let this = self.await?;
if let Some(chunk_items) = this.availability_info.available_chunk_items() {
if chunk_items
.await?
.get(
this.inner
.as_chunk_item(*ResolvedVc::upcast(this.chunking_context))
.resolve()
.to_resolved()
.await?,
)
.await?
......
......@@ -293,7 +293,7 @@ impl ChunkingContext for NodeJsChunkingContext {
#[turbo_tasks::function]
async fn chunk_group(
self: Vc<Self>,
self: ResolvedVc<Self>,
_ident: Vc<AssetIdent>,
module: ResolvedVc<Box<dyn ChunkableModule>>,
availability_info: Value<AvailabilityInfo>,
......@@ -307,7 +307,7 @@ impl ChunkingContext for NodeJsChunkingContext {
chunks,
availability_info,
} = make_chunk_group(
Vc::upcast(self),
ResolvedVc::upcast(self),
[ResolvedVc::upcast(module)],
availability_info.into_value(),
)
......@@ -334,7 +334,7 @@ impl ChunkingContext for NodeJsChunkingContext {
/// * exports the result of evaluating the given module as a CommonJS default export.
#[turbo_tasks::function]
pub async fn entry_chunk_group(
self: Vc<Self>,
self: ResolvedVc<Self>,
path: Vc<FileSystemPath>,
module: ResolvedVc<Box<dyn Module>>,
evaluatable_assets: Vc<EvaluatableAssets>,
......@@ -347,7 +347,7 @@ impl ChunkingContext for NodeJsChunkingContext {
chunks,
availability_info,
} = make_chunk_group(
Vc::upcast(self),
ResolvedVc::upcast(self),
once(module).chain(
evaluatable_assets
.await?
......@@ -378,7 +378,7 @@ impl ChunkingContext for NodeJsChunkingContext {
let asset = ResolvedVc::upcast(
EcmascriptBuildNodeEntryChunk::new(
path,
self,
*self,
Vc::cell(other_chunks),
evaluatable_assets,
*module,
......
......@@ -55,7 +55,7 @@ pub(super) async fn chunk_items(
.map(|&(chunk_item, async_module_info)| async move {
Ok((
chunk_item.id().await?,
chunk_item.code(async_module_info).await?,
chunk_item.code(async_module_info.map(|info| *info)).await?,
))
})
.try_join()
......
......@@ -210,7 +210,7 @@ impl ChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<OutputAssets>> {
let loader_references = self.module.loader().references().await?;
references_to_output_assets(loader_references.iter().map(|r| **r).collect::<_>()).await
references_to_output_assets(&*loader_references).await
}
#[turbo_tasks::function]
......
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