Skip to content

Commit 88852b7

Browse files
committed
Cleanup pass
1 parent 72bdf68 commit 88852b7

File tree

10 files changed

+125
-157
lines changed

10 files changed

+125
-157
lines changed

crates/next-api/src/pages.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use turbopack::{
4444
use turbopack_core::{
4545
asset::AssetContent,
4646
chunk::{
47-
ChunkGroupResult, ChunkType, ChunkingContext, ChunkingContextExt, EvaluatableAsset,
48-
EvaluatableAssets, availability_info::AvailabilityInfo,
47+
ChunkGroupResult, ChunkingContext, ChunkingContextExt,
48+
EvaluatableAsset, EvaluatableAssets, availability_info::AvailabilityInfo,
4949
},
5050
context::AssetContext,
5151
file_source::FileSource,
@@ -657,36 +657,28 @@ impl PageEndpoint {
657657
Vc::upcast(this.pages_project.client_module_context()),
658658
self.source(),
659659
this.pathname.clone(),
660-
);
661-
let is_chunkable = {
662-
let page_loader_resolved = page_loader.to_resolved().await?;
663-
let configs = this
664-
.pages_project
665-
.project()
666-
.client_chunking_context()
667-
.chunking_configs()
668-
.await?;
669-
let mut chunkable = false;
670-
for (chunk_type, _) in configs.iter() {
671-
if *chunk_type.accepts_module(*page_loader_resolved).await? {
672-
chunkable = true;
673-
break;
674-
}
675-
}
676-
chunkable
677-
};
660+
)
661+
.to_resolved()
662+
.await?;
663+
let is_chunkable = this
664+
.pages_project
665+
.project()
666+
.client_chunking_context()
667+
.chunking_configs()
668+
.await?
669+
.is_chunkable(page_loader)
670+
.await;
678671
if matches!(
679672
*this.pages_project.project().next_mode().await?,
680673
NextMode::Development
681674
) && is_chunkable
682675
{
683-
let module = page_loader.to_resolved().await?;
684676
return Ok(Vc::upcast(HmrEntryModule::new(
685677
AssetIdent::from_path(this.page.await?.base_path.clone()),
686-
*module,
678+
*page_loader,
687679
)));
688680
}
689-
Ok(page_loader)
681+
Ok(*page_loader)
690682
}
691683

692684
#[turbo_tasks::function]

