Skip to content

Commit

Permalink
refactor(turbopack-core): Change Modules type to use ResolvedVc
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Oct 29, 2024
1 parent ffc326c commit 89969bb
Show file tree
Hide file tree
Showing 47 changed files with 293 additions and 222 deletions.
22 changes: 11 additions & 11 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use next_core::{
use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_tasks::{
fxindexset, trace::TraceRawVcs, Completion, FxIndexMap, FxIndexSet, RcStr, TryJoinIterExt,
Value, ValueToString, Vc,
fxindexset, trace::TraceRawVcs, Completion, FxIndexMap, FxIndexSet, RcStr, ResolvedVc,
TryJoinIterExt, Value, ValueToString, Vc,
};
use turbo_tasks_env::{CustomProcessEnv, ProcessEnv};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
Expand Down Expand Up @@ -611,7 +611,7 @@ impl AppProject {
.await?
.context("expected Next.js client runtime to resolve to a module")?;

Ok(client_main_module)
Ok(*client_main_module)
}
}

Expand Down Expand Up @@ -889,7 +889,7 @@ impl AppEndpoint {
let ServerEntries {
server_component_entries,
server_utils,
} = &*find_server_entries(rsc_entry).await?;
} = &*find_server_entries(*rsc_entry).await?;

let mut client_references = client_reference_graph(
server_utils.clone(),
Expand All @@ -901,7 +901,7 @@ impl AppEndpoint {
for module in server_component_entries
.iter()
.map(|m| Vc::upcast::<Box<dyn Module>>(*m))
.chain(std::iter::once(rsc_entry))
.chain(std::iter::once(*rsc_entry))
{
let current_client_references =
client_reference_graph(vec![module], client_references.visited_nodes)
Expand Down Expand Up @@ -1085,7 +1085,7 @@ impl AppEndpoint {
let server_action_manifest_loader =
if let Some(app_server_reference_modules) = app_server_reference_modules {
let server_action_manifest = create_server_actions_manifest(
Vc::upcast(app_entry.rsc_entry),
*ResolvedVc::upcast(app_entry.rsc_entry),
app_server_reference_modules,
this.app_project.project().project_path(),
node_root,
Expand Down Expand Up @@ -1240,7 +1240,7 @@ impl AppEndpoint {

// create react-loadable-manifest for next/dynamic
let mut dynamic_import_modules = collect_next_dynamic_imports(
vec![Vc::upcast(app_entry.rsc_entry)],
vec![*ResolvedVc::upcast(app_entry.rsc_entry)],
Vc::upcast(this.app_project.client_module_context()),
VisitedDynamicImportModules::empty(),
)
Expand Down Expand Up @@ -1290,7 +1290,7 @@ impl AppEndpoint {
// create react-loadable-manifest for next/dynamic
let availability_info = Value::new(AvailabilityInfo::Root);
let mut dynamic_import_modules = collect_next_dynamic_imports(
vec![Vc::upcast(app_entry.rsc_entry)],
vec![*ResolvedVc::upcast(app_entry.rsc_entry)],
Vc::upcast(this.app_project.client_module_context()),
VisitedDynamicImportModules::empty(),
)
Expand Down Expand Up @@ -1353,10 +1353,10 @@ impl AppEndpoint {
.edge_rsc_runtime_entries()
.await?
.clone_value();
let evaluatable = Vc::try_resolve_sidecast(app_entry.rsc_entry)
let evaluatable = ResolvedVc::try_sidecast(app_entry.rsc_entry)
.await?
.context("Entry module must be evaluatable")?;
evaluatable_assets.push(evaluatable);
evaluatable_assets.push(*evaluatable);

if let Some(server_action_manifest_loader) = server_action_manifest_loader {
evaluatable_assets.push(server_action_manifest_loader);
Expand Down Expand Up @@ -1461,7 +1461,7 @@ impl AppEndpoint {
)
.into(),
),
app_entry.rsc_entry,
*app_entry.rsc_entry,
Vc::cell(evaluatable_assets),
current_chunks,
Value::new(current_availability_info),
Expand Down
26 changes: 14 additions & 12 deletions crates/next-api/src/dynamic_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use turbopack_core::{
use turbopack_ecmascript::{parse::ParseResult, resolve::esm_resolve, EcmascriptParsable};

async fn collect_chunk_group_inner<F, Fu>(
dynamic_import_entries: FxIndexMap<Vc<Box<dyn Module>>, DynamicImportedModules>,
dynamic_import_entries: FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedModules>,
mut build_chunk: F,
) -> Result<Vc<DynamicImportedChunks>>
where
Expand All @@ -46,7 +46,7 @@ where
*chunk
} else {
let Some(module) =
Vc::try_resolve_sidecast::<Box<dyn ChunkableModule>>(imported_module).await?
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(imported_module).await?
else {
bail!("module must be evaluatable");
};
Expand All @@ -57,7 +57,7 @@ where
// naive hash to have additional
// chunks in case if there are same modules being imported in different
// origins.
let chunk_group = build_chunk(module).await?;
let chunk_group = build_chunk(*module).await?;
chunks_hash.insert(imported_raw_str.clone(), chunk_group);
chunk_group
};
Expand All @@ -74,7 +74,7 @@ where

pub(crate) async fn collect_chunk_group(
chunking_context: Vc<Box<dyn ChunkingContext>>,
dynamic_import_entries: FxIndexMap<Vc<Box<dyn Module>>, DynamicImportedModules>,
dynamic_import_entries: FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedModules>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<DynamicImportedChunks>> {
collect_chunk_group_inner(dynamic_import_entries, |module| async move {
Expand All @@ -85,7 +85,7 @@ pub(crate) async fn collect_chunk_group(

pub(crate) async fn collect_evaluated_chunk_group(
chunking_context: Vc<Box<dyn ChunkingContext>>,
dynamic_import_entries: FxIndexMap<Vc<Box<dyn Module>>, DynamicImportedModules>,
dynamic_import_entries: FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedModules>,
) -> Result<Vc<DynamicImportedChunks>> {
collect_chunk_group_inner(dynamic_import_entries, |module| async move {
if let Some(module) = Vc::try_resolve_downcast::<Box<dyn EvaluatableAsset>>(module).await? {
Expand All @@ -103,7 +103,7 @@ pub(crate) async fn collect_evaluated_chunk_group(

#[turbo_tasks::value(shared)]
pub struct NextDynamicImportsResult {
pub client_dynamic_imports: FxIndexMap<Vc<Box<dyn Module>>, DynamicImportedModules>,
pub client_dynamic_imports: FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedModules>,
pub visited_modules: Vc<VisitedDynamicImportModules>,
}

Expand Down Expand Up @@ -186,7 +186,7 @@ pub(crate) async fn collect_next_dynamic_imports(
});

// Consolidate import mappings into a single indexmap
let mut import_mappings: FxIndexMap<Vc<Box<dyn Module>>, DynamicImportedModules> =
let mut import_mappings: FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedModules> =
FxIndexMap::default();

for module_mapping in imported_modules_mapping {
Expand Down Expand Up @@ -282,10 +282,10 @@ impl turbo_tasks::graph::Visit<NextDynamicVisitEntry> for NextDynamicVisit {
#[turbo_tasks::function]
async fn build_dynamic_imports_map_for_module(
client_asset_context: Vc<Box<dyn AssetContext>>,
server_module: Vc<Box<dyn Module>>,
server_module: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<OptionDynamicImportsMap>> {
let Some(ecmascript_asset) =
Vc::try_resolve_sidecast::<Box<dyn EcmascriptParsable>>(server_module).await?
ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(server_module).await?
else {
return Ok(Vc::cell(None));
};
Expand Down Expand Up @@ -409,17 +409,19 @@ impl Visit for CollectImportSourceVisitor {
}
}

pub type DynamicImportedModules = Vec<(RcStr, Vc<Box<dyn Module>>)>;
pub type DynamicImportedModules = Vec<(RcStr, ResolvedVc<Box<dyn Module>>)>;
pub type DynamicImportedOutputAssets = Vec<(RcStr, Vc<OutputAssets>)>;

/// A struct contains mapping for the dynamic imports to construct chunk per
/// each individual module (Origin Module, Vec<(ImportSourceString, Module)>)
#[turbo_tasks::value(transparent)]
pub struct DynamicImportsMap(pub (Vc<Box<dyn Module>>, DynamicImportedModules));
pub struct DynamicImportsMap(pub (ResolvedVc<Box<dyn Module>>, DynamicImportedModules));

/// An Option wrapper around [DynamicImportsMap].
#[turbo_tasks::value(transparent)]
pub struct OptionDynamicImportsMap(Option<Vc<DynamicImportsMap>>);

#[turbo_tasks::value(transparent)]
pub struct DynamicImportedChunks(pub FxIndexMap<Vc<Box<dyn Module>>, DynamicImportedOutputAssets>);
pub struct DynamicImportedChunks(
pub FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedOutputAssets>,
);
32 changes: 18 additions & 14 deletions crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use next_core::{
next_server::{get_server_runtime_entries, ServerContextType},
};
use tracing::Instrument;
use turbo_tasks::{Completion, RcStr, Value, Vc};
use turbo_tasks::{Completion, RcStr, ResolvedVc, Value, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
Expand Down Expand Up @@ -65,27 +65,31 @@ impl InstrumentationEndpoint {
}

#[turbo_tasks::function]
fn core_modules(&self) -> Vc<InstrumentationCoreModules> {
async fn core_modules(&self) -> Result<Vc<InstrumentationCoreModules>> {
let userland_module = self
.asset_context
.process(
self.source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::Instrumentation)),
)
.module();
.module()
.to_resolved()
.await?;

let edge_entry_module = wrap_edge_entry(
self.asset_context,
self.project.project_path(),
userland_module,
*userland_module,
"instrumentation".into(),
);
)
.to_resolved()
.await?;

InstrumentationCoreModules {
Ok(InstrumentationCoreModules {
userland_module,
edge_entry_module,
}
.cell()
.cell())
}

#[turbo_tasks::function]
Expand All @@ -107,15 +111,15 @@ impl InstrumentationEndpoint {
.clone_value();

let Some(module) =
Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkPlaceable>>(module).await?
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(module).await?
else {
bail!("Entry module must be evaluatable");
};

let Some(evaluatable) = Vc::try_resolve_sidecast(module).await? else {
let Some(evaluatable) = ResolvedVc::try_sidecast(module).await? else {
bail!("Entry module must be evaluatable");
};
evaluatable_assets.push(evaluatable);
evaluatable_assets.push(*evaluatable);

let edge_chunking_context = this.project.edge_chunking_context(false);

Expand All @@ -136,7 +140,7 @@ impl InstrumentationEndpoint {

let userland_module = self.core_modules().await?.userland_module;

let Some(module) = Vc::try_resolve_downcast(userland_module).await? else {
let Some(module) = ResolvedVc::try_downcast(userland_module).await? else {
bail!("Entry module must be evaluatable");
};

Expand All @@ -145,7 +149,7 @@ impl InstrumentationEndpoint {
this.project
.node_root()
.join("server/instrumentation.js".into()),
module,
*module,
get_server_runtime_entries(
Value::new(ServerContextType::Instrumentation {
app_dir: this.app_dir,
Expand Down Expand Up @@ -211,8 +215,8 @@ impl InstrumentationEndpoint {

#[turbo_tasks::value]
struct InstrumentationCoreModules {
pub userland_module: Vc<Box<dyn Module>>,
pub edge_entry_module: Vc<Box<dyn Module>>,
pub userland_module: ResolvedVc<Box<dyn Module>>,
pub edge_entry_module: ResolvedVc<Box<dyn Module>>,
}

#[turbo_tasks::value_impl]
Expand Down
4 changes: 2 additions & 2 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Endpoint for MiddlewareEndpoint {
}

#[turbo_tasks::function]
fn root_modules(self: Vc<Self>) -> Vc<Modules> {
Vc::cell(vec![self.userland_module()])
async fn root_modules(self: Vc<Self>) -> Result<Vc<Modules>> {
Ok(Vc::cell(vec![self.userland_module().to_resolved().await?]))
}
}
23 changes: 9 additions & 14 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use next_core::{
use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_tasks::{
trace::TraceRawVcs, Completion, FxIndexMap, RcStr, TaskInput, TryJoinIterExt, Value, Vc,
trace::TraceRawVcs, Completion, FxIndexMap, RcStr, ResolvedVc, TaskInput, TryJoinIterExt,
Value, Vc,
};
use turbo_tasks_fs::{
self, File, FileContent, FileSystem, FileSystemPath, FileSystemPathOption, VirtualFileSystem,
Expand Down Expand Up @@ -583,7 +584,7 @@ impl PagesProject {
.await?
.context("expected Next.js client runtime to resolve to a module")?;

Ok(client_main_module)
Ok(*client_main_module)
}
}

Expand Down Expand Up @@ -787,7 +788,7 @@ impl PageEndpoint {
};

Ok(InternalSsrChunkModule {
ssr_module,
ssr_module: ssr_module.to_resolved().await?,
runtime: config.runtime,
}
.cell())
Expand All @@ -812,7 +813,7 @@ impl PageEndpoint {
} = *self.internal_ssr_chunk_module().await?;

let dynamic_import_modules = collect_next_dynamic_imports(
vec![Vc::upcast(ssr_module)],
vec![*ResolvedVc::upcast(ssr_module)],
this.pages_project.client_module_context(),
VisitedDynamicImportModules::empty(),
)
Expand All @@ -823,7 +824,7 @@ impl PageEndpoint {
let is_edge = matches!(runtime, NextRuntime::Edge);
if is_edge {
let mut evaluatable_assets = edge_runtime_entries.await?.clone_value();
let evaluatable = Vc::try_resolve_sidecast(ssr_module)
let evaluatable = *ResolvedVc::try_sidecast(ssr_module)
.await?
.context("could not process page loader entry module")?;
evaluatable_assets.push(evaluatable);
Expand Down Expand Up @@ -860,7 +861,7 @@ impl PageEndpoint {
} = *chunking_context
.entry_chunk_group(
ssr_entry_chunk_path,
ssr_module,
*ssr_module,
runtime_entries,
OutputAssets::empty(),
Value::new(AvailabilityInfo::Root),
Expand Down Expand Up @@ -1211,16 +1212,10 @@ impl PageEndpoint {

#[turbo_tasks::value]
pub struct InternalSsrChunkModule {
pub ssr_module: Vc<Box<dyn Module>>,
pub ssr_module: ResolvedVc<Box<dyn Module>>,
pub runtime: NextRuntime,
}

#[turbo_tasks::value]
pub struct ClientChunksModules {
pub client_module: Vc<Box<dyn Module>>,
pub client_main_module: Vc<Box<dyn Module>>,
}

#[turbo_tasks::value_impl]
impl Endpoint for PageEndpoint {
#[turbo_tasks::function]
Expand Down Expand Up @@ -1314,7 +1309,7 @@ impl Endpoint for PageEndpoint {
modules.push(ssr_chunk_module.ssr_module);

if let PageEndpointType::Html = this.ty {
modules.push(self.client_module());
modules.push(self.client_module().to_resolved().await?);
}

Ok(Vc::cell(modules))
Expand Down
4 changes: 2 additions & 2 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,10 +1278,10 @@ impl Project {
#[turbo_tasks::function]
pub async fn client_main_modules(self: Vc<Self>) -> Result<Vc<Modules>> {
let pages_project = self.pages_project();
let mut modules = vec![pages_project.client_main_module()];
let mut modules = vec![pages_project.client_main_module().to_resolved().await?];

if let Some(app_project) = *self.app_project().await? {
modules.push(app_project.client_main_module());
modules.push(app_project.client_main_module().to_resolved().await?);
}

Ok(Vc::cell(modules))
Expand Down
Loading

0 comments on commit 89969bb

Please sign in to comment.