File size: 5,770 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 181 182 183 184 185 186 187 188 | 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,
};
use super::asset::EcmascriptModulePartAsset;
use crate::{
EcmascriptAnalyzable,
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkItemOptions,
EcmascriptChunkPlaceable, EcmascriptChunkType,
},
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>>,
) -> 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 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>> {
*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(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 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: None,
options: EcmascriptChunkItemOptions {
strict: true,
exports: 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())
}
}
|