turbopack/crates/turbopack-browser/src/chunking_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl ChunkingContext for BrowserChunkingContext {
688688

689689
#[turbo_tasks::function]
690690
fn chunking_configs(&self) -> Result<Vc<ChunkingConfigs>> {
691-
Ok(Vc::cell(self.chunking_configs.iter().cloned().collect()))
691+
Ok(ChunkingConfigs(self.chunking_configs.iter().cloned().collect()).cell())
692692
}
693693

694694
#[turbo_tasks::function]

turbopack/crates/turbopack-cli/src/dev/web_entry_source.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use anyhow::{Result, anyhow};
1+
use anyhow::Result;
22
use turbo_rcstr::{RcStr, rcstr};
33
use turbo_tasks::{ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc};
44
use turbo_tasks_env::ProcessEnv;
55
use turbo_tasks_fs::FileSystemPath;
66
use turbopack_browser::{BrowserChunkingContext, react_refresh::assert_can_resolve_react_refresh};
77
use turbopack_cli_utils::runtime_entry::{RuntimeEntries, RuntimeEntry};
88
use turbopack_core::{
9-
chunk::{ChunkType, ChunkingContext, EvaluatableAsset, SourceMapSourceType, SourceMapsType},
9+
chunk::{ChunkItem, ChunkingContext, EvaluatableAsset, SourceMapSourceType, SourceMapsType},
1010
environment::Environment,
1111
file_source::FileSource,
1212
module::Module,
@@ -169,37 +169,19 @@ pub async fn create_web_entry_source(
169169
let entries: Vec<_> = entries
170170
.into_iter()
171171
.map(|module| async move {
172-
let mut is_chunkable = false;
173-
{
174-
let configs = chunking_context.chunking_configs().await?;
175-
for (chunk_type, _) in configs.iter() {
176-
if *chunk_type.accepts_module(*module).await? {
177-
is_chunkable = true;
178-
break;
179-
}
180-
}
181-
}
182-
if !is_chunkable {
183-
// TODO convert into a serve-able asset
184-
return Err(anyhow!(
185-
"Entry module is not chunkable, so it can't be used to bootstrap the \
186-
application"
187-
));
188-
}
172+
let chunk_item = ChunkItem::new(*module, *module_graph, *chunking_context)
173+
.to_resolved()
174+
.await?;
189175
if let Some(entry) = ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module) {
190176
Ok(DevHtmlEntry {
191-
chunkable_module: module,
192-
module_graph,
193-
chunking_context,
177+
chunk_item,
194178
runtime_entries: Some(runtime_entries.with_entry(*entry).to_resolved().await?),
195179
})
196180
} else {
197181
// TODO this is missing runtime code, so it's probably broken and we should also
198182
// add an ecmascript chunk with the runtime code
199183
Ok(DevHtmlEntry {
200-
chunkable_module: module,
201-
module_graph,
202-
chunking_context,
184+
chunk_item,
203185
runtime_entries: None,
204186
})
205187
}

turbopack/crates/turbopack-core/src/chunk/chunking_context.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
use std::ops::Deref;
2+
13
use anyhow::{Result, bail};
24
use bincode::{Decode, Encode};
35
use rustc_hash::{FxHashMap, FxHashSet};
46
use serde::{Deserialize, Serialize};
57
use turbo_rcstr::RcStr;
6-
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, Upcast, Vc, trace::TraceRawVcs};
8+
use turbo_tasks::{
9+
IntoTraitRef, NonLocalValue, ResolvedVc, TaskInput, Upcast, Vc, trace::TraceRawVcs,
10+
};
711
use turbo_tasks_fs::FileSystemPath;
812
use turbo_tasks_hash::DeterministicHash;
913

@@ -276,8 +280,40 @@ pub struct ChunkingConfig {
276280
pub placeholder_for_future_extensions: (),
277281
}
278282

279-
#[turbo_tasks::value(transparent)]
280-
pub struct ChunkingConfigs(FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig>);
283+
#[derive(Default)]
284+
#[turbo_tasks::value(shared)]
285+
pub struct ChunkingConfigs(pub FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig>);
286+
287+
impl Deref for ChunkingConfigs {
288+
type Target = FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingConfig>;
289+
fn deref(&self) -> &Self::Target {
290+
&self.0
291+
}
292+
}
293+
294+
impl ChunkingConfigs {
295+
pub async fn is_chunkable(&self, module: ResolvedVc<Box<dyn Module>>) -> bool {
296+
self.chunk_type(module).await.is_some()
297+
}
298+
299+
/// Returns a [`ChunkType`], if one is supported by this module.
300+
pub async fn chunk_type(
301+
&self,
302+
module: ResolvedVc<Box<dyn Module>>,
303+
) -> Option<ResolvedVc<Box<dyn ChunkType>>> {
304+
for (chunk_type, _) in &self.0 {
305+
if chunk_type
306+
.into_trait_ref()
307+
.await
308+
.expect("Unexpectedly failed to cast trait")
309+
.accepts_module(module)
310+
{
311+
return Some(*chunk_type);
312+
}
313+
}
314+
None
315+
}
316+
}
281317

282318
#[turbo_tasks::value(shared)]
283319
#[derive(Debug, Clone, Copy, Hash, TaskInput, Default, Deserialize)]
@@ -373,7 +409,7 @@ pub trait ChunkingContext {
373409

374410
#[turbo_tasks::function]
375411
fn chunking_configs(self: Vc<Self>) -> Vc<ChunkingConfigs> {
376-
Vc::cell(Default::default())
412+
ChunkingConfigs::default().cell()
377413
}
378414

379415
#[turbo_tasks::function]

turbopack/crates/turbopack-core/src/chunk/mod.rs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -384,23 +384,37 @@ pub struct ChunkItem {
384384
pub module: ResolvedVc<Box<dyn Module>>,
385385
pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
386386
pub module_graph: ResolvedVc<ModuleGraph>,
387+
pub ty: ResolvedVc<Box<dyn ChunkType>>,
387388
}
388389

389390
#[turbo_tasks::value_impl]
390391
impl ChunkItem {
391392
/// Create a new [ChunkItem] from a [Module].
392393
#[turbo_tasks::function]
393-
pub fn new(
394+
pub async fn new(
394395
module: ResolvedVc<Box<dyn Module>>,
395396
module_graph: ResolvedVc<ModuleGraph>,
396397
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
397-
) -> Vc<Self> {
398-
Self {
398+
) -> Result<Vc<Self>> {
399+
let Some(ty) = chunking_context
400+
.chunking_configs()
401+
.await?
402+
.chunk_type(module)
403+
.await
404+
else {
405+
bail!(
406+
"Module {} is not chunkable (at least given the current config)",
407+
module.ident_string().await?.to_string()
408+
);
409+
};
410+
411+
Ok(Self {
399412
module,
400413
chunking_context,
401414
module_graph,
415+
ty,
402416
}
403-
.cell()
417+
.cell())
404418
}
405419

406420
/// The [AssetIdent] of the [Module] that this [ChunkItem] was created from.
@@ -416,33 +430,15 @@ impl ChunkItem {
416430
/// different when the chunk item content depends on available modules e. g.
417431
/// for chunk loaders.
418432
#[turbo_tasks::function]
419-
pub async fn content_ident(&self) -> Result<Vc<AssetIdent>> {
420-
let configs = self.chunking_context.chunking_configs().await?;
421-
for (chunk_type, _) in configs.iter() {
422-
if *chunk_type.accepts_module(*self.module).await? {
423-
return Ok(chunk_type.chunk_item_content_ident(
424-
*self.module,
425-
*self.chunking_context,
426-
*self.module_graph,
427-
));
428-
}
429-
}
430-
Ok(self.module.ident())
433+
pub async fn content_ident(&self) -> Vc<AssetIdent> {
434+
self.ty
435+
.chunk_item_content_ident(*self.module, *self.chunking_context, *self.module_graph)
431436
}
432437

433438
/// The type of chunk this item should be assembled into.
434439
#[turbo_tasks::function]
435-
pub async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
436-
let configs = self.chunking_context.chunking_configs().await?;
437-
for (chunk_type, _) in configs.iter() {
438-
if *chunk_type.accepts_module(*self.module).await? {
439-
return Ok(**chunk_type);
440-
}
441-
}
442-
bail!(
443-
"No chunk type accepts module {}",
444-
self.module.ident().to_string().await?
445-
)
440+
pub async fn ty(&self) -> Vc<Box<dyn ChunkType>> {
441+
*self.ty
446442
}
447443

448444
/// Retrieve the module associated with this ChunkItem.
@@ -461,18 +457,10 @@ impl ChunkItem {
461457
impl OutputAssetsReference for ChunkItem {
462458
#[turbo_tasks::function]
463459
async fn references(&self) -> Result<Vc<OutputAssetsWithReferenced>> {
464-
let configs = self.chunking_context.chunking_configs().await?;
465-
for (chunk_type, _) in configs.iter() {
466-
if *chunk_type.accepts_module(*self.module).await? {
467-
return Ok(chunk_type.chunk_item_output_assets(
468-
*self.module,
469-
*self.chunking_context,
470-
*self.module_graph,
471-
));
472-
}
473-
}
474-
Ok(OutputAssetsWithReferenced::from_assets(
475-
*OutputAssets::empty_resolved(),
460+
Ok(self.ty.chunk_item_output_assets(
461+
*self.module,
462+
*self.chunking_context,
463+
*self.module_graph,
476464
))
477465
}
478466
}
@@ -484,11 +472,7 @@ pub trait ChunkType: ValueToString {
484472
fn is_style(self: Vc<Self>) -> Vc<bool>;
485473

486474
/// Returns true if this chunk type can handle the given module.
487-
#[turbo_tasks::function]
488-
fn accepts_module(&self, module: ResolvedVc<Box<dyn Module>>) -> Vc<bool> {
489-
let _ = module;
490-
Vc::cell(false)
491-
}
475+
fn accepts_module(&self, module: ResolvedVc<Box<dyn Module>>) -> bool;
492476

493477
/// Create a new chunk for the given chunk items
494478
#[turbo_tasks::function]

0 commit comments

Comments
 (0)