Skip to main content

turbopack_ecmascript/chunk/
item.rs

1use std::io::Write;
2
3use anyhow::{Result, bail};
4use async_trait::async_trait;
5use bincode::{Decode, Encode};
6use smallvec::SmallVec;
7use turbo_rcstr::{RcStr, rcstr};
8use turbo_tasks::{
9    NonLocalValue, PrettyPrintError, ResolvedVc, Upcast, ValueToString, Vc, trace::TraceRawVcs,
10};
11use turbo_tasks_fs::{FileSystemPath, rope::Rope};
12use turbopack_core::{
13    chunk::{
14        AsyncModuleInfo, ChunkItem, ChunkItemWithAsyncModuleInfo, ChunkType, ChunkingContext,
15        ChunkingContextExt, ModuleId, SourceMapSourceType,
16    },
17    code_builder::{Code, CodeBuilder, PersistedCode},
18    ident::AssetIdent,
19    issue::{IssueExt, IssueSeverity, StyledString, code_gen::CodeGenerationIssue},
20    module::Module,
21    module_graph::ModuleGraph,
22    output::OutputAssetsReference,
23    source_map::{
24        structured::StructuredSourceMap,
25        utils::{absolute_fileify_source_map, relative_fileify_source_map},
26    },
27};
28
29use crate::{
30    EcmascriptModuleContent,
31    chunk::{chunk_type::EcmascriptChunkType, placeable::EcmascriptChunkPlaceable},
32    references::async_module::{AsyncModuleOptions, OptionAsyncModuleOptions},
33    runtime_functions::TURBOPACK_ASYNC_MODULE,
34    utils::StringifyJs,
35};
36
37#[turbo_tasks::task_input]
38#[derive(Debug, Clone, PartialEq, Eq, Hash, TraceRawVcs, Default, Encode, Decode)]
39pub enum RewriteSourcePath {
40    AbsoluteFilePath(FileSystemPath),
41    RelativeFilePath(FileSystemPath, RcStr),
42    #[default]
43    None,
44}
45
46// Note we don't want to persist this as `module_factory_with_code_generation_issue` is already
47// persisted and we want to avoid duplicating it.
48#[turbo_tasks::value(shared, serialization = "skip")]
49#[derive(Default, Clone)]
50pub struct EcmascriptChunkItemContent {
51    pub inner_code: Rope,
52    pub source_map: Option<StructuredSourceMap>,
53    pub additional_ids: SmallVec<[ModuleId; 1]>,
54    pub options: EcmascriptChunkItemOptions,
55    pub rewrite_source_path: RewriteSourcePath,
56    pub placeholder_for_future_extensions: (),
57}
58
59#[turbo_tasks::value_impl]
60impl EcmascriptChunkItemContent {
61    #[turbo_tasks::function]
62    pub async fn new(
63        content: Vc<EcmascriptModuleContent>,
64        chunking_context: Vc<Box<dyn ChunkingContext>>,
65        async_module_options: Vc<OptionAsyncModuleOptions>,
66    ) -> Result<Vc<Self>> {
67        let supports_arrow_functions = *chunking_context
68            .environment()
69            .runtime_versions()
70            .supports_arrow_functions()
71            .await?;
72        let externals = *chunking_context
73            .environment()
74            .supports_commonjs_externals()
75            .await?;
76
77        let content = content.await?;
78        let async_module = async_module_options.owned().await?;
79        let strict = content.strict;
80
81        Ok(EcmascriptChunkItemContent {
82            rewrite_source_path: match *chunking_context.source_map_source_type().await? {
83                SourceMapSourceType::AbsoluteFileUri => {
84                    RewriteSourcePath::AbsoluteFilePath(chunking_context.root_path().owned().await?)
85                }
86                SourceMapSourceType::RelativeUri => RewriteSourcePath::RelativeFilePath(
87                    chunking_context.root_path().owned().await?,
88                    chunking_context
89                        .relative_path_from_chunk_root_to_project_root()
90                        .owned()
91                        .await?,
92                ),
93                SourceMapSourceType::TurbopackUri => RewriteSourcePath::None,
94            },
95            inner_code: content.inner_code.clone(),
96            source_map: content.source_map.clone(),
97            additional_ids: content.additional_ids.clone(),
98            options: if content.is_esm {
99                EcmascriptChunkItemOptions {
100                    strict: true,
101                    externals,
102                    async_module,
103                    supports_arrow_functions,
104                    ..Default::default()
105                }
106            } else {
107                if async_module.is_some() {
108                    bail!("CJS module can't be async.");
109                }
110
111                EcmascriptChunkItemOptions {
112                    strict,
113                    externals,
114                    supports_arrow_functions,
115                    // These things are not available in ESM
116                    module_and_exports: true,
117                    ..Default::default()
118                }
119            },
120            ..Default::default()
121        }
122        .cell())
123    }
124}
125
126impl EcmascriptChunkItemContent {
127    async fn module_factory(&self) -> Result<ResolvedVc<PersistedCode>> {
128        let mut code = CodeBuilder::default();
129        for additional_id in self.additional_ids.iter() {
130            writeln!(code, "{}, ", StringifyJs(&additional_id))?;
131        }
132
133        if self.options.supports_arrow_functions {
134            code += "((";
135        } else {
136            code += "(function(";
137        }
138        if self.options.module_and_exports {
139            code += "__turbopack_context__, module, exports";
140        } else {
141            code += "__turbopack_context__";
142        }
143        if self.options.supports_arrow_functions {
144            code += ") => {\n";
145        } else {
146            code += "){\n";
147        }
148
149        if self.options.strict {
150            code += "\"use strict\";\n\n";
151        } else {
152            code += "\n";
153        }
154
155        if self.options.async_module.is_some() {
156            write!(code, "return {TURBOPACK_ASYNC_MODULE}")?;
157            if self.options.supports_arrow_functions {
158                code += "(async (";
159            } else {
160                code += "(async function(";
161            }
162            code += "__turbopack_handle_async_dependencies__, __turbopack_async_result__";
163            if self.options.supports_arrow_functions {
164                code += ") => {";
165            } else {
166                code += "){";
167            }
168            code += " try {\n";
169        }
170
171        let source_map = match (&self.rewrite_source_path, &self.source_map) {
172            (RewriteSourcePath::AbsoluteFilePath(path), Some(map)) => {
173                Some(absolute_fileify_source_map(map, path.clone()).await?)
174            }
175            (RewriteSourcePath::RelativeFilePath(path, relative_path), Some(map)) => {
176                Some(relative_fileify_source_map(map, path.clone(), relative_path.clone()).await?)
177            }
178            (_, map) => map.clone(),
179        };
180
181        code.push_source(&self.inner_code, source_map);
182
183        if let Some(opts) = &self.options.async_module {
184            write!(
185                code,
186                "__turbopack_async_result__();\n}} catch(e) {{ __turbopack_async_result__(e); }} \
187                 }}, {});",
188                opts.has_top_level_await
189            )?;
190        }
191
192        code += "})";
193
194        Ok(code.build().cell_persisted())
195    }
196}
197
198#[derive(PartialEq, Eq, Default, Debug, Clone, TraceRawVcs, NonLocalValue, Encode, Decode)]
199pub struct EcmascriptChunkItemOptions {
200    /// Whether this chunk item should be in "use strict" mode.
201    pub strict: bool,
202    /// Whether this chunk item's module factory should include a `module` and
203    /// `exports` argument.
204    pub module_and_exports: bool,
205    /// Whether this chunk item's module factory should include a
206    /// `__turbopack_external_require__` argument.
207    pub externals: bool,
208    /// Whether this chunk item's module is async (either has a top level await
209    /// or is importing async modules).
210    pub async_module: Option<AsyncModuleOptions>,
211    /// Whether the environment supports arrow functions (e.g. when targeting modern browsers).
212    pub supports_arrow_functions: bool,
213    pub placeholder_for_future_extensions: (),
214}
215
216#[turbo_tasks::task_input]
217#[derive(Debug, Clone, PartialEq, Eq, Hash, TraceRawVcs, Encode, Decode)]
218pub struct EcmascriptChunkItemWithAsyncInfo {
219    pub chunk_item: ResolvedVc<Box<dyn EcmascriptChunkItem>>,
220    pub async_info: Option<ResolvedVc<AsyncModuleInfo>>,
221}
222
223impl EcmascriptChunkItemWithAsyncInfo {
224    pub fn from_chunk_item(
225        chunk_item: &ChunkItemWithAsyncModuleInfo,
226    ) -> Result<EcmascriptChunkItemWithAsyncInfo> {
227        let ChunkItemWithAsyncModuleInfo {
228            chunk_item,
229            chunk_type: _,
230            module: _,
231            async_info,
232        } = chunk_item;
233        let Some(chunk_item) =
234            ResolvedVc::try_downcast::<Box<dyn EcmascriptChunkItem>>(*chunk_item)
235        else {
236            bail!("Chunk item is not an ecmascript chunk item but reporting chunk type ecmascript");
237        };
238        Ok(EcmascriptChunkItemWithAsyncInfo {
239            chunk_item,
240            async_info: *async_info,
241        })
242    }
243}
244
245#[async_trait]
246#[turbo_tasks::value_trait]
247pub trait EcmascriptChunkItem: ChunkItem + OutputAssetsReference {
248    /// Fetches the content of the chunk item with async module info.
249    /// When `estimated` is true, it's ok to provide an estimated content, since it's only used for
250    /// compute the chunking. When `estimated` is true, this function should not invoke other
251    /// chunking operations that would cause cycles.
252    async fn content_with_async_module_info(
253        &self,
254        async_module_info: Option<Vc<AsyncModuleInfo>>,
255        estimated: bool,
256    ) -> Result<Vc<EcmascriptChunkItemContent>>;
257}
258
259pub trait EcmascriptChunkItemExt {
260    /// Generates the module factory for this chunk item.
261    fn code(self: Vc<Self>, async_module_info: Option<Vc<AsyncModuleInfo>>) -> Vc<Code>;
262}
263
264impl<T> EcmascriptChunkItemExt for T
265where
266    T: Upcast<Box<dyn EcmascriptChunkItem>>,
267{
268    /// Generates the module factory for this chunk item.
269    fn code(self: Vc<Self>, async_module_info: Option<Vc<AsyncModuleInfo>>) -> Vc<Code> {
270        module_factory_with_code_generation_issue(Vc::upcast_non_strict(self), async_module_info)
271            .to_code()
272    }
273}
274
275#[turbo_tasks::function]
276async fn module_factory_with_code_generation_issue(
277    chunk_item: Vc<Box<dyn EcmascriptChunkItem>>,
278    async_module_info: Option<Vc<AsyncModuleInfo>>,
279) -> Result<Vc<PersistedCode>> {
280    async fn get_content(
281        chunk_item: Vc<Box<dyn EcmascriptChunkItem>>,
282        async_module_info: Option<Vc<AsyncModuleInfo>>,
283    ) -> Result<ResolvedVc<PersistedCode>> {
284        let chunk_item_ref = chunk_item.into_trait_ref().await?;
285        let content = chunk_item_ref
286            .content_with_async_module_info(async_module_info, false)
287            .await?
288            .await?;
289        content.module_factory().await
290    }
291    let content = get_content(chunk_item, async_module_info).await;
292    Ok(match content {
293        Ok(factory) => *factory,
294        Err(error) => {
295            let id = chunk_item.asset_ident().to_string().await;
296            let id = id.as_ref().map_or_else(|_| "unknown", |id| &**id);
297
298            // ast-grep-ignore: no-context-format
299            let error = error.context(format!(
300                "An error occurred while generating the chunk item {id}"
301            ));
302            let error_message = format!("{}", PrettyPrintError(&error)).into();
303            let js_error_message = serde_json::to_string(&error_message)?;
304            CodeGenerationIssue {
305                severity: IssueSeverity::Error,
306                path: chunk_item.asset_ident().await?.path.clone(),
307                title: StyledString::Text(rcstr!("Code generation for chunk item errored"))
308                    .resolved_cell(),
309                message: StyledString::Text(error_message).resolved_cell(),
310                source: None,
311            }
312            .resolved_cell()
313            .emit();
314            let mut code = CodeBuilder::default();
315            code += "(() => {{\n\n";
316            writeln!(code, "throw new Error({error});", error = js_error_message)?;
317            code += "\n}})";
318            *code.build().cell_persisted()
319        }
320    })
321}
322
323/// Generic chunk item that wraps any EcmascriptChunkPlaceable module.
324/// This replaces the need for individual per-module ChunkItem wrapper structs.
325#[turbo_tasks::value]
326pub struct EcmascriptModuleChunkItem {
327    module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
328    chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
329    module_graph: ResolvedVc<ModuleGraph>,
330}
331
332/// Factory function to create an EcmascriptModuleChunkItem.
333/// Use this instead of implementing ChunkableModule::as_chunk_item() on each module.
334pub fn ecmascript_chunk_item(
335    module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
336    module_graph: ResolvedVc<ModuleGraph>,
337    chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
338) -> Vc<Box<dyn ChunkItem>> {
339    Vc::upcast(
340        EcmascriptModuleChunkItem {
341            module,
342            chunking_context,
343            module_graph,
344        }
345        .cell(),
346    )
347}
348
349#[turbo_tasks::value_impl]
350impl ChunkItem for EcmascriptModuleChunkItem {
351    #[turbo_tasks::function]
352    fn asset_ident(&self) -> Vc<AssetIdent> {
353        self.module.ident()
354    }
355
356    #[turbo_tasks::function]
357    fn content_ident(&self) -> Vc<AssetIdent> {
358        self.module
359            .chunk_item_content_ident(*self.chunking_context, *self.module_graph)
360    }
361
362    fn ty(&self) -> Vc<Box<dyn ChunkType>> {
363        Vc::upcast(Vc::<EcmascriptChunkType>::default())
364    }
365
366    #[turbo_tasks::function]
367    fn module(&self) -> Vc<Box<dyn Module>> {
368        Vc::upcast(*self.module)
369    }
370
371    fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
372        *self.chunking_context
373    }
374}
375
376#[turbo_tasks::value_impl]
377impl OutputAssetsReference for EcmascriptModuleChunkItem {
378    #[turbo_tasks::function]
379    fn references(&self) -> Vc<turbopack_core::output::OutputAssetsWithReferenced> {
380        self.module
381            .chunk_item_output_assets(*self.chunking_context, *self.module_graph)
382    }
383}
384
385#[async_trait]
386#[turbo_tasks::value_impl]
387impl EcmascriptChunkItem for EcmascriptModuleChunkItem {
388    async fn content_with_async_module_info(
389        &self,
390        async_module_info: Option<Vc<AsyncModuleInfo>>,
391        estimated: bool,
392    ) -> Result<Vc<EcmascriptChunkItemContent>> {
393        Ok(self.module.chunk_item_content(
394            *self.chunking_context,
395            *self.module_graph,
396            async_module_info,
397            estimated,
398        ))
399    }
400}