Skip to content

Commit

Permalink
Refactoring module references (#5572)
Browse files Browse the repository at this point in the history
### Description

Split `AssetReference` `ResolveResult` into Module and Source variants
simplify ResolveResult
get rid of some temporary type fixup methods

graph references are now typed either Source, Module or OutputAsset
instead of only Asset in general

next.js PR: vercel/next.js#52930
  • Loading branch information
sokra committed Jul 21, 2023
1 parent 1a950b0 commit c4d1d31
Show file tree
Hide file tree
Showing 78 changed files with 1,398 additions and 1,015 deletions.
15 changes: 7 additions & 8 deletions crates/turbopack-cli-utils/src/runtime_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use turbopack_core::{
chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
context::AssetContext,
issue::{IssueSeverity, OptionIssueSource},
module::{convert_asset_to_module, Module},
module::Module,
resolve::{origin::PlainResolveOrigin, parse::Request},
source::Source,
};
Expand Down Expand Up @@ -33,26 +33,25 @@ impl RuntimeEntry {
RuntimeEntry::Request(r, path) => (r, path),
};

let assets = cjs_resolve(
let modules = cjs_resolve(
Vc::upcast(PlainResolveOrigin::new(context, path)),
request,
OptionIssueSource::none(),
IssueSeverity::Error.cell(),
)
.primary_assets()
.primary_modules()
.await?;

let mut runtime_entries = Vec::with_capacity(assets.len());
for &asset in &assets {
let mut runtime_entries = Vec::with_capacity(modules.len());
for &module in &modules {
if let Some(entry) =
Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(asset).await?
Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(module).await?
{
runtime_entries.push(entry);
} else {
let asset = convert_asset_to_module(asset);
bail!(
"runtime reference resolved to an asset ({}) that cannot be evaluated",
asset.ident().to_string().await?
module.ident().to_string().await?
);
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,17 @@ async fn build_internal(
.map(|request_vc| async move {
let ty = Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined));
let request = request_vc.await?;
Ok(*origin
origin
.resolve_asset(request_vc, origin.resolve_options(ty.clone()), ty)
.primary_assets()
.first_module()
.await?
.first()
.with_context(|| {
format!(
"Unable to resolve entry {} from directory {}.",
request.request().unwrap(),
project_dir
)
})?)
})
})
.try_join()
.await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-cli/src/dev/web_entry_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub async fn create_web_entry_source(
let ty = Value::new(ReferenceType::Entry(EntryReferenceSubType::Web));
Ok(origin
.resolve_asset(request, origin.resolve_options(ty.clone()), ty)
.primary_assets()
.primary_modules()
.await?
.first()
.copied())
Expand Down
32 changes: 0 additions & 32 deletions crates/turbopack-core/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
use anyhow::Result;
use indexmap::IndexSet;
use turbo_tasks::{Completion, Vc};
use turbo_tasks_fs::{
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType,
};

use crate::version::{VersionedAssetContent, VersionedContent};

/// A list of [Asset]s
#[turbo_tasks::value(transparent)]
#[derive(Hash)]
pub struct Assets(Vec<Vc<Box<dyn Asset>>>);

/// A set of [Asset]s
#[turbo_tasks::value(transparent)]
pub struct AssetsSet(IndexSet<Vc<Box<dyn Asset>>>);

#[turbo_tasks::value_impl]
impl Assets {
/// Creates an empty list of [Asset]s
#[turbo_tasks::function]
pub fn empty() -> Vc<Self> {
Vc::cell(Vec::new())
}
}

#[turbo_tasks::value_impl]
impl AssetsSet {
/// Creates an empty set of [Asset]s
#[turbo_tasks::function]
pub fn empty() -> Vc<AssetsSet> {
Vc::cell(IndexSet::new())
}
}

/// An asset. It also forms a graph when following [Asset::references].
#[turbo_tasks::value_trait]
pub trait Asset {
Expand All @@ -46,10 +18,6 @@ pub trait Asset {
}
}

/// An optional [Asset]
#[turbo_tasks::value(shared, transparent)]
pub struct AssetOption(Option<Vc<Box<dyn Asset>>>);

#[turbo_tasks::value(shared)]
#[derive(Clone)]
pub enum AssetContent {
Expand Down
10 changes: 5 additions & 5 deletions crates/turbopack-core/src/chunk/availability_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use turbo_tasks::Vc;

use super::available_assets::AvailableAssets;
use super::available_modules::AvailableAssets;
use crate::module::Module;

#[turbo_tasks::value(serialization = "auto_for_input")]
Expand All @@ -11,7 +11,7 @@ pub enum AvailabilityInfo {
current_availability_root: Vc<Box<dyn Module>>,
},
Inner {
available_assets: Vc<AvailableAssets>,
available_modules: Vc<AvailableAssets>,
current_availability_root: Vc<Box<dyn Module>>,
},
}
Expand All @@ -30,13 +30,13 @@ impl AvailabilityInfo {
}
}

pub fn available_assets(&self) -> Option<Vc<AvailableAssets>> {
pub fn available_modules(&self) -> Option<Vc<AvailableAssets>> {
match self {
Self::Untracked => None,
Self::Root { .. } => None,
Self::Inner {
available_assets, ..
} => Some(*available_assets),
available_modules, ..
} => Some(*available_modules),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use turbo_tasks_hash::Xxh3Hash64Hasher;
use super::{ChunkableModuleReference, ChunkingType};
use crate::{
module::{Module, ModulesSet},
reference::AssetReference,
reference::ModuleReference,
};

/// Allows to gather information about which assets are already available.
Expand Down Expand Up @@ -75,7 +75,7 @@ impl AvailableAssets {
}
}
for root in this.roots.iter() {
if chunkable_assets_set(*root).await?.contains(&asset) {
if chunkable_modules_set(*root).await?.contains(&asset) {
return Ok(Vc::cell(true));
}
}
Expand All @@ -84,7 +84,7 @@ impl AvailableAssets {
}

#[turbo_tasks::function]
pub async fn chunkable_assets_set(root: Vc<Box<dyn Module>>) -> Result<Vc<ModulesSet>> {
pub async fn chunkable_modules_set(root: Vc<Box<dyn Module>>) -> Result<Vc<ModulesSet>> {
let assets = AdjacencyMap::new()
.skip_duplicates()
.visit(once(root), |&asset: &Vc<Box<dyn Module>>| async move {
Expand All @@ -108,17 +108,9 @@ pub async fn chunkable_assets_set(root: Vc<Box<dyn Module>>) -> Result<Vc<Module
) {
return Ok(chunkable
.resolve_reference()
.primary_assets()
.primary_modules()
.await?
.iter()
.map(|&asset| async move {
Ok(Vc::try_resolve_downcast::<Box<dyn Module>>(asset).await?)
})
.try_join()
.await?
.into_iter()
.flatten()
.collect());
.clone_value());
}
}
Ok(Vec::new())
Expand Down

0 comments on commit c4d1d31

Please sign in to comment.