Skip to content

Commit

Permalink
turbopack: Extract as_chunk into shared ChunkType trait (#56506)
Browse files Browse the repository at this point in the history
### What?

Step 3 in our chunking refactors extracts ChunkItem::as_chunk into a new ChunkType trait. This new trait isn't useful yet, but will eventually allow us to collect ChunkItems of a similar type into a lists, and perform chunking over all similar items at once.

### Why?

In the end we want to avoid creating chunks from modules directly, but enforce everything going through the ChunkingContext to be chunked. This allows us to replace the existing chunking algorithm with a much more efficient one that avoid duplication between chunks in first place and doesn't require a post-chunking optimization.

### How?

vercel/turborepo#6123

Re: #56504

Closes WEB-1724

Co-authored-by: Tobias Koppers <[email protected]>
  • Loading branch information
jridgewell and sokra authored Oct 6, 2023
1 parent bc15b58 commit c6f5089
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 77 deletions.
66 changes: 33 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ swc_core = { version = "0.83.28", features = [
testing = { version = "0.34.1" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231006.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231006.4" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231006.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231006.4" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231006.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-231006.4" }

# General Deps

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use anyhow::{Context, Result};
use indoc::formatdoc;
use turbo_tasks::{TryJoinIterExt, Value, ValueToString, Vc};
use turbo_tasks::{TryJoinIterExt, ValueToString, Vc};
use turbopack_binding::{
turbo::tasks_fs::FileSystemPath,
turbopack::{
core::{
asset::{Asset, AssetContent},
chunk::{
availability_info::AvailabilityInfo, Chunk, ChunkData, ChunkItem, ChunkItemExt,
ChunkableModule, ChunkableModuleReference, ChunkingContext, ChunkingContextExt,
ChunkingType, ChunkingTypeOption, ChunksData,
ChunkData, ChunkItem, ChunkItemExt, ChunkType, ChunkableModule,
ChunkableModuleReference, ChunkingContext, ChunkingContextExt, ChunkingType,
ChunkingTypeOption, ChunksData,
},
ident::AssetIdent,
module::Module,
Expand All @@ -18,11 +18,11 @@ use turbopack_binding::{
reference::{ModuleReference, ModuleReferences, SingleOutputAssetReference},
resolve::ModuleResolveResult,
},
ecmascript::chunk::EcmascriptChunkData,
ecmascript::chunk::{EcmascriptChunkData, EcmascriptChunkType},
turbopack::ecmascript::{
chunk::{
EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkItemContent,
EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports,
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkingContext, EcmascriptExports,
},
utils::StringifyJs,
},
Expand Down Expand Up @@ -239,12 +239,13 @@ impl ChunkItem for WithClientChunksChunkItem {
}

#[turbo_tasks::function]
fn as_chunk(&self, availability_info: Value<AvailabilityInfo>) -> Vc<Box<dyn Chunk>> {
Vc::upcast(EcmascriptChunk::new(
Vc::upcast(self.context.with_layer("rsc".to_string())),
Vc::upcast(self.inner),
availability_info,
))
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}

#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(self.inner)
}
}

Expand Down
Loading

0 comments on commit c6f5089

Please sign in to comment.