Skip to main content

turbopack_core/introspect/
utils.rs

1use anyhow::Result;
2use turbo_rcstr::{RcStr, rcstr};
3use turbo_tasks::{FxIndexSet, Vc};
4use turbo_tasks_fs::FileContent;
5
6use super::{
7    IntrospectableChildren, module::IntrospectableModule, output_asset::IntrospectableOutputAsset,
8};
9use crate::{
10    asset::AssetContent,
11    chunk::ChunkingType,
12    output::OutputAssetsWithReferenced,
13    reference::{ModuleReference, ModuleReferences},
14};
15
16fn reference_ty() -> RcStr {
17    rcstr!("reference")
18}
19
20fn parallel_reference_ty() -> RcStr {
21    rcstr!("parallel reference")
22}
23
24fn parallel_inherit_async_reference_ty() -> RcStr {
25    rcstr!("parallel reference (inherit async module)")
26}
27
28fn async_reference_ty() -> RcStr {
29    rcstr!("async reference")
30}
31
32fn isolated_reference_ty() -> RcStr {
33    rcstr!("isolated reference")
34}
35
36fn shared_reference_ty() -> RcStr {
37    rcstr!("shared reference")
38}
39
40fn traced_reference_ty() -> RcStr {
41    rcstr!("traced reference")
42}
43
44fn per_entry_reference_ty() -> RcStr {
45    rcstr!("per entry reference")
46}
47
48fn emitted_reference_ty() -> RcStr {
49    rcstr!("emitted reference")
50}
51
52fn collected_reference_ty() -> RcStr {
53    rcstr!("collected reference")
54}
55
56#[turbo_tasks::function]
57pub async fn content_to_details(content: Vc<AssetContent>) -> Result<Vc<RcStr>> {
58    Ok(match &*content.await? {
59        AssetContent::File(file_content) => match &*file_content.await? {
60            FileContent::Content(file) => {
61                let content = file.content();
62                match content.to_str() {
63                    Ok(str) => Vc::cell(str.into()),
64                    Err(_) => Vc::cell(format!("{} binary bytes", content.len()).into()),
65                }
66            }
67            FileContent::NotFound => Vc::cell(rcstr!("not found")),
68        },
69        AssetContent::Redirect(content) => Vc::cell(format!("redirect to {content:?}").into()),
70    })
71}
72
73#[turbo_tasks::function]
74pub async fn children_from_module_references(
75    references: Vc<ModuleReferences>,
76) -> Result<Vc<IntrospectableChildren>> {
77    let key = reference_ty();
78    let mut children = FxIndexSet::default();
79    let references = references.await?;
80    for &reference in &*references {
81        let trait_ref = reference.into_trait_ref().await?;
82        let key = match &trait_ref.chunking_type() {
83            None => key.clone(),
84            Some(ChunkingType::Parallel { inherit_async, .. }) => {
85                if *inherit_async {
86                    parallel_inherit_async_reference_ty()
87                } else {
88                    parallel_reference_ty()
89                }
90            }
91            Some(ChunkingType::Async) => async_reference_ty(),
92            Some(ChunkingType::Isolated { .. }) => isolated_reference_ty(),
93            Some(ChunkingType::Shared { .. }) => shared_reference_ty(),
94            Some(ChunkingType::Traced { .. }) => traced_reference_ty(),
95            Some(ChunkingType::Emitted { .. }) => emitted_reference_ty(),
96            Some(ChunkingType::Collected { .. }) => collected_reference_ty(),
97            Some(ChunkingType::PerEntry) => per_entry_reference_ty(),
98        };
99
100        for &module in reference
101            .resolve_reference()
102            .await?
103            .primary_modules()
104            .await?
105            .iter()
106        {
107            children.insert((
108                key.clone(),
109                IntrospectableModule::new(*module).to_resolved().await?,
110            ));
111        }
112    }
113    Ok(Vc::cell(children))
114}
115
116#[turbo_tasks::function]
117pub async fn children_from_output_assets(
118    references: Vc<OutputAssetsWithReferenced>,
119) -> Result<Vc<IntrospectableChildren>> {
120    let key = reference_ty();
121    let mut children = FxIndexSet::default();
122    let references = references.expand_all_assets().await?;
123    for &reference in &*references {
124        children.insert((
125            key.clone(),
126            IntrospectableOutputAsset::new(*reference)
127                .to_resolved()
128                .await?,
129        ));
130    }
131    Ok(Vc::cell(children))
132}