From b7b174bfef36388ee8821e97af5424326d91c961 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Wed, 4 Oct 2023 17:28:14 -0400 Subject: [PATCH 1/5] Introduce ChunkableModule::as_chunk_item --- crates/turbopack-core/src/chunk/mod.rs | 5 +++++ crates/turbopack-css/src/asset.rs | 17 +++++++++++++---- crates/turbopack-css/src/chunk/mod.rs | 9 ++++++--- crates/turbopack-css/src/module_asset.rs | 15 ++++++++++++++- crates/turbopack-ecmascript/src/chunk/item.rs | 4 +++- .../src/chunk_group_files_asset.rs | 8 ++++++++ crates/turbopack-ecmascript/src/lib.rs | 8 ++++++++ .../src/manifest/chunk_asset.rs | 8 ++++++++ .../src/references/require_context.rs | 10 +++++++++- .../src/tree_shake/asset.rs | 8 ++++++++ crates/turbopack-json/src/lib.rs | 8 ++++++++ crates/turbopack-mdx/src/lib.rs | 17 +++++++++++++---- crates/turbopack-static/src/lib.rs | 8 ++++++++ crates/turbopack-wasm/src/module_asset.rs | 19 ++++++++++++++----- crates/turbopack-wasm/src/raw.rs | 8 ++++++++ 15 files changed, 133 insertions(+), 19 deletions(-) diff --git a/crates/turbopack-core/src/chunk/mod.rs b/crates/turbopack-core/src/chunk/mod.rs index 984cccb5cee395..ef191515b6c704 100644 --- a/crates/turbopack-core/src/chunk/mod.rs +++ b/crates/turbopack-core/src/chunk/mod.rs @@ -102,6 +102,11 @@ pub trait ChunkableModule: Module + Asset { }), ) } + + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc>; } #[turbo_tasks::value(transparent)] diff --git a/crates/turbopack-css/src/asset.rs b/crates/turbopack-css/src/asset.rs index 11faffafbab122..489aa68995770a 100644 --- a/crates/turbopack-css/src/asset.rs +++ b/crates/turbopack-css/src/asset.rs @@ -123,6 +123,14 @@ impl ChunkableModule for CssModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] @@ -194,7 +202,7 @@ impl CssChunkItem for CssModuleChunkItem { { imports.push(CssImport::Internal( import_ref, - placeable.as_chunk_item(chunking_context), + CssChunkPlaceable::as_chunk_item(placeable, chunking_context), )); } } @@ -210,9 +218,10 @@ impl CssChunkItem for CssModuleChunkItem { if let Some(placeable) = Vc::try_resolve_downcast::>(module).await? { - imports.push(CssImport::Composes( - placeable.as_chunk_item(chunking_context), - )); + imports.push(CssImport::Composes(CssChunkPlaceable::as_chunk_item( + placeable, + chunking_context, + ))); } } } diff --git a/crates/turbopack-css/src/chunk/mod.rs b/crates/turbopack-css/src/chunk/mod.rs index 56d51b80fc1ea8..4d4aae3f44e976 100644 --- a/crates/turbopack-css/src/chunk/mod.rs +++ b/crates/turbopack-css/src/chunk/mod.rs @@ -142,7 +142,7 @@ impl CssChunkContent { let mut body = CodeBuilder::default(); let mut external_imports = IndexSet::new(); for entry in this.main_entries.await?.iter() { - let entry_item = entry.as_chunk_item(this.chunking_context); + let entry_item = CssChunkPlaceable::as_chunk_item(*entry, this.chunking_context); // TODO(WEB-1261) for external_import in expand_imports(&mut body, entry_item).await? { @@ -323,7 +323,7 @@ impl OutputChunk for CssChunk { .main_entries .await? .iter() - .map(|&entry| entry.as_chunk_item(self.chunking_context)) + .map(|&entry| CssChunkPlaceable::as_chunk_item(entry, self.chunking_context)) .collect(); let included_ids = entries_chunk_items .iter() @@ -536,7 +536,10 @@ impl FromChunkableModule for Box { if let Some(placeable) = Vc::try_resolve_downcast::>(asset).await? { - return Ok(Some(placeable.as_chunk_item(chunking_context))); + return Ok(Some(CssChunkPlaceable::as_chunk_item( + placeable, + chunking_context, + ))); } Ok(None) } diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index 5de8ef3929f5df..171bbddfa0d222 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -222,6 +222,14 @@ impl ChunkableModule for ModuleCssAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] @@ -338,7 +346,12 @@ impl EcmascriptChunkItem for ModuleChunkItem { let placeable: Vc> = Vc::upcast(css_module); - let module_id = placeable.as_chunk_item(self.chunking_context).id().await?; + let module_id = EcmascriptChunkPlaceable::as_chunk_item( + placeable, + self.chunking_context, + ) + .id() + .await?; let module_id = StringifyJs(&*module_id); let original_name = StringifyJs(&original_name); exported_class_names.push(format! { diff --git a/crates/turbopack-ecmascript/src/chunk/item.rs b/crates/turbopack-ecmascript/src/chunk/item.rs index 7d4210af21b2df..45029419c99999 100644 --- a/crates/turbopack-ecmascript/src/chunk/item.rs +++ b/crates/turbopack-ecmascript/src/chunk/item.rs @@ -262,7 +262,9 @@ impl FromChunkableModule for Box { return Ok(None); }; - Ok(Some(placeable.as_chunk_item(context))) + Ok(Some(EcmascriptChunkPlaceable::as_chunk_item( + placeable, context, + ))) } async fn from_async_asset( diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index c257d1b2b35338..11aeaf1813a0cb 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -102,6 +102,14 @@ impl ChunkableModule for ChunkGroupFilesAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index a1691a6b3dd7fc..7d4ea9de1ffcab 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -415,6 +415,14 @@ impl ChunkableModule for EcmascriptModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index e99795a7f88140..0f8d580765730d 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -131,6 +131,14 @@ impl ChunkableModule for ManifestChunkAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index 36116a171795f5..2b04e5644daf86 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -282,7 +282,7 @@ impl CodeGenerateable for RequireContextAssetReference { &self, chunking_context: Vc>, ) -> Result> { - let chunk_item = self.inner.as_chunk_item(chunking_context); + let chunk_item = EcmascriptChunkPlaceable::as_chunk_item(self.inner, chunking_context); let module_id = chunk_item.id().await?.clone_value(); let mut visitors = Vec::new(); @@ -386,6 +386,14 @@ impl ChunkableModule for RequireContextAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index 5ec7e1c784bb85..cada8f64821e0d 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -142,6 +142,14 @@ impl ChunkableModule for EcmascriptModulePartAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-json/src/lib.rs b/crates/turbopack-json/src/lib.rs index 80812299768575..4b40d6ec60d457 100644 --- a/crates/turbopack-json/src/lib.rs +++ b/crates/turbopack-json/src/lib.rs @@ -77,6 +77,14 @@ impl ChunkableModule for JsonModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index 116b96e2225392..b2e9eb3800db1c 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -200,6 +200,14 @@ impl ChunkableModule for MdxModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] @@ -264,10 +272,11 @@ impl EcmascriptChunkItem for MdxChunkItem { /// apply all of the ecma transforms #[turbo_tasks::function] async fn content(&self) -> Result> { - Ok(into_ecmascript_module_asset(&self.module) - .await? - .as_chunk_item(self.chunking_context) - .content()) + Ok(EcmascriptChunkPlaceable::as_chunk_item( + into_ecmascript_module_asset(&self.module).await?, + self.chunking_context, + ) + .content()) } } diff --git a/crates/turbopack-static/src/lib.rs b/crates/turbopack-static/src/lib.rs index b637a335642f73..fd5fae3202b5f7 100644 --- a/crates/turbopack-static/src/lib.rs +++ b/crates/turbopack-static/src/lib.rs @@ -102,6 +102,14 @@ impl ChunkableModule for StaticModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-wasm/src/module_asset.rs b/crates/turbopack-wasm/src/module_asset.rs index bac27d6cf9f204..fd02d9e1e81714 100644 --- a/crates/turbopack-wasm/src/module_asset.rs +++ b/crates/turbopack-wasm/src/module_asset.rs @@ -126,6 +126,14 @@ impl ChunkableModule for WebAssemblyModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] @@ -188,7 +196,8 @@ impl ChunkItem for ModuleChunkItem { #[turbo_tasks::function] async fn references(&self) -> Result> { - let loader = self.module.loader().as_chunk_item(self.chunking_context); + let loader = + EcmascriptChunkPlaceable::as_chunk_item(self.module.loader(), self.chunking_context); Ok(loader.references()) } @@ -213,10 +222,10 @@ impl EcmascriptChunkItem for ModuleChunkItem { ) -> Result> { let loader_asset = self.module.loader(); - let chunk_item_content = loader_asset - .as_chunk_item(self.chunking_context) - .content_with_availability_info(availability_info) - .await?; + let chunk_item_content = + EcmascriptChunkPlaceable::as_chunk_item(loader_asset, self.chunking_context) + .content_with_availability_info(availability_info) + .await?; Ok(EcmascriptChunkItemContent { options: EcmascriptChunkItemOptions { diff --git a/crates/turbopack-wasm/src/raw.rs b/crates/turbopack-wasm/src/raw.rs index 9a70e8d2a9d983..fc38d0e9053984 100644 --- a/crates/turbopack-wasm/src/raw.rs +++ b/crates/turbopack-wasm/src/raw.rs @@ -84,6 +84,14 @@ impl ChunkableModule for RawWebAssemblyModuleAsset { availability_info, )) } + + #[turbo_tasks::function] + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc> { + todo!(); + } } #[turbo_tasks::value_impl] From 45e7b38057bd148727ba5ab7e09b92a8216b9273 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Wed, 4 Oct 2023 20:02:40 -0400 Subject: [PATCH 2/5] Duplicate as_chunk_item --- crates/turbopack-css/src/asset.rs | 5 +++- crates/turbopack-css/src/module_asset.rs | 20 ++++++++++++--- .../src/chunk_group_files_asset.rs | 23 ++++++++++++++--- .../src/manifest/chunk_asset.rs | 21 +++++++++++++--- .../src/references/require_context.rs | 25 ++++++++++++++++--- .../src/tree_shake/asset.rs | 19 +++++++++++--- crates/turbopack-json/src/lib.rs | 17 ++++++++++--- crates/turbopack-mdx/src/lib.rs | 17 ++++++++++--- crates/turbopack-static/src/lib.rs | 18 ++++++++++--- crates/turbopack-wasm/src/module_asset.rs | 21 +++++++++++++--- crates/turbopack-wasm/src/raw.rs | 22 +++++++++++++--- 11 files changed, 168 insertions(+), 40 deletions(-) diff --git a/crates/turbopack-css/src/asset.rs b/crates/turbopack-css/src/asset.rs index 489aa68995770a..a217b2b6f355cb 100644 --- a/crates/turbopack-css/src/asset.rs +++ b/crates/turbopack-css/src/asset.rs @@ -129,7 +129,10 @@ impl ChunkableModule for CssModuleAsset { self: Vc, chunking_context: Vc>, ) -> Vc> { - todo!(); + Vc::upcast(CssModuleChunkItem::cell(CssModuleChunkItem { + module: self, + chunking_context, + })) } } diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index 171bbddfa0d222..a2b46be6c47971 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -1,6 +1,6 @@ use std::{fmt::Write, iter::once, sync::Arc}; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use indexmap::IndexMap; use indoc::formatdoc; use swc_core::{ @@ -224,11 +224,23 @@ impl ChunkableModule for ModuleCssAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use ModuleCssAsset", + )?; + Ok(Vc::upcast( + ModuleChunkItem { + chunking_context, + module: self, + } + .cell(), + )) } } diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index 11aeaf1813a0cb..6d6e622d70680f 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use indexmap::IndexSet; use turbo_tasks::{TryJoinIterExt, Value, ValueToString, Vc}; use turbo_tasks_fs::{File, FileSystemPath}; @@ -104,11 +104,26 @@ impl ChunkableModule for ChunkGroupFilesAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let this = self.await?; + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + ChunkGroupFilesAsset", + )?; + Ok(Vc::upcast( + ChunkGroupFilesChunkItem { + chunking_context, + client_root: this.client_root, + inner: self, + } + .cell(), + )) } } diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index 0f8d580765730d..d0e200f0e3bb53 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use turbo_tasks::{Value, Vc}; use turbopack_core::{ asset::{Asset, AssetContent}, @@ -133,11 +133,24 @@ impl ChunkableModule for ManifestChunkAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + ManifestChunkAsset", + )?; + Ok(Vc::upcast( + ManifestChunkItem { + chunking_context, + manifest: self, + } + .cell(), + )) } } diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index 2b04e5644daf86..d9537d800bd3be 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -1,6 +1,6 @@ use std::{collections::VecDeque, sync::Arc}; -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use indexmap::IndexMap; use swc_core::{ common::DUMMY_SP, @@ -388,11 +388,28 @@ impl ChunkableModule for RequireContextAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + RequireContextAsset", + )?; + let this = self.await?; + Ok(Vc::upcast( + RequireContextChunkItem { + chunking_context, + inner: self, + + origin: this.origin, + map: this.map, + } + .cell(), + )) } } diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index cada8f64821e0d..86292300e5371e 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -144,11 +144,24 @@ impl ChunkableModule for EcmascriptModulePartAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + EcmascriptModulePartAsset", + )?; + Ok(Vc::upcast( + EcmascriptModulePartChunkItem { + module: self, + chunking_context, + } + .cell(), + )) } } diff --git a/crates/turbopack-json/src/lib.rs b/crates/turbopack-json/src/lib.rs index 4b40d6ec60d457..236f0d216bf816 100644 --- a/crates/turbopack-json/src/lib.rs +++ b/crates/turbopack-json/src/lib.rs @@ -11,7 +11,7 @@ use std::fmt::Write; -use anyhow::{bail, Error, Result}; +use anyhow::{bail, Context, Error, Result}; use turbo_tasks::{Value, ValueToString, Vc}; use turbo_tasks_fs::{FileContent, FileJsonContent}; use turbopack_core::{ @@ -79,11 +79,20 @@ impl ChunkableModule for JsonModuleAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use JsonModuleAsset", + )?; + Ok(Vc::upcast(JsonChunkItem::cell(JsonChunkItem { + module: self, + chunking_context, + }))) } } diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index b2e9eb3800db1c..325b691449bdf8 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -2,7 +2,7 @@ #![feature(arbitrary_self_types)] #![feature(async_fn_in_trait)] -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context, Result}; use mdxjs::{compile, Options}; use turbo_tasks::{Value, ValueDefault, Vc}; use turbo_tasks_fs::{rope::Rope, File, FileContent, FileSystemPath}; @@ -202,11 +202,20 @@ impl ChunkableModule for MdxModuleAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use MdxModuleAsset", + )?; + Ok(Vc::upcast(MdxChunkItem::cell(MdxChunkItem { + module: self, + chunking_context, + }))) } } diff --git a/crates/turbopack-static/src/lib.rs b/crates/turbopack-static/src/lib.rs index fd5fae3202b5f7..5893f406e39212 100644 --- a/crates/turbopack-static/src/lib.rs +++ b/crates/turbopack-static/src/lib.rs @@ -14,7 +14,7 @@ pub mod fixed; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context, Result}; use turbo_tasks::{Value, ValueToString, Vc}; use turbo_tasks_fs::FileContent; use turbopack_core::{ @@ -104,11 +104,21 @@ impl ChunkableModule for StaticModuleAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use StaticModuleAsset", + )?; + Ok(Vc::upcast(ModuleChunkItem::cell(ModuleChunkItem { + module: self, + chunking_context, + static_asset: self.static_asset(Vc::upcast(chunking_context)), + }))) } } diff --git a/crates/turbopack-wasm/src/module_asset.rs b/crates/turbopack-wasm/src/module_asset.rs index fd02d9e1e81714..c099be531b25c1 100644 --- a/crates/turbopack-wasm/src/module_asset.rs +++ b/crates/turbopack-wasm/src/module_asset.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use indexmap::indexmap; use turbo_tasks::{Value, Vc}; use turbo_tasks_fs::FileSystemPath; @@ -128,11 +128,24 @@ impl ChunkableModule for WebAssemblyModuleAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + WebAssemblyModuleAsset", + )?; + Ok(Vc::upcast( + ModuleChunkItem { + module: self, + chunking_context, + } + .cell(), + )) } } diff --git a/crates/turbopack-wasm/src/raw.rs b/crates/turbopack-wasm/src/raw.rs index fc38d0e9053984..c9c66b626745e4 100644 --- a/crates/turbopack-wasm/src/raw.rs +++ b/crates/turbopack-wasm/src/raw.rs @@ -1,4 +1,4 @@ -use anyhow::{bail, Result}; +use anyhow::{bail, Context, Result}; use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ asset::{Asset, AssetContent}, @@ -86,11 +86,25 @@ impl ChunkableModule for RawWebAssemblyModuleAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + RawWebAssemblyModuleAsset", + )?; + Ok(Vc::upcast( + RawModuleChunkItem { + module: self, + chunking_context, + wasm_asset: self.wasm_asset(Vc::upcast(chunking_context)), + } + .cell(), + )) } } From ce9e361c9f5293d2cd82f1cb8a5eb1a44207dc79 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Wed, 4 Oct 2023 22:19:27 -0400 Subject: [PATCH 3/5] Everything on ChunkItem --- .../src/ecmascript/node/content.rs | 1 + .../src/ecmascript/node/entry/chunk.rs | 7 +--- .../src/chunk/chunking_context.rs | 15 ++++++- crates/turbopack-core/src/chunk/mod.rs | 20 ++++++++- crates/turbopack-css/src/asset.rs | 38 ++++++++--------- crates/turbopack-css/src/chunk/mod.rs | 41 +++++++++++-------- crates/turbopack-css/src/module_asset.rs | 36 ++++++---------- .../src/ecmascript/content_entry.rs | 2 +- .../src/ecmascript/evaluate/chunk.rs | 7 +++- .../turbopack-ecmascript/src/chunk/context.rs | 19 +-------- crates/turbopack-ecmascript/src/chunk/item.rs | 22 +++------- crates/turbopack-ecmascript/src/chunk/mod.rs | 5 ++- .../src/chunk/placeable.rs | 5 --- .../src/chunk_group_files_asset.rs | 21 +++------- crates/turbopack-ecmascript/src/lib.rs | 34 ++++++++------- .../src/manifest/chunk_asset.rs | 17 +------- .../src/manifest/chunk_item.rs | 5 +++ .../src/manifest/loader_item.rs | 15 ++++--- .../src/references/esm/base.rs | 10 +++-- .../src/references/esm/module_id.rs | 20 +++++---- .../src/references/esm/url.rs | 11 +++-- .../src/references/pattern_mapping.rs | 9 ++-- .../src/references/require_context.rs | 31 ++++---------- .../src/tree_shake/asset.rs | 17 +------- .../src/tree_shake/chunk_item.rs | 7 +++- crates/turbopack-json/src/lib.rs | 16 +++----- crates/turbopack-mdx/src/lib.rs | 28 ++++++------- crates/turbopack-static/src/lib.rs | 17 +++----- crates/turbopack-wasm/src/module_asset.rs | 37 ++++++++--------- crates/turbopack-wasm/src/raw.rs | 20 +++------ 30 files changed, 237 insertions(+), 296 deletions(-) diff --git a/crates/turbopack-build/src/ecmascript/node/content.rs b/crates/turbopack-build/src/ecmascript/node/content.rs index b935edf9ba525d..6cca8255f1c781 100644 --- a/crates/turbopack-build/src/ecmascript/node/content.rs +++ b/crates/turbopack-build/src/ecmascript/node/content.rs @@ -6,6 +6,7 @@ use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbo_tasks_fs::File; use turbopack_core::{ asset::AssetContent, + chunk::ChunkItemExt, code_builder::{Code, CodeBuilder}, output::OutputAsset, source_map::{GenerateSourceMap, OptionSourceMap}, diff --git a/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs b/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs index fbb050889d9d07..42bd1d63193126 100644 --- a/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs +++ b/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs @@ -6,16 +6,13 @@ use turbo_tasks::{ValueToString, Vc}; use turbo_tasks_fs::{File, FileSystemPath}; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{ChunkingContext, EvaluatableAssets}, + chunk::{ChunkItemExt, ChunkableModule, ChunkingContext, EvaluatableAssets}, code_builder::{Code, CodeBuilder}, ident::AssetIdent, output::{OutputAsset, OutputAssets}, source_map::{GenerateSourceMap, OptionSourceMap, SourceMapAsset}, }; -use turbopack_ecmascript::{ - chunk::{EcmascriptChunkItemExt, EcmascriptChunkPlaceable}, - utils::StringifyJs, -}; +use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs}; use super::runtime::EcmascriptBuildNodeRuntimeChunk; use crate::BuildChunkingContext; diff --git a/crates/turbopack-core/src/chunk/chunking_context.rs b/crates/turbopack-core/src/chunk/chunking_context.rs index a9dcfe9fa34afd..9fbf680cee0f3d 100644 --- a/crates/turbopack-core/src/chunk/chunking_context.rs +++ b/crates/turbopack-core/src/chunk/chunking_context.rs @@ -1,9 +1,10 @@ use anyhow::Result; -use turbo_tasks::Vc; +use turbo_tasks::{ValueToString, Vc}; use turbo_tasks_fs::FileSystemPath; use super::{Chunk, EvaluatableAssets}; use crate::{ + chunk::{ChunkItem, ModuleId}, environment::Environment, ident::AssetIdent, module::Module, @@ -63,4 +64,16 @@ pub trait ChunkingContext { entry: Vc>, evaluatable_assets: Vc, ) -> Vc; + + async fn chunk_item_id( + self: Vc, + chunk_item: Vc>, + ) -> Result> { + let layer = self.layer(); + let mut ident = chunk_item.asset_ident(); + if !layer.await?.is_empty() { + ident = ident.with_modifier(layer) + } + Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell()) + } } diff --git a/crates/turbopack-core/src/chunk/mod.rs b/crates/turbopack-core/src/chunk/mod.rs index ef191515b6c704..a07eef67925ba7 100644 --- a/crates/turbopack-core/src/chunk/mod.rs +++ b/crates/turbopack-core/src/chunk/mod.rs @@ -22,7 +22,7 @@ use turbo_tasks::{ debug::ValueDebugFormat, graph::{AdjacencyMap, GraphTraversal, GraphTraversalResult, Visit, VisitControlFlow}, trace::TraceRawVcs, - ReadRef, TryJoinIterExt, Value, ValueToString, Vc, + ReadRef, TryJoinIterExt, Upcast, Value, ValueToString, Vc, }; use turbo_tasks_fs::FileSystemPath; use turbo_tasks_hash::DeterministicHash; @@ -686,7 +686,25 @@ pub trait ChunkItem { /// TODO(alexkirsz) This should have a default impl that returns empty /// references. fn references(self: Vc) -> Vc; + + fn chunking_context(self: Vc) -> Vc>; } #[turbo_tasks::value(transparent)] pub struct ChunkItems(Vec>>); + +pub trait ChunkItemExt: Send { + /// Returns the module id of this chunk item. + fn id(self: Vc) -> Vc; +} + +impl ChunkItemExt for T +where + T: Upcast>, +{ + /// Returns the module id of this chunk item. + fn id(self: Vc) -> Vc { + let chunk_item = Vc::upcast(self); + chunk_item.chunking_context().chunk_item_id(chunk_item) + } +} diff --git a/crates/turbopack-css/src/asset.rs b/crates/turbopack-css/src/asset.rs index a217b2b6f355cb..1108a9c0b47fc0 100644 --- a/crates/turbopack-css/src/asset.rs +++ b/crates/turbopack-css/src/asset.rs @@ -137,18 +137,7 @@ impl ChunkableModule for CssModuleAsset { } #[turbo_tasks::value_impl] -impl CssChunkPlaceable for CssModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast(CssModuleChunkItem::cell(CssModuleChunkItem { - module: self, - chunking_context, - })) - } -} +impl CssChunkPlaceable for CssModuleAsset {} #[turbo_tasks::value_impl] impl ResolveOrigin for CssModuleAsset { @@ -180,6 +169,11 @@ impl ChunkItem for CssModuleChunkItem { fn references(&self) -> Vc { self.module.references() } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] @@ -203,10 +197,12 @@ impl CssChunkItem for CssModuleChunkItem { if let Some(placeable) = Vc::try_resolve_downcast::>(module).await? { - imports.push(CssImport::Internal( - import_ref, - CssChunkPlaceable::as_chunk_item(placeable, chunking_context), - )); + let item = placeable.as_chunk_item(chunking_context); + if let Some(css_item) = + Vc::try_resolve_downcast::>(item).await? + { + imports.push(CssImport::Internal(import_ref, css_item)); + } } } } else if let Some(compose_ref) = @@ -221,10 +217,12 @@ impl CssChunkItem for CssModuleChunkItem { if let Some(placeable) = Vc::try_resolve_downcast::>(module).await? { - imports.push(CssImport::Composes(CssChunkPlaceable::as_chunk_item( - placeable, - chunking_context, - ))); + let item = placeable.as_chunk_item(chunking_context); + if let Some(css_item) = + Vc::try_resolve_downcast::>(item).await? + { + imports.push(CssImport::Composes(css_item)); + } } } } diff --git a/crates/turbopack-css/src/chunk/mod.rs b/crates/turbopack-css/src/chunk/mod.rs index 4d4aae3f44e976..7b457398caebfa 100644 --- a/crates/turbopack-css/src/chunk/mod.rs +++ b/crates/turbopack-css/src/chunk/mod.rs @@ -12,7 +12,7 @@ use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ availability_info::AvailabilityInfo, chunk_content, chunk_content_split, Chunk, - ChunkContentResult, ChunkItem, ChunkableModule, ChunkingContext, Chunks, + ChunkContentResult, ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, Chunks, FromChunkableModule, ModuleId, OutputChunk, OutputChunkRuntimeInfo, }, code_builder::{Code, CodeBuilder}, @@ -142,11 +142,15 @@ impl CssChunkContent { let mut body = CodeBuilder::default(); let mut external_imports = IndexSet::new(); for entry in this.main_entries.await?.iter() { - let entry_item = CssChunkPlaceable::as_chunk_item(*entry, this.chunking_context); + let entry_item = entry.as_chunk_item(this.chunking_context); - // TODO(WEB-1261) - for external_import in expand_imports(&mut body, entry_item).await? { - external_imports.insert(external_import.await?.to_owned()); + if let Some(css_item) = + Vc::try_resolve_downcast::>(entry_item).await? + { + // TODO(WEB-1261) + for external_import in expand_imports(&mut body, css_item).await? { + external_imports.insert(external_import.await?.to_owned()); + } } } @@ -323,7 +327,7 @@ impl OutputChunk for CssChunk { .main_entries .await? .iter() - .map(|&entry| CssChunkPlaceable::as_chunk_item(entry, self.chunking_context)) + .map(|&entry| entry.as_chunk_item(self.chunking_context)) .collect(); let included_ids = entries_chunk_items .iter() @@ -332,7 +336,12 @@ impl OutputChunk for CssChunk { let imports_chunk_items: Vec<_> = entries_chunk_items .iter() .map(|&chunk_item| async move { - Ok(chunk_item + let Some(css_item) = + Vc::try_resolve_downcast::>(chunk_item).await? + else { + return Ok(vec![]); + }; + Ok(css_item .content() .await? .imports @@ -492,13 +501,9 @@ impl CssChunkContext { } } +// TODO: remove #[turbo_tasks::value_trait] -pub trait CssChunkPlaceable: ChunkableModule + Module + Asset { - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc>; -} +pub trait CssChunkPlaceable: ChunkableModule + Module + Asset {} #[turbo_tasks::value(transparent)] pub struct CssChunkPlaceables(Vec>>); @@ -523,7 +528,7 @@ pub trait CssChunkItem: ChunkItem { fn content(self: Vc) -> Vc; fn chunking_context(self: Vc) -> Vc>; fn id(self: Vc) -> Vc { - CssChunkContext::of(self.chunking_context()).chunk_item_id(self) + CssChunkContext::of(CssChunkItem::chunking_context(self)).chunk_item_id(self) } } @@ -536,10 +541,10 @@ impl FromChunkableModule for Box { if let Some(placeable) = Vc::try_resolve_downcast::>(asset).await? { - return Ok(Some(CssChunkPlaceable::as_chunk_item( - placeable, - chunking_context, - ))); + let item = placeable.as_chunk_item(chunking_context); + if let Some(css_item) = Vc::try_resolve_downcast::>(item).await? { + return Ok(Some(css_item)); + } } Ok(None) } diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index a2b46be6c47971..76f5cc43675720 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -12,7 +12,8 @@ use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ - availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkableModule, ChunkingContext, + availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkItemExt, ChunkableModule, + ChunkingContext, }, context::AssetContext, ident::AssetIdent, @@ -25,8 +26,8 @@ use turbopack_core::{ }; use turbopack_ecmascript::{ chunk::{ - EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemExt, - EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports, + EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, + EcmascriptChunkingContext, EcmascriptExports, }, utils::StringifyJs, ParseResultSourceMap, @@ -246,20 +247,6 @@ impl ChunkableModule for ModuleCssAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for ModuleCssAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast( - ModuleChunkItem { - chunking_context, - module: self, - } - .cell(), - ) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.cell() @@ -296,6 +283,11 @@ impl ChunkItem for ModuleChunkItem { fn references(&self) -> Vc { self.module.references() } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] @@ -358,12 +350,10 @@ impl EcmascriptChunkItem for ModuleChunkItem { let placeable: Vc> = Vc::upcast(css_module); - let module_id = EcmascriptChunkPlaceable::as_chunk_item( - placeable, - self.chunking_context, - ) - .id() - .await?; + let module_id = placeable + .as_chunk_item(Vc::upcast(self.chunking_context)) + .id() + .await?; let module_id = StringifyJs(&*module_id); let original_name = StringifyJs(&original_name); exported_class_names.push(format! { diff --git a/crates/turbopack-dev/src/ecmascript/content_entry.rs b/crates/turbopack-dev/src/ecmascript/content_entry.rs index 60f841a9469bae..4a48b8c744a103 100644 --- a/crates/turbopack-dev/src/ecmascript/content_entry.rs +++ b/crates/turbopack-dev/src/ecmascript/content_entry.rs @@ -5,7 +5,7 @@ use indexmap::IndexMap; use tracing::{info_span, Instrument}; use turbo_tasks::{ReadRef, TryJoinIterExt, Value, ValueToString, Vc}; use turbopack_core::{ - chunk::{availability_info::AvailabilityInfo, ChunkItem, ModuleId}, + chunk::{availability_info::AvailabilityInfo, ChunkItem, ChunkItemExt, ModuleId}, code_builder::{Code, CodeBuilder}, error::PrettyPrintError, issue::{code_gen::CodeGenerationIssue, IssueExt, IssueSeverity}, diff --git a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs index 38d76e744e8d91..a6e1e92865b74e 100644 --- a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs @@ -7,7 +7,10 @@ use turbo_tasks::{ReadRef, TryJoinIterExt, Value, ValueToString, Vc}; use turbo_tasks_fs::File; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{Chunk, ChunkData, ChunkingContext, ChunksData, EvaluatableAssets, ModuleId}, + chunk::{ + Chunk, ChunkData, ChunkItemExt, ChunkableModule, ChunkingContext, ChunksData, + EvaluatableAssets, ModuleId, + }, code_builder::{Code, CodeBuilder}, ident::AssetIdent, module::Module, @@ -15,7 +18,7 @@ use turbopack_core::{ source_map::{GenerateSourceMap, OptionSourceMap, SourceMapAsset}, }; use turbopack_ecmascript::{ - chunk::{EcmascriptChunkData, EcmascriptChunkItemExt, EcmascriptChunkPlaceable}, + chunk::{EcmascriptChunkData, EcmascriptChunkPlaceable}, utils::StringifyJs, }; use turbopack_ecmascript_runtime::RuntimeType; diff --git a/crates/turbopack-ecmascript/src/chunk/context.rs b/crates/turbopack-ecmascript/src/chunk/context.rs index 6b9ff4973dfdb6..d558e1d701ce23 100644 --- a/crates/turbopack-ecmascript/src/chunk/context.rs +++ b/crates/turbopack-ecmascript/src/chunk/context.rs @@ -1,8 +1,5 @@ -use anyhow::Result; -use turbo_tasks::{ValueToString, Vc}; -use turbopack_core::chunk::{ChunkItem, ChunkingContext, ModuleId}; - -use super::item::EcmascriptChunkItem; +use turbo_tasks::Vc; +use turbopack_core::chunk::ChunkingContext; /// [`EcmascriptChunkingContext`] must be implemented by [`ChunkingContext`] /// implementors that want to operate on [`EcmascriptChunk`]s. @@ -13,16 +10,4 @@ pub trait EcmascriptChunkingContext: ChunkingContext { fn has_react_refresh(self: Vc) -> Vc { Vc::cell(false) } - - async fn chunk_item_id( - self: Vc, - chunk_item: Vc>, - ) -> Result> { - let layer = self.layer(); - let mut ident = chunk_item.asset_ident(); - if !layer.await?.is_empty() { - ident = ident.with_modifier(layer) - } - Ok(ModuleId::String(ident.to_string().await?.clone_value()).cell()) - } } diff --git a/crates/turbopack-ecmascript/src/chunk/item.rs b/crates/turbopack-ecmascript/src/chunk/item.rs index 45029419c99999..09c21b10b53725 100644 --- a/crates/turbopack-ecmascript/src/chunk/item.rs +++ b/crates/turbopack-ecmascript/src/chunk/item.rs @@ -7,7 +7,7 @@ use turbo_tasks_fs::rope::Rope; use turbopack_core::{ chunk::{ availability_info::AvailabilityInfo, available_modules::AvailableAssets, ChunkItem, - ChunkableModule, ChunkingContext, FromChunkableModule, ModuleId, + ChunkItemExt, ChunkableModule, ChunkingContext, FromChunkableModule, }, code_builder::{Code, CodeBuilder}, error::PrettyPrintError, @@ -180,9 +180,6 @@ pub trait EcmascriptChunkItem: ChunkItem { } pub trait EcmascriptChunkItemExt: Send { - /// Returns the module id of this chunk item. - fn id(self: Vc) -> Vc; - /// Generates the module factory for this chunk item. fn code(self: Vc, availability_info: Value) -> Vc; } @@ -191,12 +188,6 @@ impl EcmascriptChunkItemExt for T where T: Upcast>, { - /// Returns the module id of this chunk item. - fn id(self: Vc) -> Vc { - let chunk_item = Vc::upcast(self); - chunk_item.chunking_context().chunk_item_id(chunk_item) - } - /// Generates the module factory for this chunk item. fn code(self: Vc, availability_info: Value) -> Vc { module_factory_with_code_generation_issue(Vc::upcast(self), availability_info) @@ -255,16 +246,13 @@ impl FromChunkableModule for Box { return Ok(None); }; - let Some(context) = - Vc::try_resolve_sidecast::>(chunking_context) - .await? + let item = placeable.as_chunk_item(Vc::upcast(chunking_context)); + let Some(ecmascript_item) = + Vc::try_resolve_sidecast::>(item).await? else { return Ok(None); }; - - Ok(Some(EcmascriptChunkPlaceable::as_chunk_item( - placeable, context, - ))) + Ok(Some(ecmascript_item)) } async fn from_async_asset( diff --git a/crates/turbopack-ecmascript/src/chunk/mod.rs b/crates/turbopack-ecmascript/src/chunk/mod.rs index ee627599b79de8..dbd034d511544d 100644 --- a/crates/turbopack-ecmascript/src/chunk/mod.rs +++ b/crates/turbopack-ecmascript/src/chunk/mod.rs @@ -14,7 +14,8 @@ use turbo_tasks_fs::FileSystemPathOption; use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ - availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkingContext, Chunks, ModuleIds, + availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkItemExt, ChunkableModule, + ChunkingContext, Chunks, ModuleIds, }, ident::AssetIdent, introspect::{ @@ -165,7 +166,7 @@ impl EcmascriptChunk { .main_entries .await? .iter() - .map(|&entry| entry.as_chunk_item(this.chunking_context).id()) + .map(|&entry| entry.as_chunk_item(Vc::upcast(this.chunking_context)).id()) .collect(); Ok(Vc::cell(entries)) } diff --git a/crates/turbopack-ecmascript/src/chunk/placeable.rs b/crates/turbopack-ecmascript/src/chunk/placeable.rs index 04bcc7fe156e34..25000d3302e706 100644 --- a/crates/turbopack-ecmascript/src/chunk/placeable.rs +++ b/crates/turbopack-ecmascript/src/chunk/placeable.rs @@ -1,15 +1,10 @@ use turbo_tasks::Vc; use turbopack_core::{asset::Asset, chunk::ChunkableModule, module::Module}; -use super::{item::EcmascriptChunkItem, EcmascriptChunkingContext}; use crate::references::{async_module::OptionAsyncModule, esm::EsmExports}; #[turbo_tasks::value_trait] pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset { - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc>; fn get_exports(self: Vc) -> Vc; fn get_async_module(self: Vc) -> Vc { Vc::cell(None) diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index 6d6e622d70680f..8f7bf3e4eed962 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -129,22 +129,6 @@ impl ChunkableModule for ChunkGroupFilesAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for ChunkGroupFilesAsset { - #[turbo_tasks::function] - async fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Result>> { - let this = self.await?; - Ok(Vc::upcast( - ChunkGroupFilesChunkItem { - chunking_context, - client_root: this.client_root, - inner: self, - } - .cell(), - )) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.cell() @@ -249,6 +233,11 @@ impl ChunkItem for ChunkGroupFilesChunkItem { .collect(), )) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index 7d4ea9de1ffcab..69f1e43ab15f8b 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -28,7 +28,7 @@ pub mod typescript; pub mod utils; pub mod webpack; -use anyhow::Result; +use anyhow::{Context, Result}; use chunk::{ EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkPlaceables, EcmascriptChunkingContext, }; @@ -417,27 +417,26 @@ impl ChunkableModule for EcmascriptModuleAsset { } #[turbo_tasks::function] - fn as_chunk_item( + async fn as_chunk_item( self: Vc, chunking_context: Vc>, - ) -> Vc> { - todo!(); + ) -> Result>> { + let chunking_context = + Vc::try_resolve_sidecast::>(chunking_context) + .await? + .context( + "chunking context must impl EcmascriptChunkingContext to use \ + EcmascriptModuleAsset", + )?; + Ok(Vc::upcast(ModuleChunkItem::cell(ModuleChunkItem { + module: self, + chunking_context, + }))) } } #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for EcmascriptModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast(ModuleChunkItem::cell(ModuleChunkItem { - module: self, - chunking_context, - })) - } - #[turbo_tasks::function] async fn get_exports(self: Vc) -> Result> { Ok(self.failsafe_analyze().await?.exports) @@ -495,6 +494,11 @@ impl ChunkItem for ModuleChunkItem { fn references(&self) -> Vc { self.module.references() } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index d0e200f0e3bb53..25382086c1d2a8 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -11,8 +11,7 @@ use turbopack_core::{ use super::chunk_item::ManifestChunkItem; use crate::chunk::{ - EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkPlaceable, EcmascriptChunkingContext, - EcmascriptExports, + EcmascriptChunk, EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports, }; #[turbo_tasks::function] @@ -156,20 +155,6 @@ impl ChunkableModule for ManifestChunkAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for ManifestChunkAsset { - #[turbo_tasks::function] - async fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Result>> { - Ok(Vc::upcast( - ManifestChunkItem { - chunking_context, - manifest: self, - } - .cell(), - )) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.cell() diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs index 01f097fa0c8fd6..15d54d3358bca6 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs @@ -91,4 +91,9 @@ impl ChunkItem for ManifestChunkItem { Ok(Vc::cell(references)) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } diff --git a/crates/turbopack-ecmascript/src/manifest/loader_item.rs b/crates/turbopack-ecmascript/src/manifest/loader_item.rs index 3f3338f9876a63..c145bd97d2e155 100644 --- a/crates/turbopack-ecmascript/src/manifest/loader_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/loader_item.rs @@ -4,7 +4,7 @@ use anyhow::{anyhow, Result}; use indoc::writedoc; use turbo_tasks::{TryJoinIterExt, Vc}; use turbopack_core::{ - chunk::{ChunkData, ChunkItem, ChunkingContext, ChunksData}, + chunk::{ChunkData, ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, ChunksData}, ident::AssetIdent, module::Module, reference::{ModuleReference, ModuleReferences, SingleOutputAssetReference}, @@ -13,8 +13,8 @@ use turbopack_core::{ use super::chunk_asset::ManifestChunkAsset; use crate::{ chunk::{ - data::EcmascriptChunkData, item::EcmascriptChunkItemExt, EcmascriptChunkItem, - EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkingContext, + data::EcmascriptChunkData, EcmascriptChunkItem, EcmascriptChunkItemContent, + EcmascriptChunkPlaceable, EcmascriptChunkingContext, }, utils::StringifyJs, }; @@ -105,6 +105,11 @@ impl ChunkItem for ManifestLoaderItem { Ok(Vc::cell(references)) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Result>> { + Ok(Vc::upcast(self.manifest.await?.chunking_context)) + } } #[turbo_tasks::value_impl] @@ -131,7 +136,7 @@ impl EcmascriptChunkItem for ManifestLoaderItem { // exports a promise for all of the necessary chunk loads. let item_id = &*this .manifest - .as_chunk_item(manifest.chunking_context) + .as_chunk_item(Vc::upcast(manifest.chunking_context)) .id() .await?; @@ -142,7 +147,7 @@ impl EcmascriptChunkItem for ManifestLoaderItem { .await? .ok_or_else(|| anyhow!("asset is not placeable in ecmascript chunk"))?; let dynamic_id = &*placeable - .as_chunk_item(manifest.chunking_context) + .as_chunk_item(Vc::upcast(manifest.chunking_context)) .id() .await?; diff --git a/crates/turbopack-ecmascript/src/references/esm/base.rs b/crates/turbopack-ecmascript/src/references/esm/base.rs index a23ecae70b3b99..42f0fd4b2c9cdd 100644 --- a/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -8,7 +8,8 @@ use swc_core::{ use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ chunk::{ - ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption, ModuleId, + ChunkItemExt, ChunkableModule, ChunkableModuleReference, ChunkingContext, ChunkingType, + ChunkingTypeOption, ModuleId, }, issue::{IssueSeverity, OptionIssueSource}, module::Module, @@ -23,7 +24,7 @@ use turbopack_core::{ use crate::{ analyzer::imports::ImportAnnotations, - chunk::{item::EcmascriptChunkItemExt, EcmascriptChunkPlaceable, EcmascriptChunkingContext}, + chunk::{EcmascriptChunkPlaceable, EcmascriptChunkingContext}, code_gen::{CodeGenerateable, CodeGeneration}, create_visitor, magic_identifier, references::util::{request_to_string, throw_module_not_found_expr}, @@ -233,7 +234,10 @@ impl CodeGenerateable for EsmAssetReference { if let Some(ident) = referenced_asset.get_ident().await? { match &*referenced_asset { ReferencedAsset::Some(asset) => { - let id = asset.as_chunk_item(chunking_context).id().await?; + let id = asset + .as_chunk_item(Vc::upcast(chunking_context)) + .id() + .await?; visitors.push(create_visitor!(visit_mut_program(program: &mut Program) { let stmt = quote!( "var $name = __turbopack_import__($id);" as Stmt, diff --git a/crates/turbopack-ecmascript/src/references/esm/module_id.rs b/crates/turbopack-ecmascript/src/references/esm/module_id.rs index 2443692717eb80..ab74a9ba90cc97 100644 --- a/crates/turbopack-ecmascript/src/references/esm/module_id.rs +++ b/crates/turbopack-ecmascript/src/references/esm/module_id.rs @@ -2,14 +2,16 @@ use anyhow::Result; use swc_core::{ecma::ast::Expr, quote}; use turbo_tasks::{ValueToString, Vc}; use turbopack_core::{ - chunk::{ChunkableModuleReference, ChunkingTypeOption, ModuleId}, + chunk::{ + ChunkItemExt, ChunkableModule, ChunkableModuleReference, ChunkingTypeOption, ModuleId, + }, reference::ModuleReference, resolve::ModuleResolveResult, }; use super::{base::ReferencedAsset, EsmAssetReference}; use crate::{ - chunk::{item::EcmascriptChunkItemExt, EcmascriptChunkPlaceable, EcmascriptChunkingContext}, + chunk::EcmascriptChunkingContext, code_gen::{CodeGenerateable, CodeGeneration}, create_visitor, references::AstPath, @@ -67,13 +69,17 @@ impl CodeGenerateable for EsmModuleIdAssetReference { let mut visitors = Vec::new(); if let ReferencedAsset::Some(asset) = &*self.inner.get_referenced_asset().await? { - let id = asset.as_chunk_item(chunking_context).id().await?; + let id = asset + .as_chunk_item(Vc::upcast(chunking_context)) + .id() + .await?; + let id = Expr::Lit(match &*id { + ModuleId::String(s) => s.clone().into(), + ModuleId::Number(n) => (*n as f64).into(), + }); visitors.push( create_visitor!(self.ast_path.await?, visit_mut_expr(expr: &mut Expr) { - *expr = Expr::Lit(match &*id { - ModuleId::String(s) => s.clone().into(), - ModuleId::Number(n) => (*n as f64).into(), - }) + *expr = id.clone() }), ); } else { diff --git a/crates/turbopack-ecmascript/src/references/esm/url.rs b/crates/turbopack-ecmascript/src/references/esm/url.rs index bf58806b53959e..28e4c6d845c104 100644 --- a/crates/turbopack-ecmascript/src/references/esm/url.rs +++ b/crates/turbopack-ecmascript/src/references/esm/url.rs @@ -5,7 +5,9 @@ use swc_core::{ }; use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ - chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption}, + chunk::{ + ChunkItemExt, ChunkableModule, ChunkableModuleReference, ChunkingType, ChunkingTypeOption, + }, environment::Rendering, issue::{code_gen::CodeGenerationIssue, IssueExt, IssueSeverity, IssueSource}, reference::ModuleReference, @@ -15,7 +17,7 @@ use turbopack_core::{ use super::base::ReferencedAsset; use crate::{ - chunk::{item::EcmascriptChunkItemExt, EcmascriptChunkPlaceable, EcmascriptChunkingContext}, + chunk::EcmascriptChunkingContext, code_gen::{CodeGenerateable, CodeGeneration}, create_visitor, references::AstPath, @@ -151,7 +153,10 @@ impl CodeGenerateable for UrlAssetReference { ReferencedAsset::Some(asset) => { // We rewrite the first `new URL()` arguments to be a require() of the chunk // item, which exports the static asset path to the linked file. - let id = asset.as_chunk_item(chunking_context).id().await?; + let id = asset + .as_chunk_item(Vc::upcast(chunking_context)) + .id() + .await?; visitors.push( create_visitor!(ast_path, visit_mut_expr(new_expr: &mut Expr) { diff --git a/crates/turbopack-ecmascript/src/references/pattern_mapping.rs b/crates/turbopack-ecmascript/src/references/pattern_mapping.rs index 6c05ceb2b51397..4cbb6c37304aa7 100644 --- a/crates/turbopack-ecmascript/src/references/pattern_mapping.rs +++ b/crates/turbopack-ecmascript/src/references/pattern_mapping.rs @@ -7,8 +7,8 @@ use swc_core::{ use turbo_tasks::{debug::ValueDebug, Value, Vc}; use turbopack_core::{ chunk::{ - availability_info::AvailabilityInfo, ChunkableModule, ChunkingContext, FromChunkableModule, - ModuleId, + availability_info::AvailabilityInfo, ChunkItemExt, ChunkableModule, ChunkingContext, + FromChunkableModule, ModuleId, }, issue::{code_gen::CodeGenerationIssue, IssueExt, IssueSeverity}, resolve::{ @@ -17,10 +17,7 @@ use turbopack_core::{ }; use super::util::{request_to_string, throw_module_not_found_expr}; -use crate::{ - chunk::{item::EcmascriptChunkItemExt, EcmascriptChunkItem}, - utils::module_id_to_lit, -}; +use crate::{chunk::EcmascriptChunkItem, utils::module_id_to_lit}; /// A mapping from a request pattern (e.g. "./module", `./images/${name}.png`) /// to corresponding module ids. The same pattern can map to multiple module ids diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index d9537d800bd3be..ece2d03651a8d0 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -18,7 +18,7 @@ use turbo_tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemPath}; use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ - availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkableModule, + availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkItemExt, ChunkableModule, ChunkableModuleReference, ChunkingContext, }, ident::AssetIdent, @@ -31,8 +31,8 @@ use turbopack_core::{ use crate::{ chunk::{ - item::EcmascriptChunkItemExt, EcmascriptChunk, EcmascriptChunkItem, - EcmascriptChunkItemContent, EcmascriptChunkingContext, EcmascriptExports, + EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkItemContent, + EcmascriptChunkingContext, EcmascriptExports, }, code_gen::CodeGeneration, create_visitor, @@ -282,7 +282,7 @@ impl CodeGenerateable for RequireContextAssetReference { &self, chunking_context: Vc>, ) -> Result> { - let chunk_item = EcmascriptChunkPlaceable::as_chunk_item(self.inner, chunking_context); + let chunk_item = self.inner.as_chunk_item(Vc::upcast(chunking_context)); let module_id = chunk_item.id().await?.clone_value(); let mut visitors = Vec::new(); @@ -415,24 +415,6 @@ impl ChunkableModule for RequireContextAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for RequireContextAsset { - #[turbo_tasks::function] - async fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Result>> { - let this = self.await?; - Ok(Vc::upcast( - RequireContextChunkItem { - chunking_context, - inner: self, - - origin: this.origin, - map: this.map, - } - .cell(), - )) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.cell() @@ -540,4 +522,9 @@ impl ChunkItem for RequireContextChunkItem { fn references(&self) -> Vc { self.inner.references() } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index 86292300e5371e..8b68dffd0a72b4 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -12,8 +12,7 @@ use turbopack_core::{ use super::{chunk_item::EcmascriptModulePartChunkItem, get_part_id, split_module, SplitResult}; use crate::{ chunk::{ - EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkPlaceable, EcmascriptChunkingContext, - EcmascriptExports, + EcmascriptChunk, EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports, }, references::analyze_ecmascript_module, AnalyzeEcmascriptModuleResult, EcmascriptModuleAsset, @@ -108,20 +107,6 @@ impl Asset for EcmascriptModulePartAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset { - #[turbo_tasks::function] - async fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Result>> { - Ok(Vc::upcast( - EcmascriptModulePartChunkItem { - module: self, - chunking_context, - } - .cell(), - )) - } - #[turbo_tasks::function] async fn get_exports(self: Vc) -> Result> { Ok(self.analyze().await?.exports) diff --git a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs index 4e9939a1b866b7..560b8c6df79457 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs @@ -1,7 +1,7 @@ use anyhow::Result; use turbo_tasks::{Value, Vc}; use turbopack_core::{ - chunk::{availability_info::AvailabilityInfo, ChunkItem}, + chunk::{availability_info::AvailabilityInfo, ChunkItem, ChunkingContext}, ident::AssetIdent, module::Module, reference::ModuleReferences, @@ -86,4 +86,9 @@ impl ChunkItem for EcmascriptModulePartChunkItem { async fn asset_ident(&self) -> Result> { Ok(self.module.ident()) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } diff --git a/crates/turbopack-json/src/lib.rs b/crates/turbopack-json/src/lib.rs index 236f0d216bf816..f92edb65f9c804 100644 --- a/crates/turbopack-json/src/lib.rs +++ b/crates/turbopack-json/src/lib.rs @@ -98,17 +98,6 @@ impl ChunkableModule for JsonModuleAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for JsonModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast(JsonChunkItem::cell(JsonChunkItem { - module: self, - chunking_context, - })) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.cell() @@ -132,6 +121,11 @@ impl ChunkItem for JsonChunkItem { fn references(&self) -> Vc { self.module.references() } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index 325b691449bdf8..0d5f2ab69753aa 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -221,17 +221,6 @@ impl ChunkableModule for MdxModuleAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for MdxModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast(MdxChunkItem::cell(MdxChunkItem { - module: self, - chunking_context, - })) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.cell() @@ -268,6 +257,11 @@ impl ChunkItem for MdxChunkItem { fn references(&self) -> Vc { self.module.references() } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] @@ -281,11 +275,13 @@ impl EcmascriptChunkItem for MdxChunkItem { /// apply all of the ecma transforms #[turbo_tasks::function] async fn content(&self) -> Result> { - Ok(EcmascriptChunkPlaceable::as_chunk_item( - into_ecmascript_module_asset(&self.module).await?, - self.chunking_context, - ) - .content()) + let item = into_ecmascript_module_asset(&self.module) + .await? + .as_chunk_item(Vc::upcast(self.chunking_context)); + let ecmascript_item = Vc::try_resolve_sidecast::>(item) + .await? + .context("MdxChunkItem must generate an EcmascriptChunkItem")?; + Ok(ecmascript_item.content()) } } diff --git a/crates/turbopack-static/src/lib.rs b/crates/turbopack-static/src/lib.rs index 5893f406e39212..c45e040f586b1f 100644 --- a/crates/turbopack-static/src/lib.rs +++ b/crates/turbopack-static/src/lib.rs @@ -124,18 +124,6 @@ impl ChunkableModule for StaticModuleAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for StaticModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast(ModuleChunkItem::cell(ModuleChunkItem { - module: self, - chunking_context, - static_asset: self.static_asset(Vc::upcast(chunking_context)), - })) - } - #[turbo_tasks::function] fn get_exports(&self) -> Vc { EcmascriptExports::Value.into() @@ -215,6 +203,11 @@ impl ChunkItem for ModuleChunkItem { )), ))])) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-wasm/src/module_asset.rs b/crates/turbopack-wasm/src/module_asset.rs index c099be531b25c1..7def8f43a01787 100644 --- a/crates/turbopack-wasm/src/module_asset.rs +++ b/crates/turbopack-wasm/src/module_asset.rs @@ -151,20 +151,6 @@ impl ChunkableModule for WebAssemblyModuleAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for WebAssemblyModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast( - ModuleChunkItem { - module: self, - chunking_context, - } - .cell(), - ) - } - #[turbo_tasks::function] fn get_exports(self: Vc) -> Vc { self.loader().get_exports() @@ -209,11 +195,18 @@ impl ChunkItem for ModuleChunkItem { #[turbo_tasks::function] async fn references(&self) -> Result> { - let loader = - EcmascriptChunkPlaceable::as_chunk_item(self.module.loader(), self.chunking_context); + let loader = self + .module + .loader() + .as_chunk_item(Vc::upcast(self.chunking_context)); Ok(loader.references()) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] @@ -234,11 +227,15 @@ impl EcmascriptChunkItem for ModuleChunkItem { availability_info: Value, ) -> Result> { let loader_asset = self.module.loader(); + let item = loader_asset.as_chunk_item(Vc::upcast(self.chunking_context)); + + let ecmascript_item = Vc::try_resolve_sidecast::>(item) + .await? + .context("EcmascriptModuleAsset must implement EcmascriptChunkItem")?; - let chunk_item_content = - EcmascriptChunkPlaceable::as_chunk_item(loader_asset, self.chunking_context) - .content_with_availability_info(availability_info) - .await?; + let chunk_item_content = ecmascript_item + .content_with_availability_info(availability_info) + .await?; Ok(EcmascriptChunkItemContent { options: EcmascriptChunkItemOptions { diff --git a/crates/turbopack-wasm/src/raw.rs b/crates/turbopack-wasm/src/raw.rs index c9c66b626745e4..4b9e121b6261e1 100644 --- a/crates/turbopack-wasm/src/raw.rs +++ b/crates/turbopack-wasm/src/raw.rs @@ -110,21 +110,6 @@ impl ChunkableModule for RawWebAssemblyModuleAsset { #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for RawWebAssemblyModuleAsset { - #[turbo_tasks::function] - fn as_chunk_item( - self: Vc, - chunking_context: Vc>, - ) -> Vc> { - Vc::upcast( - RawModuleChunkItem { - module: self, - chunking_context, - wasm_asset: self.wasm_asset(Vc::upcast(chunking_context)), - } - .cell(), - ) - } - #[turbo_tasks::function] fn get_exports(self: Vc) -> Vc { EcmascriptExports::Value.cell() @@ -155,6 +140,11 @@ impl ChunkItem for RawModuleChunkItem { )), ))])) } + + #[turbo_tasks::function] + async fn chunking_context(&self) -> Vc> { + Vc::upcast(self.chunking_context) + } } #[turbo_tasks::value_impl] From e98d949dfb6f466cfee116745aac09e6fec1ae77 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 5 Oct 2023 06:34:08 +0000 Subject: [PATCH 4/5] use downcast instead of sidecast --- crates/turbopack-css/src/module_asset.rs | 2 +- crates/turbopack-ecmascript/src/chunk/item.rs | 4 ++-- crates/turbopack-ecmascript/src/chunk/mod.rs | 4 ++-- crates/turbopack-ecmascript/src/chunk_group_files_asset.rs | 2 +- crates/turbopack-ecmascript/src/lib.rs | 2 +- crates/turbopack-ecmascript/src/manifest/chunk_asset.rs | 2 +- crates/turbopack-ecmascript/src/references/require_context.rs | 2 +- crates/turbopack-ecmascript/src/tree_shake/asset.rs | 2 +- crates/turbopack-json/src/lib.rs | 2 +- crates/turbopack-mdx/src/lib.rs | 4 ++-- crates/turbopack-static/src/lib.rs | 2 +- crates/turbopack-wasm/src/module_asset.rs | 4 ++-- crates/turbopack-wasm/src/raw.rs | 2 +- 13 files changed, 17 insertions(+), 17 deletions(-) diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index 76f5cc43675720..d83ab6787524b7 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -230,7 +230,7 @@ impl ChunkableModule for ModuleCssAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use ModuleCssAsset", diff --git a/crates/turbopack-ecmascript/src/chunk/item.rs b/crates/turbopack-ecmascript/src/chunk/item.rs index 09c21b10b53725..0c032a71f4f802 100644 --- a/crates/turbopack-ecmascript/src/chunk/item.rs +++ b/crates/turbopack-ecmascript/src/chunk/item.rs @@ -248,7 +248,7 @@ impl FromChunkableModule for Box { let item = placeable.as_chunk_item(Vc::upcast(chunking_context)); let Some(ecmascript_item) = - Vc::try_resolve_sidecast::>(item).await? + Vc::try_resolve_downcast::>(item).await? else { return Ok(None); }; @@ -261,7 +261,7 @@ impl FromChunkableModule for Box { availability_info: Value, ) -> Result>> { let Some(context) = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? else { return Ok(None); diff --git a/crates/turbopack-ecmascript/src/chunk/mod.rs b/crates/turbopack-ecmascript/src/chunk/mod.rs index dbd034d511544d..3d48a2ba3fcccb 100644 --- a/crates/turbopack-ecmascript/src/chunk/mod.rs +++ b/crates/turbopack-ecmascript/src/chunk/mod.rs @@ -77,7 +77,7 @@ impl EcmascriptChunk { availability_info: Value, ) -> Result> { let Some(context) = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? else { bail!("Ecmascript chunking context not found"); @@ -97,7 +97,7 @@ impl EcmascriptChunk { main_entry: Vc>, ) -> Result> { let Some(context) = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? else { bail!("Ecmascript chunking context not found"); diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index 8f7bf3e4eed962..98ead0e14df1de 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -110,7 +110,7 @@ impl ChunkableModule for ChunkGroupFilesAsset { ) -> Result>> { let this = self.await?; let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index 69f1e43ab15f8b..fc2dcd52e6fe48 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -422,7 +422,7 @@ impl ChunkableModule for EcmascriptModuleAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index 25382086c1d2a8..705d67544c2497 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -137,7 +137,7 @@ impl ChunkableModule for ManifestChunkAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index ece2d03651a8d0..801e46ab2b8bcd 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -393,7 +393,7 @@ impl ChunkableModule for RequireContextAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index 8b68dffd0a72b4..4cc25dcfd904c2 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -134,7 +134,7 @@ impl ChunkableModule for EcmascriptModulePartAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ diff --git a/crates/turbopack-json/src/lib.rs b/crates/turbopack-json/src/lib.rs index f92edb65f9c804..d1ae37a266046c 100644 --- a/crates/turbopack-json/src/lib.rs +++ b/crates/turbopack-json/src/lib.rs @@ -84,7 +84,7 @@ impl ChunkableModule for JsonModuleAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use JsonModuleAsset", diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index 0d5f2ab69753aa..870dff5401b765 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -207,7 +207,7 @@ impl ChunkableModule for MdxModuleAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use MdxModuleAsset", @@ -278,7 +278,7 @@ impl EcmascriptChunkItem for MdxChunkItem { let item = into_ecmascript_module_asset(&self.module) .await? .as_chunk_item(Vc::upcast(self.chunking_context)); - let ecmascript_item = Vc::try_resolve_sidecast::>(item) + let ecmascript_item = Vc::try_resolve_downcast::>(item) .await? .context("MdxChunkItem must generate an EcmascriptChunkItem")?; Ok(ecmascript_item.content()) diff --git a/crates/turbopack-static/src/lib.rs b/crates/turbopack-static/src/lib.rs index c45e040f586b1f..1b646d6bff240d 100644 --- a/crates/turbopack-static/src/lib.rs +++ b/crates/turbopack-static/src/lib.rs @@ -109,7 +109,7 @@ impl ChunkableModule for StaticModuleAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use StaticModuleAsset", diff --git a/crates/turbopack-wasm/src/module_asset.rs b/crates/turbopack-wasm/src/module_asset.rs index 7def8f43a01787..3e876bf4b11bdc 100644 --- a/crates/turbopack-wasm/src/module_asset.rs +++ b/crates/turbopack-wasm/src/module_asset.rs @@ -133,7 +133,7 @@ impl ChunkableModule for WebAssemblyModuleAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ @@ -229,7 +229,7 @@ impl EcmascriptChunkItem for ModuleChunkItem { let loader_asset = self.module.loader(); let item = loader_asset.as_chunk_item(Vc::upcast(self.chunking_context)); - let ecmascript_item = Vc::try_resolve_sidecast::>(item) + let ecmascript_item = Vc::try_resolve_downcast::>(item) .await? .context("EcmascriptModuleAsset must implement EcmascriptChunkItem")?; diff --git a/crates/turbopack-wasm/src/raw.rs b/crates/turbopack-wasm/src/raw.rs index 4b9e121b6261e1..231c670193d59c 100644 --- a/crates/turbopack-wasm/src/raw.rs +++ b/crates/turbopack-wasm/src/raw.rs @@ -91,7 +91,7 @@ impl ChunkableModule for RawWebAssemblyModuleAsset { chunking_context: Vc>, ) -> Result>> { let chunking_context = - Vc::try_resolve_sidecast::>(chunking_context) + Vc::try_resolve_downcast::>(chunking_context) .await? .context( "chunking context must impl EcmascriptChunkingContext to use \ From 53c78b14dcbd986a54cd3e42f841eb2e8711b121 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 5 Oct 2023 08:34:53 +0000 Subject: [PATCH 5/5] move as_chunk/as_root_chunk to ChunkItem::as_chunk --- .../turbopack-build/src/chunking_context.rs | 2 +- crates/turbopack-cli/src/build/mod.rs | 2 +- crates/turbopack-core/src/chunk/mod.rs | 41 ++++++++++++++----- crates/turbopack-css/src/asset.rs | 22 ++++------ crates/turbopack-css/src/module_asset.rs | 22 ++++------ crates/turbopack-dev-server/src/html.rs | 2 +- crates/turbopack-dev/src/chunking_context.rs | 2 +- crates/turbopack-ecmascript/src/chunk/item.rs | 5 ++- .../src/chunk_group_files_asset.rs | 26 +++++------- crates/turbopack-ecmascript/src/lib.rs | 22 ++++------ .../src/manifest/chunk_asset.rs | 22 +++------- .../src/manifest/chunk_item.rs | 20 +++++++-- .../src/manifest/loader_item.rs | 36 ++++++++++++---- .../src/references/require_context.rs | 22 ++++------ .../src/tree_shake/asset.rs | 21 ++-------- .../src/tree_shake/chunk_item.rs | 15 +++++-- crates/turbopack-json/src/lib.rs | 22 ++++------ crates/turbopack-mdx/src/lib.rs | 22 ++++------ crates/turbopack-node/src/evaluate.rs | 2 +- crates/turbopack-node/src/lib.rs | 2 +- crates/turbopack-static/src/lib.rs | 22 ++++------ crates/turbopack-tests/tests/snapshot.rs | 5 ++- crates/turbopack-wasm/src/module_asset.rs | 22 ++++------ crates/turbopack-wasm/src/raw.rs | 22 ++++------ 24 files changed, 200 insertions(+), 201 deletions(-) diff --git a/crates/turbopack-build/src/chunking_context.rs b/crates/turbopack-build/src/chunking_context.rs index c8c39a68bf0ee5..3f2ee63d1db4ec 100644 --- a/crates/turbopack-build/src/chunking_context.rs +++ b/crates/turbopack-build/src/chunking_context.rs @@ -8,7 +8,7 @@ use turbo_tasks::{ }; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - chunk::{Chunk, ChunkableModule, ChunkingContext, Chunks, EvaluatableAssets}, + chunk::{Chunk, ChunkableModuleExt, ChunkingContext, Chunks, EvaluatableAssets}, environment::Environment, ident::AssetIdent, module::Module, diff --git a/crates/turbopack-cli/src/build/mod.rs b/crates/turbopack-cli/src/build/mod.rs index 20c931d512dc41..23453b6bf7ec37 100644 --- a/crates/turbopack-cli/src/build/mod.rs +++ b/crates/turbopack-cli/src/build/mod.rs @@ -14,7 +14,7 @@ use turbopack_build::{BuildChunkingContext, MinifyType}; use turbopack_cli_utils::issue::{ConsoleUi, LogOptions}; use turbopack_core::{ asset::Asset, - chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, + chunk::{ChunkableModule, ChunkableModuleExt, ChunkingContext, EvaluatableAssets}, environment::{BrowserEnvironment, Environment, ExecutionEnvironment}, issue::{handle_issues, IssueReporter, IssueSeverity}, module::Module, diff --git a/crates/turbopack-core/src/chunk/mod.rs b/crates/turbopack-core/src/chunk/mod.rs index a07eef67925ba7..3b425c362effa1 100644 --- a/crates/turbopack-core/src/chunk/mod.rs +++ b/crates/turbopack-core/src/chunk/mod.rs @@ -85,28 +85,47 @@ pub struct ModuleIds(Vec>); /// A [Module] that can be converted into a [Chunk]. #[turbo_tasks::value_trait] pub trait ChunkableModule: Module + Asset { + fn as_chunk_item( + self: Vc, + chunking_context: Vc>, + ) -> Vc>; +} + +pub trait ChunkableModuleExt { fn as_chunk( self: Vc, chunking_context: Vc>, availability_info: Value, - ) -> Vc>; - + ) -> Vc> + where + Self: Send; fn as_root_chunk( self: Vc, chunking_context: Vc>, + ) -> Vc> + where + Self: Send; +} + +impl>> ChunkableModuleExt for T { + fn as_chunk( + self: Vc, + chunking_context: Vc>, + availability_info: Value, ) -> Vc> { - self.as_chunk( - chunking_context, - Value::new(AvailabilityInfo::Root { - current_availability_root: Vc::upcast(self), - }), - ) + let chunk_item = self.as_chunk_item(chunking_context); + chunk_item.as_chunk(availability_info) } - fn as_chunk_item( + fn as_root_chunk( self: Vc, chunking_context: Vc>, - ) -> Vc>; + ) -> Vc> { + let chunk_item = self.as_chunk_item(chunking_context); + chunk_item.as_chunk(Value::new(AvailabilityInfo::Root { + current_availability_root: Vc::upcast(self), + })) + } } #[turbo_tasks::value(transparent)] @@ -677,6 +696,8 @@ where #[turbo_tasks::value_trait] pub trait ChunkItem { + fn as_chunk(self: Vc, availability_info: Value) -> Vc>; + /// The [AssetIdent] of the [Module] that this [ChunkItem] was created from. /// For most chunk types this must uniquely identify the asset as it's the /// source of the module id used at runtime. diff --git a/crates/turbopack-css/src/asset.rs b/crates/turbopack-css/src/asset.rs index 1108a9c0b47fc0..04697bad2ab2cd 100644 --- a/crates/turbopack-css/src/asset.rs +++ b/crates/turbopack-css/src/asset.rs @@ -111,19 +111,6 @@ impl Asset for CssModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for CssModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(CssChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] fn as_chunk_item( self: Vc, @@ -174,6 +161,15 @@ impl ChunkItem for CssModuleChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(CssChunk::new( + self.chunking_context, + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index d83ab6787524b7..49523329e9e3da 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -211,19 +211,6 @@ impl ModuleCssAsset { #[turbo_tasks::value_impl] impl ChunkableModule for ModuleCssAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -288,6 +275,15 @@ impl ChunkItem for ModuleChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-dev-server/src/html.rs b/crates/turbopack-dev-server/src/html.rs index 0b8ecb375af9cd..13e60ae7b290f8 100644 --- a/crates/turbopack-dev-server/src/html.rs +++ b/crates/turbopack-dev-server/src/html.rs @@ -5,7 +5,7 @@ use turbo_tasks_fs::{File, FileSystemPath}; use turbo_tasks_hash::{encode_hex, Xxh3Hash64Hasher}; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets}, + chunk::{ChunkableModule, ChunkableModuleExt, ChunkingContext, EvaluatableAssets}, ident::AssetIdent, output::{OutputAsset, OutputAssets}, version::{Version, VersionedContent}, diff --git a/crates/turbopack-dev/src/chunking_context.rs b/crates/turbopack-dev/src/chunking_context.rs index d73916456b5052..b9b52099fad4a6 100644 --- a/crates/turbopack-dev/src/chunking_context.rs +++ b/crates/turbopack-dev/src/chunking_context.rs @@ -6,7 +6,7 @@ use turbo_tasks::{ }; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - chunk::{Chunk, ChunkableModule, ChunkingContext, Chunks, EvaluatableAssets}, + chunk::{Chunk, ChunkableModuleExt, ChunkingContext, Chunks, EvaluatableAssets}, environment::Environment, ident::AssetIdent, module::Module, diff --git a/crates/turbopack-ecmascript/src/chunk/item.rs b/crates/turbopack-ecmascript/src/chunk/item.rs index 0c032a71f4f802..753deefd8f0b12 100644 --- a/crates/turbopack-ecmascript/src/chunk/item.rs +++ b/crates/turbopack-ecmascript/src/chunk/item.rs @@ -286,7 +286,10 @@ impl FromChunkableModule for Box { let manifest_asset = ManifestChunkAsset::new(module, context, Value::new(next_availability_info)); - Ok(Some(Vc::upcast(ManifestLoaderItem::new(manifest_asset)))) + Ok(Some(Vc::upcast(ManifestLoaderItem::new( + manifest_asset, + chunking_context, + )))) } } diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index 98ead0e14df1de..20b2b986376933 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -5,8 +5,8 @@ use turbo_tasks_fs::{File, FileSystemPath}; use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{ - availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkableModule, ChunkingContext, - EvaluatableAssets, + availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkableModule, ChunkableModuleExt, + ChunkingContext, EvaluatableAssets, }, ident::AssetIdent, introspect::{ @@ -90,19 +90,6 @@ impl Asset for ChunkGroupFilesAsset { #[turbo_tasks::value_impl] impl ChunkableModule for ChunkGroupFilesAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -238,6 +225,15 @@ impl ChunkItem for ChunkGroupFilesChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.inner), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index fc2dcd52e6fe48..048c6017f803ff 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -403,19 +403,6 @@ impl Asset for EcmascriptModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for EcmascriptModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -499,6 +486,15 @@ impl ChunkItem for ModuleChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index 705d67544c2497..05f9612a30f89f 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -2,7 +2,10 @@ use anyhow::{Context, Result}; use turbo_tasks::{Value, Vc}; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{availability_info::AvailabilityInfo, Chunk, ChunkableModule, ChunkingContext}, + chunk::{ + availability_info::AvailabilityInfo, Chunk, ChunkableModule, ChunkableModuleExt, + ChunkingContext, + }, ident::AssetIdent, module::Module, output::OutputAssets, @@ -10,9 +13,7 @@ use turbopack_core::{ }; use super::chunk_item::ManifestChunkItem; -use crate::chunk::{ - EcmascriptChunk, EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports, -}; +use crate::chunk::{EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports}; #[turbo_tasks::function] fn modifier() -> Vc { @@ -118,19 +119,6 @@ impl Asset for ManifestChunkAsset { #[turbo_tasks::value_impl] impl ChunkableModule for ManifestChunkAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs index 15d54d3358bca6..7b327dff379876 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs @@ -1,8 +1,11 @@ use anyhow::Result; use indoc::formatdoc; -use turbo_tasks::{TryJoinIterExt, Vc}; +use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbopack_core::{ - chunk::{ChunkData, ChunkItem, ChunkingContext, ChunksData}, + chunk::{ + availability_info::AvailabilityInfo, Chunk, ChunkData, ChunkItem, ChunkingContext, + ChunksData, + }, ident::AssetIdent, module::Module, reference::{ModuleReferences, SingleOutputAssetReference}, @@ -11,8 +14,8 @@ use turbopack_core::{ use super::chunk_asset::ManifestChunkAsset; use crate::{ chunk::{ - data::EcmascriptChunkData, EcmascriptChunkItem, EcmascriptChunkItemContent, - EcmascriptChunkingContext, + data::EcmascriptChunkData, EcmascriptChunk, EcmascriptChunkItem, + EcmascriptChunkItemContent, EcmascriptChunkingContext, }, utils::StringifyJs, }; @@ -96,4 +99,13 @@ impl ChunkItem for ManifestChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.manifest), + availability_info, + )) + } } diff --git a/crates/turbopack-ecmascript/src/manifest/loader_item.rs b/crates/turbopack-ecmascript/src/manifest/loader_item.rs index c145bd97d2e155..894a738d22ee20 100644 --- a/crates/turbopack-ecmascript/src/manifest/loader_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/loader_item.rs @@ -2,9 +2,12 @@ use std::io::Write as _; use anyhow::{anyhow, Result}; use indoc::writedoc; -use turbo_tasks::{TryJoinIterExt, Vc}; +use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbopack_core::{ - chunk::{ChunkData, ChunkItem, ChunkItemExt, ChunkableModule, ChunkingContext, ChunksData}, + chunk::{ + availability_info::AvailabilityInfo, Chunk, ChunkData, ChunkItem, ChunkItemExt, + ChunkableModule, ChunkingContext, ChunksData, + }, ident::AssetIdent, module::Module, reference::{ModuleReference, ModuleReferences, SingleOutputAssetReference}, @@ -13,8 +16,8 @@ use turbopack_core::{ use super::chunk_asset::ManifestChunkAsset; use crate::{ chunk::{ - data::EcmascriptChunkData, EcmascriptChunkItem, EcmascriptChunkItemContent, - EcmascriptChunkPlaceable, EcmascriptChunkingContext, + data::EcmascriptChunkData, EcmascriptChunk, EcmascriptChunkItem, + EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkingContext, }, utils::StringifyJs, }; @@ -39,22 +42,28 @@ fn modifier() -> Vc { #[turbo_tasks::value] pub struct ManifestLoaderItem { manifest: Vc, + chunking_context: Vc>, } #[turbo_tasks::value_impl] impl ManifestLoaderItem { #[turbo_tasks::function] - pub fn new(manifest: Vc) -> Vc { - Self::cell(ManifestLoaderItem { manifest }) + pub fn new( + manifest: Vc, + chunking_context: Vc>, + ) -> Vc { + Self::cell(ManifestLoaderItem { + manifest, + chunking_context, + }) } #[turbo_tasks::function] pub async fn chunks_data(self: Vc) -> Result> { let this = self.await?; let chunks = this.manifest.manifest_chunks(); - let manifest = this.manifest.await?; Ok(ChunkData::from_assets( - manifest.chunking_context.output_root(), + this.chunking_context.output_root(), chunks, )) } @@ -108,7 +117,16 @@ impl ChunkItem for ManifestLoaderItem { #[turbo_tasks::function] async fn chunking_context(&self) -> Result>> { - Ok(Vc::upcast(self.manifest.await?.chunking_context)) + Ok(Vc::upcast(self.chunking_context)) + } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + self.chunking_context, + Vc::upcast(self.manifest), + availability_info, + )) } } diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index 801e46ab2b8bcd..79a8d3001161e4 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -374,19 +374,6 @@ impl Asset for RequireContextAsset { #[turbo_tasks::value_impl] impl ChunkableModule for RequireContextAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -527,4 +514,13 @@ impl ChunkItem for RequireContextChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.inner), + availability_info, + )) + } } diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index 4cc25dcfd904c2..aca45d5a033a6a 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -1,8 +1,8 @@ use anyhow::{bail, Context, Result}; -use turbo_tasks::{Value, Vc}; +use turbo_tasks::Vc; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{availability_info::AvailabilityInfo, Chunk, ChunkableModule, ChunkingContext}, + chunk::{ChunkableModule, ChunkingContext}, ident::AssetIdent, module::Module, reference::{ModuleReferences, SingleModuleReference}, @@ -11,9 +11,7 @@ use turbopack_core::{ use super::{chunk_item::EcmascriptModulePartChunkItem, get_part_id, split_module, SplitResult}; use crate::{ - chunk::{ - EcmascriptChunk, EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports, - }, + chunk::{EcmascriptChunkPlaceable, EcmascriptChunkingContext, EcmascriptExports}, references::analyze_ecmascript_module, AnalyzeEcmascriptModuleResult, EcmascriptModuleAsset, }; @@ -115,19 +113,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset { #[turbo_tasks::value_impl] impl ChunkableModule for EcmascriptModulePartAsset { - #[turbo_tasks::function] - async fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, diff --git a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs index 560b8c6df79457..3d0b9c60854a23 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs @@ -1,7 +1,7 @@ use anyhow::Result; use turbo_tasks::{Value, Vc}; use turbopack_core::{ - chunk::{availability_info::AvailabilityInfo, ChunkItem, ChunkingContext}, + chunk::{availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkingContext}, ident::AssetIdent, module::Module, reference::ModuleReferences, @@ -10,8 +10,8 @@ use turbopack_core::{ use super::{asset::EcmascriptModulePartAsset, part_of_module, split_module}; use crate::{ chunk::{ - placeable::EcmascriptChunkPlaceable, EcmascriptChunkItem, EcmascriptChunkItemContent, - EcmascriptChunkingContext, + placeable::EcmascriptChunkPlaceable, EcmascriptChunk, EcmascriptChunkItem, + EcmascriptChunkItemContent, EcmascriptChunkingContext, }, EcmascriptModuleContent, }; @@ -91,4 +91,13 @@ impl ChunkItem for EcmascriptModulePartChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } diff --git a/crates/turbopack-json/src/lib.rs b/crates/turbopack-json/src/lib.rs index d1ae37a266046c..e9ceeffc3555f2 100644 --- a/crates/turbopack-json/src/lib.rs +++ b/crates/turbopack-json/src/lib.rs @@ -65,19 +65,6 @@ impl Asset for JsonModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for JsonModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -126,6 +113,15 @@ impl ChunkItem for JsonChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index 870dff5401b765..2f0640113077ce 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -188,19 +188,6 @@ impl Asset for MdxModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for MdxModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -262,6 +249,15 @@ impl ChunkItem for MdxChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-node/src/evaluate.rs b/crates/turbopack-node/src/evaluate.rs index c06b1a59f7cc31..aa3385af692f36 100644 --- a/crates/turbopack-node/src/evaluate.rs +++ b/crates/turbopack-node/src/evaluate.rs @@ -22,7 +22,7 @@ use turbo_tasks_fs::{ }; use turbopack_core::{ asset::AssetContent, - chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset, EvaluatableAssets}, + chunk::{ChunkableModuleExt, ChunkingContext, EvaluatableAsset, EvaluatableAssets}, context::AssetContext, file_source::FileSource, ident::AssetIdent, diff --git a/crates/turbopack-node/src/lib.rs b/crates/turbopack-node/src/lib.rs index ea42d53e670b6c..42a753ea5c7f1e 100644 --- a/crates/turbopack-node/src/lib.rs +++ b/crates/turbopack-node/src/lib.rs @@ -17,7 +17,7 @@ use turbo_tasks_env::ProcessEnv; use turbo_tasks_fs::{to_sys_path, File, FileSystemPath}; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset, EvaluatableAssets}, + chunk::{ChunkableModuleExt, ChunkingContext, EvaluatableAsset, EvaluatableAssets}, module::Module, output::{OutputAsset, OutputAssetsSet}, source_map::GenerateSourceMap, diff --git a/crates/turbopack-static/src/lib.rs b/crates/turbopack-static/src/lib.rs index 1b646d6bff240d..1a3b9aeb5d0625 100644 --- a/crates/turbopack-static/src/lib.rs +++ b/crates/turbopack-static/src/lib.rs @@ -90,19 +90,6 @@ impl Asset for StaticModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for StaticModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -208,6 +195,15 @@ impl ChunkItem for ModuleChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-tests/tests/snapshot.rs b/crates/turbopack-tests/tests/snapshot.rs index 6196ad3540457e..9c8da2f1ea4d66 100644 --- a/crates/turbopack-tests/tests/snapshot.rs +++ b/crates/turbopack-tests/tests/snapshot.rs @@ -29,7 +29,10 @@ use turbopack::{ use turbopack_build::{BuildChunkingContext, MinifyType}; use turbopack_core::{ asset::Asset, - chunk::{ChunkableModule, ChunkingContext, EvaluatableAssetExt, EvaluatableAssets}, + chunk::{ + ChunkableModule, ChunkableModuleExt, ChunkingContext, EvaluatableAssetExt, + EvaluatableAssets, + }, compile_time_defines, compile_time_info::CompileTimeInfo, context::AssetContext, diff --git a/crates/turbopack-wasm/src/module_asset.rs b/crates/turbopack-wasm/src/module_asset.rs index 3e876bf4b11bdc..1d0a65484b1d0e 100644 --- a/crates/turbopack-wasm/src/module_asset.rs +++ b/crates/turbopack-wasm/src/module_asset.rs @@ -114,19 +114,6 @@ impl Asset for WebAssemblyModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for WebAssemblyModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -207,6 +194,15 @@ impl ChunkItem for ModuleChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-wasm/src/raw.rs b/crates/turbopack-wasm/src/raw.rs index 231c670193d59c..00a80845251a86 100644 --- a/crates/turbopack-wasm/src/raw.rs +++ b/crates/turbopack-wasm/src/raw.rs @@ -72,19 +72,6 @@ impl Asset for RawWebAssemblyModuleAsset { #[turbo_tasks::value_impl] impl ChunkableModule for RawWebAssemblyModuleAsset { - #[turbo_tasks::function] - fn as_chunk( - self: Vc, - chunking_context: Vc>, - availability_info: Value, - ) -> Vc> { - Vc::upcast(EcmascriptChunk::new( - chunking_context, - Vc::upcast(self), - availability_info, - )) - } - #[turbo_tasks::function] async fn as_chunk_item( self: Vc, @@ -145,6 +132,15 @@ impl ChunkItem for RawModuleChunkItem { async fn chunking_context(&self) -> Vc> { Vc::upcast(self.chunking_context) } + + #[turbo_tasks::function] + fn as_chunk(&self, availability_info: Value) -> Vc> { + Vc::upcast(EcmascriptChunk::new( + Vc::upcast(self.chunking_context), + Vc::upcast(self.module), + availability_info, + )) + } } #[turbo_tasks::value_impl]