File size: 3,179 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
use next_api::module_graph_snapshot::{ModuleGraphSnapshot, ModuleInfo, ModuleReference};
use turbo_rcstr::RcStr;
use turbopack_core::chunk::ChunkingType;

#[napi(object)]
pub struct NapiModuleReference {
    /// The index of the referenced/referencing module in the modules list.
    pub index: u32,
    /// The export used in the module reference.
    pub export: String,
    /// The type of chunking for the module reference.
    pub chunking_type: String,
}

impl From<&ModuleReference> for NapiModuleReference {
    fn from(reference: &ModuleReference) -> Self {
        Self {
            index: reference.index as u32,
            export: reference.export.to_string(),
            chunking_type: match &reference.chunking_type {
                ChunkingType::Parallel { hoisted: true, .. } => "hoisted".to_string(),
                ChunkingType::Parallel { hoisted: false, .. } => "sync".to_string(),
                ChunkingType::Async => "async".to_string(),
                ChunkingType::Isolated {
                    merge_tag: None, ..
                } => "isolated".to_string(),
                ChunkingType::Isolated {
                    merge_tag: Some(name),
                    ..
                } => format!("isolated {name}"),
                ChunkingType::Shared {
                    merge_tag: None, ..
                } => "shared".to_string(),
                ChunkingType::Shared {
                    merge_tag: Some(name),
                    ..
                } => format!("shared {name}"),
                ChunkingType::Traced => "traced".to_string(),
            },
        }
    }
}

#[napi(object)]
pub struct NapiModuleInfo {
    pub ident: RcStr,
    pub path: RcStr,
    pub depth: u32,
    pub size: u32,
    pub retained_size: u32,
    pub references: Vec<NapiModuleReference>,
    pub incoming_references: Vec<NapiModuleReference>,
}

impl From<&ModuleInfo> for NapiModuleInfo {
    fn from(info: &ModuleInfo) -> Self {
        Self {
            ident: info.ident.clone(),
            path: info.path.clone(),
            depth: info.depth,
            size: info.size,
            retained_size: info.retained_size,
            references: info
                .references
                .iter()
                .map(NapiModuleReference::from)
                .collect(),
            incoming_references: info
                .incoming_references
                .iter()
                .map(NapiModuleReference::from)
                .collect(),
        }
    }
}

#[napi(object)]
#[derive(Default)]
pub struct NapiModuleGraphSnapshot {
    pub modules: Vec<NapiModuleInfo>,
    pub entries: Vec<u32>,
}

impl From<&ModuleGraphSnapshot> for NapiModuleGraphSnapshot {
    fn from(snapshot: &ModuleGraphSnapshot) -> Self {
        Self {
            modules: snapshot.modules.iter().map(NapiModuleInfo::from).collect(),
            entries: snapshot
                .entries
                .iter()
                .map(|&i| {
                    // If you have more that 4294967295 entries, you probably have other problems...
                    i.try_into().unwrap()
                })
                .collect(),
        }
    }
}