File size: 2,577 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
use anyhow::Error;
use rustc_hash::FxHashSet;
use swc_core::ecma::{
    ast::{Module, ModuleDecl, ModuleItem},
    atoms::Atom,
};

use super::{PartId, graph::find_turbopack_part_id_in_asserts};

/// A loader used to merge module items after splitting.
pub trait Load {
    /// Loads a module while returning [None] if the module is already loaded.
    fn load(&mut self, uri: &str, part_id: u32) -> Result<Option<Module>, Error>;
}

/// A module merger.
///
/// This ensures that a module is loaded only once.
pub struct Merger<L>
where
    L: Load,
{
    loader: L,

    done: FxHashSet<(Atom, u32)>,
}

impl<L> Merger<L>
where
    L: Load,
{
    /// Creates a module merger.
    pub fn new(loader: L) -> Self {
        Merger {
            loader,
            done: Default::default(),
        }
    }

    /// Merges module content by appending the content of imported modules. This
    /// is recursive, so a single call is enough.
    pub fn merge_recursively(&mut self, entry: Module) -> Result<Module, Error> {
        let mut content = vec![];
        let mut extra_body = vec![];

        for stmt in entry.body {
            match stmt {
                ModuleItem::ModuleDecl(ModuleDecl::Import(import)) => {
                    // Try to prepend the content of module

                    let part_id = import
                        .with
                        .as_deref()
                        .and_then(find_turbopack_part_id_in_asserts);

                    if let Some(PartId::Internal(part_id, _)) = part_id {
                        if self.done.insert((import.src.value.clone(), part_id)) {
                            if let Some(dep) = self.loader.load(&import.src.value, part_id)? {
                                let mut dep = self.merge_recursively(dep)?;

                                extra_body.append(&mut dep.body);
                            } else {
                                content.push(ModuleItem::ModuleDecl(ModuleDecl::Import(import)));
                            }
                        } else {
                            // Remove import
                        }
                    } else {
                        // Preserve normal imports
                        content.push(ModuleItem::ModuleDecl(ModuleDecl::Import(import)));
                    }
                }
                _ => extra_body.push(stmt),
            }
        }

        content.append(&mut extra_body);

        Ok(Module {
            span: entry.span,
            body: content,
            shebang: entry.shebang,
        })
    }
}