Unverified Commit ff636469 authored by Matt Mastracci's avatar Matt Mastracci Committed by GitHub

[turbopack] Simplify JS `ChunkItem` with a single impl in most cases (#89548)

# What

Removes the vast majority of `ChunkItem` implementations in favour of
one `EcmascriptModuleChunkItem`.

# Why

Almost all of the `ChunkItem` implementations are trivial and were, for
the most part, just places where additional traits could be implemented.

By removing all of the various, trivial implementations, this might open
up opportunities to better implement other features across the entire
codebase.

# How

Most chunk items are migrated over to EcmascriptModuleChunkItem. The
EcmascriptChunkPlaceable trait gains three new methods that modules can
optionally override:

  -  `chunk_item_content()` - generates the chunk item's JavaScript code
- `chunk_item_content_ident()` - returns the content identity for cache
invalidation
- `chunk_item_output_assets()` - returns output assets the chunk item
depends on

The generic EcmascriptModuleChunkItem delegates to these trait methods,
eliminating the need for per-module wrapper structs.
parent 9b0277f0
......@@ -7,21 +7,20 @@ use turbo_tasks_fs::{FileSystem, VirtualFileSystem, rope::RopeBuilder};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{
ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ChunkingType, ChunkingTypeOption,
EvaluatableAsset,
AsyncModuleInfo, ChunkItem, ChunkableModule, ChunkingContext, ChunkingType,
ChunkingTypeOption, EvaluatableAsset,
},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::{ModuleReference, ModuleReferences},
resolve::ModuleResolveResult,
source::OptionSource,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions,
EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkPlaceable,
EcmascriptExports, ecmascript_chunk_item,
},
runtime_functions::TURBOPACK_REQUIRE,
utils::StringifyJs,
......@@ -90,14 +89,7 @@ impl ChunkableModule for HmrEntryModule {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn ChunkItem>> {
Vc::upcast(
HmrEntryChunkItem {
module: self,
module_graph,
chunking_context,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -115,6 +107,36 @@ impl EcmascriptChunkPlaceable for HmrEntryModule {
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
EcmascriptExports::None.cell()
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = self.await?;
let module = this.module;
let chunk_item = module.as_chunk_item(module_graph, chunking_context);
let id = chunking_context
.chunk_item_id_strategy()
.await?
.get_id(chunk_item)
.await?;
let mut code = RopeBuilder::default();
writeln!(code, "{TURBOPACK_REQUIRE}({});", StringifyJs(&id))?;
Ok(EcmascriptChunkItemContent {
inner_code: code.build(),
options: EcmascriptChunkItemOptions {
strict: true,
..Default::default()
},
..Default::default()
}
.cell())
}
}
#[turbo_tasks::value_impl]
......@@ -149,72 +171,10 @@ impl ModuleReference for HmrEntryModuleReference {
}
#[turbo_tasks::function]
fn chunking_type(self: Vc<Self>) -> Vc<ChunkingTypeOption> {
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Parallel {
inherit_async: false,
hoisted: false,
}))
}
}
/// The chunk item for [`HmrEntryModule`].
#[turbo_tasks::value]
struct HmrEntryChunkItem {
module: ResolvedVc<HmrEntryModule>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for HmrEntryChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for HmrEntryChunkItem {
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for HmrEntryChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = self.module.await?;
let module = this.module;
let chunk_item = module.as_chunk_item(*self.module_graph, *self.chunking_context);
let id = self
.chunking_context
.chunk_item_id_strategy()
.await?
.get_id(chunk_item)
.await?;
let mut code = RopeBuilder::default();
writeln!(code, "{TURBOPACK_REQUIRE}({});", StringifyJs(&id))?;
Ok(EcmascriptChunkItemContent {
inner_code: code.build(),
options: EcmascriptChunkItemOptions {
strict: true,
..Default::default()
},
..Default::default()
}
.cell())
}
}
......@@ -281,6 +281,17 @@ impl EcmascriptChunkPlaceable for EcmascriptClientReferenceModule {
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
self.proxy_module().get_exports()
}
#[turbo_tasks::function]
fn chunk_item_content(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
bail!("Attempted to get chunk_item_content for EcmascriptClientReferenceModule")
}
}
/// This wrapper only exists to overwrite the `asset_ident` method of the
......
......@@ -5,19 +5,18 @@ use indoc::formatdoc;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::{ModuleReferences, SingleChunkableModuleReference},
resolve::ExportUsage,
source::OptionSource,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
references::esm::{EsmExport, EsmExports},
runtime_functions::{TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_IMPORT},
......@@ -84,14 +83,7 @@ impl ChunkableModule for NextDynamicEntryModule {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
NextDynamicEntryChunkItem {
chunking_context,
module_graph,
inner: self,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -125,25 +117,16 @@ impl EcmascriptChunkPlaceable for NextDynamicEntryModule {
)
.cell())
}
}
#[turbo_tasks::value]
struct NextDynamicEntryChunkItem {
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
module_graph: ResolvedVc<ModuleGraph>,
inner: ResolvedVc<NextDynamicEntryModule>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for NextDynamicEntryChunkItem {}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for NextDynamicEntryChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let inner = self.inner.await?;
let module_id = inner.module.chunk_item_id(*self.chunking_context).await?;
async fn chunk_item_content(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module_id = self.module.chunk_item_id(chunking_context).await?;
Ok(EcmascriptChunkItemContent {
inner_code: formatdoc!(
r#"
......@@ -157,26 +140,3 @@ impl EcmascriptChunkItem for NextDynamicEntryChunkItem {
.cell())
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for NextDynamicEntryChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.inner.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.inner)
}
}
......@@ -6,18 +6,17 @@ use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::ModuleReferences,
source::OptionSource,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
references::esm::{EsmExport, EsmExports},
runtime_functions::{TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_IMPORT},
......@@ -101,14 +100,7 @@ impl ChunkableModule for NextServerComponentModule {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
NextServerComponentChunkItem {
module_graph,
chunking_context,
inner: self,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -138,25 +130,16 @@ impl EcmascriptChunkPlaceable for NextServerComponentModule {
)
.cell())
}
}
#[turbo_tasks::value]
struct NextServerComponentChunkItem {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
inner: ResolvedVc<NextServerComponentModule>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for NextServerComponentChunkItem {}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for NextServerComponentChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let inner = self.inner.await?;
let module_id = inner.module.chunk_item_id(*self.chunking_context).await?;
async fn chunk_item_content(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module_id = self.module.chunk_item_id(chunking_context).await?;
Ok(EcmascriptChunkItemContent {
inner_code: formatdoc!(
r#"
......@@ -170,26 +153,3 @@ impl EcmascriptChunkItem for NextServerComponentChunkItem {
.cell())
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for NextServerComponentChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.inner.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.inner)
}
}
......@@ -6,18 +6,17 @@ use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::ModuleReferences,
source::OptionSource,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
references::esm::{EsmExport, EsmExports},
runtime_functions::{TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_IMPORT},
......@@ -82,14 +81,7 @@ impl ChunkableModule for NextServerUtilityModule {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
NextServerComponentChunkItem {
module_graph,
chunking_context,
inner: self,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -119,28 +111,21 @@ impl EcmascriptChunkPlaceable for NextServerUtilityModule {
)
.cell())
}
}
#[turbo_tasks::value]
struct NextServerComponentChunkItem {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
inner: ResolvedVc<NextServerUtilityModule>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for NextServerComponentChunkItem {}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for NextServerComponentChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let inner = self.inner.await?;
let module_id = inner.module.chunk_item_id(*self.chunking_context).await?;
async fn chunk_item_content(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module_id = self.module.chunk_item_id(chunking_context).await?;
Ok(EcmascriptChunkItemContent {
inner_code: formatdoc!(
"{TURBOPACK_EXPORT_NAMESPACE}({TURBOPACK_IMPORT}({}));",
r#"
{TURBOPACK_EXPORT_NAMESPACE}({TURBOPACK_IMPORT}({}));
"#,
StringifyJs(&module_id),
)
.into(),
......@@ -149,26 +134,3 @@ impl EcmascriptChunkItem for NextServerComponentChunkItem {
.cell())
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for NextServerComponentChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.inner.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.inner)
}
}
......@@ -11,7 +11,7 @@ use turbo_tasks_fs::{FileContent, rope::Rope};
use turbopack::{ModuleAssetContext, module_options::CustomModuleType};
use turbopack_core::{
asset::Asset,
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext},
code_builder::CodeBuilder,
compile_time_info::{
CompileTimeDefineValue, CompileTimeInfo, DefinableNameSegmentRef, DefinableNameSegmentRefs,
......@@ -21,7 +21,6 @@ use turbopack_core::{
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
resolve::ModulePart,
source::{OptionSource, Source},
source_map::GenerateSourceMap,
......@@ -29,8 +28,8 @@ use turbopack_core::{
use turbopack_ecmascript::{
EcmascriptInputTransforms,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions,
EcmascriptChunkPlaceable, EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkPlaceable,
EcmascriptExports, ecmascript_chunk_item,
},
source_map::{extract_source_mapping_url_from_content, parse_source_map_comment},
utils::StringifyJs,
......@@ -110,16 +109,10 @@ impl ChunkableModule for RawEcmascriptModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: Vc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
RawEcmascriptChunkItem {
module: self,
chunking_context,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -129,53 +122,22 @@ impl EcmascriptChunkPlaceable for RawEcmascriptModule {
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::CommonJs.cell()
}
}
#[turbo_tasks::value]
struct RawEcmascriptChunkItem {
module: ResolvedVc<RawEcmascriptModule>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for RawEcmascriptChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for RawEcmascriptChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for RawEcmascriptChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
async fn chunk_item_content(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let span = tracing::info_span!(
"code generation raw module",
name = display(self.module.ident().to_string().await?)
name = display(self.ident().to_string().await?)
);
async {
let module = self.module.await?;
let module = self.await?;
let source = module.source;
let content = source.content().file_content().await?;
let content = match &*content {
......@@ -246,12 +208,9 @@ impl EcmascriptChunkItem for RawEcmascriptChunkItem {
code += "(function(){\n";
let source_mapping_url = extract_source_mapping_url_from_content(&content_str);
let source_map = if let Some((source_map, _)) = parse_source_map_comment(
source,
source_mapping_url,
&*self.module.ident().path().await?,
)
.await?
let source_map = if let Some((source_map, _)) =
parse_source_map_comment(source, source_mapping_url, &*self.ident().path().await?)
.await?
{
let source_map = source_map.generate_source_map().await?;
source_map.as_content().map(|f| f.content().clone())
......
......@@ -32,7 +32,7 @@ use turbopack_core::{
use turbopack_ecmascript::{
async_chunk::module::AsyncLoaderModule,
chunk::EcmascriptChunk,
manifest::{chunk_asset::ManifestAsyncModule, loader_item::ManifestLoaderChunkItem},
manifest::{chunk_asset::ManifestAsyncModule, loader_module::ManifestLoaderModule},
};
use turbopack_ecmascript_runtime::RuntimeType;
......@@ -902,22 +902,24 @@ impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
async fn async_loader_chunk_item(
self: Vc<Self>,
self: ResolvedVc<Self>,
module: Vc<Box<dyn ChunkableModule>>,
module_graph: Vc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Result<Vc<Box<dyn ChunkItem>>> {
let chunking_context = ResolvedVc::upcast::<Box<dyn ChunkingContext>>(self);
Ok(if self.await?.manifest_chunks {
let manifest_asset =
ManifestAsyncModule::new(module, module_graph, Vc::upcast(self), availability_info);
Vc::upcast(ManifestLoaderChunkItem::new(
manifest_asset,
let manifest_asset = ManifestAsyncModule::new(
module,
module_graph,
Vc::upcast(self),
))
*chunking_context,
availability_info,
);
let loader_module = ManifestLoaderModule::new(manifest_asset);
loader_module.as_chunk_item(module_graph, *chunking_context)
} else {
let module = AsyncLoaderModule::new(module, Vc::upcast(self), availability_info);
module.as_chunk_item(module_graph, Vc::upcast(self))
let module = AsyncLoaderModule::new(module, *chunking_context, availability_info);
module.as_chunk_item(module_graph, *chunking_context)
})
}
......@@ -927,7 +929,7 @@ impl ChunkingContext for BrowserChunkingContext {
module: Vc<Box<dyn ChunkableModule>>,
) -> Result<Vc<AssetIdent>> {
Ok(if self.await?.manifest_chunks {
ManifestLoaderChunkItem::asset_ident_for(module)
ManifestLoaderModule::asset_ident_for(module)
} else {
AsyncLoaderModule::asset_ident_for(module)
})
......
......@@ -8,7 +8,7 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, IntoTraitRef, ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileSystemPath, rope::Rope};
use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
context::{AssetContext, ProcessResult},
ident::AssetIdent,
issue::{
......@@ -17,7 +17,6 @@ use turbopack_core::{
},
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::{ModuleReference, ModuleReferences},
reference_type::{CssReferenceSubType, ReferenceType},
resolve::{origin::ResolveOrigin, parse::Request},
......@@ -25,8 +24,8 @@ use turbopack_core::{
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
parse::generate_js_source_map,
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_IMPORT},
......@@ -253,14 +252,7 @@ impl ChunkableModule for ModuleCssAsset {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
ModuleChunkItem {
chunking_context,
module_graph,
module: self,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -270,61 +262,16 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
}
#[turbo_tasks::value_impl]
impl ResolveOrigin for ModuleCssAsset {
#[turbo_tasks::function]
fn origin_path(&self) -> Vc<FileSystemPath> {
self.source.ident().path()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
}
}
#[turbo_tasks::value]
struct ModuleChunkItem {
module: ResolvedVc<ModuleCssAsset>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for ModuleChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let classes = self.module.classes().await?;
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let classes = self.classes().await?;
let mut code = format!("{TURBOPACK_EXPORT_VALUE}({{\n");
for (export_name, class_names) in &*classes {
......@@ -342,7 +289,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
CssModuleComposesIssue {
severity: IssueSeverity::Error,
// TODO(PACK-4879): this should include detailed location information
source: IssueSource::from_source_only(self.module.await?.source),
source: IssueSource::from_source_only(self.await?.source),
message: formatdoc! {
r#"
Module {from} referenced in `composes: ... from {from};` can't be resolved.
......@@ -359,7 +306,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
CssModuleComposesIssue {
severity: IssueSeverity::Error,
// TODO(PACK-4879): this should include detailed location information
source: IssueSource::from_source_only(self.module.await?.source),
source: IssueSource::from_source_only(self.await?.source),
message: formatdoc! {
r#"
Module {from} referenced in `composes: ... from {from};` is not a CSS module.
......@@ -376,7 +323,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
let placeable: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>> =
ResolvedVc::upcast(css_module);
let module_id = placeable.chunk_item_id(*self.chunking_context).await?;
let module_id = placeable.chunk_item_id(chunking_context).await?;
let module_id = StringifyJs(&module_id);
let original_name = StringifyJs(&original_name);
exported_class_names
......@@ -397,9 +344,8 @@ impl EcmascriptChunkItem for ModuleChunkItem {
)?;
}
code += "});\n";
let source_map = *self
.chunking_context
.reference_module_source_maps(*ResolvedVc::upcast(self.module))
let source_map = *chunking_context
.reference_module_source_maps(Vc::upcast(self))
.await?;
Ok(EcmascriptChunkItemContent {
inner_code: code.clone().into(),
......@@ -407,7 +353,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
// displayed in dev tools.
source_map: if source_map {
Some(generate_minimal_source_map(
self.module.ident().to_string().await?.to_string(),
self.ident().to_string().await?.to_string(),
code,
)?)
} else {
......@@ -419,6 +365,19 @@ impl EcmascriptChunkItem for ModuleChunkItem {
}
}
#[turbo_tasks::value_impl]
impl ResolveOrigin for ModuleCssAsset {
#[turbo_tasks::function]
fn origin_path(&self) -> Vc<FileSystemPath> {
self.source.ident().path()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
}
}
fn generate_minimal_source_map(filename: String, source: String) -> Result<Rope> {
let mut mappings = vec![];
// Start from 1 because 0 is reserved for dummy spans in SWC.
......
use anyhow::Result;
use indoc::formatdoc;
use tracing::Instrument;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbopack_core::{
chunk::{
AsyncModuleInfo, ChunkData, ChunkItem, ChunkType, ChunkingContext, ChunkingContextExt,
ChunksData, ModuleChunkItemIdExt,
},
ident::AssetIdent,
module::Module,
module_graph::{
ModuleGraph, chunk_group_info::ChunkGroup, module_batch::ChunkableModuleOrBatch,
},
output::{OutputAssetsReference, OutputAssetsWithReferenced},
};
use crate::{
async_chunk::module::AsyncLoaderModule,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, data::EcmascriptChunkData,
},
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_LOAD},
utils::{StringifyJs, StringifyModuleId},
};
#[turbo_tasks::value(shared)]
pub struct AsyncLoaderChunkItem {
pub module: ResolvedVc<AsyncLoaderModule>,
pub module_graph: ResolvedVc<ModuleGraph>,
pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl AsyncLoaderChunkItem {
#[turbo_tasks::function]
pub(super) async fn chunk_group(&self) -> Result<Vc<OutputAssetsWithReferenced>> {
let module = self.module.await?;
if let Some(chunk_items) = module.availability_info.available_modules() {
let inner_module = ResolvedVc::upcast(module.inner);
let batches = self
.module_graph
.module_batches(self.chunking_context.batching_config())
.await?;
let module_or_batch = batches.get_entry(inner_module).await?;
if let Some(chunkable_module_or_batch) =
ChunkableModuleOrBatch::from_module_or_batch(module_or_batch)
&& *chunk_items.get(chunkable_module_or_batch.into()).await?
{
return Ok(OutputAssetsWithReferenced {
assets: ResolvedVc::cell(vec![]),
referenced_assets: ResolvedVc::cell(vec![]),
references: ResolvedVc::cell(vec![]),
}
.cell());
}
}
Ok(self.chunking_context.chunk_group_assets(
module.inner.ident(),
ChunkGroup::Async(ResolvedVc::upcast(module.inner)),
*self.module_graph,
module.availability_info,
))
}
#[turbo_tasks::function]
async fn chunks_data(self: Vc<Self>) -> Result<Vc<ChunksData>> {
let this = self.await?;
let span = tracing::info_span!(
"compute async chunks",
name = this.module.ident().to_string().await?.as_str()
);
async move {
Ok(ChunkData::from_assets(
this.chunking_context.output_root().owned().await?,
*self.chunk_group().await?.assets,
))
}
.instrument(span)
.await
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for AsyncLoaderChunkItem {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = self.await?;
let module = this.module.await?;
let id = if let Some(placeable) =
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(module.inner)
{
Some(placeable.chunk_item_id(*this.chunking_context).await?)
} else {
None
};
let id = id.as_ref();
let chunks_data = self.chunks_data().await?;
let chunks_data = chunks_data.iter().try_join().await?;
let chunks_data: Vec<_> = chunks_data
.iter()
.map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect();
let code = match (id, chunks_data.is_empty()) {
(Some(id), true) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.resolve().then(() => {{
return parentImport({id});
}});
}});
"#,
id = StringifyModuleId(id),
}
}
(Some(id), false) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.all({chunks:#}.map((chunk) => {TURBOPACK_LOAD}(chunk))).then(() => {{
return parentImport({id});
}});
}});
"#,
chunks = StringifyJs(&chunks_data),
id = StringifyModuleId(id),
}
}
(None, true) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.resolve();
}});
"#,
}
}
(None, false) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.all({chunks:#}.map((chunk) => {TURBOPACK_LOAD}(chunk))).then(() => {{}});
}});
"#,
chunks = StringifyJs(&chunks_data),
}
}
};
Ok(EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell())
}
#[turbo_tasks::function]
fn content_with_async_module_info(
self: Vc<Self>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
estimated: bool,
) -> Vc<EcmascriptChunkItemContent> {
if estimated {
let code = formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.all([].map((chunk) => {TURBOPACK_LOAD}(chunk))).then(() => {{}});
}});
"#,
};
EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell()
} else {
self.content()
}
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for AsyncLoaderChunkItem {
#[turbo_tasks::function]
fn references(self: Vc<Self>) -> Vc<OutputAssetsWithReferenced> {
self.chunk_group()
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for AsyncLoaderChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
async fn content_ident(self: Vc<Self>) -> Result<Vc<AssetIdent>> {
let mut ident = self.module().ident();
let this = self.await?;
let nested_async_availability = this
.chunking_context
.is_nested_async_availability_enabled()
.await?;
let availability_ident = if *nested_async_availability {
Some(self.chunks_data().hash().await?.to_string().into())
} else {
this.module.await?.availability_info.ident().await?
};
if let Some(availability_ident) = availability_ident {
ident = ident.with_modifier(availability_ident)
}
Ok(ident)
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
}
use anyhow::Result;
use indoc::formatdoc;
use tracing::Instrument;
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use turbopack_core::{
chunk::{ChunkableModule, ChunkingContext, availability_info::AvailabilityInfo},
chunk::{
AsyncModuleInfo, ChunkData, ChunkableModule, ChunkingContext, ChunkingContextExt,
ChunksData, ModuleChunkItemIdExt, availability_info::AvailabilityInfo,
},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
module_graph::{
ModuleGraph, chunk_group_info::ChunkGroup, module_batch::ChunkableModuleOrBatch,
},
output::OutputAssetsWithReferenced,
reference::{ModuleReferences, SingleModuleReference},
};
use crate::async_chunk::chunk_item::AsyncLoaderChunkItem;
use crate::{
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
data::EcmascriptChunkData, ecmascript_chunk_item,
},
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_LOAD},
utils::{StringifyJs, StringifyModuleId},
};
/// The AsyncLoaderModule is a module that loads another module async, by
/// putting it into a separate chunk group.
......@@ -39,6 +54,54 @@ impl AsyncLoaderModule {
pub fn asset_ident_for(module: Vc<Box<dyn ChunkableModule>>) -> Vc<AssetIdent> {
module.ident().with_modifier(rcstr!("async loader"))
}
#[turbo_tasks::function]
pub(super) async fn chunk_group(
&self,
module_graph: Vc<ModuleGraph>,
) -> Result<Vc<OutputAssetsWithReferenced>> {
if let Some(chunk_items) = self.availability_info.available_modules() {
let inner_module = ResolvedVc::upcast(self.inner);
let batches = module_graph
.module_batches(self.chunking_context.batching_config())
.await?;
let module_or_batch = batches.get_entry(inner_module).await?;
if let Some(chunkable_module_or_batch) =
ChunkableModuleOrBatch::from_module_or_batch(module_or_batch)
&& *chunk_items.get(chunkable_module_or_batch.into()).await?
{
return Ok(OutputAssetsWithReferenced {
assets: ResolvedVc::cell(vec![]),
referenced_assets: ResolvedVc::cell(vec![]),
references: ResolvedVc::cell(vec![]),
}
.cell());
}
}
Ok(self.chunking_context.chunk_group_assets(
self.inner.ident(),
ChunkGroup::Async(ResolvedVc::upcast(self.inner)),
module_graph,
self.availability_info,
))
}
#[turbo_tasks::function]
async fn chunks_data(self: Vc<Self>, module_graph: Vc<ModuleGraph>) -> Result<Vc<ChunksData>> {
let this = self.await?;
let span = tracing::info_span!(
"compute async chunks",
name = self.ident().to_string().await?.as_str()
);
async move {
Ok(ChunkData::from_assets(
this.chunking_context.output_root().owned().await?,
*self.chunk_group(module_graph).await?.assets,
))
}
.instrument(span)
.await
}
}
#[turbo_tasks::value_impl]
......@@ -79,13 +142,152 @@ impl ChunkableModule for AsyncLoaderModule {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
AsyncLoaderChunkItem {
chunking_context,
module_graph,
module: self,
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkPlaceable for AsyncLoaderModule {
#[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
if estimated {
let code = formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.all([].map((chunk) => {TURBOPACK_LOAD}(chunk))).then(() => {{}});
}});
"#,
};
return Ok(EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell(),
)
.cell());
}
let this = self.await?;
let id = if let Some(placeable) =
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkPlaceable>>(this.inner)
{
Some(placeable.chunk_item_id(chunking_context).await?)
} else {
None
};
let id = id.as_ref();
let chunks_data = self.chunks_data(module_graph).await?;
let chunks_data = chunks_data.iter().try_join().await?;
let chunks_data: Vec<_> = chunks_data
.iter()
.map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect();
let code = match (id, chunks_data.is_empty()) {
(Some(id), true) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.resolve().then(() => {{
return parentImport({id});
}});
}});
"#,
id = StringifyModuleId(id),
}
}
(Some(id), false) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.all({chunks:#}.map((chunk) => {TURBOPACK_LOAD}(chunk))).then(() => {{
return parentImport({id});
}});
}});
"#,
chunks = StringifyJs(&chunks_data),
id = StringifyModuleId(id),
}
}
(None, true) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.resolve();
}});
"#,
}
}
(None, false) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.all({chunks:#}.map((chunk) => {TURBOPACK_LOAD}(chunk))).then(() => {{}});
}});
"#,
chunks = StringifyJs(&chunks_data),
}
}
};
Ok(EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell())
}
#[turbo_tasks::function]
async fn chunk_item_content_ident(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
) -> Result<Vc<AssetIdent>> {
let mut ident = self.ident();
let this = self.await?;
let nested_async_availability = this
.chunking_context
.is_nested_async_availability_enabled()
.await?;
let availability_ident = if *nested_async_availability {
Some(
self.chunks_data(module_graph)
.hash()
.await?
.to_string()
.into(),
)
} else {
this.availability_info.ident().await?
};
if let Some(availability_ident) = availability_ident {
ident = ident.with_modifier(availability_ident)
}
Ok(ident)
}
#[turbo_tasks::function]
fn chunk_item_output_assets(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
self.chunk_group(module_graph)
}
}
......@@ -11,17 +11,21 @@ use turbo_tasks::{
use turbo_tasks_fs::{FileSystemPath, rope::Rope};
use turbopack_core::{
chunk::{
AsyncModuleInfo, ChunkItem, ChunkItemWithAsyncModuleInfo, ChunkingContext,
AsyncModuleInfo, ChunkItem, ChunkItemWithAsyncModuleInfo, ChunkType, ChunkingContext,
ChunkingContextExt, ModuleId, SourceMapSourceType,
},
code_builder::{Code, CodeBuilder},
ident::AssetIdent,
issue::{IssueExt, IssueSeverity, StyledString, code_gen::CodeGenerationIssue},
module::Module,
module_graph::ModuleGraph,
output::OutputAssetsReference,
source_map::utils::{absolute_fileify_source_map, relative_fileify_source_map},
};
use crate::{
EcmascriptModuleContent,
chunk::{chunk_type::EcmascriptChunkType, placeable::EcmascriptChunkPlaceable},
references::async_module::{AsyncModuleOptions, OptionAsyncModuleOptions},
runtime_functions::TURBOPACK_ASYNC_MODULE,
utils::StringifyJs,
......@@ -296,3 +300,90 @@ async fn module_factory_with_code_generation_issue(
}
})
}
/// Generic chunk item that wraps any EcmascriptChunkPlaceable module.
/// This replaces the need for individual per-module ChunkItem wrapper structs.
#[turbo_tasks::value]
pub struct EcmascriptModuleChunkItem {
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
module_graph: ResolvedVc<ModuleGraph>,
}
/// Factory function to create an EcmascriptModuleChunkItem.
/// Use this instead of implementing ChunkableModule::as_chunk_item() on each module.
pub fn ecmascript_chunk_item(
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn ChunkItem>> {
Vc::upcast(
EcmascriptModuleChunkItem {
module,
chunking_context,
module_graph,
}
.cell(),
)
}
#[turbo_tasks::value_impl]
impl ChunkItem for EcmascriptModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn content_ident(&self) -> Vc<AssetIdent> {
self.module
.chunk_item_content_ident(*self.chunking_context, *self.module_graph)
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for EcmascriptModuleChunkItem {
#[turbo_tasks::function]
fn references(&self) -> Vc<turbopack_core::output::OutputAssetsWithReferenced> {
self.module
.chunk_item_output_assets(*self.chunking_context, *self.module_graph)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for EcmascriptModuleChunkItem {
#[turbo_tasks::function]
fn content(&self) -> Vc<EcmascriptChunkItemContent> {
self.module
.chunk_item_content(*self.chunking_context, *self.module_graph, None, false)
}
#[turbo_tasks::function]
fn content_with_async_module_info(
&self,
async_module_info: Option<Vc<AsyncModuleInfo>>,
estimated: bool,
) -> Vc<EcmascriptChunkItemContent> {
self.module.chunk_item_content(
*self.chunking_context,
*self.module_graph,
async_module_info,
estimated,
)
}
}
......@@ -34,7 +34,7 @@ pub use self::{
data::EcmascriptChunkData,
item::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemExt,
EcmascriptChunkItemOptions, EcmascriptChunkItemWithAsyncInfo,
EcmascriptChunkItemOptions, EcmascriptChunkItemWithAsyncInfo, ecmascript_chunk_item,
},
placeable::{EcmascriptChunkPlaceable, EcmascriptExports},
};
......
......@@ -9,19 +9,25 @@ use turbo_tasks_fs::{
};
use turbopack_core::{
asset::Asset,
chunk::ChunkableModule,
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext},
file_source::FileSource,
ident::AssetIdent,
issue::{
Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, OptionIssueSource,
OptionStyledString, StyledString,
},
module::Module,
module_graph::ModuleGraph,
output::{OutputAssets, OutputAssetsWithReferenced},
resolve::{FindContextFileResult, find_context_file, package_json},
};
use crate::references::{
async_module::OptionAsyncModule,
esm::{EsmExport, EsmExports},
use crate::{
chunk::EcmascriptChunkItemContent,
references::{
async_module::OptionAsyncModule,
esm::{EsmExport, EsmExports},
},
};
#[turbo_tasks::value_trait]
......@@ -32,6 +38,42 @@ pub trait EcmascriptChunkPlaceable: ChunkableModule + Module {
fn get_async_module(self: Vc<Self>) -> Vc<OptionAsyncModule> {
Vc::cell(None)
}
/// Generate chunk item content directly on the module.
/// This replaces the need for separate ChunkItem wrapper structs.
/// The `estimated` parameter is used during size estimation - when true, implementations
/// should avoid calling chunking context APIs that would cause cycles.
#[turbo_tasks::function]
fn chunk_item_content(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Vc<EcmascriptChunkItemContent>;
/// Returns the content identity for cache invalidation.
/// Override this for modules whose content depends on more than just the module source
/// (e.g., async loaders that depend on available modules).
#[turbo_tasks::function]
fn chunk_item_content_ident(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<AssetIdent> {
self.ident()
}
/// Returns output assets that this chunk item depends on.
/// Override this for modules that reference static assets, manifests, etc.
#[turbo_tasks::function]
fn chunk_item_output_assets(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
OutputAssetsWithReferenced::from_assets(OutputAssets::empty())
}
}
#[turbo_tasks::value]
......
......@@ -82,7 +82,7 @@ use turbo_tasks::{
use turbo_tasks_fs::{FileJsonContent, FileSystemPath, glob::Glob, rope::Rope};
use turbopack_core::{
chunk::{
AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext, EvaluatableAsset,
AsyncModuleInfo, ChunkItem, ChunkableModule, ChunkingContext, EvaluatableAsset,
MergeableModule, MergeableModuleExposure, MergeableModules, MergeableModulesExposed,
MinifyType, ModuleChunkItemIdExt, ModuleId,
},
......@@ -91,7 +91,6 @@ use turbopack_core::{
ident::AssetIdent,
module::{Module, ModuleSideEffects, OptionModule},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::ModuleReferences,
reference_type::InnerAssets,
resolve::{
......@@ -105,8 +104,8 @@ use turbopack_core::{
use crate::{
analyzer::graph::EvalContext,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
placeable::{SideEffectsDeclaration, get_side_effect_free_declaration},
},
code_gen::{CodeGeneration, CodeGenerationHoistedStmt, CodeGens, ModifiableAst},
......@@ -784,15 +783,12 @@ impl Module for EcmascriptModuleAsset {
#[turbo_tasks::value_impl]
impl ChunkableModule for EcmascriptModuleAsset {
#[turbo_tasks::function]
async fn as_chunk_item(
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: ResolvedVc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn ChunkItem>> {
Vc::upcast(ModuleChunkItem::cell(ModuleChunkItem {
module: self,
chunking_context,
}))
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -807,6 +803,29 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset {
async fn get_async_module(self: Vc<Self>) -> Result<Vc<OptionAsyncModule>> {
Ok(*self.analyze().await?.async_module)
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let span = tracing::info_span!(
"code generation",
name = display(self.ident().to_string().await?)
);
async {
let async_module_options = self.get_async_module().module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
EcmascriptChunkItemContent::new(content, chunking_context, async_module_options)
.resolve()
.await
}
.instrument(span)
.await
}
}
#[turbo_tasks::value_impl]
......@@ -869,78 +888,6 @@ impl ResolveOrigin for EcmascriptModuleAsset {
}
}
#[turbo_tasks::value]
struct ModuleChunkItem {
module: ResolvedVc<EcmascriptModuleAsset>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for ModuleChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should not be called");
}
#[turbo_tasks::function]
async fn content_with_async_module_info(
self: Vc<Self>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let span = tracing::info_span!(
"code generation",
name = display(self.asset_ident().to_string().await?)
);
async {
let this = self.await?;
let async_module_options = this
.module
.get_async_module()
.module_options(async_module_info);
// TODO check if we need to pass async_module_info at all
let content = this
.module
.module_content(*this.chunking_context, async_module_info);
EcmascriptChunkItemContent::new(content, *this.chunking_context, async_module_options)
.resolve()
.await
}
.instrument(span)
.await
}
}
/// The transformed contents of an Ecmascript module.
#[turbo_tasks::value(shared)]
pub struct EcmascriptModuleContent {
......
use anyhow::Result;
use indoc::formatdoc;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::{
chunk::{
ChunkableModule, ChunkingContext, ChunkingContextExt, availability_info::AvailabilityInfo,
AsyncModuleInfo, ChunkData, ChunkableModule, ChunkingContext, ChunkingContextExt,
ChunksData, availability_info::AvailabilityInfo,
},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
......@@ -14,8 +16,14 @@ use turbopack_core::{
reference::{ModuleReferences, SingleOutputAssetReference},
};
use super::chunk_item::ManifestChunkItem;
use crate::chunk::{EcmascriptChunkPlaceable, EcmascriptExports};
use crate::{
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
data::EcmascriptChunkData, ecmascript_chunk_item,
},
runtime_functions::TURBOPACK_EXPORT_VALUE,
utils::StringifyJs,
};
/// The manifest module is deferred until requested by the manifest loader
/// item when the dynamic `import()` expression is reached.
......@@ -108,6 +116,15 @@ impl ManifestAsyncModule {
}
Ok(ident)
}
#[turbo_tasks::function]
async fn chunks_data(self: Vc<Self>) -> Result<Vc<ChunksData>> {
let this = self.await?;
Ok(ChunkData::from_assets(
this.chunking_context.output_root().owned().await?,
*self.chunk_group().await?.assets,
))
}
}
fn manifest_chunk_reference_description() -> RcStr {
......@@ -162,16 +179,10 @@ impl ChunkableModule for ManifestAsyncModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: Vc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
ManifestChunkItem {
chunking_context,
manifest: self,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -181,4 +192,51 @@ impl EcmascriptChunkPlaceable for ManifestAsyncModule {
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let chunks_data = self.chunks_data().await?;
let chunks_data = chunks_data.iter().try_join().await?;
let chunks_data: Vec<_> = chunks_data
.iter()
.map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect();
let code = formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}({:#});
"#,
StringifyJs(&chunks_data)
};
Ok(EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell())
}
#[turbo_tasks::function]
fn chunk_item_content_ident(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<AssetIdent> {
self.content_ident()
}
#[turbo_tasks::function]
fn chunk_item_output_assets(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
self.chunk_group()
}
}
use anyhow::Result;
use indoc::formatdoc;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::{
chunk::{ChunkData, ChunkItem, ChunkType, ChunkingContext, ChunksData},
ident::AssetIdent,
module::Module,
output::{OutputAssetsReference, OutputAssetsWithReferenced},
};
use super::chunk_asset::ManifestAsyncModule;
use crate::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType,
data::EcmascriptChunkData,
},
runtime_functions::TURBOPACK_EXPORT_VALUE,
utils::StringifyJs,
};
/// The ManifestChunkItem generates a __turbopack_load__ call for every chunk
/// necessary to load the real asset. Once all the loads resolve, it is safe to
/// __turbopack_import__ the actual module that was dynamically imported.
#[turbo_tasks::value(shared)]
pub(super) struct ManifestChunkItem {
pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
pub manifest: ResolvedVc<ManifestAsyncModule>,
}
#[turbo_tasks::value_impl]
impl ManifestChunkItem {
#[turbo_tasks::function]
async fn chunks_data(&self) -> Result<Vc<ChunksData>> {
Ok(ChunkData::from_assets(
self.chunking_context.output_root().owned().await?,
*self.manifest.chunk_group().await?.assets,
))
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ManifestChunkItem {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<EcmascriptChunkItemContent>> {
let chunks_data = self.chunks_data().await?;
let chunks_data = chunks_data.iter().try_join().await?;
let chunks_data: Vec<_> = chunks_data
.iter()
.map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect();
let code = formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}({:#});
"#,
StringifyJs(&chunks_data)
};
Ok(EcmascriptChunkItemContent {
inner_code: code.into(),
..Default::default()
}
.cell())
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for ManifestChunkItem {
#[turbo_tasks::function]
fn references(&self) -> Vc<OutputAssetsWithReferenced> {
self.manifest.chunk_group()
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for ManifestChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.manifest.ident()
}
#[turbo_tasks::function]
fn content_ident(&self) -> Vc<AssetIdent> {
self.manifest.content_ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.manifest)
}
}
......@@ -6,20 +6,21 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::{
chunk::{
ChunkData, ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ChunksData,
AsyncModuleInfo, ChunkData, ChunkableModule, ChunkingContext, ChunksData,
ModuleChunkItemIdExt,
},
ident::AssetIdent,
module::Module,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::{OutputAssetsReference, OutputAssetsWithReferenced},
output::OutputAssetsWithReferenced,
reference::ModuleReferences,
};
use super::chunk_asset::ManifestAsyncModule;
use crate::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, data::EcmascriptChunkData,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
data::EcmascriptChunkData, ecmascript_chunk_item,
},
runtime_functions::{TURBOPACK_EXPORT_VALUE, TURBOPACK_LOAD, TURBOPACK_REQUIRE},
utils::{StringifyJs, StringifyModuleId},
......@@ -29,47 +30,39 @@ fn modifier() -> RcStr {
rcstr!("loader")
}
/// The manifest loader item is shipped in the same chunk that uses the dynamic
/// The manifest loader module is shipped in the same chunk that uses the dynamic
/// `import()` expression.
///
/// Its responsibility is to load the manifest chunk from the server. The
/// dynamic import has been rewritten to import this manifest loader item,
/// dynamic import has been rewritten to import this manifest loader module,
/// which will load the manifest chunk from the server, which will load all
/// the chunks needed by the dynamic import. Finally, we'll be able to import
/// the module we're trying to dynamically import.
///
/// Splitting the dynamic import into a quickly generate-able manifest loader
/// item and a slow-to-generate manifest chunk allows for faster incremental
/// module and a slow-to-generate manifest chunk allows for faster incremental
/// compilation. The traversal won't be performed until the dynamic import is
/// actually reached, instead of eagerly as part of the chunk that the dynamic
/// import appears in.
#[turbo_tasks::value]
pub struct ManifestLoaderChunkItem {
manifest: ResolvedVc<ManifestAsyncModule>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
pub struct ManifestLoaderModule {
pub manifest: ResolvedVc<ManifestAsyncModule>,
}
#[turbo_tasks::value_impl]
impl ManifestLoaderChunkItem {
impl ManifestLoaderModule {
#[turbo_tasks::function]
pub fn new(
manifest: ResolvedVc<ManifestAsyncModule>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Self> {
Self::cell(ManifestLoaderChunkItem {
manifest,
module_graph,
chunking_context,
})
pub fn new(manifest: ResolvedVc<ManifestAsyncModule>) -> Vc<Self> {
Self::cell(ManifestLoaderModule { manifest })
}
#[turbo_tasks::function]
pub async fn chunks_data(&self) -> Result<Vc<ChunksData>> {
let chunks = self.manifest.manifest_chunk_group().await?.assets;
pub async fn chunks_data(self: Vc<Self>) -> Result<Vc<ChunksData>> {
let this = self.await?;
let manifest = this.manifest.await?;
let chunks = this.manifest.manifest_chunk_group().await?.assets;
Ok(ChunkData::from_assets(
self.chunking_context.output_root().owned().await?,
manifest.chunking_context.output_root().owned().await?,
*chunks,
))
}
......@@ -81,48 +74,56 @@ impl ManifestLoaderChunkItem {
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for ManifestLoaderChunkItem {
#[turbo_tasks::function]
fn references(&self) -> Vc<OutputAssetsWithReferenced> {
self.manifest.manifest_chunk_group()
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for ManifestLoaderChunkItem {
impl Module for ManifestLoaderModule {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
fn ident(&self) -> Vc<AssetIdent> {
self.manifest.module_ident().with_modifier(modifier())
}
#[turbo_tasks::function]
fn content_ident(&self) -> Vc<AssetIdent> {
self.manifest.content_ident().with_modifier(modifier())
fn source(&self) -> Vc<turbopack_core::source::OptionSource> {
Vc::cell(None)
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
fn references(&self) -> Vc<ModuleReferences> {
Vc::cell(vec![])
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
ModuleSideEffects::SideEffectFree.cell()
}
}
#[turbo_tasks::value_impl]
impl ChunkableModule for ManifestLoaderModule {
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.manifest)
fn as_chunk_item(
self: ResolvedVc<Self>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ManifestLoaderChunkItem {
impl EcmascriptChunkPlaceable for ManifestLoaderModule {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = &*self.await?;
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = self.await?;
let mut code = Vec::new();
let manifest = this.manifest.await?;
......@@ -182,4 +183,30 @@ impl EcmascriptChunkItem for ManifestLoaderChunkItem {
}
.cell())
}
#[turbo_tasks::function]
fn chunk_item_content_ident(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<AssetIdent> {
self.content_ident()
}
#[turbo_tasks::function]
fn chunk_item_output_assets(
&self,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
self.manifest.manifest_chunk_group()
}
}
#[turbo_tasks::value_impl]
impl ManifestLoaderModule {
#[turbo_tasks::function]
pub fn content_ident(&self) -> Vc<AssetIdent> {
self.manifest.content_ident().with_modifier(modifier())
}
}
pub mod chunk_asset;
pub mod chunk_item;
pub mod loader_item;
pub mod loader_module;
......@@ -2,19 +2,21 @@ use anyhow::{Context, Result};
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{
AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext,
MergeableModuleExposure, MergeableModules, MergeableModulesExposed,
AsyncModuleInfo, ChunkableModule, ChunkingContext, MergeableModuleExposure,
MergeableModules, MergeableModulesExposed,
},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::ModuleReferences,
};
use crate::{
EcmascriptAnalyzable, EcmascriptModuleContent, EcmascriptOptions,
chunk::{EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType},
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
};
#[turbo_tasks::value(shared)]
......@@ -94,72 +96,34 @@ impl ChunkableModule for MergedEcmascriptModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: ResolvedVc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn ChunkItem>> {
Vc::upcast(
MergedEcmascriptModuleChunkItem {
module: self,
chunking_context,
}
.cell(),
)
}
}
#[turbo_tasks::value]
struct MergedEcmascriptModuleChunkItem {
module: ResolvedVc<MergedEcmascriptModule>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for MergedEcmascriptModuleChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for MergedEcmascriptModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for MergedEcmascriptModuleChunkItem {
impl EcmascriptChunkPlaceable for MergedEcmascriptModule {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should not be called");
fn get_exports(&self) -> Vc<EcmascriptExports> {
panic!("get_exports() should not be called on merged modules")
}
#[turbo_tasks::function]
async fn content_with_async_module_info(
&self,
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module = self.module.await?;
let module = self.await?;
let modules = &module.modules;
let entry_points = &module.entry_points;
let options = modules
.iter()
.map(|(m, _)| m.module_content_options(*self.chunking_context, async_module_info))
.map(|(m, _)| m.module_content_options(chunking_context, async_module_info))
.collect::<Vec<_>>();
let content = EcmascriptModuleContent::new_merged(
......@@ -173,7 +137,7 @@ impl EcmascriptChunkItem for MergedEcmascriptModuleChunkItem {
Ok(EcmascriptChunkItemContent::new(
content,
*self.chunking_context,
chunking_context,
async_module_options,
))
}
......
......@@ -8,7 +8,7 @@ use turbo_tasks_fs::{FileSystem, FileSystemPath, LinkType, VirtualFileSystem, ro
use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext},
ident::{AssetIdent, Layer},
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
......@@ -30,8 +30,8 @@ use turbopack_resolve::ecmascript::{cjs_resolve, esm_resolve};
use crate::{
EcmascriptModuleContent,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
references::async_module::{AsyncModule, OptionAsyncModule},
runtime_functions::{
......@@ -367,16 +367,10 @@ impl ChunkableModule for CachedExternalModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: Vc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn ChunkItem>> {
Vc::upcast(
CachedExternalModuleChunkItem {
module: self,
chunking_context,
}
.cell(),
)
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -410,25 +404,34 @@ impl EcmascriptChunkPlaceable for CachedExternalModule {
},
)
}
}
#[turbo_tasks::value]
pub struct CachedExternalModuleChunkItem {
module: ResolvedVc<CachedExternalModule>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::function]
fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Vc<EcmascriptChunkItemContent> {
let async_module_options = self.get_async_module().module_options(async_module_info);
EcmascriptChunkItemContent::new(self.content(), chunking_context, async_module_options)
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for CachedExternalModuleChunkItem {
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<OutputAssetsWithReferenced>> {
let module = self.module.await?;
async fn chunk_item_output_assets(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Result<Vc<OutputAssetsWithReferenced>> {
let module = self.await?;
let chunking_context_resolved = chunking_context.to_resolved().await?;
let assets = if let Some(target) = &module.target {
ResolvedVc::cell(vec![ResolvedVc::upcast(
ExternalsSymlinkAsset::new(
*self.chunking_context,
*chunking_context_resolved,
hashed_package_name(target).into(),
module.target.clone().unwrap(),
target.clone(),
)
.to_resolved()
.await?,
......@@ -445,55 +448,6 @@ impl OutputAssetsReference for CachedExternalModuleChunkItem {
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for CachedExternalModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn ty(self: Vc<Self>) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for CachedExternalModuleChunkItem {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should not be called");
}
#[turbo_tasks::function]
fn content_with_async_module_info(
&self,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Vc<EcmascriptChunkItemContent> {
let async_module_options = self
.module
.get_async_module()
.module_options(async_module_info);
EcmascriptChunkItemContent::new(
self.module.content(),
*self.chunking_context,
async_module_options,
)
}
}
/// A wrapper "passthrough" module type that always returns `false` for `is_self_async` and
/// `SideEffects` for `side_effects`.Be careful when using it, as it may hide async dependencies.
#[turbo_tasks::value]
......
......@@ -22,14 +22,13 @@ use turbo_tasks::{
use turbo_tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemPath};
use turbopack_core::{
chunk::{
ChunkItem, ChunkType, ChunkableModule, ChunkingContext, ChunkingType, ChunkingTypeOption,
AsyncModuleInfo, ChunkableModule, ChunkingContext, ChunkingType, ChunkingTypeOption,
MinifyType, ModuleChunkItemIdExt,
},
ident::AssetIdent,
issue::IssueSource,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
reference::{ModuleReference, ModuleReferences},
reference_type::CommonJsReferenceSubType,
resolve::{ModuleResolveResult, ResolveErrorMode, origin::ResolveOrigin, parse::Request},
......@@ -39,9 +38,7 @@ use turbopack_resolve::ecmascript::cjs_resolve;
use crate::{
EcmascriptChunkPlaceable,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType, EcmascriptExports,
},
chunk::{EcmascriptChunkItemContent, EcmascriptExports, ecmascript_chunk_item},
code_gen::{CodeGen, CodeGeneration, IntoCodeGenReference},
create_visitor,
references::{
......@@ -288,7 +285,7 @@ impl ModuleReference for RequireContextAssetReference {
}
#[turbo_tasks::function]
fn chunking_type(self: Vc<Self>) -> Vc<ChunkingTypeOption> {
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Parallel {
inherit_async: false,
hoisted: false,
......@@ -379,7 +376,7 @@ impl ModuleReference for ResolvedModuleReference {
}
#[turbo_tasks::function]
fn chunking_type(self: Vc<Self>) -> Vc<ChunkingTypeOption> {
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
Vc::cell(Some(ChunkingType::Parallel {
inherit_async: false,
hoisted: false,
......@@ -451,23 +448,12 @@ impl Module for RequireContextAsset {
#[turbo_tasks::value_impl]
impl ChunkableModule for RequireContextAsset {
#[turbo_tasks::function]
async fn as_chunk_item(
fn as_chunk_item(
self: ResolvedVc<Self>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Box<dyn turbopack_core::chunk::ChunkItem>>> {
let this = self.await?;
Ok(Vc::upcast(
RequireContextChunkItem {
module_graph,
chunking_context,
inner: self,
origin: this.origin,
map: this.map,
}
.cell(),
))
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -477,27 +463,17 @@ impl EcmascriptChunkPlaceable for RequireContextAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
}
#[turbo_tasks::value]
pub struct RequireContextChunkItem {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
inner: ResolvedVc<RequireContextAsset>,
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
map: ResolvedVc<RequireContextMap>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for RequireContextChunkItem {}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for RequireContextChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
async fn chunk_item_content(
&self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let map = &*self.map.await?;
let minify = self.chunking_context.minify_type().await?;
let minify = chunking_context.minify_type().await?;
let mut context_map = ObjectLit {
span: DUMMY_SP,
......@@ -508,7 +484,7 @@ impl EcmascriptChunkItem for RequireContextChunkItem {
let pm = PatternMapping::resolve_request(
*entry.request,
*self.origin,
*self.chunking_context,
chunking_context,
*entry.result,
ResolveType::ChunkItem,
)
......@@ -576,28 +552,3 @@ impl EcmascriptChunkItem for RequireContextChunkItem {
.cell())
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for RequireContextChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.inner.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.inner)
}
}
use anyhow::Result;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkItem, ChunkType, ChunkingContext},
ident::AssetIdent,
module::Module,
output::OutputAssetsReference,
};
use super::module::EcmascriptModuleFacadeModule;
use crate::{
EcmascriptAnalyzableExt,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType,
},
};
/// The chunk item for [EcmascriptModuleFacadeModule].
#[turbo_tasks::value(shared)]
pub struct EcmascriptModuleFacadeChunkItem {
pub(crate) module: ResolvedVc<EcmascriptModuleFacadeModule>,
pub(crate) chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for EcmascriptModuleFacadeChunkItem {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should never be called");
}
#[turbo_tasks::function]
fn content_with_async_module_info(
&self,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let chunking_context = self.chunking_context;
let content = self
.module
.module_content(*chunking_context, async_module_info);
let async_module_options = self
.module
.get_async_module()
.module_options(async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
*chunking_context,
async_module_options,
))
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for EcmascriptModuleFacadeChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for EcmascriptModuleFacadeChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
}
......@@ -14,11 +14,14 @@ use turbopack_core::{
resolve::{ExportUsage, ModulePart},
};
use super::chunk_item::EcmascriptModuleFacadeChunkItem;
use crate::{
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleContent,
EcmascriptModuleContentOptions, EcmascriptOptions, MergedEcmascriptModule, SpecifiedModuleType,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptAnalyzableExt,
EcmascriptModuleContent, EcmascriptModuleContentOptions, EcmascriptOptions,
MergedEcmascriptModule, SpecifiedModuleType,
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
code_gen::CodeGens,
export::Liveness,
references::{
......@@ -343,6 +346,25 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleFacadeModule {
async fn get_async_module(self: Vc<Self>) -> Result<Vc<OptionAsyncModule>> {
Ok(Vc::cell(Some(self.async_module().to_resolved().await?)))
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let content = self.module_content(chunking_context, async_module_info);
let async_module_options = self.get_async_module().module_options(async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
chunking_context,
async_module_options,
))
}
}
#[turbo_tasks::value_impl]
......@@ -350,16 +372,10 @@ impl ChunkableModule for EcmascriptModuleFacadeModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: ResolvedVc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Box<dyn turbopack_core::chunk::ChunkItem>>> {
Ok(Vc::upcast(
EcmascriptModuleFacadeChunkItem {
module: self,
chunking_context,
}
.cell(),
))
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......
use anyhow::Result;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkItem, ChunkType, ChunkingContext},
ident::AssetIdent,
module::Module,
output::OutputAssetsReference,
};
use crate::{
EcmascriptAnalyzableExt,
chunk::{EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType},
side_effect_optimization::locals::module::EcmascriptModuleLocalsModule,
};
/// The chunk item for [EcmascriptModuleLocalsModule].
#[turbo_tasks::value(shared)]
pub struct EcmascriptModuleLocalsChunkItem {
pub(super) module: ResolvedVc<EcmascriptModuleLocalsModule>,
pub(super) chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for EcmascriptModuleLocalsChunkItem {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should never be called");
}
#[turbo_tasks::function]
async fn content_with_async_module_info(
&self,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module = self.module.await?;
let chunking_context = self.chunking_context;
let original_module = module.module;
let analyze = original_module.analyze();
let analyze_result = analyze.await?;
let async_module_options = analyze_result
.async_module
.module_options(async_module_info);
let content = self
.module
.module_content(*chunking_context, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
*chunking_context,
async_module_options,
))
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for EcmascriptModuleLocalsChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for EcmascriptModuleLocalsChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
}
......@@ -14,11 +14,14 @@ use turbopack_core::{
resolve::ModulePart,
};
use super::chunk_item::EcmascriptModuleLocalsChunkItem;
use crate::{
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleContent, EcmascriptModuleContentOptions, MergedEcmascriptModule,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptAnalyzableExt,
EcmascriptModuleAsset, EcmascriptModuleContent, EcmascriptModuleContentOptions,
MergedEcmascriptModule,
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
references::{
async_module::OptionAsyncModule,
esm::{EsmExport, EsmExports},
......@@ -168,6 +171,26 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleLocalsModule {
fn get_async_module(&self) -> Vc<OptionAsyncModule> {
self.module.get_async_module()
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let analyze = self.await?.module.analyze().await?;
let async_module_options = analyze.async_module.module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
chunking_context,
async_module_options,
))
}
}
#[turbo_tasks::value_impl]
......@@ -175,16 +198,10 @@ impl ChunkableModule for EcmascriptModuleLocalsModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: ResolvedVc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
EcmascriptModuleLocalsChunkItem {
module: self,
chunking_context,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......
......@@ -10,15 +10,15 @@ use turbopack_core::{
resolve::{ExportUsage, ModulePart},
};
use super::{
SplitResult, chunk_item::EcmascriptModulePartChunkItem, get_part_id, part_of_module,
split_module,
};
use super::{SplitResult, get_part_id, part_of_module, split_module};
use crate::{
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptModuleAsset,
EcmascriptModuleAssetType, EcmascriptModuleContent, EcmascriptModuleContentOptions,
EcmascriptParsable,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
AnalyzeEcmascriptModuleResult, EcmascriptAnalyzable, EcmascriptAnalyzableExt,
EcmascriptModuleAsset, EcmascriptModuleAssetType, EcmascriptModuleContent,
EcmascriptModuleContentOptions, EcmascriptParsable,
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
parse::ParseResult,
references::{
FollowExportsResult, analyze_ecmascript_module, esm::FoundExportType, follow_reexports,
......@@ -350,6 +350,26 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset {
async fn get_exports(self: Vc<Self>) -> Result<Vc<EcmascriptExports>> {
Ok(*self.analyze().await?.exports)
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let analyze = self.analyze().await?;
let async_module_options = analyze.async_module.module_options(async_module_info);
let content = self.module_content(chunking_context, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
chunking_context,
async_module_options,
))
}
}
#[turbo_tasks::value_impl]
......@@ -357,16 +377,10 @@ impl ChunkableModule for EcmascriptModulePartAsset {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: ResolvedVc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
EcmascriptModulePartChunkItem {
module: self,
chunking_context,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......
use anyhow::Result;
use turbo_tasks::{ResolvedVc, ValueDefault, Vc};
use turbo_tasks_fs::rope::RopeBuilder;
use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkItem, ChunkType, ChunkingContext, ModuleChunkItemIdExt},
ident::AssetIdent,
module::Module,
module_graph::ModuleGraph,
output::OutputAssetsReference,
};
use super::asset::EcmascriptModulePartAsset;
use crate::{
EcmascriptAnalyzableExt,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions,
EcmascriptChunkPlaceable, EcmascriptChunkType, item::RewriteSourcePath,
},
references::async_module::AsyncModuleOptions,
runtime_functions::{TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_IMPORT},
tree_shake::side_effect_module::SideEffectsModule,
utils::StringifyModuleId,
};
/// This is an implementation of [ChunkItem] for
/// [Vc<EcmascriptModulePartAsset>].
///
/// This is a pointer to a part of an ES module.
#[turbo_tasks::value(shared)]
pub struct EcmascriptModulePartChunkItem {
pub(super) module: ResolvedVc<EcmascriptModulePartAsset>,
pub(super) chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for EcmascriptModulePartChunkItem {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should never be called");
}
#[turbo_tasks::function]
async fn content_with_async_module_info(
&self,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let analyze = self.module.analyze();
let analyze_ref = analyze.await?;
let async_module_options = analyze_ref.async_module.module_options(async_module_info);
let content = self
.module
.module_content(*self.chunking_context, async_module_info);
Ok(EcmascriptChunkItemContent::new(
content,
*self.chunking_context,
async_module_options,
))
}
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for EcmascriptModulePartChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for EcmascriptModulePartChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
}
#[turbo_tasks::value(shared)]
pub(super) struct SideEffectsModuleChunkItem {
pub module: ResolvedVc<SideEffectsModule>,
pub module_graph: ResolvedVc<ModuleGraph>,
pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for SideEffectsModuleChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for SideEffectsModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(EcmascriptChunkType::value_default())
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for SideEffectsModuleChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let mut code = RopeBuilder::default();
let mut has_top_level_await = false;
let module = self.module.await?;
for &side_effect in self.module.await?.side_effects.iter() {
let need_await = 'need_await: {
let async_module = *side_effect.get_async_module().await?;
if let Some(async_module) = async_module
&& async_module.await?.has_top_level_await
{
break 'need_await true;
}
false
};
if !has_top_level_await && need_await {
has_top_level_await = true;
}
code.push_bytes(
format!(
"{}{TURBOPACK_IMPORT}({});\n",
if need_await { "await " } else { "" },
StringifyModuleId(&side_effect.chunk_item_id(*self.chunking_context).await?)
)
.as_bytes(),
);
}
code.push_bytes(
format!(
"{TURBOPACK_EXPORT_NAMESPACE}({TURBOPACK_IMPORT}({}));\n",
StringifyModuleId(
&module
.resolved_as
.chunk_item_id(*self.chunking_context)
.await?
)
)
.as_bytes(),
);
let code = code.build();
Ok(EcmascriptChunkItemContent {
inner_code: code,
source_map: None,
rewrite_source_path: RewriteSourcePath::None,
options: EcmascriptChunkItemOptions {
strict: true,
async_module: if has_top_level_await {
Some(AsyncModuleOptions {
has_top_level_await: true,
})
} else {
None
},
..Default::default()
},
additional_ids: Default::default(),
placeholder_for_future_extensions: (),
}
.cell())
}
}
......@@ -25,7 +25,6 @@ use crate::{
};
pub mod asset;
pub mod chunk_item;
mod graph;
pub mod merge;
mod optimizations;
......
use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::rope::RopeBuilder;
use turbopack_core::{
chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset},
chunk::{
AsyncModuleInfo, ChunkableModule, ChunkingContext, EvaluatableAsset, ModuleChunkItemIdExt,
},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
......@@ -12,8 +15,13 @@ use turbopack_core::{
use crate::{
EcmascriptModuleAsset,
chunk::{EcmascriptChunkPlaceable, EcmascriptExports},
tree_shake::chunk_item::SideEffectsModuleChunkItem,
chunk::{
EcmascriptChunkItemContent, EcmascriptChunkItemOptions, EcmascriptChunkPlaceable,
EcmascriptExports, ecmascript_chunk_item, item::RewriteSourcePath,
},
references::async_module::AsyncModuleOptions,
runtime_functions::{TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_IMPORT},
utils::StringifyModuleId,
};
#[turbo_tasks::value]
......@@ -126,24 +134,85 @@ impl EcmascriptChunkPlaceable for SideEffectsModule {
fn get_exports(&self) -> Vc<EcmascriptExports> {
self.resolved_as.get_exports()
}
#[turbo_tasks::function]
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module = self.await?;
let mut code = RopeBuilder::default();
let mut has_top_level_await = false;
for &side_effect in module.side_effects.iter() {
let need_await = 'need_await: {
let async_module = *side_effect.get_async_module().await?;
if let Some(async_module) = async_module
&& async_module.await?.has_top_level_await
{
break 'need_await true;
}
false
};
if !has_top_level_await && need_await {
has_top_level_await = true;
}
code.push_bytes(
format!(
"{}{TURBOPACK_IMPORT}({});\n",
if need_await { "await " } else { "" },
StringifyModuleId(&side_effect.chunk_item_id(chunking_context).await?)
)
.as_bytes(),
);
}
code.push_bytes(
format!(
"{TURBOPACK_EXPORT_NAMESPACE}({TURBOPACK_IMPORT}({}));\n",
StringifyModuleId(&module.resolved_as.chunk_item_id(chunking_context).await?)
)
.as_bytes(),
);
let code = code.build();
Ok(EcmascriptChunkItemContent {
inner_code: code,
source_map: None,
rewrite_source_path: RewriteSourcePath::None,
options: EcmascriptChunkItemOptions {
strict: true,
async_module: if has_top_level_await {
Some(AsyncModuleOptions {
has_top_level_await: true,
})
} else {
None
},
..Default::default()
},
additional_ids: Default::default(),
placeholder_for_future_extensions: (),
}
.cell())
}
}
#[turbo_tasks::value_impl]
impl ChunkableModule for SideEffectsModule {
#[turbo_tasks::function]
async fn as_chunk_item(
self: Vc<Self>,
fn as_chunk_item(
self: ResolvedVc<Self>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Box<dyn turbopack_core::chunk::ChunkItem>>> {
Ok(Vc::upcast(
SideEffectsModuleChunkItem {
module: self.to_resolved().await?,
module_graph,
chunking_context,
}
.cell(),
))
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......
pub mod chunk_item;
pub mod module;
pub mod worker_type;
......
......@@ -17,18 +17,17 @@ use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::{FileContent, FileJsonContent};
use turbopack_core::{
asset::Asset,
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext},
code_builder::CodeBuilder,
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::OutputAssetsReference,
source::Source,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
runtime_functions::TURBOPACK_EXPORT_VALUE,
};
......@@ -69,13 +68,10 @@ impl ChunkableModule for JsonModuleAsset {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: Vc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(JsonChunkItem::cell(JsonChunkItem {
module: self,
chunking_context,
}))
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -85,49 +81,19 @@ impl EcmascriptChunkPlaceable for JsonModuleAsset {
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
}
#[turbo_tasks::value]
struct JsonChunkItem {
module: ResolvedVc<JsonModuleAsset>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for JsonChunkItem {}
#[turbo_tasks::value_impl]
impl ChunkItem for JsonChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for JsonChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
async fn chunk_item_content(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
// We parse to JSON and then stringify again to ensure that the
// JSON is valid.
let content = self.module.await?.source.content().file_content();
let this = self.await?;
let content = this.source.content().file_content();
let data = content.parse_json().await?;
match &*data {
FileJsonContent::Content(data) => {
......@@ -152,7 +118,7 @@ impl EcmascriptChunkItem for JsonChunkItem {
// `uri_from_file`) need to handle percent encoding correctly first.
//
// See turbopack/crates/turbopack-core/src/source_map/utils.rs as well
"sources": [format!("turbopack:///{}", self.module.ident().path().to_string().await?)],
"sources": [format!("turbopack:///{}", self.ident().path().to_string().await?)],
"sourcesContent": [&data_str],
"names": [],
// Maps 0:0 in the output code to 0:0 in the `source_code`. Sufficient for
......@@ -183,10 +149,7 @@ impl EcmascriptChunkItem for JsonChunkItem {
Err(Error::msg(message))
}
FileJsonContent::NotFound => {
bail!(
"JSON file not found: {}",
self.module.ident().to_string().await?
);
bail!("JSON file not found: {}", self.ident().to_string().await?);
}
}
}
......
......@@ -26,7 +26,7 @@ use turbopack_core::{
use turbopack_ecmascript::{
async_chunk::module::AsyncLoaderModule,
chunk::EcmascriptChunk,
manifest::{chunk_asset::ManifestAsyncModule, loader_item::ManifestLoaderChunkItem},
manifest::{chunk_asset::ManifestAsyncModule, loader_module::ManifestLoaderModule},
};
use turbopack_ecmascript_runtime::RuntimeType;
......@@ -638,17 +638,24 @@ impl ChunkingContext for NodeJsChunkingContext {
module_graph: Vc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Result<Vc<Box<dyn ChunkItem>>> {
let chunking_context: ResolvedVc<Box<dyn ChunkingContext>> =
Vc::upcast::<Box<dyn ChunkingContext>>(self)
.to_resolved()
.await?;
Ok(if self.await?.manifest_chunks {
let manifest_asset =
ManifestAsyncModule::new(module, module_graph, Vc::upcast(self), availability_info);
Vc::upcast(ManifestLoaderChunkItem::new(
manifest_asset,
let manifest_asset = ManifestAsyncModule::new(
module,
module_graph,
Vc::upcast(self),
))
*chunking_context,
availability_info,
)
.to_resolved()
.await?;
let loader_module = ManifestLoaderModule::new(*manifest_asset);
loader_module.as_chunk_item(module_graph, *chunking_context)
} else {
let module = AsyncLoaderModule::new(module, Vc::upcast(self), availability_info);
module.as_chunk_item(module_graph, Vc::upcast(self))
let module = AsyncLoaderModule::new(module, *chunking_context, availability_info);
module.as_chunk_item(module_graph, *chunking_context)
})
}
......@@ -658,7 +665,7 @@ impl ChunkingContext for NodeJsChunkingContext {
module: Vc<Box<dyn ChunkableModule>>,
) -> Result<Vc<AssetIdent>> {
Ok(if self.await?.manifest_chunks {
ManifestLoaderChunkItem::asset_ident_for(module)
ManifestLoaderModule::asset_ident_for(module)
} else {
AsyncLoaderModule::asset_ident_for(module)
})
......
......@@ -2,17 +2,17 @@ use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{AssetSuffix, ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
chunk::{AssetSuffix, AsyncModuleInfo, ChunkableModule, ChunkingContext},
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::{OutputAsset, OutputAssetsReference, OutputAssetsWithReferenced},
output::{OutputAsset, OutputAssetsWithReferenced},
source::Source,
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
runtime_functions::{TURBOPACK_EXPORT_URL, TURBOPACK_EXPORT_VALUE},
utils::StringifyJs,
......@@ -71,22 +71,12 @@ impl Module for StaticUrlJsModule {
#[turbo_tasks::value_impl]
impl ChunkableModule for StaticUrlJsModule {
#[turbo_tasks::function]
async fn as_chunk_item(
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: Vc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Box<dyn turbopack_core::chunk::ChunkItem>>> {
Ok(Vc::upcast(StaticUrlJsChunkItem::cell(
StaticUrlJsChunkItem {
module: self,
chunking_context,
static_asset: self
.static_output_asset(*chunking_context)
.to_resolved()
.await?,
tag: self.await?.tag.clone(),
},
)))
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -96,61 +86,22 @@ impl EcmascriptChunkPlaceable for StaticUrlJsModule {
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
}
#[turbo_tasks::value]
struct StaticUrlJsChunkItem {
module: ResolvedVc<StaticUrlJsModule>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
static_asset: ResolvedVc<StaticOutputAsset>,
tag: Option<RcStr>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for StaticUrlJsChunkItem {
#[turbo_tasks::function]
fn references(&self) -> Vc<OutputAssetsWithReferenced> {
OutputAssetsWithReferenced::from_assets(Vc::cell(vec![ResolvedVc::upcast(
self.static_asset,
)]))
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for StaticUrlJsChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
*ResolvedVc::upcast(self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for StaticUrlJsChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let url = self
.chunking_context
.asset_url(self.static_asset.path().owned().await?, self.tag.clone())
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let this = self.await?;
let static_asset = self.static_output_asset(chunking_context);
let url = chunking_context
.asset_url(static_asset.path().owned().await?, this.tag.clone())
.await?;
let url_behavior = self.chunking_context.url_behavior(self.tag.clone()).await?;
let url_behavior = chunking_context.url_behavior(this.tag.clone()).await?;
let inner_code = match &url_behavior.suffix {
AssetSuffix::None => {
......@@ -188,4 +139,27 @@ impl EcmascriptChunkItem for StaticUrlJsChunkItem {
}
.cell())
}
#[turbo_tasks::function]
fn chunk_item_output_assets(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
static_url_js_output_assets(self, chunking_context)
}
}
#[turbo_tasks::function]
async fn static_url_js_output_assets(
module: Vc<StaticUrlJsModule>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<OutputAssetsWithReferenced>> {
let static_asset = module
.static_output_asset(chunking_context)
.to_resolved()
.await?;
Ok(OutputAssetsWithReferenced::from_assets(Vc::cell(vec![
ResolvedVc::upcast(static_asset),
])))
}
......@@ -7,7 +7,7 @@ error - [code gen] /turbopack/crates/turbopack-tests/tests/snapshot/imports/json
Debug info:
- An error occurred while generating the chunk item [project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/json/input/invalid.json (json)
- Execution of <JsonChunkItem as EcmascriptChunkItem>::content failed
- Execution of <JsonModuleAsset as EcmascriptChunkPlaceable>::chunk_item_content failed
- Unable to make a module from invalid JSON: expected `,` or `}` at line 3 column 26
at nested.?
1 | {
......
use anyhow::{Context, Result, bail};
use anyhow::{Result, bail};
use turbo_rcstr::rcstr;
use turbo_tasks::{IntoTraitRef, ResolvedVc, Vc, fxindexmap};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
chunk::{
AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext,
chunk_group::references_to_output_assets,
AsyncModuleInfo, ChunkableModule, ChunkingContext, chunk_group::references_to_output_assets,
},
context::AssetContext,
ident::AssetIdent,
module::{Module, ModuleSideEffects, OptionModule},
module_graph::ModuleGraph,
output::{OutputAssetsReference, OutputAssetsWithReferenced},
output::OutputAssetsWithReferenced,
reference::{ModuleReferences, SingleChunkableModuleReference},
reference_type::ReferenceType,
resolve::{ExportUsage, origin::ResolveOrigin, parse::Request},
......@@ -19,8 +18,8 @@ use turbopack_core::{
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
references::async_module::OptionAsyncModule,
};
......@@ -161,14 +160,7 @@ impl ChunkableModule for WebAssemblyModuleAsset {
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
Vc::upcast(
ModuleChunkItem {
module: self,
module_graph,
chunking_context,
}
.cell(),
)
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -183,92 +175,49 @@ impl EcmascriptChunkPlaceable for WebAssemblyModuleAsset {
fn get_async_module(self: Vc<Self>) -> Vc<OptionAsyncModule> {
self.loader().get_async_module()
}
}
#[turbo_tasks::value_impl]
impl ResolveOrigin for WebAssemblyModuleAsset {
#[turbo_tasks::function]
fn origin_path(&self) -> Vc<FileSystemPath> {
self.source.ident().path()
}
#[turbo_tasks::function]
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
}
#[turbo_tasks::function]
fn get_inner_asset(self: Vc<Self>, request: Vc<Request>) -> Vc<OptionModule> {
self.loader_as_resolve_origin().get_inner_asset(request)
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
module_graph: Vc<ModuleGraph>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
// Delegate to the loader's chunk item content
Ok(self.loader().chunk_item_content(
chunking_context,
module_graph,
async_module_info,
estimated,
))
}
}
#[turbo_tasks::value]
struct ModuleChunkItem {
module: ResolvedVc<WebAssemblyModuleAsset>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
module_graph: ResolvedVc<ModuleGraph>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for ModuleChunkItem {
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<OutputAssetsWithReferenced>> {
let loader_references = self.module.loader().references().await?;
async fn chunk_item_output_assets(
self: Vc<Self>,
_chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Result<Vc<OutputAssetsWithReferenced>> {
let loader_references = self.loader().references().await?;
references_to_output_assets(&*loader_references).await
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
impl ResolveOrigin for WebAssemblyModuleAsset {
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
fn origin_path(&self) -> Vc<FileSystemPath> {
self.source.ident().path()
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ModuleChunkItem {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<EcmascriptChunkItemContent> {
panic!("content() should not be called");
fn asset_context(&self) -> Vc<Box<dyn AssetContext>> {
*self.asset_context
}
#[turbo_tasks::function]
async fn content_with_async_module_info(
&self,
async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let loader_asset = self.module.loader();
let item = loader_asset.as_chunk_item(*self.module_graph, *self.chunking_context);
let ecmascript_item =
ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkItem>>(item.to_resolved().await?)
.context("EcmascriptModuleAsset must implement EcmascriptChunkItem")?;
let chunk_item_content = ecmascript_item
.content_with_async_module_info(async_module_info, false)
.owned()
.await?;
Ok(chunk_item_content.cell())
fn get_inner_asset(self: Vc<Self>, request: Vc<Request>) -> Vc<OptionModule> {
self.loader_as_resolve_origin().get_inner_asset(request)
}
}
......@@ -2,18 +2,18 @@ use anyhow::{Result, bail};
use turbo_rcstr::rcstr;
use turbo_tasks::{IntoTraitRef, ResolvedVc, Vc};
use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext},
context::AssetContext,
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
output::{OutputAsset, OutputAssetsReference, OutputAssetsWithReferenced},
output::{OutputAsset, OutputAssetsWithReferenced},
source::{OptionSource, Source},
};
use turbopack_ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkType, EcmascriptExports,
EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptExports,
ecmascript_chunk_item,
},
runtime_functions::TURBOPACK_EXPORT_URL,
utils::StringifyJs,
......@@ -74,19 +74,12 @@ impl Module for RawWebAssemblyModuleAsset {
#[turbo_tasks::value_impl]
impl ChunkableModule for RawWebAssemblyModuleAsset {
#[turbo_tasks::function]
async fn as_chunk_item(
fn as_chunk_item(
self: ResolvedVc<Self>,
_module_graph: Vc<ModuleGraph>,
module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Box<dyn turbopack_core::chunk::ChunkItem>>> {
Ok(Vc::upcast(
RawModuleChunkItem {
module: self,
chunking_context,
wasm_asset: self.wasm_asset(*chunking_context).to_resolved().await?,
}
.cell(),
))
) -> Vc<Box<dyn turbopack_core::chunk::ChunkItem>> {
ecmascript_chunk_item(ResolvedVc::upcast(self), module_graph, chunking_context)
}
}
......@@ -96,54 +89,18 @@ impl EcmascriptChunkPlaceable for RawWebAssemblyModuleAsset {
fn get_exports(self: Vc<Self>) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
}
#[turbo_tasks::value]
struct RawModuleChunkItem {
module: ResolvedVc<RawWebAssemblyModuleAsset>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
wasm_asset: ResolvedVc<WebAssemblyAsset>,
}
#[turbo_tasks::value_impl]
impl OutputAssetsReference for RawModuleChunkItem {
#[turbo_tasks::function]
fn references(&self) -> Vc<OutputAssetsWithReferenced> {
OutputAssetsWithReferenced::from_assets(Vc::cell(vec![ResolvedVc::upcast(self.wasm_asset)]))
}
}
#[turbo_tasks::value_impl]
impl ChunkItem for RawModuleChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.module.ident()
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
}
#[turbo_tasks::function]
async fn ty(&self) -> Result<Vc<Box<dyn ChunkType>>> {
Ok(Vc::upcast(
Vc::<EcmascriptChunkType>::default().resolve().await?,
))
}
#[turbo_tasks::function]
fn module(&self) -> Vc<Box<dyn Module>> {
Vc::upcast(*self.module)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for RawModuleChunkItem {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<EcmascriptChunkItemContent>> {
let path = self.wasm_asset.path().await?;
let output_root = self.chunking_context.output_root().await?;
async fn chunk_item_content(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
_async_module_info: Option<Vc<AsyncModuleInfo>>,
_estimated: bool,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let wasm_asset = self.wasm_asset(chunking_context);
let path = wasm_asset.path().await?;
let output_root = chunking_context.output_root().await?;
let Some(path) = output_root.get_path_to(&path) else {
bail!("WASM asset ident is not relative to output root");
......@@ -155,4 +112,16 @@ impl EcmascriptChunkItem for RawModuleChunkItem {
}
.cell())
}
#[turbo_tasks::function]
async fn chunk_item_output_assets(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
_module_graph: Vc<ModuleGraph>,
) -> Result<Vc<OutputAssetsWithReferenced>> {
let wasm_asset = self.wasm_asset(chunking_context).to_resolved().await?;
Ok(OutputAssetsWithReferenced::from_assets(Vc::cell(vec![
ResolvedVc::upcast(wasm_asset),
])))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment