File size: 5,580 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
use anyhow::{Context, Result};
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{
AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext,
MergeableModuleExposure, MergeableModules, MergeableModulesExposed,
},
ident::AssetIdent,
module::Module,
module_graph::ModuleGraph,
reference::ModuleReferences,
};
use crate::{
EcmascriptAnalyzable, EcmascriptModuleContent, EcmascriptOptions,
chunk::{EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkType},
};
#[turbo_tasks::value(shared)]
pub(crate) struct MergedEcmascriptModule {
modules: Vec<(
ResolvedVc<Box<dyn EcmascriptAnalyzable>>,
MergeableModuleExposure,
)>,
entry_points: Vec<ResolvedVc<Box<dyn EcmascriptAnalyzable>>>,
options: ResolvedVc<EcmascriptOptions>,
}
impl MergedEcmascriptModule {
pub async fn new(
modules: Vc<MergeableModulesExposed>,
entry_points: Vc<MergeableModules>,
options: ResolvedVc<EcmascriptOptions>,
) -> Result<ResolvedVc<Self>> {
Ok(MergedEcmascriptModule {
modules: modules
.await?
.iter()
.map(|(m, exposed)| {
Ok((
ResolvedVc::try_sidecast::<Box<dyn EcmascriptAnalyzable>>(*m)
.context("expected EcmascriptAnalyzable")?,
*exposed,
))
})
.collect::<Result<Vec<_>>>()?,
entry_points: entry_points
.await?
.iter()
.map(|m| {
ResolvedVc::try_sidecast::<Box<dyn EcmascriptAnalyzable>>(*m)
.context("expected EcmascriptAnalyzable")
})
.collect::<Result<Vec<_>>>()?,
options,
}
.resolved_cell())
}
}
#[turbo_tasks::value_impl]
impl Asset for MergedEcmascriptModule {
#[turbo_tasks::function]
fn content(&self) -> Vc<AssetContent> {
panic!("content() should not be called");
}
}
#[turbo_tasks::value_impl]
impl Module for MergedEcmascriptModule {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
// This purposely reuses the module's ident as it has replaced the original module, thus
// there can never be a collision.
self.entry_points.first().unwrap().ident()
}
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
panic!("references() should not be called");
}
#[turbo_tasks::function]
async fn is_self_async(&self) -> Result<Vc<bool>> {
panic!("is_self_async() should not be called");
}
}
#[turbo_tasks::value_impl]
impl ChunkableModule for MergedEcmascriptModule {
#[turbo_tasks::function]
fn as_chunk_item(
self: ResolvedVc<Self>,
_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 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>> {
*ResolvedVc::upcast(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 MergedEcmascriptModuleChunkItem {
#[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,
async_module_info: Option<Vc<AsyncModuleInfo>>,
) -> Result<Vc<EcmascriptChunkItemContent>> {
let module = self.module.await?;
let modules = &module.modules;
let entry_points = &module.entry_points;
let options = modules
.iter()
.map(|(m, _)| {
let Some(m) = ResolvedVc::try_downcast::<Box<dyn EcmascriptAnalyzable>>(*m) else {
anyhow::bail!("Expected EcmascriptAnalyzable in scope hoisting group");
};
Ok(m.module_content_options(*self.chunking_context, async_module_info))
})
.collect::<Result<Vec<_>>>()?;
let content = EcmascriptModuleContent::new_merged(
modules.clone(),
options,
ResolvedVc::deref_vec(entry_points.clone()),
);
// Currently, merged modules never include async modules.
let async_module_options = Vc::cell(None);
Ok(EcmascriptChunkItemContent::new(
content,
*self.chunking_context,
async_module_options,
))
}
}
|